PIXELBANKv9.1.0
Menu

CosineAnnealingLR Scheduler

Problem Statement

Use CosineAnnealingLR for cosine-shaped learning rate decay.

Background

CosineAnnealingLR follows a cosine curve from initial LR down to eta_min over T_max epochs, then back up.

Your Task

The starter code creates a model and SGD optimizer. Create a CosineAnnealingLR scheduler that decays over 20 epochs to a minimum LR of 0.001.

The epoch loop is pre-filled.

Output Format

Returns a dictionary with "lr_history" (20 values), "max_lr", "min_lr", and "mid_lr".

Example:

Input:
None
Output:
{'lr_history': [0.1, 0.0976, 0.0905, 0.079, 0.0638, 0.0505, 0.0362, 0.021, 0.0095, 0.0034, 0.001, 0.0034, 0.0095, 0.021, 0.0362, 0.0505, 0.0638, 0.079, 0.0905, 0.0976], 'max_lr': 0.1, 'min_lr': 0.001, 'mid_lr': 0.001}
Reasoning:
  • The cosine_lr_test() function initializes a learning rate of 0.1 and creates a CosineAnnealingLR scheduler with Tmax=20T_{max} = 20 and ηmin=0.001\eta_{min} = 0.001.
  • The scheduler applies a cosine-shaped learning rate decay: lr=ηmin+12(η−ηmin)(1+cos⁡(πtTmax))lr = \eta_{min} + \frac{1}{2}(\eta - \eta_{min})(1 + \cos(\frac{\pi t}{T_{max}})), where tt is the current epoch and η\eta is the initial learning rate.
  • Over 20 epochs, the learning rate decays and then increases, following the cosine curve, resulting in the recorded lr_history values.
  • The maximum, minimum, and mid-point learning rates are extracted from the lr_history to produce the final output dictionary values: max_lr, min_lr, and mid_lr.

Constraints:

  • T_max=20, eta_min=0.001
  • Initial lr=0.1
  • 20 epochs
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
CosineAnnealingLR Scheduler - Medium | PixelBank