Keras TimeDistributed Conv1D错误 [英] Keras TimeDistributed Conv1D Error

查看:261
本文介绍了Keras TimeDistributed Conv1D错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

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序列标签器,并且不断收到此错误:

I want to train a character-level CNN sequence labeler and I keep receiving this error:

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的.如果更改输入形状,则会收到此错误:

The input is 3D, as it should be. If I change the input shape I receive this error:

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

推荐答案

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

Recommended solution: There is no need to use TimeDistributed in this case. You can fix the issue with following piece of code:

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

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

Just in case, if you like to use TimeDistributed you can do something like:

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个维,但只能找到一个.

The input to the TimeDistributed is something like batch_size * seq_len * emb_size. When Conv1D apply to each sequence, it needs 2 dimensions but found only one.

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

You can fix the problem by adding one dimension to your sequences:

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

这篇关于Keras TimeDistributed Conv1D错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆