Exponential LR schedulerEasy

Exponential LR scheduler

Background

ExponentialLR decays the learning rate by a constant multiplicative factor γ\gamma at every epoch — a smooth geometric decay, in contrast to StepLR's discrete drops. It is a common, simple schedule when you want the rate to taper continuously over training.

Problem statement

Implement an ExponentialLRScheduler class with:

  • __init__(self, initial_lr, gamma)
  • get_lr(self, epoch) returning the learning rate at a given (0-indexed) epoch:
lr(e)=lr0γe\text{lr}(e) = \text{lr}_0 \cdot \gamma^{\,e}

Round the result to 4 decimals.

Input

  • initial_lrfloat, the starting learning rate lr0\text{lr}_0.
  • gammafloat, the per-epoch decay factor (e.g. 0.9 reduces the rate 10% each epoch).
  • epochint, the current epoch (0-indexed) passed to get_lr.

Output

get_lr(epoch) returns a float learning rate, rounded to 4 decimals.

Examples

Example 1

Input:  ExponentialLRScheduler(initial_lr=0.1, gamma=0.9)
        get_lr(0), get_lr(1), get_lr(2), get_lr(3)
Output: 0.1, 0.09, 0.081, 0.0729

Explanation: each epoch multiplies the previous rate by γ=0.9\gamma=0.9: 0.10.090.0810.07290.1 \to 0.09 \to 0.081 \to 0.0729.

Constraints

  • The exponent is the epoch itself (decay applies every epoch, not in blocks).
  • Round the returned learning rate to 4 decimals.

Notes

  • ExponentialLR is StepLR with step_size = 1: the rate drops by γ\gamma on every epoch.
  • Geometric decay means the rate approaches (but never reaches) zero — useful for a long, gentle annealing tail.
Python
Loading...

This problem ships 4 hidden tests. They run in your browser via Pyodide — no backend, no submission queue. Press ▶ Run tests to execute.

  • Reference example sequence
  • Epoch 0 returns the initial rate
  • Decays geometrically each epoch
  • gamma = 1 keeps the rate constant