0

Is it possible to apply Early Stopping after specified number of epochs in Keras. For example I want to train my NN for 45 epoch, and then start using EarlyStopping.

So far I did it like this:

early_stop = EarlyStopping(monitor='val_loss', mode='min', verbose=1, baseline = 0.1, patience = 3)

opt = Adam(lr = .00001, beta_1=0.9, beta_2=0.999, epsilon=1e-8)
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics = ['accuracy'])
mod = model.fit_generator(train_batches, steps_per_epoch = 66, validation_data = valid_batches, validation_steps = 22, epochs = 45, verbose = 1)
mod = model.fit_generator(train_batches, steps_per_epoch = 66, validation_data = valid_batches, validation_steps = 22, epochs = 50, callbacks = [early_stop], verbose = 1)

But doing it this way, produces only a few step graph for training

enter image description here

Is there a way I can write this all together? Any help much appreciated!

  • Does [this](https://stackoverflow.com/questions/46287403/is-there-a-way-to-implement-early-stopping-in-keras-only-after-the-first-say-1) answer your question? – Parth Shah Jul 12 '20 at 10:41

1 Answers1

-1

I think we can use a custom callback from Keras library to do this. Define your custom callback as follows:

# Custom Callback
class myCallback(keras.callbacks.Callback):
  def on_epoch_end(self, epoch, logs={}):
    if epoch == 45:
      self.model.stop_training = True
callback = myCallback()

opt = Adam(lr = .00001, beta_1=0.9, beta_2=0.999, epsilon=1e-8)
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics = ['accuracy'])
# Include myCallback in callbacks so that the model stops training after 45th epoch
mod = model.fit_generator(train_batches, steps_per_epoch = 66, validation_data = valid_batches, validation_steps = 22, epochs = 50, callbacks = [early_stop, myCallback], verbose = 1)
Dharman
  • 30,962
  • 25
  • 85
  • 135
sai nikhit
  • 24
  • 2