EarlyStopping#
- class ignite.handlers.early_stopping.EarlyStopping(patience, score_function, trainer, threshold=0.0, cumulative=False, threshold_mode='abs', mode='max', min_delta=None, min_delta_mode=None, cumulative_delta=None)[source]#
EarlyStopping handler can be used to stop the training if no improvement after a given number of events.
- Parameters:
patience (int) – Number of events to wait if no improvement and then stop the training.
score_function (Callable) – It should be a function taking a single argument, an
Engineobject, and return a scorefloat. An improvement is considered if the score is higher (formode='max') or lower (formode='min').trainer (Engine) – Trainer engine to stop the run if no improvement.
threshold (float) – A minimum change in the score to qualify as an improvement. For
mode='max', it is a minimum increase; formode='min', it is a minimum decrease. An improvement is only considered if the change exceeds the threshold determined bythresholdandthreshold_mode.cumulative (bool) – If True,
thresholddefines the change since the lastpatiencereset, otherwise it defines the change after the last event. Default value is False.threshold_mode (Literal['abs', 'rel']) –
Determines whether
thresholdis an absolute change or a relative change.In
'abs'mode:For
mode='max': improvement ifscore > best_score + thresholdFor
mode='min': improvement ifscore < best_score - threshold
In
'rel'mode:For
mode='max': improvement ifscore > best_score * (1 + threshold)For
mode='min': improvement ifscore < best_score * (1 - threshold)
Possible values are
"abs"and"rel". Default value is"abs".mode (Literal['min', 'max']) – Whether to maximize (
'max') or minimize ('min') the score. Default is'max'.min_delta (float | None) –
min_delta_mode (Literal['abs', 'rel'] | None) –
cumulative_delta (bool | None) –
Examples
from ignite.engine import Engine, Events from ignite.handlers import EarlyStopping def score_function(engine): val_loss = engine.state.metrics["nll"] return -val_loss handler = EarlyStopping( patience=10, score_function=score_function, trainer=trainer, ) # Note: the handler is attached to an *Evaluator* evaluator.add_event_handler(Events.COMPLETED, handler)
Changed in version 0.6.0: Renamed
min_delta_modetothreshold_mode. Renamedmin_deltatothreshold. Renamedcumulative_deltatocumulative.Changed in version 0.5.4: Added mode parameter to support minimization in addition to maximization. Added min_delta_mode parameter to support both absolute and relative improvements.
Methods
Attaches the early stopping handler to an engine and registers its reset callback.
Method replace internal state of the class with provided state dict data.
Reset the early stopping state, including the counter and best score.
Method returns state dict with
counterandbest_score.- attach(engine, event=Events.COMPLETED, reset_engine=None, reset_event=Events.STARTED, *args, **kwargs)[source]#
Attaches the early stopping handler to an engine and registers its reset callback.
This method will: 1. Add the early stopping evaluation logic (
self) toengineon the givenevent. 2. Add theresetmethod toreset_engine(orengineif not provided) on the givenreset_event.- Parameters:
engine (Engine) – The engine to attach the early stopping evaluation to (typically an evaluator).
event (Any) – The event on
enginethat triggers the early stopping check. Default isCOMPLETED.reset_engine (Engine | None) – The engine to attach the reset callback to (typically the trainer). If
None, defaults toengine.reset_event (Any) – The event on
reset_enginethat triggers the handler state reset. Default isSTARTED.args (Any) –
kwargs (Any) –
- Return type:
None
New in version 0.5.4.
- load_state_dict(state_dict)[source]#
Method replace internal state of the class with provided state dict data.
- Parameters:
state_dict (Mapping) – a dict with “counter” and “best_score” keys/values.
- Return type:
None
- reset()[source]#
Reset the early stopping state, including the counter and best score.
New in version 0.5.4.
- Return type:
None