![](/img/trans.png)
[英]LSTM and CNN: ValueError: Error when checking target: expected time_distributed_1 to have 3 dimensions, but got array with shape (400, 256)
[英]Keras LSTM multidimensional output error — expected time_distributed_17 to have 3 dimensions, but got array with shape (1824, 3)
我正在尝试按顺序预测多维值,例如[[0, 0, 2], [1, 0, 3], [2, 3, 4], [3, 2, 5], [4, 0, 6], [5, 0, 7] ... ]
并希望LSTM捕获每个[x, y, z]
尺寸。
当我尝试在以下模型上运行model.fit()
时,出现标题错误,
ValueError: Error when checking target: expected time_distributed_19 to have 3 dimensions, but got array with shape (1824, 3)
我知道输出层应该具有三个维度,但是在思考如何使用LSTM处理n维值序列时,我感到困惑。
这是我的模特。 请注意,如果我按照一些解决方案的建议取消注释Flatten()
行, model.compile()
在model.compile()
上收到一个非描述性的AssertionError
。
# X shape: (1824, 256, 3)
# Y shape: (1824, 3)
model = Sequential()
model.add(LSTM(units=128, input_shape=(X.shape[1], X.shape[2]), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=128, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=128, return_sequences=True))
model.add(Dropout(0.2))
# model.add(Flatten())
model.add(TimeDistributed(Dense(Y.shape[1], activation='softmax')))
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')
这是模型摘要:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_145 (LSTM) (None, 256, 128) 67584
_________________________________________________________________
dropout_140 (Dropout) (None, 256, 128) 0
_________________________________________________________________
lstm_146 (LSTM) (None, 256, 128) 131584
_________________________________________________________________
dropout_141 (Dropout) (None, 256, 128) 0
_________________________________________________________________
time_distributed_19 (TimeDis (None, 256, 3) 387
=================================================================
Total params: 199,555
Trainable params: 199,555
Non-trainable params: 0
_________________________________________________________________
None
在添加TimeDistributed()
包装器之前,该模型已运行(尽管我必须从最后一个隐藏层中删除return_sequences=True
才能使它起作用),但我添加了TimeDistributed()
因为我认为我的3中没有单独的变量维特征值已被捕获。
非常感谢任何见解,谢谢。
UPDATE
多亏nuric
对我的第一个问题的快速回答,我确认我以前的做法是“正确的方法”,而我的困惑源于我得到的预测。 给定一个来自X的序列,我得到一个3D向量,像这样: [9.915069e-01 1.084390e-04 8.384804e-03]
(并且总是[1, 0, 0]
[9.915069e-01 1.084390e-04 8.384804e-03]
[1, 0, 0]
)
在我以前的LSTM模型中,此预测矢量的最大值对应于我对字母/单词的单次编码时的索引,但是在这里,我想要的是对序列中下一个3D矢量的x,y和z值的预测。
您对模型的预测(当前为3D)和目标(二维)不匹配。 您有2个选择:
Flatten
并删除TimeDistributed
,这意味着模型将基于整个序列进行预测。 return_sequences=True
,以使LSTM压缩序列,然后再次删除TimeDistributed
。 这样,模型将基于最后的LSTM输出而不是序列进行预测。 考虑到序列的大小和隐藏单元的数量,我希望使用第二个选项。 如果您只是展平序列,即参数过多,则选项一将为Dense层创建一个非常大的内核。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.