PyTorch深度学习实践 Lecture04 反向传播


🤵 AuthorHorizon John

编程技巧篇各种操作小结

🎇 机器视觉篇会变魔术 OpenCV

💥 深度学习篇简单入门 PyTorch

🏆 神经网络篇经典网络模型

💻 算法篇再忙也别忘了 LeetCode


视频链接:Lecture 04 Back_Propagation
文档资料:

//Here is the link:
课件链接:https://pan.baidu.com/s/1vZ27gKp8Pl-qICn_p2PaSw
提取码:cxe4

Back_Propagation(反向传播)

概述

前面所说的都是深度学习中 前向传播 的过程,即网络参数向前计算得到 Loss值 的过程;

而这里提到 反向传播 的过程,顾名思意,就是网络参数向后计算的过程;

通过反向求导得到偏导数,找到 梯度下降 的方向。



总结:

前向传播: 根据输入的数据(这里为x,w)计算得到 y-hat,利用 (y-hat - y) 计算得到 误差损失(Loss)

反向传播: 利用前向传播得到的 误差损失(Loss) 找到梯度下降的方向来更新 权重参数(这里是 w)。

以之前的 线性模型 为例 :


在这里插入图片描述

Tensor in PyTorch


在PyTorch中只需将参数设置为 " requires_grad = True " ,它便自动求导得到 " Grad " 值。


Code

# Here is the code :

import torch
x_data = [1.0, 2.0, 3.0]      # data
y_data = [2.0, 4.0, 6.0]
 
w = torch.tensor([1.0])      # 设置w初始值
w.requires_grad = True       # 计算Grad
 
def forward(x):      # Define the linear model
    return x*w       # 这里面 w 是 Tensor
 
 
def loss(x, y):      # Define the loss function
    y_pred = forward(x)
    return (y_pred - y)**2      # 均方差
 
print("predict (before training)", 4, forward(4).item())
 
for epoch in range(100):
    for x, y in zip(x_data, y_data):
        l = loss(x,y)       # forward, compute the loss
        l.backward()       # backward, compute grad for Tensor whose requires_grad set to True
        print('\tgrad:', x, y, w.grad.item())
        w.data = w.data - 0.01 * w.grad.data       # The grad is utilized to updateweight
 
        w.grad.data.zero_()      # after update, remember set the grad to zero
                                 # 这里必须清零,不然后面的 epoch 计算 grad 会累加 !
 
    print('progress:', epoch, l.item())      # 这里取值都需要使用 .item()函数,l是 tensor 格式
 
print("predict (after training)", 4, forward(4).item())

运行结果

predict (before training) 4  4.0
	grad: 1.0 2.0 -2.0
	grad: 2.0 4.0 -7.840000152587891
	grad: 3.0 6.0 -16.228801727294922
progress: 0 7.315943717956543
	grad: 1.0 2.0 -1.478623867034912
	grad: 2.0 4.0 -5.796205520629883
	grad: 3.0 6.0 -11.998146057128906
progress: 1 3.9987640380859375
	grad: 1.0 2.0 -1.0931644439697266
	grad: 2.0 4.0 -4.285204887390137
	grad: 3.0 6.0 -8.870372772216797
progress: 2 2.1856532096862793
          ... ...
	grad: 1.0 2.0 -7.152557373046875e-07
	grad: 2.0 4.0 -2.86102294921875e-06
	grad: 3.0 6.0 -5.7220458984375e-06
progress: 98 9.094947017729282e-13
	grad: 1.0 2.0 -7.152557373046875e-07
	grad: 2.0 4.0 -2.86102294921875e-06
	grad: 3.0 6.0 -5.7220458984375e-06
progress: 99 9.094947017729282e-13
predict (after training) 4  7.999998569488525

# print('progress:', epoch, l) 的输出

progress: 0 tensor([7.3159], grad_fn=<PowBackward0>)

附录:相关文档资料

PyTorch 官方文档: PyTorch Documentation
PyTorch 中文手册: PyTorch Handbook


《PyTorch深度学习实践》系列链接:

  Lecture01 Overview
  Lecture02 Linear_Model
  Lecture03 Gradient_Descent
  Lecture04 Back_Propagation
  Lecture05 Linear_Regression_with_PyTorch
  Lecture06 Logistic_Regression
  Lecture07 Multiple_Dimension_Input
  Lecture08 Dataset_and_Dataloader
  Lecture09 Softmax_Classifier
  Lecture10 Basic_CNN
  Lecture11 Advanced_CNN
  Lecture12 Basic_RNN
  Lecture13 RNN_Classifier

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Horizon John

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值