简体   繁体   English

Keras LSTM input_shape 的问题:预期 lstm_1_input 的形状为 (500, 2) 但得到的数组的形状为 (500, 5)

[英]Problem with Keras LSTM input_shape: expected lstm_1_input to have shape (500, 2) but got array with shape (500, 5)

x_train and y_train are input and output of my model with shapes of (6508, 500, 5), (6508, 5) respectively. x_trainy_train是我的 model 的输入和 output,形状分别为(6508, 500, 5), (6508, 5)

And the model is like this:而model是这样的:

model = Sequential()
model.add(LSTM(units=96, return_sequences=True, input_shape=x_train.shape[1:]))
model.add(Dropout(0.2))
model.add(LSTM(units=96, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=96))
model.add(Dropout(0.2))
model.add(Dense(units=5))

model.compile(loss='mean_squared_error', optimizer='adam', metrics=['mse'])

model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size)

Model Summary: Model 总结:

Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_1 (LSTM)                (None, 500, 96)           39168     
_________________________________________________________________
dropout_1 (Dropout)          (None, 500, 96)           0         
_________________________________________________________________
lstm_2 (LSTM)                (None, 500, 96)           74112     
_________________________________________________________________
dropout_2 (Dropout)          (None, 500, 96)           0         
_________________________________________________________________
lstm_3 (LSTM)                (None, 96)                74112     
_________________________________________________________________
dropout_3 (Dropout)          (None, 96)                0         
_________________________________________________________________
dense_1 (Dense)              (None, 5)                 485       
=================================================================
Total params: 187,877
Trainable params: 187,877
Non-trainable params: 0

The problem is lstm_1 requires input_shape (500, 2) and my data shape is (500, 5):问题是lstm_1需要 input_shape (500, 2) 而我的数据形状是 (500, 5):

ValueError: Error when checking input: expected lstm_1_input to have shape (500, 2) but got array with shape (500, 5)

And I print layers' shape:我打印图层的形状:

for layer in model.layers:
    print(layer.input_shape, end='\t')

# (None, 500, 5)  (None, 500, 96) (None, 500, 96) (None, 500, 96) (None, 500, 96) (None, 96)      (None, 96)

It prints (None, 500, 5) for lstm_1 so I can't figure out the problem.它为lstm_1打印(None, 500, 5)所以我无法找出问题所在。

Keras==2.3.0
tf==1.14.0

UPDATE:更新:

Using keras==2.2.5 or tf.keras solves the problem.使用keras==2.2.5tf.keras可以解决问题。

Mentioning the Solution in Answer Section for the Benefit of the Community.为了社区的利益,在回答部分提到解决方案。

Using tf.keras instead of keras has resolved the problem.使用tf.keras代替keras已经解决了这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 预期 lstm_1_input 有 3 个维度,但得到了形状为 (0, 1) 的数组 - expected lstm_1_input to have 3 dimensions, but got array with shape (0, 1) 检查输入时出错:预期 lstm_1_input 有 3 个维度,但得到形状为 (5, 3) 的数组 - Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (5, 3) keras 中的 LSTM 输入形状 - LSTM input_shape in keras ValueError:检查输入时出错:预期lstm_1_input具有3个维,但数组的形状为(393613,50) - ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (393613, 50) ValueError:检查输入时出错:预期lstm_1_input具有3个维,但数组的形状为(10,1) - ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (10, 1) 检查模型输入时发生错误:预期lstm_1_input具有3维,但数组的形状为(339732,29) - Error when checking model input: expected lstm_1_input to have 3 dimensions, but got array with shape (339732, 29) ValueError:检查输入时出错:预期lstm_1_input具有形状(973,215),但数组的形状为(61,215) - ValueError: Error when checking input: expected lstm_1_input to have shape (973, 215) but got array with shape (61, 215) 检查输入时出错:预期 lstm_1_input 的形状为 (71, 768) 但得到的数组形状为 (72, 768) - Error when checking input: expected lstm_1_input to have shape (71, 768) but got array with shape (72, 768) keras lstm 输入形状不正确 - keras lstm incorrect input_shape 检查输入时出错:预期 lstm_1_input 具有 3 个维度,但仅在 epoch>1 和特定数据集拆分时获得形状为 (0, 1) 的数组 - Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (0, 1) only on epoch>1 and at a specific dataset split
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM