Keras TimeDistributed Conv1D 错误

Keras TimeDistributed Conv1D Error

这是我的代码:

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

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

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

以防万一,如果你喜欢使用 TimeDistributed,你可以这样做:

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

不推荐:根据文档:

This wrapper applies a layer to every temporal slice of an input.

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

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

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