0

I'm not very familiar with python super functionality and inheritance. I tried to copy and use the keras custom callback example I found in this post, but I'm getting the error:

    super(EarlyStopping, self).__init__()
TypeError: super(type, obj): obj must be an instance or subtype of type

Here's the example code:

import numpy as np
from tensorflow.keras.callbacks import Callback, EarlyStopping

class OverfitEarlyStopping(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  # Allow instances to be re-used

    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 ratio current_loss / current_val_loss > self.ratio
        if self.monitor_op(np.divide(current_train,current_val),self.ratio):
            self.wait = 0
        else:
            if 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:
            print('Epoch %05d: early stopping due to overfitting.' % (self.stopped_epoch))

overfit_callback = OverfitEarlyStopping(ratio=0.8, patience=3, verbose=1)

I'm using Python 3.5 and tensorflow.keras. Has the use of super changed in the versions I'm using, or was this callback written incorrectly in the first place?

Austin
  • 6,921
  • 12
  • 73
  • 138

1 Answers1

0

There is no need to initialize super when extending the base class keras.callbacks.Callback. The model is passed to the functions you choose to override. You can see some examples here

Also, why not use tf.keras.callbacks.EarlyStopping

Kurtis Streutker
  • 1,157
  • 10
  • 13
  • Thanks. I’d like to stop when overfitting which requires knowledge of loss and val_loss – Austin Jan 13 '19 at 20:06
  • When you initialize ```EarlyStopping``` you can set the monitor to ```loss``` or ```val_loss``` – Kurtis Streutker Jan 13 '19 at 20:22
  • The network is overfitting if loss is going down at a higher rate than val_loss, so you need to monitor the ratio of the two. You wouldn’t want a network where val_loss is going down by a negligible amount each epoch while loss is going down drastically. – Austin Jan 13 '19 at 20:23