![](/img/trans.png)
[英]Correcting input dimensions for CNN, LSTM based classifier using Keras, Python
[英]Keras in Python: LSTM Dimensions
我正在建立一個LSTM網絡。 我的數據如下:
X_train.shape = (134, 300000, 4)
X_train包含134個序列,具有300000個時間步長和4個特征。
Y_train.shape = (134, 2)
Y_train包含134個標簽,[1,0]表示True,[0,1]表示False。
下面是我在Keras中的模型。
model = Sequential()
model.add(LSTM(4, input_shape=(300000, 4), return_sequences=True))
model.compile(loss='categorical_crossentropy', optimizer='adam')
每當我運行模型時,都會出現以下錯誤:
Error when checking target: expected lstm_52 to have 3 dimensions, but got array with shape (113, 2)
它的形狀似乎是(113,2),與我的Y_train數據有關。
謝謝!
您的LSTM層的輸出形狀為(batch_size, 300000, 4)
(因為return_sequences=True
)。 因此,您的模型期望目標y_train
具有3維,但是您傳遞的數組只有2維(batch_size, 2)
。
您可能想改用return_sequences=False
。 在這種情況下,LSTM層的輸出形狀將為(batch_size, 4)
。 此外,您應該在模型中添加最終的softmax圖層,以具有所需的輸出形狀(batch_size, 2)
:
model = Sequential()
model.add(LSTM(4, input_shape=(300000, 4), return_sequences=False))
model.add(Dense(2, activation='softmax')) # 2 neurons because you have 2 classes
model.compile(loss='categorical_crossentropy', optimizer='adam')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.