Skip to content

Instantly share code, notes, and snippets.

@Waateur
Created September 17, 2015 07:57
Show Gist options
  • Select an option

  • Save Waateur/e8e84c8d0ec9452ebba7 to your computer and use it in GitHub Desktop.

Select an option

Save Waateur/e8e84c8d0ec9452ebba7 to your computer and use it in GitHub Desktop.
class EarlyStopping(Callback):
def __init__(self, monitor='val_loss', patience=0, verbose=0,cmp=None):
super(Callback, self).__init__()
self.monitor = monitor
self.patience = patience
self.verbose = verbose
self.best = np.Inf
self.wait = 0
if cmp :
self.cmp=cmp
else :
if "acc" in self.monitor:
self.cmp = max
else:
self.cmp = min
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 self.cmp(current,self.best) == current:
self.best = current
self.wait = 0
else:
if self.wait >= self.patience:
if self.verbose > 0:
print("Epoch %05d: early stopping" % (epoch))
self.model.stop_training = True
self.wait += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment