Created
October 8, 2018 16:51
-
-
Save sdcubber/58e71f307f5348a6d0abdf6d6f96ff33 to your computer and use it in GitHub Desktop.
This file contains 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
from keras.callbacks import EarlyStopping # use as base class | |
class MyCallBack(EarlyStopping): | |
def __init__(self, threshold, min_epochs, **kwargs): | |
super(MyCallBack, self).__init__(**kwargs) | |
self.threshold = threshold # threshold for validation loss | |
self.min_epochs = min_epochs # min number of epochs to run | |
def on_epoch_end(self, epoch, logs=None): | |
current = logs.get(self.monitor) | |
if current is None: | |
warnings.warn( | |
'Early stopping conditioned on metric `%s` ' | |
'which is not available. Available metrics are: %s' % | |
(self.monitor, ','.join(list(logs.keys()))), RuntimeWarning | |
) | |
return | |
# implement your own logic here | |
if (epoch >= self.min_epochs) & (current >= self.threshold): | |
self.stopped_epoch = epoch | |
self.model.stop_training = True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment