如何将RNN与CNN结合?

时间:2018-10-22 15:49:54

标签: python keras deep-learning conv-neural-network lstm

我正在尝试将LSTM与CNN结合使用,但由于出错而卡住了。 这是我要实现的模型:

model=Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(28, 28,3), activation='relu'))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(32, activation='relu'))
model.add(LSTM(128, return_sequences=True,input_shape=(1,32), activation='relu'))
model.add(LSTM(256))
model.add(Dropout(0.25))
model.add(Dense(37))
model.compile(loss='categorical_crossentropy', optimizer='adam')

第一个LSTM层中发生错误:

ERROR: Input 0 is incompatible with layer lstm_12: expected ndim=3, found ndim=2

1 个答案:

答案 0 :(得分:2)

LSTM层的输入应该是一个3D数组,该数组代表一个序列或一个时间序列(这就是错误试图说的:expected ndim=3)。但是,在您的模型中,LSTM层的输入实际上是之前的Dense层的输出,是2D数组(即found ndim=2)。要使其变成形状为(n_samples, n_timesteps, n_features)的3D数组,一种解决方案是使用RepeatVector层将其重复多达时间步长(您需要在代码中指定):

model.add(Dense(32, activation='relu'))
model.add(RepeatVector(n_timesteps))
model.add(LSTM(128, return_sequences=True, input_shape=(n_timesteps,32), activation='relu'))