【问题标题】:Keras LSTM input/output shapeKeras LSTM 输入/输出形状
【发布时间】:2020-09-24 06:51:49
【问题描述】:

我需要每个循环层的输出,我的设置如下:

100 个训练示例,每个示例 3 个时间步长,每个单独元素的 20 维特征向量。

x_train: (100,3,20)

y_train: (100,20)

LSTM 架构:

model.add(LSTM(20, input_shape=(3,20), return_sequences=True))
model.compile(loss='mean_absolute_error', optimizer='adam', metrics=['accuracy'])
model.summary()

培训:

history = model.fit(x_train, y_train, epochs=50, validation_data=(x_test, y_test))

错误:

ValueError: Dimensions must be equal, but are 20 and 3 for '{{node Equal}} = Equal[T=DT_FLOAT, incompatible_shape_error=true](IteratorGetNext:1, Cast_1)' with input shapes: [?,20], [?,3].

请帮助我正确输入/输出 LSTM 尺寸。 谢谢

【问题讨论】:

标签: python-3.x lstm keras-layer keyerror


【解决方案1】:

LSTM(20, input_shape=(3,20), return_sequences=True)(100,3,20) 作为输入形状并返回(100,3,20)。但是,您的目标输出编码为(100,20)

从维度上,我假设您想将每个序列映射到非序列,即您可以这样做:

LSTM(20, input_shape=(3,20), return_sequences=False)

这将返回最终的隐藏状态,即与您的目标输出匹配的 (100,20) 形状。

【讨论】:

  • 嘿@runDOSrun,应该是seq2seq模型和(100, 3, 20)
  • 嘿 runDOSrun, 'LSTM(20, input_shape=(3,20), return_sequences=False)' 工作正常。但是我需要建立一个 seq2seq 模型,其中对于输入形状“(100,3,20)”,我可以为每个时间步提取“y”。您会在我的代码中建议哪些更改?我的数据就像(假设所有单词都有 20-d 嵌入)'(x,y): (cat dog mouse, zebra)' '''(x,y): (dog mouse zebra hen)''' Naturally ,我的 x_train 和 y_train 将分别是 '(3x20)' 和 '(20)'。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-27
  • 2019-01-20
  • 1970-01-01
  • 1970-01-01
  • 2018-06-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多