Skip to content
Snippets Groups Projects
Verified Commit 16c5edfe authored by iliya.saroukha's avatar iliya.saroukha :first_quarter_moon:
Browse files

refined epsilon value

parent f1f1bde2
Branches
No related tags found
No related merge requests found
......@@ -33,7 +33,7 @@ def base_gd(f: Function, init_pt: list[float], lr: float) -> \
f_call = callable_func(f)
while iter < 1e4:
if np.linalg.norm(grad) < 1e-5:
if np.linalg.norm(grad) < 1e-6:
break
grad = np.array([partialx(x, y), partialy(x, y)])
df.loc[iter] = [x, y, f_call(x, y), np.linalg.norm(grad)]
......@@ -63,7 +63,7 @@ def momentum_gd(f: Function, init_pt: list[float], lr: float, momentum: float)\
step = np.array([0, 0])
while iter < 1e4:
if np.linalg.norm(grad) < 1e-5:
if np.linalg.norm(grad) < 1e-6:
break
grad = np.array([partialx(x, y), partialy(x, y)])
......@@ -95,7 +95,7 @@ def nesterov_gd(f: Function, init_pt: list[float], lr: float, momentum: float)\
while iter < 1e4:
grad = np.array([partialx(x, y), partialy(x, y)])
if np.linalg.norm(grad) < 1e-5:
if np.linalg.norm(grad) < 1e-6:
break
grad_with_prev_step = np.array([partialx(x - step[0], y - step[1]),
......@@ -115,8 +115,8 @@ def nesterov_gd(f: Function, init_pt: list[float], lr: float, momentum: float)\
if __name__ == "__main__":
x, y = symbols('x y')
f: Function = x**2 + 6 * y**2
# f: Function = 1 - exp(-10 * x**2 - y**2)
# f: Function = x**2 + 6 * y**2
f: Function = 1 - exp(-10 * x**2 - y**2)
# f: Function = x**2 * y - 2 * x * y**3 + 3 * x * y + 4
# Rosenbrock(x, y)
......@@ -131,17 +131,16 @@ if __name__ == "__main__":
# Ackley(x, y)
# f: Function = -20.0 * exp(-0.2 * sqrt(0.5 * (x**2 + y**2))) - \
# exp(0.5 * (cos(2 * pi * x) + cos(2 * pi * y))) + exp(1) + 20
# exp(0.5 * (cos(2 * pi * x) + cos(2 * pi * y))) + exp(1) + 20
f_call = callable_func(f)
LR = 1e-2
MOMENTUM = 0.9
plot_range = (30, 30)
plot_range = (2, 2)
# init_pt = [9, -8]
init_pt = [20, 30]
init_pt = [1, 1]
# init_pt = np.array([np.random.randint(-plot_range[0], plot_range[0] + 1),
# np.random.randint(-plot_range[1], plot_range[1] + 1)])
......@@ -170,20 +169,20 @@ if __name__ == "__main__":
ax.contour(X, Y, Z, 20, cmap='RdGy', offset=0)
ax.plot(base['x'], base['y'], '-o', color='red', label='base')
ax.plot(base['x'], base['y'], '-o', color='red', label='Base')
ax.plot(momentum['x'], momentum['y'], '-o',
color='violet', label='momentum')
color='cyan', label='Momentum')
ax.plot(nesterov['x'], nesterov['y'], '-o',
color='green', label='nesterov')
color='orange', label='Nesterov')
ax.legend()
plt.figure(2)
plt.contour(X, Y, Z, 20, cmap='RdGy')
plt.plot(base['x'], base['y'], '-o', color='red', label='base')
plt.plot(base['x'], base['y'], '-o', color='red', label='Base')
plt.plot(momentum['x'], momentum['y'], '-o',
color='violet', label='momentum')
color='cyan', label='Momentum')
plt.plot(nesterov['x'], nesterov['y'], '-o',
color='green', label='nesterov')
color='orange', label='Nesterov')
plt.legend()
plt.xlabel('x')
plt.ylabel('y')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment