class CustomEarlyStopping(Callback):

    def __init__(self, ratio=0.0, patience=0, verbose=0):
        super(EarlyStopping, self).__init__()
self.ratio = ratio
self.patience = patience
self.verbose = verbose
self.wait = 0
self.stopped_epoch = 0
self.monitor_op = np.greater

def on_train_begin(self, logs=None):
    self.wait = 0

def on_epoch_end(self, epoch, logs=None):
    current_val = logs.get('val_loss')
current_train = logs.get('loss')
if current_val is None:
    warnings.warn('Early stopping requires %s available!' % self.monitor,
        RuntimeWarning)
if self.monitor_op(np.divide(current_train, current_val), self.ratio):
    self.wait = 0
elif self.wait >= self.patience:
    self.stopped_epoch = epoch
self.model.stop_training = True
self.wait += 1

def on_train_end(self, logs=None):
    if self.stopped_epoch > 0 and self.verbose > 0:
        pass
Keras Callback EarlyStopping comparing training and validation loss