Skip to content

EarlyStoppingCallback

Module implementing early stopping functionality for model training.

This module provides an early stopping callback that monitors a metric during training and stops the training process if no improvement is seen for a specified number of evaluations. This helps prevent overfitting by stopping training when the model performance plateaus or starts to degrade on validation data.

EarlyStoppingCallback

EarlyStoppingCallback(patience: int = 3, min_delta: float = 0.0)

Callback to implement early stopping during training.

ATTRIBUTE DESCRIPTION
patience

Number of evaluations with no improvement after which training will be stopped.

TYPE: int

min_delta

Minimum change in the monitored metric to qualify as an improvement.

TYPE: float

best_score

Best score observed so far.

TYPE: float

counter

Number of consecutive evaluations with no improvement.

TYPE: int

stop_training

Flag to indicate whether to stop training.

TYPE: bool

Source code in src/training/common/early_stopping.py
def __init__(self, patience: int = 3, min_delta: float = 0.0):
    self.patience = patience
    self.min_delta = min_delta
    self.best_score = None
    self.counter = 0
    self.stop_training = False

best_score instance-attribute

best_score = None

counter instance-attribute

counter = 0

min_delta instance-attribute

min_delta = min_delta

patience instance-attribute

patience = patience

stop_training instance-attribute

stop_training = False

on_evaluate

on_evaluate(score: float, epoch: int, steps: int)

Evaluate current score and update early stopping state.

PARAMETER DESCRIPTION
score

Current evaluation score to compare against best score

TYPE: float

epoch

Current training epoch number

TYPE: int

steps

Current training step number

TYPE: int

This method compares the current score against the best score seen so far. If the score does not improve by at least min_delta for patience number of evaluations, it sets stop_training to True.

Source code in src/training/common/early_stopping.py
def on_evaluate(self, score: float, epoch: int, steps: int):  # pylint: disable=unused-argument
    """
    Evaluate current score and update early stopping state.

    Args:
        score (float): Current evaluation score to compare against best score
        epoch (int): Current training epoch number
        steps (int): Current training step number

    This method compares the current score against the best score seen so far.
    If the score does not improve by at least min_delta for patience number
    of evaluations, it sets stop_training to True.
    """
    if self.best_score is None:
        self.best_score = score
    elif score < self.best_score + self.min_delta:
        self.counter += 1
        logging.info("EarlyStoppingCounter: %d/%d", self.counter, self.patience)
        if self.counter >= self.patience:
            logging.info("Early stopping triggered.")
            self.stop_training = True
    else:
        self.best_score = score
        self.counter = 0