Title: Unknown output dimension of dilated convolution produces error on downstream Concatenate Layer
Tags: python;keras;conv-neural-network;convolution;tensorflow2.0
Question: I'm trying to implement a simplified module like wavenet with dilated convolutions. A simple example is below:
import tensorflow as tf
tfkl = tf.keras.layers
output_dim = 3
def waveres(inpt, n_filters, kernel_size, i):
tanh_out = tfkl.Conv1D(n_filters,
kernel_size,
dilation_rate = kernel_size ** i,
padding='causal',
name=f'dilated_conv_{kernel_size**i}_tanh',
activation='tanh'
)(inpt)
sigm_out = tfkl.Conv1D(n_filters,
kernel_size,
dilation_rate = kernel_size**i,
padding='causal',
name=f'dilated_conv_{kernel_size**i}_sigm',
activation='sigmoid'
)(inpt)
z = tfkl.Multiply(name=f'gated_activation_{i}')([tanh_out, sigm_out])
skip = tfkl.Conv1D(n_filters, 1, name=f'skip_{i}')(z)
res = tfkl.Add(name=f'residual_block_{i}')([skip, inpt])
return res, skip
def wavenet(inpt,depth,n_filters=32,kernel_size=2):
skip_connections = []
out = tfkl.Conv1D(n_filters, kernel_size, dilation_rate=1, activation='linear',padding='causal', name='wavenet_conv_1',input_shape=inpt.get_shape())(inpt)
for i in range(1, depth + 1):
out, skip = waveres(out, n_filters, kernel_size, i)
skip_connections.append(skip)
out = tfkl.Add(name='skip_connections')(skip_connections)
out = tfkl.Activation('relu')(out)
out = tfkl.Conv1D(n_filters, 32, strides=1, padding='causal', name='wavenet_final_conv', activation='relu')(out)
out = tfkl.AveragePooling1D(7, 1, padding='same', name='wavenet_avgpool')(out)
return out
def _model(inputs,wave_depth=4):
x = tfkl.Dense(256)(inputs)
kyma = wavenet(inputs,wave_depth)
junc = tfkl.Concatenate()([x,kyma])
fc = tfkl.Dense(32)(junc)
out = tfkl.Dense(output_dim)(fc)
return out
model = tf.keras.Model(inpt_,_model(inpt_))
The problem that I'm facing is that after the dilated convolutions, the second dimension become None and this prevents concatenation with parallel layers.
I've got two questions:
Is this expected behavior?
How can I work with the output of dilated convolutional layers?
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-29-3bcd4eb984b7&gt; in <module&gt;
49
50
---&gt; 51 model = tf.keras.Model(inpt_,_model(inpt_))
<ipython-input-29-3bcd4eb984b7&gt; in _model(inputs, wave_depth)
43     kyma = wavenet(inputs,wave_depth)
44     print(kyma.get_shape())
---&gt; 45     junc = tfkl.Concatenate()([x,kyma])
46     fc = tfkl.Dense(32)(junc)
47     out = tfkl.Dense(output_dim)(fc)
~\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in __call__(self, inputs, *args, **kwargs)
614           # Build layer if applicable (if the `build` method has been
615           # overridden).
--&gt; 616           self._maybe_build(inputs)
617
618           # Wrapping `call` function in autograph to allow for dynamic control
~\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in _maybe_build(self, inputs)
1964         # operations.
1965         with tf_utils.maybe_init_scope(self):
-&gt; 1966           self.build(input_shapes)
1967       # We must set self.built since user defined build functions are not
1968       # constrained to set self.built.
~\Anaconda3\lib\site-packages\tensorflow\python\keras\utils\tf_utils.py in wrapper(instance, input_shape)
294     if input_shape is not None:
295       input_shape = convert_shapes(input_shape, to_tuples=True)
--&gt; 296     output_shape = fn(instance, input_shape)
297     # Return shapes from `fn` as TensorShapes.
298     if output_shape is not None:
~\Anaconda3\lib\site-packages\tensorflow\python\keras\layers\merge.py in build(self, input_shape)
389                        'inputs with matching shapes '
390                        'except for the concat axis. '
--&gt; 391                        'Got inputs shapes: %s' % (input_shape))
392
393   def _merge_function(self, inputs):
ValueError: A `Concatenate` layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 10, 256), (None, None, 32)]
Running this on Tensorflow 2.0.0beta1



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:08/09/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><keras><conv-neural-network><convolution><tensorflow2.0>', 'Title': 'Unknown output dimension of dilated convolution produces error on downstream Concatenate Layer', 'ViewCount': 175, 'id': 57430663, 'user_AboutMe': '', 'user_AccountId': 8699563.0, 'user_DisplayName': 'Jed', 'user_Reputation': 588.0, 'user_Views': 83.0, 'user_id': 6509922.0}}