#155 · Machine Learning · Medium
⊣ Solve on deep-ml.comImplement the CosineAnnealingLR learning rate scheduler. The learning rate follows a cosine curve from an initial value down to a minimum value eta_min over T_max epochs, then optionally restarts.
import math
def cosine_annealing_lr(initial_lr: float, eta_min: float, T_max: int, epoch: int) -> float:
return eta_min + 0.5 * (initial_lr - eta_min) * (1 + math.cos(math.pi * epoch / T_max))T_max steps.cos(0) = 1, so LR = initial_lr.T_max, cos(pi) = -1, so LR = eta_min.