Skip to content

Commit c4f4588

Browse files
author
Philippe Rémy
authored
Merge pull request #57 from philipperemy/activation
activation o = act(f(x)+x)
2 parents 7f24c8d + 432fa28 commit c4f4588

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ model.fit(x, y) # Keras model.
9696

9797
### Arguments
9898

99-
`TCN(nb_filters=64, kernel_size=2, nb_stacks=1, dilations=[1, 2, 4, 8, 16, 32], padding='causal', use_skip_connections=True, dropout_rate=0.0, return_sequences=True, name='tcn')`
99+
`TCN(nb_filters=64, kernel_size=2, nb_stacks=1, dilations=[1, 2, 4, 8, 16, 32], padding='causal', use_skip_connections=True, dropout_rate=0.0, return_sequences=True, activation='linear', name='tcn')`
100100

101101
- `nb_filters`: Integer. The number of filters to use in the convolutional layers. Would be similar to `units` for LSTM.
102102
- `kernel_size`: Integer. The size of the kernel to use in each convolutional layer.
@@ -106,6 +106,7 @@ model.fit(x, y) # Keras model.
106106
- `use_skip_connections`: Boolean. If we want to add skip connections from input to each residual block.
107107
- `return_sequences`: Boolean. Whether to return the last output in the output sequence, or the full sequence.
108108
- `dropout_rate`: Float between 0 and 1. Fraction of the input units to drop.
109+
- `activation`: The activation used in the residual blocks o = activation(x + F(x)).
109110
- `name`: Name of the model. Useful when having multiple TCN.
110111

111112
### Input shape

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name='keras-tcn',
5-
version='2.6.7',
5+
version='2.7.0',
66
description='Keras TCN',
77
author='Philippe Remy',
88
license='MIT',

tcn/tcn.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
from keras.models import Input, Model
1111

1212

13-
def residual_block(x, dilation_rate, nb_filters, kernel_size, padding, dropout_rate=0):
14-
# type: (Layer, int, int, int, str, float) -> Tuple[Layer, Layer]
13+
def residual_block(x, dilation_rate, nb_filters, kernel_size, padding, activation='relu', dropout_rate=0):
14+
# type: (Layer, int, int, int, str, str, float) -> Tuple[Layer, Layer]
1515
"""Defines the residual block for the WaveNet TCN
1616
1717
Args:
@@ -20,8 +20,8 @@ def residual_block(x, dilation_rate, nb_filters, kernel_size, padding, dropout_r
2020
nb_filters: The number of convolutional filters to use in this block
2121
kernel_size: The size of the convolutional kernel
2222
padding: The padding used in the convolutional layers, 'same' or 'causal'.
23+
activation: The final activation used in o = Activation(x + F(x))
2324
dropout_rate: Float between 0 and 1. Fraction of the input units to drop.
24-
2525
Returns:
2626
A tuple where the first element is the residual model layer, and the second
2727
is the skip connection.
@@ -39,6 +39,7 @@ def residual_block(x, dilation_rate, nb_filters, kernel_size, padding, dropout_r
3939
# 1x1 conv to match the shapes (channel dimension).
4040
prev_x = Conv1D(nb_filters, 1, padding='same')(prev_x)
4141
res_x = keras.layers.add([prev_x, x])
42+
res_x = Activation(activation)(res_x)
4243
return res_x, x
4344

4445

@@ -69,6 +70,7 @@ class TCN:
6970
padding: The padding to use in the convolutional layers, 'causal' or 'same'.
7071
use_skip_connections: Boolean. If we want to add skip connections from input to each residual block.
7172
return_sequences: Boolean. Whether to return the last output in the output sequence, or the full sequence.
73+
activation: The activation used in the residual blocks o = Activation(x + F(x)).
7274
dropout_rate: Float between 0 and 1. Fraction of the input units to drop.
7375
name: Name of the model. Useful when having multiple TCN.
7476
@@ -85,6 +87,7 @@ def __init__(self,
8587
use_skip_connections=True,
8688
dropout_rate=0.0,
8789
return_sequences=False,
90+
activation='linear',
8891
name='tcn'):
8992
self.name = name
9093
self.return_sequences = return_sequences
@@ -94,6 +97,7 @@ def __init__(self,
9497
self.nb_stacks = nb_stacks
9598
self.kernel_size = kernel_size
9699
self.nb_filters = nb_filters
100+
self.activation = activation
97101
self.padding = padding
98102

99103
if padding != 'causal' and padding != 'same':
@@ -118,6 +122,7 @@ def __call__(self, inputs):
118122
nb_filters=self.nb_filters,
119123
kernel_size=self.kernel_size,
120124
padding=self.padding,
125+
activation=self.activation,
121126
dropout_rate=self.dropout_rate)
122127
skip_connections.append(skip_out)
123128
if self.use_skip_connections:
@@ -140,6 +145,7 @@ def compiled_tcn(num_feat, # type: int
140145
regression=False, # type: bool
141146
dropout_rate=0.05, # type: float
142147
name='tcn', # type: str,
148+
activation='linear', # type:str,
143149
opt='adam',
144150
lr=0.002):
145151
# type: (...) -> keras.Model
@@ -159,6 +165,7 @@ def compiled_tcn(num_feat, # type: int
159165
return_sequences: Boolean. Whether to return the last output in the output sequence, or the full sequence.
160166
regression: Whether the output should be continuous or discrete.
161167
dropout_rate: Float between 0 and 1. Fraction of the input units to drop.
168+
activation: The activation used in the residual blocks o = Activation(x + F(x)).
162169
name: Name of the model. Useful when having multiple TCN.
163170
opt: Optimizer name.
164171
lr: Learning rate.
@@ -171,7 +178,8 @@ def compiled_tcn(num_feat, # type: int
171178
input_layer = Input(shape=(max_len, num_feat))
172179

173180
x = TCN(nb_filters, kernel_size, nb_stacks, dilations, padding,
174-
use_skip_connections, dropout_rate, return_sequences, name)(input_layer)
181+
use_skip_connections, dropout_rate, return_sequences,
182+
activation, name)(input_layer)
175183

176184
print('x.shape=', x.shape)
177185

0 commit comments

Comments
 (0)