import numpy as np
# 导入动画包
import matplotlib.animation as animation
data = np.array([
[80,200],
[95,230],
[104,245],
[112,247],
[125,259],
[135,262]
])
# 两个数组记录m和b的变化过程
mhistroy=[]
bhistroy=[]
# 记录mse的变化过程
msehistory=[]
Weight =np.ones((2,1)) # m和b 采用矩阵的方式指定权重
ones = np.ones((len(data),1))
Feature = np.hstack((data[:,0:1],ones))
label = data[:,1:2]
learningrate = 0.1
# 记录历史的梯度变换 m,b
cache = np.ones((2,1))
decay = 0.9
def grandentdecent2():
global Weight,cache,learningrate
# 计算mse
mse = np.sum(np.square(np.dot(Feature,Weight)-label))
msehistory.append(mse)
# 计算slop
slop = np.dot(Feature.T,(np.dot(Feature,Weight)-label))
##关键代码,rmsprop
## 记录历史变换,使用decay表示衰减
cache = decay*cache + (1-decay)*slop**2
Weight = Weight - learningrate * slop/np.sqrt(cache+0.000000001)
mhistroy.append(Weight[0][0])
bhistroy.append(Weight[1][0])
for i in range(50000):
grandentdecent2()
## 以动画的方式展示m和b收敛的过程
%matplotlib notebook
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(6,6),dpi=60)
plt.xlim(0,5)
plt.ylim(0,130)
axis_name, = plt.plot(mhistroy[0:100],bhistroy[0:100],c='r')
plt.annotate("goal",xy=(1.0859,122.68), xytext=(+10, +15),
textcoords='offset points', fontsize=12,
arrowprops=dict(arrowstyle="->"))
def update(num):
axis_name.set_data(mhistroy[0:num*100],bhistroy[0:num*100])
animation.FuncAnimation(fig,update,np.arange(0,501),interval=20,repeat=False)