簡體   English   中英

如何修復“檢查輸入時出錯:預期 input_1 有 2 個維度,但得到的數組形狀為 (32, 168, 5)”

[英]How to fix 'Error when checking input: expected input_1 to have 2 dimensions, but got array with shape (32, 168, 5)'

我正在嘗試實現一個多輸入 LSTM-DNN 混合 model,其中兩層的 output 連接在一起。 不幸的是,在開始訓練后會發生這種情況:

ValueError: Error when checking input: expected input_1 to have 2 dimensions, but got array with shape (32, 168, 5)

我研究了其他方法,但最關心的是得到的數組的維度小於預期的情況。 我已經讀過 flatten 可能會有所幫助,但我不確定如何實現它。

這是我的 model:

# first input model
input_1 = Input(shape=(5, ))
input_1_expand = tf.expand_dims(input_1, axis=-1) 
dense_1 = Dense(units=64, input_shape=(None, 5,))(input_1_expand)

# second input model
input_2 = Input(shape=(7, ))
input_2_expand = tf.expand_dims(input_2, axis=-1)
lstm_1 = LSTM(units=64, return_sequences=True, input_shape=(None, 7,))(input_2_expand)

# merge input models
merge = concatenate([dense_1, lstm_1], axis=1)
output = Dense(num_y_signals, activation='sigmoid')(merge)
model = Model(inputs=[input_1, input_2], outputs=output)
# summarize layers
print(model.summary())

model 培訓:

%time
model.fit_generator(generator=generator,
                    epochs=10,
                    steps_per_epoch=30,
                    validation_data=validation_data,
                    callbacks=callbacks)

其中 generator 是產生的 batch_generator function

 [x_batch_1, x_batch_2], y_batch
x_batch_1 shape: (32, 168, 5)
x_batch_2 shape: (32, 168, 7)
y_batch shape: (32, 168, 1)

其中 32 是批量大小,168 是序列長度

我也不確定我是如何實現 tf.expand 和連接軸的。 我剛剛嘗試了使 model 編譯的組合

編輯:我忘了包括validation_data:

validation_data = ([np.expand_dims(x_test1_scaled, axis=0),
                    np.expand_dims(x_test2_scaled, axis=0)],
                   np.expand_dims(y_test_scaled, axis=0))

在哪里

expanded x_test1_scaled Shape: (1, 5808, 5)
expanded x_test2_scaled Shape: (1, 5808, 7)
expanded y_test_scaled Shape: (1, 5808, 1)

您還需要將sequencr length放入輸入形狀中,形狀必須與x_batch.shape[1:]相同

像這樣

input_1 = Input(shape=(168, 5, ))
dense_1 = Dense(units=64, input_shape=(None, 5,))(input_1)

input_2 = Input(shape=(168, 7, ))
lstm_1 = LSTM(units=64, return_sequences=True, input_shape=(None, 7,))(input_2)

我想你試圖使用expand_dims來解決這個問題,所以我不包括在內。

您的 input_1 和 input_2 樣本有 1 個維度,這使得訓練 input_1 或 input_2 只有 2 個維度,但您的生成器每個都有 3 個維度。

input_1 = Input(shape=(5, ))

input_2 = Input(shape=(7, ))

暫無
暫無

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

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