Shortcuts

EarlyStopping#

class ignite.handlers.early_stopping.EarlyStopping(patience, score_function, trainer, min_delta=0.0, cumulative_delta=False)[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 Engine object, and return a score float. An improvement is considered if the score is higher.

  • trainer (Engine) – Trainer engine to stop the run if no improvement.

  • min_delta (float) – A minimum increase in the score to qualify as an improvement, i.e. an increase of less than or equal to min_delta, will count as no improvement.

  • cumulative_delta (bool) – It True, min_delta defines an increase since the last patience reset, otherwise, it defines an increase after the last event. Default value is False.

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* (runs one epoch on validation dataset).
evaluator.add_event_handler(Events.COMPLETED, handler)

Methods

load_state_dict

Method replace internal state of the class with provided state dict data.

state_dict

Method returns state dict with counter and best_score.

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

state_dict()[source]#

Method returns state dict with counter and best_score. Can be used to save internal state of the class.

Return type

OrderedDict[str, float]