composer.callbacks.early_stopper#
Early stopping callback.
Classes
This callback tracks a training or evaluation metric and halts training if the metric does not improve within a given interval. |
- class composer.callbacks.early_stopper.EarlyStopper(monitor, dataloader_label, comp=None, min_delta=0.0, patience=1)[source]#
Bases:
composer.core.callback.Callback
This callback tracks a training or evaluation metric and halts training if the metric does not improve within a given interval.
Example
>>> from composer.callbacks.early_stopper import EarlyStopper >>> from torchmetrics.classification.accuracy import Accuracy >>> # constructing trainer object with this callback >>> early_stopper = EarlyStopper("Accuracy", "my_evaluator", patience=1) >>> evaluator = Evaluator( ... dataloader = eval_dataloader, ... label = 'my_evaluator', ... metrics = Accuracy() ... ) >>> trainer = Trainer( ... model=model, ... train_dataloader=train_dataloader, ... eval_dataloader=evaluator, ... optimizers=optimizer, ... max_duration="1ep", ... callbacks=[early_stopper], ... )
- Parameters
monitor (str) โ The name of the metric to monitor.
dataloader_label (str) โ The label of the dataloader or evaluator associated with the tracked metric. If monitor is in an Evaluator, the dataloader_label field should be set to the Evaluatorโs label. If monitor is a training metric or an ordinary evaluation metric not in an Evaluator, dataloader_label should be set to โtrainโ or โevalโ respectively.
comp (Union[str, Callable[[Any, Any], Any]], optional) โ A comparison operator to measure change of the monitored metric. The comparison operator will be called
comp(current_value, prev_best)
. For metrics where the optimal value is low (error, loss, perplexity), use a less than operator and for metrics like accuracy where the optimal value is higher, use a greater than operator. Defaults totorch.less()
if loss, error, or perplexity are substrings of the monitored metric, otherwise defaults totorch.greater()
min_delta (float, optional) โ An optional float that requires a new value to exceed the best value by at least that amount. Defaults to 0.
patience (int | str | Time, optional) โ The interval of time the monitored metric can not improve without stopping training. Defaults to 1 epoch. If patience is an integer, it is interpreted as the number of epochs.