Skip to content

Commit 57b1114

Browse files
authored
Merge pull request #492 from komaksym/add_new_problem_exponentialLR
add new problem: exponentialLR learning rate scheduler
2 parents c660ee2 + a88edcf commit 57b1114

File tree

7 files changed

+131
-0
lines changed

7 files changed

+131
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Problem
2+
3+
Write a Python class ExponentialLRScheduler to implement a learning rate scheduler based on the ExponentialLR strategy. Your class should have an __init__ method to initialize with an initial_lr (float) and gamma (float) parameter. It should also have a get_lr(self, epoch) method that returns the current learning rate for a given epoch (int). The learning rate should be decreased by gamma every epoch. The returned learning rate should be rounded to 4 decimal places. Only use standard Python.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"input": "scheduler = ExponentialLRScheduler(initial_lr=0.1, gamma=0.9)\nprint(f\"{scheduler.get_lr(epoch=0):.4f}\")\nprint(f\"{scheduler.get_lr(epoch=1):.4f}\")\nprint(f\"{scheduler.get_lr(epoch=2):.4f}\")\nprint(f\"{scheduler.get_lr(epoch=3):.4f}\")",
3+
"output": "0.1000\n0.0900\n0.0810\n0.0729",
4+
"reasoning": "The initial learning rate is 0.1. At epoch 1, it decays by 0.9 to 0.09. At epoch 2, it decays again to 0.081, and so on, decaying by gamma every single epoch. All results are rounded to 4 decimal places."
5+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# **Learning Rate Schedulers: ExponentialLR**
2+
3+
## **1. Definition**
4+
A **learning rate scheduler** is a component used in machine learning, especially in neural network training, to adjust the learning rate during the training process. The **learning rate** is a hyperparameter that determines the step size at each iteration while moving towards a minimum of a loss function.
5+
6+
**ExponentialLR (Exponential Learning Rate)** is a common type of learning rate scheduler that decays the learning rate by a fixed multiplicative factor γ (gamma) at *every* epoch. This results in an exponential decrease of the learning rate over time. It's often used when a rapid and continuous reduction of the learning rate is desired.
7+
8+
## **2. Why Use Learning Rate Schedulers?**
9+
* **Faster Convergence:** A higher initial learning rate can help quickly move through the loss landscape.
10+
* **Improved Performance:** A smaller learning rate towards the end of training allows for finer adjustments and helps in converging to a better local minimum, avoiding oscillations around the minimum.
11+
* **Stability:** Reducing the learning rate prevents large updates that could lead to divergence or instability.
12+
13+
## **3. ExponentialLR Mechanism**
14+
The learning rate is reduced by a factor γ (gamma) every epoch.
15+
16+
The formula for the learning rate at a given epoch e is:
17+
18+
$$LR_e = LR_{\text{initial}} \times \gamma^e$$
19+
20+
Where:
21+
* $LR_e$: The learning rate at epoch e.
22+
* $LR_{\text{initial}}$: The initial learning rate.
23+
* γ (gamma): The multiplicative factor by which the learning rate is reduced per epoch (usually between 0 and 1, e.g., 0.9, 0.99).
24+
* e: The current epoch number (0-indexed).
25+
26+
**Example:**
27+
If initial learning rate = 0.1, and γ = 0.9:
28+
* Epoch 0: $LR_0 = 0.1 \times 0.9^0 = 0.1 \times 1 = 0.1$
29+
* Epoch 1: $LR_1 = 0.1 \times 0.9^1 = 0.1 \times 0.9 = 0.09$
30+
* Epoch 2: $LR_2 = 0.1 \times 0.9^2 = 0.1 \times 0.81 = 0.081$
31+
* Epoch 3: $LR_3 = 0.1 \times 0.9^3 = 0.1 \times 0.729 = 0.0729$
32+
33+
## **4. Applications of Learning Rate Schedulers**
34+
Learning rate schedulers, including ExponentialLR, are widely used in training various machine learning models, especially deep neural networks, across diverse applications such as:
35+
* **Image Classification:** Training Convolutional Neural Networks (CNNs) for tasks like object recognition.
36+
* **Natural Language Processing (NLP):** Training Recurrent Neural Networks (RNNs) and Transformers for tasks like machine translation, text generation, and sentiment analysis.
37+
* **Speech Recognition:** Training models for converting spoken language to text.
38+
* **Reinforcement Learning:** Optimizing policies in reinforcement learning agents.
39+
* **Any optimization problem** where gradient descent or its variants are used.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"id": "154",
3+
"title": "ExponentialLR Learning Rate Scheduler",
4+
"difficulty": "easy",
5+
"category": "Machine Learning",
6+
"video": "",
7+
"likes": "0",
8+
"dislikes": "0",
9+
"contributor": [
10+
{
11+
"profile_link": "https://github.com/komaksym",
12+
"name": "komaksym"
13+
}
14+
]
15+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class ExponentialLRScheduler:
2+
def __init__(self, initial_lr, gamma):
3+
"""
4+
Initializes the ExponentialLR scheduler.
5+
6+
Args:
7+
initial_lr (float): The initial learning rate.
8+
gamma (float): The multiplicative factor of learning rate decay per epoch.
9+
(e.g., 0.9 for reducing LR by 10% each epoch).
10+
"""
11+
self.initial_lr = initial_lr
12+
self.gamma = gamma
13+
14+
def get_lr(self, epoch):
15+
"""
16+
Calculates and returns the current learning rate for a given epoch,
17+
rounded to 4 decimal places.
18+
19+
Args:
20+
epoch (int): The current epoch number (0-indexed).
21+
22+
Returns:
23+
float: The calculated learning rate for the current epoch, rounded to 4 decimal places.
24+
"""
25+
# Apply the decay factor 'gamma' raised to the power of the current 'epoch'
26+
# to the initial learning rate.
27+
current_lr = self.initial_lr * (self.gamma ** epoch)
28+
29+
# Round the learning rate to 4 decimal places
30+
return round(current_lr, 4)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class ExponentialLRScheduler:
2+
def __init__(self, initial_lr, gamma):
3+
# Initialize initial_lr and gamma
4+
pass
5+
6+
def get_lr(self, epoch):
7+
# Calculate and return the learning rate for the given epoch
8+
pass
9+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[
2+
{
3+
"test": "scheduler = ExponentialLRScheduler(initial_lr=0.1, gamma=0.9)\nprint(f\"{scheduler.get_lr(epoch=0):.4f}\")",
4+
"expected_output": "0.1000"
5+
},
6+
{
7+
"test": "scheduler = ExponentialLRScheduler(initial_lr=0.1, gamma=0.9)\nprint(f\"{scheduler.get_lr(epoch=1):.4f}\")",
8+
"expected_output": "0.0900"
9+
},
10+
{
11+
"test": "scheduler = ExponentialLRScheduler(initial_lr=0.1, gamma=0.9)\nprint(f\"{scheduler.get_lr(epoch=2):.4f}\")",
12+
"expected_output": "0.0810"
13+
},
14+
{
15+
"test": "scheduler = ExponentialLRScheduler(initial_lr=0.1, gamma=0.9)\nprint(f\"{scheduler.get_lr(epoch=3):.4f}\")",
16+
"expected_output": "0.0729"
17+
},
18+
{
19+
"test": "scheduler = ExponentialLRScheduler(initial_lr=1.0, gamma=0.5)\nprint(f\"{scheduler.get_lr(epoch=0):.4f}\\n{scheduler.get_lr(epoch=1):.4f}\\n{scheduler.get_lr(epoch=2):.4f}\\n{scheduler.get_lr(epoch=3):.4f}\")",
20+
"expected_output": "1.0000\n0.5000\n0.2500\n0.1250"
21+
},
22+
{
23+
"test": "scheduler = ExponentialLRScheduler(initial_lr=0.005, gamma=0.99)\nprint(f\"{scheduler.get_lr(epoch=0):.4f}\\n{scheduler.get_lr(epoch=10):.4f}\\n{scheduler.get_lr(epoch=20):.4f}\")",
24+
"expected_output": "0.0050\n0.0045\n0.0041"
25+
},
26+
{
27+
"test": "scheduler = ExponentialLRScheduler(initial_lr=0.001, gamma=1.0)\nprint(f\"{scheduler.get_lr(epoch=5):.4f}\\n{scheduler.get_lr(epoch=10):.4f}\")",
28+
"expected_output": "0.0010\n0.0010"
29+
}
30+
]

0 commit comments

Comments
 (0)