mse优化方法(一)——momentum

jupyter notebook 编写,windows建议创建虚拟环境,ubuntu直接安装

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,mse的变化过程
mhistory = []
bhistory = []
msehistory = []

Weight = np.ones((2,1))
ones = np.ones((len(data),1))
Feature = np.hstack((data[:,0:1],ones))
print(Feature)
label = data[:,1:2]
print(label)
learningrate = 0.00002

v = np.zeros((2,1))

def gradentdecent():
    global learningrate,v,Weight
    mse = np.sum(np.square(np.dot(Feature,Weight)-label))
    msehistory.append(mse)
    #计算m和b的梯度
    slop = np.dot(Feature.T,(np.dot(Feature,Weight)-label))
    v = v*0.99-learningrate*slop
    Weight = Weight+v
    mhistory.append(Weight[0][0])
    bhistory.append(Weight[1][0])
for i in range (50000):
    gradentdecent()
%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(mhistory[0:100],bhistory[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(mhistory[0:num*100],bhistory[0:num*100])

animation.FuncAnimation(fig,update,np.arange(0,501),interval=20,repeat=False)

图形化展示:

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值