Keras TimeDistributed Conv1D错误

时间:2018-08-24 16:37:47

标签: keras conv-neural-network keras-layer keras-2

这是我的代码:

cnn_input = Input(shape=(cnn_max_length,)) 
emb_output = Embedding(num_chars + 1, output_dim=32, input_length=cnn_max_length, trainable=True)(cnn_input)
output = TimeDistributed(Convolution1D(filters=128, kernel_size=4, activation='relu'))(emb_output)

我想训练一个字符级的CNN序列标签器,并且不断收到此错误:

Traceback (most recent call last):
  File "word_lstm_char_cnn.py", line 24, in <module>
    output = kl.TimeDistributed(kl.Convolution1D(filters=128, kernel_size=4, activation='relu'))(emb_output)
  File "/home/user/anaconda3/envs/thesisenv/lib/python3.6/site-packages/keras/engine/base_layer.py", line 457, in __call__
    output = self.call(inputs, **kwargs)
es/keras/layers/wrappers.py", line 248, in call
    y = self.layer.call(inputs, **kwargs)
  File "/home/user/anaconda3/envs/thesisenv/lib/python3.6/site-packages/keras/layers/convolutional.py", line 160, in call
    dilation_rate=self.dilation_rate[0])
  File "/home/user/anaconda3/envs/thesisenv/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 3526, in conv1d
    data_format=tf_data_format)
  File "/home/user/anaconda3/envs/thesisenv/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py", line 779, in convolution
    data_format=data_format)
  File "/home/user/anaconda3/envs/thesisenv/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py", line 828, in __init__
    input_channels_dim = input_shape[num_spatial_dims + 1]
  File "/home/user/anaconda3/envs/thesisenv/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py", line 615, in __getitem__
    return self._dims[key]
IndexError: list index out of range

输入应该是3D的。如果更改输入形状,则会收到此错误:

ValueError: Input 0 is incompatible with layer time_distributed_1: expected ndim=3, found ndim=4

1 个答案:

答案 0 :(得分:1)

推荐的解决方案: 在这种情况下,无需使用TimeDistributed。您可以使用以下代码解决问题:

output = Convolution1D(filters=128, kernel_size=4, activation='relu')(emb_output)

以防万一,如果您想使用TimeDistributed,可以执行以下操作:

output = TimeDistributed(Dense(100,activation='relu'))(emb_output)

不推荐:根据文档:

  

此包装器将一层应用于输入的每个时间片。

TimeDistributed的输入类似于batch_size * seq_len * emb_size。当Conv1D应用于每个序列时,它需要2个维,但仅找到一个。

您可以通过在序列中添加一维来解决问题:

TimeDistributed(Conv1D(100, 1))(keras.backend.reshape(emb, [-1, sequence_len, embeding_dim, 1]))