10
10
from keras .models import Input , Model
11
11
12
12
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]
15
15
"""Defines the residual block for the WaveNet TCN
16
16
17
17
Args:
@@ -20,8 +20,8 @@ def residual_block(x, dilation_rate, nb_filters, kernel_size, padding, dropout_r
20
20
nb_filters: The number of convolutional filters to use in this block
21
21
kernel_size: The size of the convolutional kernel
22
22
padding: The padding used in the convolutional layers, 'same' or 'causal'.
23
+ activation: The final activation used in o = Activation(x + F(x))
23
24
dropout_rate: Float between 0 and 1. Fraction of the input units to drop.
24
-
25
25
Returns:
26
26
A tuple where the first element is the residual model layer, and the second
27
27
is the skip connection.
@@ -39,6 +39,7 @@ def residual_block(x, dilation_rate, nb_filters, kernel_size, padding, dropout_r
39
39
# 1x1 conv to match the shapes (channel dimension).
40
40
prev_x = Conv1D (nb_filters , 1 , padding = 'same' )(prev_x )
41
41
res_x = keras .layers .add ([prev_x , x ])
42
+ res_x = Activation (activation )(res_x )
42
43
return res_x , x
43
44
44
45
@@ -69,6 +70,7 @@ class TCN:
69
70
padding: The padding to use in the convolutional layers, 'causal' or 'same'.
70
71
use_skip_connections: Boolean. If we want to add skip connections from input to each residual block.
71
72
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)).
72
74
dropout_rate: Float between 0 and 1. Fraction of the input units to drop.
73
75
name: Name of the model. Useful when having multiple TCN.
74
76
@@ -85,6 +87,7 @@ def __init__(self,
85
87
use_skip_connections = True ,
86
88
dropout_rate = 0.0 ,
87
89
return_sequences = False ,
90
+ activation = 'linear' ,
88
91
name = 'tcn' ):
89
92
self .name = name
90
93
self .return_sequences = return_sequences
@@ -94,6 +97,7 @@ def __init__(self,
94
97
self .nb_stacks = nb_stacks
95
98
self .kernel_size = kernel_size
96
99
self .nb_filters = nb_filters
100
+ self .activation = activation
97
101
self .padding = padding
98
102
99
103
if padding != 'causal' and padding != 'same' :
@@ -118,6 +122,7 @@ def __call__(self, inputs):
118
122
nb_filters = self .nb_filters ,
119
123
kernel_size = self .kernel_size ,
120
124
padding = self .padding ,
125
+ activation = self .activation ,
121
126
dropout_rate = self .dropout_rate )
122
127
skip_connections .append (skip_out )
123
128
if self .use_skip_connections :
@@ -140,6 +145,7 @@ def compiled_tcn(num_feat, # type: int
140
145
regression = False , # type: bool
141
146
dropout_rate = 0.05 , # type: float
142
147
name = 'tcn' , # type: str,
148
+ activation = 'linear' , # type:str,
143
149
opt = 'adam' ,
144
150
lr = 0.002 ):
145
151
# type: (...) -> keras.Model
@@ -159,6 +165,7 @@ def compiled_tcn(num_feat, # type: int
159
165
return_sequences: Boolean. Whether to return the last output in the output sequence, or the full sequence.
160
166
regression: Whether the output should be continuous or discrete.
161
167
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)).
162
169
name: Name of the model. Useful when having multiple TCN.
163
170
opt: Optimizer name.
164
171
lr: Learning rate.
@@ -171,7 +178,8 @@ def compiled_tcn(num_feat, # type: int
171
178
input_layer = Input (shape = (max_len , num_feat ))
172
179
173
180
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 )
175
183
176
184
print ('x.shape=' , x .shape )
177
185
0 commit comments