-
Notifications
You must be signed in to change notification settings - Fork 0
/
gd_test2.py
38 lines (32 loc) · 1014 Bytes
/
gd_test2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import numpy as np
import matplotlib.pyplot as plt
import gd
def f(xx):
x = xx[0]
y = xx[1]
return 5 * x**2 - 6 * x * y + 3 * y**2 + 6 * x - 6 * y
def df(xx):
x = xx[0]
y = xx[1]
return np.array([10 * x - 6 * y + 6, -6 * x + 6 * y - 6])
xmin, xmax, ymin, ymax = -3, 3, -3, 3
algos = []
initital = np.array([1,1])
alphas = [0.1, 0.2]
for alpha in alphas:
algo = gd.GradientDecent(f, df, alpha)
algo.solve(np.array(initital))
algos.append(algo)
xs = np.linspace(xmin, xmax, 300)
ys = np.linspace(ymin, ymax, 300)
xmesh, ymesh = np.meshgrid(xs, ys)
xx = np.r_[xmesh.reshape(1, -1), ymesh.reshape(1, -1)]
fig, ax = plt.subplots(1, 2)
levels = [-3, -2.9, -2.8, -2.6, -2.4,
-2.2, -2, -1, 0, 1, 2, 3, 4]
plt.scatter(initital[0], initital[1], color="k", marker="o")
plt.plot(algo.path_[:, 0], algo.path_[:, 1], color="k", linewidth=1.5)
plt.contour(xs, ys, f(xx).reshape(xmesh.shape),
levels=levels,
colors="k", linestyles="dotted")
plt.show()