reduce_lr_on_plateau
reduce_lr_on_plateau
¶
ReduceLROnPlateau callback.
ReduceLROnPlateauCallback
¶
ReduceLROnPlateauCallback(monitor='val_loss', mode='min', factor=0.5, patience=10, threshold=0.0001, threshold_mode='rel', cooldown=0, min_lr=0.0, eps=1e-08)
Bases: Callback
Callback to reduce learning rate when validation loss plateaus.
Wraps PyTorch's ReduceLROnPlateau scheduler to work with Lightning. Reduces learning rate by a factor when validation metric hasn't improved for a given number of epochs (patience).
Initialize ReduceLROnPlateau callback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
monitor
|
str
|
Metric to monitor (default: "val_loss") |
'val_loss'
|
mode
|
str
|
"min" to reduce LR when metric stops decreasing, "max" for increasing |
'min'
|
factor
|
float
|
Factor by which to reduce LR (new_lr = lr * factor) |
0.5
|
patience
|
int
|
Number of epochs with no improvement before reducing LR |
10
|
threshold
|
float
|
Threshold for measuring improvement |
0.0001
|
threshold_mode
|
str
|
"rel" for relative threshold, "abs" for absolute |
'rel'
|
cooldown
|
int
|
Number of epochs to wait before resuming normal operation after LR reduction |
0
|
min_lr
|
float
|
Minimum learning rate |
0.0
|
eps
|
float
|
Minimal decay applied to lr |
1e-08
|
Source code in src/versatil/training/callbacks/reduce_lr_on_plateau.py
on_fit_start
¶
Create ReduceLROnPlateau scheduler at start of training.
Restores any scheduler state stashed by load_state_dict so that
best, num_bad_epochs, and cooldown_counter survive
checkpoint resumes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trainer
|
Trainer
|
Lightning trainer |
required |
pl_module
|
LightningModule
|
Lightning module |
required |
Source code in src/versatil/training/callbacks/reduce_lr_on_plateau.py
state_dict
¶
Return the underlying torch scheduler state for checkpointing.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The torch scheduler's state dict, or an empty dict when the |
dict[str, Any]
|
scheduler has not been created yet. |
Source code in src/versatil/training/callbacks/reduce_lr_on_plateau.py
load_state_dict
¶
Stash checkpointed scheduler state until the scheduler exists.
on_fit_start recreates the torch scheduler and applies this state
onto it, mirroring how the EMA callback handles resume state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_dict
|
dict[str, Any]
|
Scheduler state saved by |
required |
Source code in src/versatil/training/callbacks/reduce_lr_on_plateau.py
on_validation_epoch_end
¶
Update scheduler with validation metric at end of epoch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trainer
|
Trainer
|
Lightning trainer |
required |
pl_module
|
LightningModule
|
Lightning module |
required |
Source code in src/versatil/training/callbacks/reduce_lr_on_plateau.py
on_train_epoch_end
¶
Update scheduler on train epochs when no validation loop runs.
Runs without a validation dataloader never trigger
on_validation_epoch_end, so a train-metric monitor must be
stepped from the training loop instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trainer
|
Trainer
|
Lightning trainer |
required |
pl_module
|
LightningModule
|
Lightning module |
required |