class

LambdaLR

extends_LRScheduler
LambdaLR(optimizer: Optimizer, lr_lambda: Callable[[int], float] | list[Callable[[int], float]], last_epoch: int = -1, verbose: bool = False)
source

Set the learning rate using a user-supplied multiplicative factor function.

At each epoch tt, the learning rate for param group ii is:

ηt(i)=η0(i)λi(t)\eta_t^{(i)} = \eta_0^{(i)} \cdot \lambda_i(t)

where η0(i)\eta_0^{(i)} is the initial learning rate of group ii and λi\lambda_i is the corresponding callable.

Parameters

optimizerOptimizer
Wrapped optimizer.
lr_lambdacallable or list of callable
A function f(epoch: int) -> float returning the multiplicative factor for the learning rate. If a single callable is given it is applied to every param group; if a list is given it must have the same length as optimizer.param_groups.
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

lr_lambdaslist of callable
One factor function per param group.

Notes

LambdaLR provides maximum flexibility: any monotone or cyclic schedule can be encoded as a Python function. For a linearly increasing warmup followed by constant LR:

.. code-block:: python

warmup = 5
fn = lambda t: min(1.0, t / warmup)
scheduler = optim.LambdaLR(optimizer, lr_lambda=fn)

Examples

>>> import lucid.optim as optim
>>> optimizer = optim.Adam(model.parameters(), lr=1e-3)
>>> scheduler = optim.LambdaLR(optimizer, lr_lambda=lambda t: 0.95 ** t)
>>> for epoch in range(50):
...     train(...)
...     optimizer.step()
...     scheduler.step()

Methods (2)

dunder

__init__

None
__init__(optimizer: Optimizer, lr_lambda: Callable[[int], float] | list[Callable[[int], float]], last_epoch: int = -1, verbose: bool = False)
source

Initialise the LambdaLR. 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.