-1
adam = tf.keras.optimizers.Adam(learning_rate = 0.0001, beta_1 = 0.9, beta_2 = 0.999, amsgrad = False)
my_model.compile(loss = "categorical_crossentropy", optimizer = adam , metrics = ['accuracy'])

earlystopping = EarlyStopping(monitor = 'val_loss', verbose = 1, patience = 20, restore_best_weights=True)

history = my_model.fit(train_gen, validation_data=val_gen, batch_size = 32, epochs = 20, callbacks=[earlystopping])

I applied Earlystopping, then fit function run for all 20 epochs and did not stop even when val_loss increased. What should be the right way to use earlystopping?

Reda El Hail
  • 966
  • 1
  • 7
  • 17

1 Answers1

0

There is no problem with your code. You should only decrease the patience argument in EarlyStopping

You have 20 epochs and setting patience to 20 is the problem.

This is the definition given by Tensorflow:

patience: Number of epochs with no improvement after which training will be stopped.

I propose to change it to this:

earlystopping = EarlyStopping(monitor = 'val_loss', verbose = 1, patience = 5, restore_best_weights=True)

Now if the val_loss don't decrease for 5 consecutive epochs, the training will stop.

Reda El Hail
  • 966
  • 1
  • 7
  • 17