多层LSTM神经网络),出现ValueError: Input 0 is incompatible with layer lstm_21: expected ndim=3, found错误

下面是LSTM模型,运行后出现ValueError: Input 0 is incompatible with layer lstm_21: expected ndim=3, found错误

model = Sequential()

model.add(LSTM(50, activation='tanh',input_shape=(X_train.shape[1],X_train.shape[2])))
model.add(Dropout(0.4))
model.add(LSTM(30, activation='tanh'))
model.add(Dropout(0.4))
model.add(Dense(5,activation='softmax')) 
#model.compile(loss='categorical_crossentropy', optimizer=SGD(lr=0.003, momentum=0.9, nesterov=True), metrics=['accuracy'])
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

model.summary()

在LSTM层前加上当前层 的上一层中添加return_sequences=True这个参数就可以了,使其返回ndim=3的序列。

model.add(LSTM(50, activation='tanh',input_shape=(X_train.shape[1],X_train.shape[2]),return_sequences=True))

你可能感兴趣的:(Python数据处理)