QBoard » Artificial Intelligence & ML » AI and ML - Python » How to tell Keras stop training based on loss value?

How to tell Keras stop training based on loss value?

  • Currently I use the following code:
    callbacks = [
        EarlyStopping(monitor='val_loss', patience=2, verbose=0),
        ModelCheckpoint(kfold_weights_path, monitor='val_loss', save_best_only=True, verbose=0),
    ]
    model.fit(X_train.astype('float32'), Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
          shuffle=True, verbose=1, validation_data=(X_valid, Y_valid),
          callbacks=callbacks)​

    It tells Keras to stop training when loss didn't improve for 2 epochs. But I want to stop training after loss became smaller than some constant "THR":
    if val_loss < THR:
        break​
      September 2, 2021 10:53 PM IST
    0
  • I found the answer. I looked into Keras sources and find out code for EarlyStopping. I made my own callback, based on it:

    class EarlyStoppingByLossVal(Callback):
        def __init__(self, monitor='val_loss', value=0.00001, verbose=0):
            super(Callback, self).__init__()
            self.monitor = monitor
            self.value = value
            self.verbose = verbose
    
        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 current < self.value:
                if self.verbose > 0:
                    print("Epoch %05d: early stopping THR" % epoch)
                self.model.stop_training = True

     

    And usage:

    callbacks = [
        EarlyStoppingByLossVal(monitor='val_loss', value=0.00001, verbose=1),
        # EarlyStopping(monitor='val_loss', patience=2, verbose=0),
        ModelCheckpoint(kfold_weights_path, monitor='val_loss', save_best_only=True, verbose=0),
    ]
    model.fit(X_train.astype('float32'), Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
          shuffle=True, verbose=1, validation_data=(X_valid, Y_valid),
          callbacks=callbacks)
      September 3, 2021 12:56 PM IST
    0
  • One solution is to call model.fit(nb_epoch=1, ...) inside a for loop, then you can put a break statement inside the for loop and do whatever other custom control flow you want.
      September 22, 2021 12:07 AM IST
    0
  • I solved the same problem using custom callback.

    In the following custom callback code assign THR with the value at which you want to stop training and add the callback to your model.

    from keras.callbacks import Callback
    
    class stopAtLossValue(Callback):
    
            def on_batch_end(self, batch, logs={}):
                THR = 0.03 #Assign THR with the value at which you want to stop training.
                if logs.get('loss') <= THR:
                     self.model.stop_training = True
      September 28, 2021 1:51 PM IST
    0
  • The keras.callbacks.EarlyStopping callback does have a min_delta argument. From Keras documentation:

    min_delta: minimum change in the monitored quantity to qualify as an improvement, i.e. an absolute change of less than min_delta, will count as no improvement.

      September 29, 2021 2:19 PM IST
    0
  • Keras is an open-source neural network library written in Python. There are many classes in Keras. Keras supports the early stopping of training via a callback called EarlyStopping. You can use

    tf.keras.callbacks.EarlyStopping
    

    For example:

    #python implementation
    
    callbacks = [ EarlyStoppingByLossVal(monitor='val_loss', value=0.00001, verbose=1)
    
    ModelCheckpoint(kfold_weights_path, monitor='val_loss', save_best_only=True, verbose=0), ] model.fit(X_train.astype('float32'),
    
    Y_train, batch_size=batch_size, nb_epoch=nb_epoch, shuffle=True, verbose=1, validation_data=(X_valid, Y_valid), callbacks=callbacks)

    It stops training when a monitored quantity(threshold value) has stopped improving. This callback allows you to specify the performance measure to monitor the trigger, and once triggered, it will stop the training process. The EarlyStopping callback is configured when instantiated via arguments.

      October 7, 2021 1:17 PM IST
    0