class

ConstantLR

extends_LRScheduler
ConstantLR(optimizer: Optimizer, factor: float = 1.0 / 3, total_iters: int = 5, last_epoch: int = -1, verbose: bool = False)
source

Hold the learning rate at a constant scaled value, then restore the base LR.

For the first total_iters epochs the learning rate is scaled by factor; after that it is restored to the original base learning rate:

ηt={η0factort<Tη0tT\eta_t = \begin{cases} \eta_0 \cdot \text{factor} & t < T \\ \eta_0 & t \ge T \end{cases}

where T=total_itersT = \text{total\_iters}.

Parameters

optimizerOptimizer
Wrapped optimizer.
factorfloat= 1.0 / 3
Multiplicative factor applied to the base LR during the constant phase (default: 1/3).
total_itersint= 5
Number of epochs to hold the scaled LR before restoring the base value (default: 5).
last_epochint= -1
The index of the last epoch (default: -1).
verbosebool= False
Print the updated LR after each step if True (default: False).

Attributes

factorfloat
LR scaling factor during the constant phase.
total_itersint
Duration of the constant phase in epochs.

Notes

ConstantLR is useful as the first stage in a SequentialLR chain, e.g. to hold a reduced LR during an initial burn-in period before switching to a main decay schedule.

Examples

>>> import lucid.optim as optim
>>> optimizer = optim.SGD(model.parameters(), lr=0.1)
>>> # Hold LR at 0.1/3 for 5 epochs, then jump back to 0.1
>>> scheduler = optim.ConstantLR(optimizer, factor=1/3, total_iters=5)
>>> for epoch in range(50):
...     train(...)
...     optimizer.step()
...     scheduler.step()

Methods (2)

dunder

__init__

None
__init__(optimizer: Optimizer, factor: float = 1.0 / 3, total_iters: int = 5, last_epoch: int = -1, verbose: bool = False)
source

Initialise the ConstantLR. See the class docstring for parameter semantics.

fn

get_lr

list[float]
get_lr()
source

Compute the learning rate for each parameter group at the current step.

Returns

list[float]

One learning rate per param group, derived from the schedule formula documented in the class docstring.