ReduceLROnPlateau Scheduler
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:
None
{'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}- The
plateau_lr_test()function initializes the learning rate (LR) to 0.1 and creates aReduceLROnPlateauscheduler 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]. 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.05.
- The loss remains at 0.8 for the next 4 epochs, triggering a second LR reduction to 0.05â‹…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].
Constraints:
- mode='min', factor=0.5, patience=2
- Pass simulated losses to scheduler.step()
- Count LR reductions
Background Knowledge
The ReduceLROnPlateau scheduler is a type of learning rate scheduler in PyTorch, which is used to adjust the learning rate during the training process. The main idea behind this scheduler is to reduce the learning rate when the model's performance on the validation set stops improving. This is done to prevent overfitting and to help the model converge to a better solution. The ReduceLROnPlateau scheduler takes several parameters, including the optimizer, mode, factor, and patience. The mode parameter specifies whether the scheduler is minimizing or maximizing a metric, the factor parameter specifies the amount by which the learning rate is reduced, and the patience parameter specifies the number of epochs to wait before reducing the learning rate.
The learning rate is a hyperparameter that controls how quickly the model learns from the training data. A high learning rate can lead to fast convergence but may also cause the model to overshoot the optimal solution, while a low learning rate can lead to slow convergence but may also help the model to converge to a better solution. The ReduceLROnPlateau scheduler helps to find a good balance between these two extremes by adjusting the learning rate based on the model's performance.
In the context of this problem, we are given a list of loss values over 10 epochs and are asked to simulate the ReduceLROnPlateau scheduler to reduce the learning rate when the loss stops improving. We will use the SGD optimizer with an initial learning rate of 0.1 and create a ReduceLROnPlateau scheduler with a mode of 'min', a factor of 0.5, and a patience of 2.
Algorithm/Approach
The general approach to solving this type of problem involves the following steps:
- Create an optimizer with an initial learning rate
- Create a ReduceLROnPlateau scheduler with the specified parameters
- Simulate the training process by iterating over the list of loss values
- At each epoch, record the current learning rate and call the step method of the scheduler with the corresponding loss value
- After the simulation is complete, record the final learning rate and the number of times the learning rate was reduced
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
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.