CyclicLR
_LRSchedulerCyclicLR(optimizer: Optimizer, base_lr: float, max_lr: float, step_size_up: int = 2000, mode: str = 'triangular', gamma: float = 1.0, last_epoch: int = -1, verbose: bool = False)Cycle the learning rate between base_lr and max_lr.
Implements the triangular, triangular2, and exp_range cyclic policies.
Within each cycle of length the
learning rate rises linearly from base_lr to max_lr and then
falls back:
where .
For mode="triangular2" the amplitude halves each cycle:
For mode="exp_range" the amplitude decays exponentially each step:
Parameters
optimizerOptimizerbase_lrfloatmax_lrfloatstep_size_upint= 20002000).modestr= 'triangular'"triangular" (constant amplitude), "triangular2"
(amplitude halves each cycle), or "exp_range" (amplitude
decays by each step). Default: "triangular".gammafloat= 1.0"exp_range" mode (default: 1.0).last_epochint= -1-1).verbosebool= FalseTrue (default: False).Attributes
base_lr_valfloatmax_lr_valfloatstep_size_upintmodestrgammafloat"exp_range" mode.Notes
Cyclic learning rates can reduce the need for careful manual tuning by
automatically exploring a range of rates. Use step_size_up between
2 and 10 times the number of iterations per epoch.
Examples
>>> import lucid.optim as optim
>>> optimizer = optim.SGD(model.parameters(), lr=0.01)
>>> scheduler = optim.CyclicLR(
... optimizer, base_lr=1e-4, max_lr=1e-2, step_size_up=500
... )
>>> for batch in dataloader:
... train_step(batch)
... optimizer.step()
... scheduler.step()Methods (2)
__init__
→None__init__(optimizer: Optimizer, base_lr: float, max_lr: float, step_size_up: int = 2000, mode: str = 'triangular', gamma: float = 1.0, last_epoch: int = -1, verbose: bool = False)Initialise the CyclicLR. See the class docstring for parameter semantics.
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.