From b43070fe038cf3704e94d0ea1178635fabba1c6b Mon Sep 17 00:00:00 2001 From: philipperemy Date: Wed, 9 Jan 2019 13:07:45 +0900 Subject: [PATCH 1/4] refactoring --- setup.py | 2 +- tcn/tcn.py | 35 +++++++++++++++-------------------- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/setup.py b/setup.py index 66d320b..e5cb2b0 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='keras-tcn', - version='2.3.4', + version='2.3.5', description='Keras TCN', author='Philippe Remy', license='MIT', diff --git a/tcn/tcn.py b/tcn/tcn.py index 1eb6a42..cbe1f53 100644 --- a/tcn/tcn.py +++ b/tcn/tcn.py @@ -45,7 +45,7 @@ def wave_net_activation(x): def residual_block(x, s, i, activation, nb_filters, kernel_size, padding, dropout_rate=0, name=''): - # type: (Layer, int, int, str, int, int, float, str) -> Tuple[Layer, Layer] + # type: (Layer, int, int, str, int, int, str, float, str) -> Tuple[Layer, Layer] """Defines the residual block for the WaveNet TCN Args: @@ -100,8 +100,10 @@ def is_power_of_two(num): class TCN: """Creates a TCN layer. + Input shape: + A tensor of shape (batch_size, timesteps, input_dim). + Args: - input_layer: A tensor of shape (batch_size, timesteps, input_dim). nb_filters: The number of filters to use in the convolutional layers. kernel_size: The size of the kernel to use in each convolutional layer. dilations: The list of the dilations. Example is: [1, 2, 4, 8, 16, 32, 64]. @@ -121,7 +123,7 @@ def __init__(self, nb_filters=64, kernel_size=2, nb_stacks=1, - dilations=None, + dilations=[1, 2, 4, 8, 16, 32], activation='norm_relu', padding='causal', use_skip_connections=True, @@ -139,12 +141,8 @@ def __init__(self, self.nb_filters = nb_filters self.padding = padding - # backwards incompatibility warning. - # o = tcn.TCN(i, return_sequences=False) => - # o = tcn.TCN(return_sequences=False)(i) - if padding != 'causal' and padding != 'same': - raise ValueError("Only 'causal' or 'same' paddings are compatible for this layer.") + raise ValueError("Only 'causal' or 'same' padding are compatible for this layer.") if not isinstance(nb_filters, int): print('An interface change occurred after the version 2.1.2.') @@ -154,8 +152,6 @@ def __init__(self, raise Exception() def __call__(self, inputs): - if self.dilations is None: - self.dilations = [1, 2, 4, 8, 16, 32] x = inputs x = Convolution1D(self.nb_filters, 1, padding=self.padding, name=self.name + '_initial_conv')(x) skip_connections = [] @@ -232,18 +228,17 @@ def compiled_tcn(num_feat, # type: int # https://github.com/keras-team/keras/pull/11373 # It's now in Keras@master but still not available with pip. - # TODO To remove later. - def accuracy(y_true, y_pred): - # reshape in case it's in shape (num_samples, 1) instead of (num_samples,) - if K.ndim(y_true) == K.ndim(y_pred): - y_true = K.squeeze(y_true, -1) - # convert dense predictions to labels - y_pred_labels = K.argmax(y_pred, axis=-1) - y_pred_labels = K.cast(y_pred_labels, K.floatx()) - return K.cast(K.equal(y_true, y_pred_labels), K.floatx()) + # def accuracy(y_true, y_pred): + # # reshape in case it's in shape (num_samples, 1) instead of (num_samples,) + # if K.ndim(y_true) == K.ndim(y_pred): + # y_true = K.squeeze(y_true, -1) + # # convert dense predictions to labels + # y_pred_labels = K.argmax(y_pred, axis=-1) + # y_pred_labels = K.cast(y_pred_labels, K.floatx()) + # return K.cast(K.equal(y_true, y_pred_labels), K.floatx()) adam = optimizers.Adam(lr=0.002, clipnorm=1.) - model.compile(adam, loss='sparse_categorical_crossentropy', metrics=[accuracy]) + model.compile(adam, loss='sparse_categorical_crossentropy', metrics=['accuracy']) print('Adam with norm clipping.') else: # regression From a1db425858b406f5b230c639b58e9d4da762cb45 Mon Sep 17 00:00:00 2001 From: philipperemy Date: Wed, 9 Jan 2019 13:13:12 +0900 Subject: [PATCH 2/4] refactoring --- tcn/tcn.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/tcn/tcn.py b/tcn/tcn.py index cbe1f53..b3a4f14 100644 --- a/tcn/tcn.py +++ b/tcn/tcn.py @@ -228,17 +228,18 @@ def compiled_tcn(num_feat, # type: int # https://github.com/keras-team/keras/pull/11373 # It's now in Keras@master but still not available with pip. - # def accuracy(y_true, y_pred): - # # reshape in case it's in shape (num_samples, 1) instead of (num_samples,) - # if K.ndim(y_true) == K.ndim(y_pred): - # y_true = K.squeeze(y_true, -1) - # # convert dense predictions to labels - # y_pred_labels = K.argmax(y_pred, axis=-1) - # y_pred_labels = K.cast(y_pred_labels, K.floatx()) - # return K.cast(K.equal(y_true, y_pred_labels), K.floatx()) + # TODO To remove later. + def accuracy(y_true, y_pred): + # reshape in case it's in shape (num_samples, 1) instead of (num_samples,) + if K.ndim(y_true) == K.ndim(y_pred): + y_true = K.squeeze(y_true, -1) + # convert dense predictions to labels + y_pred_labels = K.argmax(y_pred, axis=-1) + y_pred_labels = K.cast(y_pred_labels, K.floatx()) + return K.cast(K.equal(y_true, y_pred_labels), K.floatx()) adam = optimizers.Adam(lr=0.002, clipnorm=1.) - model.compile(adam, loss='sparse_categorical_crossentropy', metrics=['accuracy']) + model.compile(adam, loss='sparse_categorical_crossentropy', metrics=[accuracy]) print('Adam with norm clipping.') else: # regression From 8fe601628cee97253d5cfa92ca4d4be51b19f6f8 Mon Sep 17 00:00:00 2001 From: philipperemy Date: Wed, 9 Jan 2019 13:16:02 +0900 Subject: [PATCH 3/4] refactoring --- tcn/tcn.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tcn/tcn.py b/tcn/tcn.py index b3a4f14..345c291 100644 --- a/tcn/tcn.py +++ b/tcn/tcn.py @@ -67,7 +67,7 @@ def residual_block(x, s, i, activation, nb_filters, kernel_size, padding, dropou original_x = x conv = Conv1D(filters=nb_filters, kernel_size=kernel_size, dilation_rate=i, padding=padding, - name=name + '_dilated_conv_%d_tanh_s%d' % (i, s))(x) + name=name + '_d_%s_conv_%d_tanh_s%d' % (padding, i, s))(x) if activation == 'norm_relu': x = Activation('relu')(conv) x = Lambda(channel_normalization)(x) From a86b5b39415238068c61be0e97a8feee4ea93e2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20R=C3=A9my?= Date: Wed, 9 Jan 2019 13:17:25 +0900 Subject: [PATCH 4/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 63b2dd0..29e0f4f 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ model.fit(x, y) # Keras model. ### Arguments -`tcn.TCN(nb_filters=64, kernel_size=2, nb_stacks=1, dilations=None, activation='norm_relu', padding='causal', use_skip_connections=True, dropout_rate=0.0, return_sequences=True, name='tcn')` +`tcn.TCN(nb_filters=64, kernel_size=2, nb_stacks=1, dilations=[1, 2, 4, 8, 16, 32], activation='norm_relu', padding='causal', use_skip_connections=True, dropout_rate=0.0, return_sequences=True, name='tcn')` - `nb_filters`: Integer. The number of filters to use in the convolutional layers. - `kernel_size`: Integer. The size of the kernel to use in each convolutional layer.