I am using a stateful LSTM regression model and I would like to apply the EarlyStopping function. In stateful LSTMs as I was reading the states should be reset at each epoch. However, I noticed that when I was resetting the states the EarlyStopping method wasn't working at all. I attach also the code.
model = Sequential()
model.add(LSTM(256, batch_input_shape=(batch_size, timesteps, features), return_sequences=False, stateful=True))
model.add(Dropout(rate=0.2))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='mean_squared_error', optimizer='adam')
mc = ModelCheckpoint('best_model.h5', monitor='val_loss', mode='min', verbose=0, save_best_only=True)
es = EarlyStopping(monitor='val_loss', mode='min', patience=1, restore_best_weights=True, verbose=1)
for epoch in range(epochs):
print("Epoch: ", epoch + 1)
hist = model.fit(train_x, train_y, epochs=1, batch_size, shuffle=False,
validation_data=(validation_x, validation_y), verbose=2, callbacks=[mc, es])
model.reset_states()
If I run the above code without the for loop and without the reset of states then the EarlyStopping works fine. Is there any way to apply the EarlyStopping in a for loop?
Thank you in advance