Search code examples
keras

How to use EarlyStopping to stop training when val_acc reaches a certain percentage


I want to stop the training when it reaches a certain percentage, say 98%. I tried many ways and search on Google with no luck. What I do is using EarlyStopping like this:

es = EarlyStopping(monitor='val_acc', baseline=0.98, verbose=1)
model.fit(tr_X, tr_y, epochs=1000, batch_size=1000, validation_data=(ts_X, ts_y), verbose=1, callbacks=[es])

_, train_acc = model.evaluate(tr_X, tr_y, verbose=0)
_, test_acc = model.evaluate(ts_X, ts_y, verbose=0)
print('>> Train: %.3f, Test: %.3f' % (train_acc, test_acc))

This isn't correct. I would truly appreciate if someone can suggest a way to achieve this goal.

Thank you,


Solution

  • You can create a new callback like that :

    class EarlyStoppingByValAcc(Callback):
        def __init__(self, monitor='val_acc', value=0.98, verbose=1):
            super(Callback, self).__init__()
            self.monitor = monitor
            self.value = value
            self.verbose = verbose
    
        def on_epoch_end(self, epoch, logs={}):
            current = logs.get(self.monitor)
            if current is None:
                warnings.warn("Early stopping requires %s available!" % self.monitor, RuntimeWarning)
    
            if current > self.value:
                if self.verbose > 0:
                    print("Epoch %05d: early stopping THR" % epoch)
                self.model.stop_training = True
    

    and you can use it like that :

    callbacks = [
                 EarlyStoppingByValAcc(monitor='val_acc', value=0.98, verbose=1),
                ]
    model.fit(tr_X, tr_y, epochs=1000, batch_size=1000, validation_data=(ts_X, ts_y), verbose=1, callbacks=callbacks)