tf.keras Custom Callback to reduce overfitting which save model weights IFF (val_lass < loss) & (val_loss < min_val_loss)

Viewed 174

How can I build a Custom Callback in Keras which gives me the BEST out of training. In both of ModelCheckPoint and EarlyStopping, I can stop the model and save weights but it only looks at the val_loss most of the time and does not consider the overfitting part while training. I want to build a custom callback which takes into account the minimum validation loss that has ever reached, current validation loss and the current training loss.

I want this callback to Save the weights if the validation loss is less that training loss and it is less than the minimum validation loss that has ever occured. With this, I think I won't have to to use any of the above two and I can be sure that no matter how much I run my model, it won't ever overfit.

I can use the keras.callbacks.Callback as a base class for my model. and access the current loss, current validation loss as:

current_loss = logs.get("loss")
current_val_loss = logs.get("val_loss")
class NoOverfit(keras.callbacks.Callback):

    def __init__(self, patience=0):
        super(NoOverfit, self).__init__():


    def on_train_begin(self, logs=None):
        
        self.min_val_loss = np.Inf

    def on_epoch_end(self, epoch, logs=None):
        current_loss = logs.get("loss")
        current_val_loss = logs.get("val_loss")
        if np.less(current_val_loss, current_loss) & np.less(current_val_loss, self.min_val_loss):
            self.min_val_loss = current_val_loss
        
            self.best_weights = self.model.get_weights()
            # how do I save weights now?

but I'm unable to wrap my head around it how can put it into code that it works and going through the tensorflow documentation tutorial did not help me either.

0 Answers
Related