《PyTorch深度学习实践》第三讲 梯度下降算法

B站刘二大人《PyTorch深度学习实践》第三讲  梯度下降算法   原理以及代码的实现

 

 代码如下:

import matplotlib.pyplot as plt       #导入绘图包

x_data = [1.0,2.0,3.0]     #初始化数据集
y_data = [2.0,4.0,6.0]

w = 1.0         #定义初始权值w=1.0

def forward(x):
    return x * w   #定义线性模型

def cost(xs,ys):    #定义MSE损失
    cost = 0
    for x,y in zip(xs,ys):      #zip把对应x,y的值进行拼装
        y_pred = forward(x)
        cost += (y_pred-y) ** 2
    return cost/len(xs)

def gradient(xs,ys):    #定义梯度函数gd
    grad = 0
    for x,y in zip(xs,ys):
        grad += 2*x*(x*w-y)
    return grad/len(xs)
epoch_list = []
cost_list = []

print('predicted (before training)',4,forward(4))    #打印训练之前的预测值

for epoch in range(100):                      #进行100轮训练
    cost_val = cost(x_data,y_data)
    grad_val = gradient(x_data,y_data)
    w -= 0.01*grad_val                  #学习率0.01
    print('epoch:',epoch,'w=',w,'loss=',cost_val)   #训练,打印日志
    epoch_list.append(epoch)
    cost_list.append(cost_val)
print('predicted (after training)',4,forward(4))     #打印训练之后的预测值

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

 打印日志及运行结果:

 ........

随机梯度下降算法:

 梯度下降算法是对所有的数据损失求平均损失,而随机梯度下降算法是随机取一个数据进行计算求损失。

代码如下:

import matplotlib.pyplot as plt       #导入绘图包

x_data = [1.0,2.0,3.0]     #初始化数据集
y_data = [2.0,4.0,6.0]

w = 1.0         #定义初始权值w=1.0

def forward(x):
    return x * w   #定义线性模型

def loss(x,y):
    y_pred = forward(x)
    return (y_pred-y)**2

def gradient(x,y):
    return 2*x*(w*x-y)

epoch_list = []
loss_list = []

print('predicted (before training)',4,forward(4))    #打印训练之前的预测值

for epoch in range(100):                      #进行100轮训练
    for x,y in zip(x_data,y_data):
        grad_val = gradient(x,y)
        w -= 0.01*grad_val                  #学习率0.01
        print('\tgrad:',x,y,grad_val)   #训练,打印日志
    loss_val = loss(x,y)
    print('processing:',epoch,'w=',w,'loss=',loss_val)

    epoch_list.append(epoch)
    loss_list.append(loss_val)
print('predicted (after training)',4,forward(4))     #打印训练之后的预测值

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

 打印日志,运行结果:

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值