膨胀卷积的未知输出尺寸在下游连接层上产生错误

时间:2019-08-09 13:03:31

标签: python keras conv-neural-network convolution tensorflow2.0

我正在尝试实现一个简化的模块,例如具有扩展卷积的wavenet。下面是一个简单的示例:

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_))

我面临的问题是,在扩大的卷积之后,第二维变为无,这将阻止与平行层的连接。

我有两个问题:

这是预期的行为吗?

如何处理膨胀卷积层的输出?

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-29-3bcd4eb984b7> in <module>
     49 
     50 
---> 51 model = tf.keras.Model(inpt_,_model(inpt_))

<ipython-input-29-3bcd4eb984b7> in _model(inputs, wave_depth)
     43     kyma = wavenet(inputs,wave_depth)
     44     print(kyma.get_shape())
---> 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).
--> 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):
-> 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)
--> 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. '
--> 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)]

NB:

在Tensorflow 2.0.0beta1上运行此

0 个答案:

没有答案