Early Stopping Keras after specified number of epoch

794 Views Asked by At

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!

1

There are 1 best solutions below

1
On

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)