class
LambdaLR
extends
_LRSchedulerLambdaLR(optimizer: Optimizer, lr_lambda: Callable[[int], float] | list[Callable[[int], float]], last_epoch: int = -1, verbose: bool = False)Set the learning rate using a user-supplied multiplicative factor function.
At each epoch , the learning rate for param group is:
where is the initial learning rate of group and is the corresponding callable.
Parameters
optimizerOptimizerWrapped optimizer.
lr_lambdacallable or list of callableA 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= -1The index of the last epoch (default:
-1).verbosebool= FalsePrint the updated LR after each step if
True (default: False).Attributes
lr_lambdaslist of callableOne 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)Initialise the LambdaLR. See the class docstring for parameter semantics.
fn
get_lr
→list[float]get_lr()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.