In addition to the basic early stopping features in KERAS, I would like to stop training if the validation accuracy is still below 0.80 at epoch 40.

How can I do that?


1 Answer 1


I don't think an existing callback with such functionality exists, but you can write a custom callback to accomplish this. See the following example, copied from this stackoverflow answer and modified for your specific example:

from keras.callbacks import Callback

class TerminateOnBaseline(Callback):
    Callback that terminates training when either acc or val_acc reaches a specified
    baseline after a specified number of epochs
    def __init__(self, monitor='val_accuracy', baseline=0.8, n_epochs=40):
        super(TerminateOnBaseline, self).__init__()
        self.monitor = monitor
        self.baseline = baseline
        self.n_epochs = n_epochs

    def on_epoch_end(self, epoch, logs=None):
        logs = logs or {}
        acc = logs.get(self.monitor)
        if acc is not None:
            if acc < self.baseline and epoch >= self.n_epochs:
                print('Epoch %d: Accuracy has not reached the baseline, terminating training' % (epoch))
                self.model.stop_training = True

You can then include this callback when training your model using the callbacks argument in the fit method.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.