Skip to content

Commit 6cb580e

Browse files
committed
update adadelta optimizer question to new format
1 parent b191f9a commit 6cb580e

File tree

8 files changed

+112
-149
lines changed

8 files changed

+112
-149
lines changed

Problems/X_adadelta_optimizer/solution.py

Lines changed: 0 additions & 147 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 Adadelta optimizer update step function. Your function should take the current parameter value, gradient, and moving averages as inputs, and return the updated parameter value and new moving averages. The function should handle both scalar and array inputs, and include proper input validation.
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, u = 1.0, v = 1.0, rho = 0.95, epsilon = 1e-6",
3+
"output": "(0.89743, 0.9505, 0.95053)",
4+
"explanation": "The Adadelta optimizer computes updated values for the parameter, first moment (u), and second moment (v). With input values parameter=1.0, grad=0.1, u=1.0, v=1.0, and rho=0.95, the updated parameter becomes 0.89743."
5+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ Return tuple: (updated_parameter, updated_v, updated_u)
5656
# Example usage:
5757
parameter = 1.0
5858
grad = 0.1
59-
v = 0.0
60-
u = 0.0
59+
v = 1.0
60+
u = 1.0
6161

6262
new_param, new_v, new_u = adadelta_optimizer(parameter, grad, v, u)
6363
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"id": "X",
3+
"title": "Adadelta 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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import numpy as np
2+
3+
def adadelta_optimizer(parameter, grad, u, v, rho=0.95, epsilon=1e-6):
4+
"""
5+
Update parameters using the AdaDelta optimizer.
6+
AdaDelta is an extension of AdaGrad that seeks to reduce its aggressive,
7+
monotonically decreasing learning rate.
8+
9+
Args:
10+
parameter: Current parameter value
11+
grad: Current gradient
12+
u: Running average of squared gradients
13+
v: Running average of squared parameter updates
14+
rho: Decay rate for the moving average (default=0.95)
15+
epsilon: Small constant for numerical stability (default=1e-6)
16+
17+
Returns:
18+
tuple: (updated_parameter, updated_u, updated_v)
19+
"""
20+
assert 0 <= rho < 1, "Rho must be between 0 and 1"
21+
assert epsilon > 0, "Epsilon must be positive"
22+
assert all(u >= 0) if isinstance(u, np.ndarray) else u >= 0, "u must be non-negative"
23+
assert all(v >= 0) if isinstance(v, np.ndarray) else v >= 0, "v must be non-negative"
24+
25+
# Update running average of squared gradients
26+
u = rho * u + (1 - rho) * grad**2
27+
28+
# Compute RMS of gradient
29+
RMS_g = np.sqrt(u + epsilon)
30+
31+
# Compute RMS of parameter updates
32+
RMS_dx = np.sqrt(v + epsilon)
33+
34+
# Compute parameter update
35+
dx = -RMS_dx / RMS_g * grad
36+
37+
# Update running average of squared parameter updates
38+
v = rho * v + (1 - rho) * dx**2
39+
40+
# Update parameters
41+
parameter = parameter + dx
42+
43+
return np.round(parameter, 5), np.round(u, 5), np.round(v, 5)
44+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import numpy as np
2+
3+
def adadelta_optimizer(parameter, grad, u, v, rho=0.95, epsilon=1e-6):
4+
"""
5+
Update parameters using the AdaDelta optimizer.
6+
AdaDelta is an extension of AdaGrad that seeks to reduce its aggressive,
7+
monotonically decreasing learning rate.
8+
9+
Args:
10+
parameter: Current parameter value
11+
grad: Current gradient
12+
u: Running average of squared gradients
13+
v: Running average of squared parameter updates
14+
rho: Decay rate for the moving average (default=0.95)
15+
epsilon: Small constant for numerical stability (default=1e-6)
16+
17+
Returns:
18+
tuple: (updated_parameter, updated_u, updated_v)
19+
"""
20+
# Your code here
21+
return np.round(parameter, 5), np.round(u, 5), np.round(v, 5)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[
2+
{
3+
"test": "print(adadelta_optimizer(1., 0.5, 1., 1., 0.95, 1e-6))",
4+
"expected_output": "(0.49035, 0.9625, 0.96299)"
5+
},
6+
{
7+
"test": "print(adadelta_optimizer(np.array([1., 2.]), np.array([0.1, 0.2]), np.array([1., 1.]), np.array([1., 1.]), 0.95, 1e-6))",
8+
"expected_output": "(array([0.89743, 1.79502]), array([0.9505, 0.952]), array([0.95053, 0.9521]))"
9+
},
10+
{
11+
"test": "print(adadelta_optimizer(np.array([1., 2.]), np.array([0., 0.2]), np.array([0., 1.]), np.array([0., 1.]), 0.95, 1e-6))",
12+
"expected_output": "(array([1., 1.79502]), array([0., 0.952]), array([0., 0.9521]))"
13+
},
14+
{
15+
"test": "print(adadelta_optimizer(np.array([1., 1.]), np.array([1., 1.]), np.array([10000., 1.]), np.array([1., 1.]), 0.95, 1e-6))",
16+
"expected_output": "(array([0.98974, 0.]), array([9500.05, 1.]), array([0.95001, 1.]))"
17+
},
18+
{
19+
"test": "print(adadelta_optimizer(1., 0.5, 1., 1., 0., 1e-6))",
20+
"expected_output": "(0.999, 0.25, 0.0)"
21+
}
22+
]

0 commit comments

Comments
 (0)