实验一:Pass 实验二: 效果图: 学习率a = 0.9 学习率a = 0.1:
'''
f(x) = w^2
the derivative of f(x): f`(x) = 2*w
Initilize w = 1, a = 0.4
'''
import matplotlib.pyplot as plt
import numpy as np
k = 4
w = [1]
a = 0.9
for i in range(k):
w.append(w[-1] - a * 2 * w[-1])
plt.ion()
x = np.linspace(-1, 1, 30)
plt.plot(x, np.square(x))
for i in range(0, len(w)-1):
plt.plot(w[i:i+2], np.square(w[i:i+2]), 'r-o')
print(w[i], '---->', w[i+1])
plt.pause(2)
plt.ioff()
print("绘制图像完成!")
plt.waitforbuttonpress()
实验三: 学习率 a = 0.1: 学习率 a = 0.9:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
'''
f(W) = w1^2 + w2^2
W = (1, 3)
a = 0.1
'''
w = np.array([[1, 3]])
a = 0.1
k = 20
for i in range(k):
w = np.concatenate((w, np.array([[w[-1][0] - a * 2 * w[-1][0], w[-1][1] - a * 2 * w[-1][1]]])), axis=0)
w1 = np.linspace(-3, 3, 20)
w2 = np.linspace(-3, 3, 20)
plt.ion()
x, y = np.meshgrid(w1, w2)
fig = plt.figure()
ax = plt.gca(projection='3d')
ax.set_zlabel('Z', fontdict={'size': 15, 'color': 'red'})
ax.set_ylabel('Y', fontdict={'size': 15, 'color': 'blue'})
ax.set_xlabel('X', fontdict={'size': 15, 'color': 'green'})
ax.plot_surface(x, y, np.square(x)+np.square(y), alpha = 0.5)
print(w)
for i in range(len(w) - 10):
ax.plot(w[i:i+2, 0], w[i:i+2, 1], [sum(np.square(w[i])), sum(np.square(w[i+1]))], 'r-o')
print('x: ', w[i:i+2, 0], ' ---> y: ', w[i:i+2, 1],' ----> z: ', [sum(np.square(w[i])), sum(np.square(w[i+1]))])
plt.pause(0.5)
plt.ioff()
def press(event):
if event.key == "escape":
plt.close()
fig.canvas.mpl_connect('key_press_event', press)
plt.show()
|