Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EarlyStopping callback behaving mysteriously in Keras

Tags:

My model stops training after the 4th epoch even though I expect it to continue training beyond that. I've set monitor to validation loss and patience to 2, which I thought means that training stops after validation loss increases consecutively for 2 epochs. However, training seems to stop before that happens.

I've defined EarlyStopping as follows:

callbacks = [
        EarlyStopping(monitor='val_loss', patience=2, verbose=0),
    ]

And in the fit function I use it like this:

hist = model.fit_generator(
            generator(imgIds, batch_size=batch_size, is_train=True),
            validation_data=generator(imgIds, batch_size=batch_size, is_val=True),
            validation_steps=steps_per_val,
            steps_per_epoch=steps_per_epoch,
            epochs=epoch_count,
            verbose=verbose_level,
            callbacks=callbacks)

I don't understand why training ends after the 4th epoch.

675/675 [==============================] - 1149s - loss: 0.1513 - val_loss: 0.0860
Epoch 2/30
675/675 [==============================] - 1138s - loss: 0.0991 - val_loss: 0.1096
Epoch 3/30
675/675 [==============================] - 1143s - loss: 0.1096 - val_loss: 0.1040
Epoch 4/30
675/675 [==============================] - 1139s - loss: 0.1072 - val_loss: 0.1019
Finished training intermediate1.
like image 812
megashigger Avatar asked Oct 10 '17 00:10

megashigger


People also ask

What is EarlyStopping in Keras?

EarlyStopping classStop training when a monitored metric has stopped improving. Assuming the goal of a training is to minimize the loss. With this, the metric to be monitored would be 'loss' , and mode would be 'min' .

What is EarlyStopping callback?

Early Stopping in Keras. Keras supports the early stopping of training via a callback called EarlyStopping. 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 ...

What does callback do in Keras?

A callback is an object that can perform actions at various stages of training (e.g. at the start or end of an epoch, before or after a single batch, etc). You can use callbacks to: Write TensorBoard logs after every batch of training to monitor your metrics. Periodically save your model to disk.

What is the purpose of mode parameter in TF Keras callbacks EarlyStopping with value min?

In min mode, training will stop when the quantity monitored has stopped decreasing; in "max" mode it will stop when the quantity monitored has stopped increasing; in "auto" mode, the direction is automatically inferred from the name of the monitored quantity. Baseline value for the monitored quantity.


1 Answers

I think your interpretation of the EarlyStopping callback is a little off; it stops when the loss doesn't improve from the best loss it has ever seen for patience epochs. The best loss your model had was 0.0860 at epoch 1, and for epochs 2 and 3 the loss did not improve, so it should have stopped training after epoch 3. However, it continues to train for one more epoch due to an off-by-one error, at least I would call it that given what the docs say about patience, which is:

patience: number of epochs with no improvement after which training will be stopped.

From the Keras source code (edited slightly for clarity):

class EarlyStopping(Callback):
    def on_epoch_end(self, epoch, logs=None):
        current = logs.get(self.monitor)

        if np.less(current - self.min_delta, self.best):
            self.best = current
            self.wait = 0
        else:
            if self.wait >= self.patience:
                self.stopped_epoch = epoch
                self.model.stop_training = True
            self.wait += 1

Notice how self.wait isn't incremented until after the check against self.patience, so while your model should have stopped training after epoch 3, it continued for one more epoch.

Unfortunately it seems if you want a callback that behaves the way you described, where it stops training without consecutive improvement in patience epochs, you'd have to write it yourself. But I think you could just modify the EarlyStopping callback slightly to accomplish this.

Edit: The off-by-one error is fixed.

like image 191
Nicole White Avatar answered Oct 04 '22 20:10

Nicole White