Pytorch深度学习实践(二):梯度下降算法

假设一个场景:小明被扔在山顶,但是他有恐高症,需要找到最快的下山路径,但是困于自己是近视眼,无法确认最快的路径,这时候就可以用到梯度下降方法。

梯度下降算法基本思想:以他当前的位置为基准,寻找坡度最大的地方,然后朝着这个方向往下走一小段距离,然后以此为新的出发点,重复以上的过程,最终下山。

                                 梯度:\frac{\partial cost}{\partial \omega }(高数基本知识)

更新:\omega =\omega -\alpha \frac{\partial cost}{\partial \omega }(α是学习率,学习率一般设置为0.01,过大的话导致梯度变化太大)

平均梯度下降算法代码实现:

import numpy as np
import matplotlib.pyplot as plt

x_data = [1.0,2.0,3.0]
y_data = [2.0,4.0,6.0]

w = 1.0   #initial gradient guess

def forward(x):
        return x * w

def cost(xs,ys):
    cost = 0
    for x,y in zip(xs,ys):
        y_pred = forward(x)
        cost += (y_pred - y) **2
    return cost / len(xs)

def gradient(xs,ys):
    grad = 0
    for x,y in zip(xs,ys):
        grad += 2 * x * (x * w - y)
    return grad / len(xs)

print('Predict (before training)',4,forward(4))

epoch_list = []
cost_list = []

for epoch in range(100):
    cost_val = cost(x_data,y_data)
    grad_val = gradient(x_data,y_data)
    learning_rate = 0.01    #it is normally 0.01
    w -= learning_rate*grad_val     
    print('Epoch:',epoch,'w=',w,'loss=',cost_val)
    epoch_list.append(epoch)
    cost_list.append(cost_val)

print('Predict (after training)',4,forward(4))

plt.plot(epoch_list,cost_list)
plt.ylabel('Loss')
plt.xlabel('epoch')
plt.show()

但是这种做法带来的问题是只能取到局部最优解,对于小明来说可能走到了山的中间,还没完全下山,也就是说鞍点问题(一个不是全局最小的驻点,也就是一阶导数为0的点),因此有可能存在更优的点。

随机梯度算法在全部样本里面随机选择一个样本作梯度下降,当算出来的值低于我们所设定的阈值,就能找到全局最优解。

随机梯度下降算法代码实现:

import numpy as np
import matplotlib.pyplot as plt

x_data = [1.0,2.0,3.0]
y_data = [2.0,4.0,6.0]

w = 1.0   #initial gradient guess

def forward(x):
        return x * w

def loss(xs,ys):    #calculate single sample gradient
      y_pred = forward(x)
      return  (y_pred - ys) ** 2

def gradient(xs,ys):
    return 2 * xs *(xs * w - ys)

print('Predict (before training)',4,forward(4))

epoch_list = []
cost_list = []

for epoch in range(100):
    for x,y in zip(x_data,y_data):
        grad_val = gradient(x,y)
        learning_rate = 0.01
        w -= learning_rate * grad_val
        print('\tgrad:',x,y,grad_val)
        l = loss(x,y)
 
    print('progress:',epoch,'w=',w,'loss=',l)
    epoch_list.append(epoch)
    loss_list.append(l)

print('Predict (after training)',4,forward(4))

plt.plot(epoch_list,loss_list)
plt.ylabel('Loss')
plt.xlabel('epoch')
plt.show()

 参考资料

https://zhuanlan.zhihu.com/p/33340316

https://blog.csdn.net/weixin_44425647/article/details/107746060

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值