简体   繁体   English

当input_shape指定为3-d时,Keras SimpleRNN出错

[英]Error on Keras SimpleRNN when input_shape is specified to be 3-d

I'm doing trying to train from text on a SimpleRNN on Keras. 我正在尝试从Keras上的SimpleRNN上的文本进行训练。

In Keras, i specified a very simple parameters for SimpleRNN as below: 在Keras中,我为SimpleRNN指定了一个非常简单的参数,如下所示:

model = Sequential()
model.add(SimpleRNN(output_dim=1, input_shape=(1,1,1))

I understand that input_shape should be (nb_samples, timesteps, input_dim), the same as my train_x.shape 我知道input_shape应该是(nb_samples,timesteps,input_dim),与我的train_x.shape相同

so i was surprised that i received the following error. 所以我很惊讶我收到了以下错误。

Traceback (most recent call last):
  File "C:/Users/xxx/xxxx/xxx/xxx.py", line 262, in <module>
    model.add(SimpleRNN(output_dim=vocab_size, input_shape=train_x.shape))
  File "C:\Anaconda3\envs\py34\lib\site-packages\keras\models.py", line 275, in add
    layer.create_input_layer(batch_input_shape, input_dtype)
  File "C:\Anaconda3\envs\py34\lib\site-packages\keras\engine\topology.py", line 367, in create_input_layer
    self(x)
  File "C:\Anaconda3\envs\py34\lib\site-packages\keras\engine\topology.py", line 467, in __call__
    self.assert_input_compatibility(x)
  File "C:\Anaconda3\envs\py34\lib\site-packages\keras\engine\topology.py", line 408, in assert_input_compatibility
    str(K.ndim(x)))
Exception: Input 0 is incompatible with layer simplernn_1: expected ndim=3, found ndim=4

Not sure why keras "found ndim=4" when only 3 was specified! 不知道为什么当指定只有3时keras“找到了ndim = 4”!

for clarity, my 为清楚起见,我的

train_x.shape = (73, 84, 400) train_x.shape =(73,84,400)

and

vocab_size=400 vocab_size = 400

. As long as input_shape is fed 3d and above, i realised an error will result. 只要input_shape被送入3d及以上,我就会意识到会出现错误。

Any help will be greatly appreciated!!! 任何帮助将不胜感激!!! :)) :))

You are not supposed to include n_samples in the input shape of the model. 您不应该在模型的输入形状中包含n_samples So you have to specify a tuple of size 2 for input shape of a your layer (or set the first element of shape to None ). 因此,您必须为图层的输入形状指定大小为2的元组(或将形状的第一个元素设置为None )。 Here Keras automatically adds None to your input shape resulting in ndim=4 . Keras会自动在输入形状中添加None ,从而导致ndim=4 More info on this can be found here . 有关这方面的更多信息,请点击此处

Also it appears that your input_dim=400 (assuming you use one-hot coding representation of words in vocabulary) and that your training data consists of 73 texts (pretty small) each having length of 84 . 此外,您的input_dim=400 (假设您在词汇表中使用单词的单热编码表示),并且您的训练数据由73文本(非常小)组成,每个文本的长度为84 So you should probably set input_shape=(84,400). 所以你应该设置input_shape=(84,400).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM