-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgradient_descent_3D.py
More file actions
45 lines (35 loc) · 928 Bytes
/
gradient_descent_3D.py
File metadata and controls
45 lines (35 loc) · 928 Bytes
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
39
40
41
42
43
44
45
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import random
def f(x, y):
return x ** 2 + y ** 2 + 25 * (2 * np.sin(x) ** 2)
def df_dx(x):
return 2 * (x + 25 * np.sin(2*x))
def df_dy(y):
return 2 * y
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = y = np.arange(-20.0, 20.0, 0.05)
X, Y = np.meshgrid(x, y)
zs = np.array([f(x, y) for x, y in zip(np.ravel(X), np.ravel(Y))])
Z = zs.reshape(X.shape)
x_ = 9.0
y_ = 9.0
xx = []
yy = []
learning_rate = 0.15
for i in range(1000):
x_ = x_ - learning_rate*np.random.uniform(0,2) * df_dx(x_)
y_ = y_ - learning_rate*np.random.uniform(0,2) * df_dy(y_)
xx.append(x_)
yy.append(y_)
xx = np.array(xx)
yy = np.array(yy)
zz = f(xx, yy)
ax.scatter(xx,yy,zz,color="k",s=20)
ax.plot_surface(X, Y, Z, alpha=0.4)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()