Skip to content

Commit daa5f89

Browse files
committed
update momentum optimizer question to new format
1 parent 70e5c02 commit daa5f89

File tree

8 files changed

+88
-84
lines changed

8 files changed

+88
-84
lines changed

Problems/X_momentum_optimizer/solution.py

Lines changed: 0 additions & 83 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Implement the momentum optimizer update step function. Your function should take the current parameter value, gradient, and velocity as inputs, and return the updated parameter value and new velocity. The function should also handle scalar and array inputs.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"input": "parameter = 1.0, grad = 0.1, velocity = 0.1",
3+
"output": "(0.909, 0.091)",
4+
"reasoning": "The momentum optimizer computes updated values for the parameter and the velocity. With input values parameter=1.0, grad=0.1, and velocity=0.1, the updated parameter becomes 0.909 and the updated velocity becomes 0.091."
5+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Return tuple: (updated_parameter, updated_velocity)
4545
# Example usage:
4646
parameter = 1.0
4747
grad = 0.1
48-
velocity = 0.0
48+
velocity = 0.1
4949

5050
new_param, new_velocity = momentum_optimizer(parameter, grad, velocity)
5151
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"id": "X",
3+
"title": "Momentum Optimizer",
4+
"difficulty": "easy",
5+
"category": "Deep Learning",
6+
"video": "",
7+
"likes": "0",
8+
"dislikes": "0",
9+
"contributor": [
10+
{
11+
"profile_link": "https://github.com/mavleo96",
12+
"name": "Vijayabharathi Murugan"
13+
}
14+
],
15+
"tinygrad_difficulty": null,
16+
"pytorch_difficulty": null
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import numpy as np
2+
3+
def momentum_optimizer(parameter, grad, velocity, learning_rate=0.01, momentum=0.9):
4+
"""
5+
Update parameters using the momentum optimizer.
6+
Uses momentum to accelerate learning in relevant directions and dampen oscillations.
7+
8+
Args:
9+
parameter: Current parameter value
10+
grad: Current gradient
11+
velocity: Current velocity/momentum term
12+
learning_rate: Learning rate (default=0.01)
13+
momentum: Momentum coefficient (default=0.9)
14+
15+
Returns:
16+
tuple: (updated_parameter, updated_velocity)
17+
"""
18+
assert learning_rate > 0, "Learning rate must be positive"
19+
assert 0 <= momentum < 1, "Momentum must be between 0 and 1"
20+
21+
# Update velocity
22+
velocity = momentum * velocity + learning_rate * grad
23+
24+
# Update parameters
25+
parameter = parameter - velocity
26+
27+
return np.round(parameter, 5), np.round(velocity, 5)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import numpy as np
2+
3+
def momentum_optimizer(parameter, grad, velocity, learning_rate=0.01, momentum=0.9):
4+
"""
5+
Update parameters using the momentum optimizer.
6+
Uses momentum to accelerate learning in relevant directions and dampen oscillations.
7+
8+
Args:
9+
parameter: Current parameter value
10+
grad: Current gradient
11+
velocity: Current velocity/momentum term
12+
learning_rate: Learning rate (default=0.01)
13+
momentum: Momentum coefficient (default=0.9)
14+
15+
Returns:
16+
tuple: (updated_parameter, updated_velocity)
17+
"""
18+
# Your code here
19+
return np.round(parameter, 5), np.round(velocity, 5)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[
2+
{
3+
"test": "print(momentum_optimizer(1., 0.1, 0.5, 0.01, 0.9))",
4+
"expected_output": "(0.549, 0.451)"
5+
},
6+
{
7+
"test": "print(momentum_optimizer(np.array([1., 2.]), np.array([0.1, 0.2]), np.array([0.5, 1.0]), 0.01, 0.9))",
8+
"expected_output": "(array([0.549, 1.098]), array([0.451, 0.902]))"
9+
},
10+
{
11+
"test": "print(momentum_optimizer(np.array([1., 2.]), np.array([0.1, 0.2]), np.array([0.5, 1.0]), 0.01, 0.))",
12+
"expected_output": "(array([0.999, 1.998]), array([0.001, 0.002]))"
13+
},
14+
{
15+
"test": "print(momentum_optimizer(np.array([1., 2.]), np.array([0., 0.]), np.array([0.5, 0.5]), 0.01, 0.9))",
16+
"expected_output": "(array([0.55, 1.55]), array([0.45, 0.45]))"
17+
}
18+
]

0 commit comments

Comments
 (0)