Pytorch学习笔记(二)--autograd and dynamic-graph

本文深入探讨PyTorch中的自动求导机制,包括简单的自动求导示例,复杂情况下的求导计算,以及如何进行多次求导。同时,介绍了PyTorch动态图的概念,并通过实例演示了其工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前请提要 
Pytorch学习笔记(一)--Tensor和Variable

一.Pycharm

  1. 在上次pytorch的学习中是使用文本编辑的方式运行pytorch代码,缺少IDE的许多快捷键,于是本次学习前安装好了pycharm.
  2. 安装首先在官网下载对应的安装包,随后进行破解(这里网上教程很多,在此不再赘述)
  3. 进入pycharm页面之后需要导入对应的torch包,如下图打开对应的选项卡,选择add,笔者的torch包没有在anaconda的目录下,所以选择了对应的python3,如果安装了anaconda就选择对应的目录
  4. pycharm里在在edit->font 中可以调节字体的大小
  5. 选中代码按下ctrl+/可以注释快速注释代码  ctrl+F5快速运行
    在这里插入图片描述

二.autograd(自动求导)

1. 简单情况自动求导

  • 例一
import torch
from torch.autograd import Variable

x =  Variable(torch.Tensor([3]),requires_grad=True)
y = x + 2
z = y ** 2 + 3
print("x=3 z=")
print(z)
z.backward()
print("x=3,z对x求导")
print(x.grad)

运行结果:

tensor([2., 3.], requires_grad=True)
tensor([[0., 0.]])

  • 例二
x = Variable(torch.randn(10,20),requires_grad=True)
y = Variable(torch.randn(10, 5),requires_grad=True)
w = Variable(torch.randn(20, 5),requires_grad=True)

out = torch.mean(y - torch.matmul(x,w))  #矩阵乘法A=(m,n) B=(n,k) C=A*B=(m,k)
out.backward()
print(x.grad)

tensor([10.])
tensor([[ 0.0207, -0.0222, 0.0032, -0.0017, 0.0777, -0.0293, 0.0058, 0.0518,
0.0615, -0.0786, -0.0213, -0.0042, 0.0795, -0.0344, 0.0648, 0.0586,
-0.0092, -0.0342, 0.0373, 0.1116],
[ 0.0207, -0.0222, 0.0032, -0.0017, 0.0777, -0.0293, 0.0058, 0.0518,
0.0615, -0.0786, -0.0213, -0.0042, 0.0795, -0.0344, 0.0648, 0.0586,
-0.0092, -0.0342, 0.0373, 0.1116],
[ 0.0207, -0.0222, 0.0032, -0.0017, 0.0777, -0.0293, 0.0058, 0.0518,
0.0615, -0.0786, -0.0213, -0.0042, 0.0795, -0.0344, 0.0648, 0.0586,
-0.0092, -0.0342, 0.0373, 0.1116],
[ 0.0207, -0.0222, 0.0032, -0.0017, 0.0777, -0.0293, 0.0058, 0.0518,
0.0615, -0.0786, -0.0213, -0.0042, 0.0795, -0.0344, 0.0648, 0.0586,
-0.0092, -0.0342, 0.0373, 0.1116],
[ 0.0207, -0.0222, 0.0032, -0.0017, 0.0777, -0.0293, 0.0058, 0.0518,
0.0615, -0.0786, -0.0213, -0.0042, 0.0795, -0.0344, 0.0648, 0.0586,
-0.0092, -0.0342, 0.0373, 0.1116],
[ 0.0207, -0.0222, 0.0032, -0.0017, 0.0777, -0.0293, 0.0058, 0.0518,
0.0615, -0.0786, -0.0213, -0.0042, 0.0795, -0.0344, 0.0648, 0.0586,
-0.0092, -0.0342, 0.0373, 0.1116],
[ 0.0207, -0.0222, 0.0032, -0.0017, 0.0777, -0.0293, 0.0058, 0.0518,
0.0615, -0.0786, -0.0213, -0.0042, 0.0795, -0.0344, 0.0648, 0.0586,
-0.0092, -0.0342, 0.0373, 0.1116],
[ 0.0207, -0.0222, 0.0032, -0.0017, 0.0777, -0.0293, 0.0058, 0.0518,
0.0615, -0.0786, -0.0213, -0.0042, 0.0795, -0.0344, 0.0648, 0.0586,
-0.0092, -0.0342, 0.0373, 0.1116],
[ 0.0207, -0.0222, 0.0032, -0.0017, 0.0777, -0.0293, 0.0058, 0.0518,
0.0615, -0.0786, -0.0213, -0.0042, 0.0795, -0.0344, 0.0648, 0.0586,
-0.0092, -0.0342, 0.0373, 0.1116],
[ 0.0207, -0.0222, 0.0032, -0.0017, 0.0777, -0.0293, 0.0058, 0.0518,
0.0615, -0.0786, -0.0213, -0.0042, 0.0795, -0.0344, 0.0648, 0.0586,
-0.0092, -0.0342, 0.0373, 0.1116]])

2. 复杂情况自动求导

  • 例三
m = Variable(torch.Tensor([[2,3]]),requires_grad=True)
n = Variable(torch.zeros(1,2))
print("m=",m)
print("n=",n)

n[0,0] = m[0,0] ** 2
n[0,1] = m[0,1] ** 3

print("通过m中的值计算n中的值",n)

n.backward(torch.ones_like(n)) # 将 (w0, w1) 取成 (1, 1)
print(m.grad)

这里遇到了一个小问题,开始自己编写时候少了一个'[]'Variable(torch.Tensor([2,3]),报错too many indices for tensor of dimension 1

m= tensor([[2., 3.]], requires_grad=True)
n= tensor([[0., 0.]])
通过m中的值计算n中的值 tensor([[ 4., 27.]], grad_fn=)
tensor([[ 4., 27.]])

通过自动求导我们得到了梯度是 4 和 27,我们可以验算一下
∂n/∂m0=w0∂n0/∂m0+w1∂n1/∂m0=2m0+0=2×2=4
∂n/∂m1=w0∂n0/∂m1+w1∂n1/∂m1=0+3m21=3×32=27
通过验算我们可以得到相同的结果

3. 多次自动求导
过调用 backward 我们可以进行一次自动求导,如果我们再调用一次 backward,会发现程序报错,没有办法再做一次。这是因为 PyTorch 默认做完一次自动求导之后,计算图就被丢弃了,所以两次自动求导需要手动设置一个东西.

x = Variable(torch.FloatTensor([3]),requires_grad=True)
y = x * 2 + x ** 2 + 3
print(y)

#设置retain_graph来保留计算图
y.backward(retain_graph=True)
print(x.grad)
y.backward()
print(x.grad)

tensor([18.]
tensor([8.])
tensor([16.])

三.dynamic-graph(动态图)

import torch
first_counter = torch.Tensor([0])
second_counter = torch.Tensor([10])

while(first_counter < second_counter)[0]:
    first_counter += 2
    second_counter += 1

print(first_counter)
print(second_counter)

tensor([20.])
tensor([20.])

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值