如何在ConvLSTM模型中使用多层

时间:2019-05-16 07:07:12

标签: keras conv-neural-network lstm

我喜欢使用多层ConvLSTM模型检查我的模型。我的训练数据的形状是

trainX.shape (5000, 200, 4) # testX.shape (2627, 200, 4)

以下是我的代码,效果很好

print('trainX.shape', trainX.shape) # trainX.shape (5000, 200, 4)
print('testX.shape', testX.shape) #  testX.shape (2627, 200, 4)
# reshape into subsequences (samples, time steps, rows, cols, channels)
samples, n_features = trainX.shape[0], trainX.shape[2]
n_steps, n_length = 8, 25
trainX = trainX.reshape((samples, n_steps, 1, n_length, n_features)) # 
print('trainX.shape', trainX.shape) # (5000, 8, 1, 25, 4)
testX = testX.reshape((testX.shape[0], n_steps, 1, n_length, n_features))
print('testX.shape', testX.shape) # (2627, 8, 1, 25, 4)

# define model
model = Sequential()
model.add(ConvLSTM2D(filters=64, kernel_size=(1,3), activation='relu', input_shape=(n_steps, 1, n_length, n_features)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='softmax'))   

print(model.summary())

为什么我尝试添加另一个convlstm2d层,但出现错误。我认为当我们添加另一层时,无需输入形状。以下是我用来添加另一层的代码。

model = Sequential()
model.add(ConvLSTM2D(filters=64, kernel_size=(1,3), activation='relu', input_shape=(n_steps, 1, n_length, n_features)))
model.add(ConvLSTM2D(filters=64, kernel_size=(1,3), activation='relu'))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='softmax'))  

我收到以下Value错误。

ValueError: Input 0 is incompatible with layer conv_lst_m2d_11: expected ndim=5, found ndim=4

1 个答案:

答案 0 :(得分:1)

对于ConvLSTM(),神经网络的输入形状为[样本,时间步长,行,列,特征]。

我可以看到您已将数据正确输入到ConvLSTM中。

尝试在第一个ConvLSTM2D 中使用return_sequences = True ,在第二个ConvLSTM2D 层中使用return_sequences = False