Search code examples
kerasconv-neural-networklstm

How to use multiple layers in ConvLSTM model


I like to check my model using multiple layers of ConvLSTM model. Shape of my training data is

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

Following is my code that works fine

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())

Whey I try to add another convlstm2d layer, it gives an error. I thought when we add another layer, there is no need to input shape. Following is the code I use to add another layer.

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'))  

I get following Value error.

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

Solution

  • For a ConvLSTM(), the input shape to your neural network must be in the form [samples, timesteps, rows, columns, features].

    I can see that you correctly input the data to your ConvLSTM.

    Try using return_sequences = True in the first ConvLSTM2D() and return_sequences = False in the second ConvLSTM2D() layer.