input_shape和模型结构不匹配

时间:2017-07-28 17:41:01

标签: python keras lstm

这是代码:

model = Sequential()
model.add(LSTM(24, input_shape = (trainX.shape[0], 1, 4)))
model.add(Dense(12, activation = 'softmax'))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, epochs=100, batch_size=1, verbose=2)

跑完之后我得到了这个:

ValueError: Input 0 is incompatible with layer lstm_5: expected ndim=3, found ndim=4

任何人都可以向我解释这个吗?以及input_shape和模型结构之间的关系。

1 个答案:

答案 0 :(得分:1)

您的input_shape应为(trainX.shape[1], trainX.shape[2])trainX.shape[0]是培训样本的数量,input_shape不关心; input_shape只关注每个样本的维度,其格式为(timesteps, features)

model.add(LSTM(24, input_shape = (trainX.shape[1], trainX.shape[2])))