PIXELBANKv9.1.0
Menu

Problem Statement

Use ReduceLROnPlateau to automatically reduce LR when a metric stops improving.

Background

ReduceLROnPlateau monitors a metric and reduces LR when it stops improving for patience epochs. Unlike other schedulers, it requires passing the metric value to scheduler.step(loss).

Your Task

The starter code creates a model, optimizer, and simulated loss values. Create a ReduceLROnPlateau scheduler that halves the LR when loss doesn't improve for 2 epochs.

The epoch loop (which passes each loss to scheduler.step(loss)) is pre-filled.

Output Format

Returns a dictionary with "lr_history" (10 values), "num_reductions", and "final_lr".

Example:

Input:
None
Output:
{'lr_history': [0.1, 0.1, 0.1, 0.1, 0.1, 0.05, 0.05, 0.05, 0.025, 0.025], 'num_reductions': 2, 'final_lr': 0.025}
Reasoning:
  • The plateau_lr_test() function initializes the learning rate (LR) to 0.1 and creates a ReduceLROnPlateau scheduler with a patience of 2 epochs and a reduction factor of 0.5.
  • The function then simulates 10 epochs with the given loss values: [1.0,0.9,0.8,0.8,0.8,0.8,0.8,0.8,0.8,0.8][1.0, 0.9, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8]. The scheduler reduces the LR when the loss stops improving for 2 consecutive epochs.
  • After the first 2 epochs, the loss improves from 1.0 to 0.9 to 0.8, so the LR remains at 0.1. However, the loss plateaus at 0.8 for the next 2 epochs, triggering the first LR reduction to 0.1â‹…0.5=0.050.1 \cdot 0.5 = 0.05.
  • The loss remains at 0.8 for the next 4 epochs, triggering a second LR reduction to 0.05â‹…0.5=0.0250.05 \cdot 0.5 = 0.025 after the 6th epoch, resulting in an LR history of [0.1,0.1,0.1,0.1,0.1,0.05,0.05,0.05,0.025,0.025][0.1, 0.1, 0.1, 0.1, 0.1, 0.05, 0.05, 0.05, 0.025, 0.025].

Constraints:

  • mode='min', factor=0.5, patience=2
  • Pass simulated losses to scheduler.step()
  • Count LR reductions
🔒

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.
ReduceLROnPlateau Scheduler - Medium | PixelBank