Keras LSTM多维输出错误-预期time_distributed_17具有3个维度,但数组的形状为(1824,3) [英] 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]
尺寸.
I am trying to predict multidimensional values in sequence, e.g.
[[0, 0, 2], [1, 0, 3], [2, 3, 4], [3, 2, 5], [4, 0, 6], [5, 0, 7] ... ]
and want each of the [x, y, z]
dimensions to be captured by the LSTM.
当我尝试在以下模型上运行model.fit()
时,出现标题错误
When I attempt to run model.fit()
on the model below, I get the error in the title,
ValueError: Error when checking target: expected time_distributed_19 to have 3 dimensions, but got array with shape (1824, 3)
我知道输出层应该具有3维,但是在思考如何使用LSTM处理n维值序列时,我感到困惑.
I know the output layer should have three dimensions, but I'm getting confused in my thinking about how I need the LSTM to deal with my sequence of n-dimensional values.
这是我的模特.请注意,如果我取消注释某些解决方案所建议的Flatten()
行,则会在model.compile()
Here is my model. Note that if I uncomment the Flatten()
line as some solutions suggest, I get a nondescript AssertionError
on model.compile()
# 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维特征值被捕获了.
This model was running before I added the TimeDistributed()
wrapper (though I had to remove return_sequences=True
from the last hidden layer for it to work), but I added TimeDistributed()
because I don't think the individual variables of my 3-dimensional feature values were being captured.
非常感谢您提供任何见识.
Any insight is greatly appreciated, thank you.
更新
由于nuric
对我的最初问题的快速回答,我确认我以前的做法是正确的方法",而我的困惑源于我得到的预测.给定一个来自X的序列,我得到一个3D向量,像这样:[9.915069e-01 1.084390e-04 8.384804e-03]
(并且总是关于[1, 0, 0]
)
Thanks to nuric
's quick answer to my initial question, I confirmed that the way I was previously doing it was the "right way" and my confusion stems from the predictions I'm getting. Given a sequence from X, I get a 3D vector like this: [9.915069e-01 1.084390e-04 8.384804e-03]
(and it's always about [1, 0, 0]
)
在我以前的LSTM模型中,此预测向量的最大值对应于我对字母/单词的单次编码中的索引,但是这里我要的是对下一个3D向量的x,y和z值的预测按顺序.
In my previous LSTM models, this prediction vector's max value corresponded to the index in my one-hot encoding of letters/words, but here what I want is predictions for the x, y, and z values of the next 3D vector in the sequence.
推荐答案
您对模型的预测(当前为3D)和目标(二维)不匹配.您有2个选择:
You have a mismatch in what the model predicts, currently 3D, and what the target is, 2D. You have 2 options:
- 应用
Flatten
并删除TimeDistributed
,这意味着模型将基于整个序列进行预测. - 从最后一个LSTM中删除
return_sequences=True
,以使LSTM压缩序列,然后再次删除TimeDistributed
.这样,模型将基于最后的LSTM输出而不是序列进行预测.
- Apply
Flatten
and removeTimeDistributed
which means the model will predict based on the entire sequence. - Remove
return_sequences=True
from last LSTM to let the LSTM compress the sequence and again removeTimeDistributed
. This way the model will predict based on the last LSTM output not the sequences.
鉴于序列的大小和隐藏单元的数量,我宁愿选择第二个选项.如果您只是展平序列,即参数太多,则选项1将为Dense层创建一个非常大的内核.
I would prefer the second option given the size of the sequence and the number of hidden units you have. Option one will create a very large kernel for the Dense layer if you just flatten the sequence, i.e. too many parameters.
这篇关于Keras LSTM多维输出错误-预期time_distributed_17具有3个维度,但数组的形状为(1824,3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!