![](/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.