李弘毅代码

回归

import numpy as np
import matplotlib.pyplot as plt

# y = b + wx
# x_data = np.array([338,333,328,207,226,25,179,60,208,606],dtype=np.float)
x_data = [338.,333.,328.,207.,226.,25.,179.,60.,208.,606.]
# y_data = np.array([640,633,619,393,428,27,193,66,226,1591])
y_data = [640.,633.,619.,393.,428.,27.,193.,66,226.,1591.]

# initial value
w, b = -4, -120
# learning rate
lr = 1 # 设置不同参数,效果更好
iteration = 100000

w_history = [w]
b_history = [b]

b_lr = 0
w_lr = 0
for i in range(iteration):
    b_grad = 0
    w_grad = 0
    for i in range(len(x_data)):
        b_grad += -2 * (y_data[i] - (b + w * x_data[i]))
        w_grad += -2 * (y_data[i] - (b + w * x_data[i]))*x_data[i]
    b_lr += b_grad**2

    w_lr = w_lr + w_grad**2

    b = b - lr/np.sqrt(b_lr) * b_grad # 保证参数同时更新

    w = w - lr/np.sqrt(w_lr) * w_grad
    w_history.append(w)
    b_history.append(b)


X = np.arange(-200,-100)
Y = np.arange(-5,5,0.1)
Z = np.zeros((len(X),len(Y)))
# xs,ys = np.meshgrid(X,Y)

for i in range(len(X)):
    for j in range(len(Y)):
        for k in range(len(x_data)):
             Z[j,i] += (y_data[k] - (X[i]+Y[j]*x_data[k]))**2
        # Z[j,i] /=len(x_data)

plt.contour(X,Y,Z,50,alpha=0.6,cmap=plt.get_cmap('jet'))
plt.xlim([-200,-100])
plt.ylim([-5,5])
plt.plot([-188.4],[2.67],'rx',ms=12,markeredgewidth=3)
plt.plot(b_history,w_history,'o-')

plt.xlabel(r'$b$',fontsize=16)
plt.show()



猜你喜欢

转载自blog.csdn.net/baidu_41867252/article/details/88919645