繁体   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]

这是代码:

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')
])

这是错误消息:

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

我正在使用 tensorflow 1.15.0 并在 Google Colab 上运行它。 我该如何解决。

正如 Marco 在评论中所说,解码器需要 3d,但它得到 2d,因此在解码器工作之前应用 RepeatVector 层。 修正后的模型:

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')
])

我添加了 RepeatVector 层使输出形状为 3D,并删除了 Reshape 层,因为它现在没有用了。

感谢马可的帮助!

暂无
暂无

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

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