简体   繁体   English

ValueError: 层 lstm_17 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。 收到的完整形状:[无,128]

[英]ValueError: Input 0 of layer lstm_17 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 128]

Here is the code:这是代码:

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, LSTM, RepeatVector, Dense, Reshape

Model = Sequential([
          Embedding(vocab_size, 256, input_length=49),
          LSTM(256, return_sequences=True),
          LSTM(128, return_sequences=False),
          LSTM(128),
          Reshape((128, 1)),
          Dense(vocab_size, activation='softmax')
])

And this is the error message:这是错误消息:

ValueError: Input 0 of layer lstm_11 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 128]

I am using tensorflow 1.15.0 and running it on Google Colab.我正在使用 tensorflow 1.15.0 并在 Google Colab 上运行它。 How can I fix it.我该如何解决。

As also said by Marco in the comments, the decoder expects 3d but it gets 2d, so applying a RepeatVector layer before the decoder worked.正如 Marco 在评论中所说,解码器需要 3d,但它得到 2d,因此在解码器工作之前应用 RepeatVector 层。 The corrected Model:修正后的模型:

Model = Sequential([
      Embedding(vocab_size, 256, input_length=49),
      LSTM(256, return_sequences=True),
      LSTM(128, return_sequences=False),
      RepeatVector(1),
      LSTM(128),
      Dense(vocab_size, activation='softmax')
])

I added RepeatVector layer to make the output shape 3D, and removed the Reshape layer since now it has no use.我添加了 RepeatVector 层使输出形状为 3D,并删除了 Reshape 层,因为它现在没有用了。

Thanks Marco for the help!感谢马可的帮助!

暂无
暂无

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

相关问题 ValueError: 层 lstm 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。 收到的完整形状:[无,18] - ValueError : Input 0 of layer lstm is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 18] ValueError:层“lstm”的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。 收到完整形状:(无,1024) - ValueError: Input 0 of layer "lstm" is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 1024) LSTM 模型:ValueError:层“lstm”的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。 收到的完整形状:(无,7) - LSTM model: ValueError: Input 0 of layer "lstm" is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 7) ValueError:global_average_pooling2d 层的输入 0 与该层不兼容:预期 ndim=4,发现 ndim=2。 收到的完整形状:[无,128] - ValueError: Input 0 of layer global_average_pooling2d is incompatible with the layer: expected ndim=4, found ndim=2. Full shape received: [None, 128] ValueError: 层序贯_17 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。 收到的完整形状:[无,121] - ValueError: Input 0 of layer sequential_17 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 121] ValueError: 层 lstm_1 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。 收到的完整形状:(无,64) - ValueError: Input 0 of layer lstm_1 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 64) ValueError: 层 lstm_21 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。 收到的完整形状:(无,546) - ValueError: Input 0 of layer lstm_21 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 546) ValueError: 层 lstm_5 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。 收到完整形状:(无,43264) - ValueError: Input 0 of layer lstm_5 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 43264) ValueError:层“lstm_121”的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。 收到的完整形状:(无,50) - ValueError: Input 0 of layer "lstm_121" is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 50) ValueError: 层序的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。 收到的完整形状:(无,1) - ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 1)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM