Package ai.djl.training.listener
Class EarlyStoppingListener
java.lang.Object
ai.djl.training.listener.EarlyStoppingListener
- All Implemented Interfaces:
TrainingListener
Listener that allows the training to be stopped early if the validation loss is not improving, or
if time has expired.
Note: Ensure that Metrics are set on the trainer.
Usage: Add this listener to the training config, and add it as the last one.
new DefaultTrainingConfig(...) .addTrainingListeners(EarlyStoppingListener.builder() .setEpochPatience(1) .setEarlyStopPctImprovement(1) .setMaxDuration(Duration.ofMinutes(42)) .setMinEpochs(1) .build() );
Then surround the fit with a try catch that catches the EarlyStoppingListener.EarlyStoppedException
.
Example:
try { EasyTrain.fit(trainer, 5, trainDataset, testDataset); } catch (EarlyStoppingListener.EarlyStoppedException e) { // handle early stopping log.info("Stopped early at epoch {} because: {}", e.getEpoch(), e.getMessage()); }
Note: Ensure that Metrics are set on the trainer.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
A builder for aEarlyStoppingListener
.static class
Thrown when training is stopped early, the message will contain the reason why it is stopped early.Nested classes/interfaces inherited from interface ai.djl.training.listener.TrainingListener
TrainingListener.BatchData, TrainingListener.Defaults
-
Method Summary
Modifier and TypeMethodDescriptionbuilder()
Creates a builder to build aEarlyStoppingListener
.void
Listens to the end of an epoch during training.void
onTrainingBatch
(Trainer trainer, TrainingListener.BatchData batchData) Listens to the end of training one batch of data during training.void
onTrainingBegin
(Trainer trainer) Listens to the beginning of training.void
onTrainingEnd
(Trainer trainer) Listens to the end of training.void
onValidationBatch
(Trainer trainer, TrainingListener.BatchData batchData) Listens to the end of validating one batch of data during validation.
-
Method Details
-
onEpoch
Listens to the end of an epoch during training.- Specified by:
onEpoch
in interfaceTrainingListener
- Parameters:
trainer
- the trainer the listener is attached to
-
onTrainingBatch
Listens to the end of training one batch of data during training.- Specified by:
onTrainingBatch
in interfaceTrainingListener
- Parameters:
trainer
- the trainer the listener is attached tobatchData
- the data from the batch
-
onValidationBatch
Listens to the end of validating one batch of data during validation.- Specified by:
onValidationBatch
in interfaceTrainingListener
- Parameters:
trainer
- the trainer the listener is attached tobatchData
- the data from the batch
-
onTrainingBegin
Listens to the beginning of training.- Specified by:
onTrainingBegin
in interfaceTrainingListener
- Parameters:
trainer
- the trainer the listener is attached to
-
onTrainingEnd
Listens to the end of training.- Specified by:
onTrainingEnd
in interfaceTrainingListener
- Parameters:
trainer
- the trainer the listener is attached to
-
builder
Creates a builder to build aEarlyStoppingListener
.- Returns:
- a new builder
-