class

Logarithm::EarlyStopping

Inherits Reference < Object

Early stopping mechanism to prevent overfitting during training.

This class monitors the validation loss during training and stops the training process when the loss stops improving, helping to prevent overfitting.

Example usage:

early_stopping = EarlyStopping.new(patience: 10, min_delta: 0.001)
loop do
  train_epoch()
  val_loss = validate()
  break if early_stopping.should_stop?(val_loss)
end

Constructors

new(patience : Int32 = 10, min_delta : Float64 = 0.0, restore_best_weights : Bool = false)

Initialize early stopping mechanism.

Parameters:

  • patience: Number of epochs to wait for improvement before stopping
  • min_delta: Minimum change in loss to qualify as an improvement
  • restore_best_weights: Whether to restore the best weights when stopping
Source

Instance methods

best_loss

Get the best loss achieved during training.

Source
best_weights

Get the best weights (if restore_best_weights was enabled).

Source
min_delta
Source
patience
Source
reset

Reset the early stopping state for a new training run.

Source
restore_best_weights?
Source
should_stop?(current_loss : Float64, current_weights : String | Nil = nil) : Bool

Check if training should stop based on current validation loss.

Parameters:

  • current_loss: Current validation loss
  • current_weights: Optional model weights to save (for restoration)

Returns: True if training should stop, false otherwise

Source
stats

Get training statistics.

Source
stopped_epoch

Get the epoch when training was stopped.

Source