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 classA builder for aEarlyStoppingListener.static classThrown 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.voidListens to the end of an epoch during training.voidonTrainingBatch(Trainer trainer, TrainingListener.BatchData batchData) Listens to the end of training one batch of data during training.voidonTrainingBegin(Trainer trainer) Listens to the beginning of training.voidonTrainingEnd(Trainer trainer) Listens to the end of training.voidonValidationBatch(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:
onEpochin 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:
onTrainingBatchin 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:
onValidationBatchin interfaceTrainingListener- Parameters:
trainer- the trainer the listener is attached tobatchData- the data from the batch
-
onTrainingBegin
Listens to the beginning of training.- Specified by:
onTrainingBeginin interfaceTrainingListener- Parameters:
trainer- the trainer the listener is attached to
-
onTrainingEnd
Listens to the end of training.- Specified by:
onTrainingEndin interfaceTrainingListener- Parameters:
trainer- the trainer the listener is attached to
-
builder
Creates a builder to build aEarlyStoppingListener.- Returns:
- a new builder
-