Package ai.djl.training.listener
Class EarlyStoppingListener
- java.lang.Object
-
- ai.djl.training.listener.EarlyStoppingListener
-
- All Implemented Interfaces:
TrainingListener
public final class EarlyStoppingListener extends java.lang.Object implements TrainingListener
Listener that allows the training to be stopped early if the validation loss is not improving, or if time has expired.
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 Classes Modifier and Type Class Description static classEarlyStoppingListener.BuilderA builder for aEarlyStoppingListener.static classEarlyStoppingListener.EarlyStoppedExceptionThrown 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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static EarlyStoppingListener.Builderbuilder()Creates a builder to build aEarlyStoppingListener.voidonEpoch(Trainer trainer)Listens 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 Detail
-
onEpoch
public void onEpoch(Trainer trainer)
Listens to the end of an epoch during training.- Specified by:
onEpochin interfaceTrainingListener- Parameters:
trainer- the trainer the listener is attached to
-
onTrainingBatch
public void onTrainingBatch(Trainer trainer, TrainingListener.BatchData batchData)
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
public void onValidationBatch(Trainer trainer, TrainingListener.BatchData batchData)
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
public void onTrainingBegin(Trainer trainer)
Listens to the beginning of training.- Specified by:
onTrainingBeginin interfaceTrainingListener- Parameters:
trainer- the trainer the listener is attached to
-
onTrainingEnd
public void onTrainingEnd(Trainer trainer)
Listens to the end of training.- Specified by:
onTrainingEndin interfaceTrainingListener- Parameters:
trainer- the trainer the listener is attached to
-
builder
public static EarlyStoppingListener.Builder builder()
Creates a builder to build aEarlyStoppingListener.- Returns:
- a new builder
-
-