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.
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' .
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 ...
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With