I want to implement a Stateful Convolutional LSTM in this way:
# build CNN/LSTM and train it.
#
model = Sequential()
# build CNN/LSTM and train it.
model.add(TimeDistributed(Conv2D(16, (3, 3), padding='same'), input_shape=(210, 22, 26, 1)))
model.add(Activation('elu'))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))
model.add(Dropout(0.2))
model.add(TimeDistributed(Conv2D(32, (3, 3), padding='same')))
model.add(Activation('elu'))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))
model.add(Dropout(0.2))
model.add(TimeDistributed(Conv2D(64, (3, 3), padding='same')))
model.add(Activation('elu'))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))
model.add(TimeDistributed(Flatten()))
model.add(Conv1D(16, 3, padding='same'))
model.add(Activation('elu'))
model.add(MaxPooling1D(pool_size=8))
model.add(Bidirectional(LSTM(64, batch_input_shape=(32, 26, 16), return_sequences=True, stateful=True)))
model.add(Activation('elu'))
model.add(Bidirectional(LSTM(128, return_sequences=False, stateful=True)))
model.add(Activation('elu'))
model.add(Dense(1, activation='sigmoid'))
adammgm = keras.optimizers.Adam(lr=0.0005, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0001)
model.compile(loss='mean_squared_error', optimizer=adammgm, metrics=['accuracy'])
print(model.summary())
# saves the model weights after each epoch
# if the validation loss decreased
#
checkpointer = ModelCheckpoint(filepath= odir_a + "/temp_lstm_model_weights-{epoch:02d}.h5")
model.fit_generator(generate_reduced_dimension_arrays_from_file(ilist_a, edfroot_a, featroot_a, labroot_a, feat_ext_a, lab_ext_a, num_channels_a, feat_fdur_a, win_len_a, models_order, lstm_batch_size_a, NEDC_DEF_BATCH_FILE), steps_per_epoch=NEDC_DEF_STEPS_PER_EPOCH, epochs=lstm_epoch_size_a, callbacks=[checkpointer])
But I got this error when I wanted to implement this structure:
ValueError: If a RNN is stateful, it needs to know its batch size. Specify the batch size of your input tensors:
- If using a Sequential model, specify the batch size by passing a batch_input_shape
argument to your first layer.
- If using the functional API, specify the time dimension by passing a batch_shape
argument to your Input layer.
I real all the posts but I still do not know how to fix this issue.
The problem was solved. The solution is using the batch_input_shape instead of input_shape in the first layer of CNN: