Created
September 17, 2015 07:57
-
-
Save Waateur/e8e84c8d0ec9452ebba7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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