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