EarlyStopping

Contents

EarlyStopping#

class stable_pretraining.callbacks.EarlyStopping(mode: str = 'min', milestones: dict[int, float] | None = None, metric_name: str | None = None, patience: int = 10)[source]#

Bases: Module

Early stopping mechanism with support for metric milestones and patience.

This module provides flexible early stopping capabilities that can halt training based on metric performance. It supports both milestone-based stopping (stop if metric doesn’t reach target by specific epochs) and patience-based stopping (stop if metric doesn’t improve for N epochs).

Parameters:
  • mode – Optimization direction - ‘min’ for metrics to minimize (e.g., loss), ‘max’ for metrics to maximize (e.g., accuracy).

  • milestones – Dict mapping epoch numbers to target metric values. Training stops if targets are not met at specified epochs.

  • metric_name – Name of the metric to monitor if metric is a dict.

  • patience – Number of epochs with no improvement before stopping.

Example

>>> early_stop = EarlyStopping(mode="max", milestones={10: 0.8, 20: 0.9})
>>> # Stops if accuracy < 0.8 at epoch 10 or < 0.9 at epoch 20