Skip to content

Commit 6e18f91

Browse files
committed
update nesterov optimizer to new format
1 parent f363442 commit 6e18f91

File tree

8 files changed

+94
-99
lines changed

8 files changed

+94
-99
lines changed

Problems/X_nag_optimizer/solution.py

Lines changed: 0 additions & 98 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 Nesterov Accelerated Gradient (NAG) optimizer update step function. Your function should take the current parameter value, gradient function, and velocity as inputs, and return the updated parameter value and new velocity. The function should use the "look-ahead" approach where momentum is applied before computing the gradient, and should handle both 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_fn = lambda x: x, velocity = 0.1",
3+
"output": "(0.9009, 0.0991)",
4+
"reasoning": "The Nesterov Accelerated Gradient optimizer computes updated values for the parameter and velocity using a look-ahead approach. With input values parameter=1.0, grad_fn=lambda x: x, and velocity=0.1, the updated parameter becomes 0.9009 and the updated velocity becomes 0.0991."
5+
}

Problems/X_nag_optimizer/learn.md renamed to questions/x_nesterov-accelerated-gradient-optimizer/learn.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def grad_func(parameter):
5454
pass
5555

5656
parameter = 1.0
57-
velocity = 0.0
57+
velocity = 0.1
5858

5959
new_param, new_velocity = nag_optimizer(parameter, grad_func, velocity)
6060
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"id": "X",
3+
"title": "Nesterov Accelerated Gradient 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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import numpy as np
2+
3+
def nag_optimizer(parameter, grad_fn, velocity, learning_rate=0.01, momentum=0.9):
4+
"""
5+
Update parameters using the Nesterov Accelerated Gradient optimizer.
6+
Uses a "look-ahead" approach to improve convergence by applying momentum before computing the gradient.
7+
8+
Args:
9+
parameter: Current parameter value
10+
grad_fn: Function that computes the gradient at a given position
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 0 <= momentum < 1, "Momentum must be between 0 and 1"
19+
assert learning_rate > 0, "Learning rate must be positive"
20+
21+
# Compute look-ahead position
22+
look_ahead = parameter - momentum * velocity
23+
24+
# Compute gradient at look-ahead position
25+
grad = grad_fn(look_ahead)
26+
27+
# Update velocity using momentum and gradient
28+
velocity = momentum * velocity + learning_rate * grad
29+
30+
# Update parameters using the new velocity
31+
parameter = parameter - velocity
32+
33+
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 nag_optimizer(parameter, grad_fn, velocity, learning_rate=0.01, momentum=0.9):
4+
"""
5+
Update parameters using the Nesterov Accelerated Gradient optimizer.
6+
Uses a "look-ahead" approach to improve convergence by applying momentum before computing the gradient.
7+
8+
Args:
9+
parameter: Current parameter value
10+
grad_fn: Function that computes the gradient at a given position
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": "import numpy as np\ndef gradient_function(x):\n if isinstance(x, np.ndarray):\n n = len(x)\n return x - np.arange(n)\n else:\n return x - 0\nprint(nag_optimizer(1., gradient_function, 0.5, 0.01, 0.9))",
4+
"expected_output": "(0.5445, 0.4555)"
5+
},
6+
{
7+
"test": "import numpy as np\ndef gradient_function(x):\n if isinstance(x, np.ndarray):\n n = len(x)\n return x - np.arange(n)\n else:\n return x - 0\nprint(nag_optimizer(np.array([1.0, 2.0]), gradient_function, np.array([0.5, 1.0]), 0.01, 0.9))",
8+
"expected_output": "(array([0.5445, 1.099]), array([0.4555, 0.901]))"
9+
},
10+
{
11+
"test": "import numpy as np\ndef gradient_function(x):\n if isinstance(x, np.ndarray):\n n = len(x)\n return x - np.arange(n)\n else:\n return x - 0\nprint(nag_optimizer(np.array([1.0, 2.0]), gradient_function, np.array([0.5, 1.0]), 0.01, 0.0))",
12+
"expected_output": "(array([0.99, 1.99]), array([0.01, 0.01]))"
13+
},
14+
{
15+
"test": "import numpy as np\ndef gradient_function(x):\n if isinstance(x, np.ndarray):\n n = len(x)\n return x - np.arange(n)\n else:\n return x - 0\nprint(nag_optimizer(0.9, gradient_function, 1, 0.01, 0.9))",
16+
"expected_output": "(0.0, 0.9)"
17+
}
18+
]

0 commit comments

Comments
 (0)