lr_scheduler
sorix.optim.lr_scheduler ¶
Learning rate schedulers for sorix optimizers.
Schedulers adjust the learning rate of each parameter group in an optimizer
following a policy. They do not touch gradients or parameters — they only
rewrite optimizer.param_groups[*]['lr'].
A scheduler is advanced with scheduler.step() after optimizer.step(),
once per epoch (not once per mini-batch)::
optimizer = sorix.optim.Adam(model.parameters(), lr=1e-3)
scheduler = sorix.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=100)
for epoch in range(100):
for X_batch, y_batch in loader:
optimizer.zero_grad()
loss = criterion(model(X_batch), y_batch)
loss.backward() # computes gradients
optimizer.step() # applies them using the current lr
scheduler.step() # picks the lr for the next epoch
ReduceLROnPlateau is the exception: it is metric-driven, so it is stepped
with the monitored value, scheduler.step(val_loss).
StepLR ¶
Bases: _LRScheduler
Decays the learning rate of each parameter group by gamma every
step_size epochs, following the staircase schedule
.. math:: \eta_t = \eta_0 \cdot \gamma^{\lfloor t / s \rfloor}
Parameters:
-
optimizer(Optimizer) –Wrapped optimizer.
-
step_size(int) –Period of learning rate decay. Must be >= 1.
-
gamma(float, default:0.1) –Multiplicative factor of learning rate decay. Default: 0.1.
-
last_epoch(int, default:-1) –Index of the last completed epoch. Default: -1.
Example::
scheduler = StepLR(optimizer, step_size=30, gamma=0.1)
# lr decays by 0.1× every 30 epochs
Source code in sorix/optim/lr_scheduler.py
ExponentialLR ¶
Bases: _LRScheduler
Decays the learning rate of each parameter group by gamma every epoch,
following
.. math:: \eta_t = \eta_0 \cdot \gamma^{t}
Parameters:
-
optimizer(Optimizer) –Wrapped optimizer.
-
gamma(float) –Multiplicative factor of learning rate decay.
-
last_epoch(int, default:-1) –Index of the last completed epoch. Default: -1.
Example::
scheduler = ExponentialLR(optimizer, gamma=0.95)
# lr is multiplied by 0.95 each epoch
Source code in sorix/optim/lr_scheduler.py
CosineAnnealingLR ¶
Bases: _LRScheduler
Anneals the learning rate along a half cosine over T_max epochs:
.. math::
\eta_t = \eta_{\min}
+ \tfrac{1}{2}(\eta_0 - \eta_{\min})
\left(1 + \cos\left(\frac{\pi t}{T_{\max}}\right)\right)
So lr goes from base_lr at t = 0 down to eta_min at
t = T_max.
Note
The formula is periodic with period 2 * T_max. Stepping past
T_max makes the learning rate rise back towards base_lr
(a "warm restart"). If you train for more than T_max epochs and do
not want that, stop stepping the scheduler at T_max.
Parameters:
-
optimizer(Optimizer) –Wrapped optimizer.
-
T_max(int) –Maximum number of iterations (half-period of the cosine). Must be >= 1.
-
eta_min(float, default:0.0) –Minimum learning rate. Default: 0.
-
last_epoch(int, default:-1) –Index of the last completed epoch. Default: -1.
Example::
scheduler = CosineAnnealingLR(optimizer, T_max=100, eta_min=1e-6)
Source code in sorix/optim/lr_scheduler.py
ReduceLROnPlateau ¶
Reduces learning rate when a metric has stopped improving. Models often benefit from reducing the learning rate by a factor once learning stagnates.
Unlike the epoch-driven schedulers, this one is metric-driven: call
step(metric) with the monitored value after each validation pass.
Parameters:
-
optimizer(Optimizer) –Wrapped optimizer.
-
mode(str, default:'min') –'min'or'max'. In'min'mode, lr will be reduced when the quantity monitored has stopped decreasing; in'max'mode it will be reduced when the quantity has stopped increasing. Default:'min'. -
factor(float, default:0.1) –Factor by which the learning rate will be reduced. Default: 0.1.
-
patience(int, default:10) –Number of epochs with no improvement after which learning rate will be reduced. Default: 10.
-
min_lr(float, default:0.0) –A lower bound on the learning rate. Default: 0.
-
threshold(float, default:0.0001) –Absolute improvement required to reset the patience counter. Default: 1e-4.
Example::
scheduler = ReduceLROnPlateau(optimizer, mode='min', patience=5, factor=0.5)
for epoch in range(epochs):
train(...)
val_loss = validate(...)
scheduler.step(val_loss)
Source code in sorix/optim/lr_scheduler.py
step ¶
Call after validation with the monitored metric value.
Source code in sorix/optim/lr_scheduler.py
get_last_lr ¶
state_dict ¶
Returns the state of the scheduler as a dict (excluding the optimizer).
load_state_dict ¶
Loads the scheduler state.
Note
This scheduler mutates the optimizer's learning rate incrementally,
so the restored learning rate is whatever the optimizer currently
holds — load the optimizer's own state_dict alongside this one.