这是代码:

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与模型结构之间的关系。

最佳答案

您的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])))

关于python - input_shape和模型结构不匹配,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45379437/

10-12 22:49