簡體   English   中英

Keras LSTM 層序貫_10的輸入0與層不兼容

[英]Keras LSTM Input 0 of layer sequential_10 is incompatible with the layer

我的 LSTM 代碼如下:

def myLSTM(i_shape, o_shape):
    input = keras.layers.Input(i_shape)
    model = Sequential()
    x = keras.layers.LSTM(128, return_sequences = True, input_shape = (x_train.shape[1], 1))(input)
    x = keras.layers.Dropout(0.2)(x)
    x = keras.layers.LSTM(128, return_sequences = True)(x)
    x = keras.layers.Dropout(0.2)(x)
    x = keras.layers.LSTM(64, return_sequences = True)(x)
    x = keras.layers.Dropout(0.2)(x)
    output = layers.Dense(units = 1, activation='softmax')(x)
    return Model(input, output)

my_lstm = myLSTM(x_train.shape[1:], y_train.shape[1:])
my_lstm.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])
my_lstm.summary()

我收到以下錯誤:

ValueError: Input 0 of layer lstm is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 20)

這個錯誤讓我感到困惑,因為我覺得一個 3 維形狀被傳遞到 LSTM 中,但它表明檢測到一個 2 維形狀。

我的數據的維度如下:x_train 形狀是(207, 20),y_train 形狀是(207, 5),x_test 形狀是(24, 20),y_test 形狀是(24, 5),

正如您在我的代碼中看到的那樣,我還在為分類用例運行此 LSTM。

正如@Andrey 提到的那樣,LSTM 期望有一個 3D 形狀數據[batch_size, time_steps, feature_size]

例如,如果我們為 32 個批次樣本中的每一個提供 10 個時間步長中的每一個,一個 8 維向量: 輸入數據形狀應該類似於,

X_train = tf.random.normal([32, 10, 8])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM