pytorch2 AutoGrad

python 复制代码
from __future__ import print_function
import torch
x = torch.randn(3,3,requires_grad = True)
print(x.grad_fn)
复制代码
C:\Users\12035\.conda\envs\yolov8\lib\site-packages\numpy\_distributor_init.py:30: UserWarning: loaded more than 1 DLL from .libs:
C:\Users\12035\.conda\envs\yolov8\lib\site-packages\numpy\.libs\libopenblas64__v0.3.21-gcc_10_3_0.dll
C:\Users\12035\.conda\envs\yolov8\lib\site-packages\numpy\.libs\libopenblas64__v0.3.23-gcc_10_3_0.dll
  warnings.warn("loaded more than 1 DLL from .libs:"


None
python 复制代码
from __future__ import print_function
python 复制代码
# 计算导数
# 设置requires_grad=True用来追踪其计算历史
x = torch.ones(2, 2, requires_grad=True)
print(x)
print(x.grad_fn)
复制代码
tensor([[1., 1.],
        [1., 1.]], requires_grad=True)
None
python 复制代码
y = x**2
print(y)
print(y.grad_fn)
复制代码
tensor([[1., 1.],
        [1., 1.]], grad_fn=<PowBackward0>)
<PowBackward0 object at 0x0000021CE85562B0>
python 复制代码
z = y * y * 3
out = z.mean()
print(z, out)
复制代码
tensor([[3., 3.],
        [3., 3.]], grad_fn=<MulBackward0>) tensor(3., grad_fn=<MeanBackward0>)
python 复制代码
out.backward()
python 复制代码
print(x.grad)
复制代码
tensor([[3., 3.],
        [3., 3.]])

注意:grad在反向传播过程中是累加的(accumulated),这意味着每一次运行反向传播,梯度都会累加之前的梯度,所以一般在反向传播之前需把梯度清零

python 复制代码
# 再来反向传播⼀一次,注意grad是累加的
out2 = x.sum()
out2.backward()
print(x.grad)
复制代码
tensor([[4., 4.],
        [4., 4.]])
python 复制代码
# 梯度清零 再求导
out3 = x.sum()
x.grad.data.zero_()
out3.backward()
print(x.grad)
复制代码
tensor([[1., 1.],
        [1., 1.]])
python 复制代码
x = torch.randn(3, requires_grad=True)
print(x)

y = x * 2
i = 0
# norm y每个元素进行平方,然后对它们求和,最后取平方根 ,L2范式
while y.data.norm() < 1000:
    print(y.data.norm())
    y = y * 2
    i = i + 1
print(y)
print(i)
复制代码
tensor([0.3964, 0.0265, 0.1927], requires_grad=True)
tensor(0.8830)
tensor(1.7661)
tensor(3.5322)
tensor(7.0644)
tensor(14.1288)
tensor(28.2575)
tensor(56.5151)
tensor(113.0301)
tensor(226.0603)
tensor(452.1205)
tensor(904.2410)
tensor([1623.5339,  108.3694,  789.3040], grad_fn=<MulBackward0>)
11
python 复制代码
# 也可以通过将代码块包装在 with torch.no_grad(): 中,
#来阻止 autograd 跟踪设置了.requires_grad=True的张量的历史记录。
print(x.requires_grad)
print((x ** 2).requires_grad)

with torch.no_grad():
    print((x ** 2).requires_grad)
复制代码
True
True
False
python 复制代码
#如果我们想要修改 tensor 的数值,但是又不希望被 autograd 记录(即不会影响反向传播), 
#那么我们可以对 tensor.data 进行操作
x = torch.ones(1,requires_grad=True)

print(x.data) # 还是一个tensor
print(x.data.requires_grad) # 但是已经是独立于计算图之外

y = 2 * x
print(y)
x.data *= 100 # 只改变了值,不会记录在计算图,所以不会影响梯度传播
print(x) # 更改data的值也会影响tensor的值 
y.backward()

print(x.grad)
复制代码
tensor([1.])
False
tensor([2.], grad_fn=<MulBackward0>)
tensor([100.], requires_grad=True)
tensor([2.])

小结

torch.Tensor 是这个包的核心类。如果设置它的属性 .requires_grad 为 True,那么它将会追踪对于该张量的所有操作。当完成计算后可以通过调用 .backward(),来自动计算所有的梯度。这个张量的所有梯度将会自动累加到.grad属性。

复制代码
注意:在 y.backward() 时,如果 y 是标量,则不需要为 backward() 传入任何参数;否则,需要传入一个与 y 同形的Tensor。

requires_grad参数说明:

复制代码
requires_grad=True时,表示参数需要参与训练,并在训练过程中进行梯度计算
requires_grad=False时,表示参数不需要参与训练,可以将代码块包装在 with torch.no_grad(): 中

autograd 要点:

复制代码
Tensor 和 Function 互相连接生成了一个无环图 (acyclic graph),它编码了完整的计算历史。每个张量都有一个.grad_fn属性,该属性引用了创建 Tensor 自身的Function
张量是用户手动创建的,即这个张量的grad_fn是 None
  • 梯度会进行累加,所以要及时清零 x.grad.data.zero_()
  • 不想进行求导时,with torch.no_grad():
相关推荐
10岁的博客11 分钟前
PyTorch快速搭建CV模型实战
人工智能·pytorch·python
WWZZ202533 分钟前
快速上手大模型:深度学习3(实践:线性神经网络Softmax)
人工智能·深度学习·神经网络·机器人·大模型·slam·具身感知
兩尛34 分钟前
神经网络补充知识
人工智能·神经网络·机器学习
焦点链创研究所35 分钟前
x402支付协议:促AI资产从概念走向落地
人工智能
寒秋丶36 分钟前
AutoGen多智能体协作、人机交互与终止条件
人工智能·python·microsoft·ai·人机交互·ai编程·ai写作
Turnsole_y44 分钟前
pytest与Selenium结合使用指南
开发语言·python
达芬奇科普2 小时前
俄罗斯全面禁止汽油出口对俄、欧、中能源市场的多维影响分析
大数据·人工智能
AI量化投资实验室2 小时前
年化398%,回撤11%,夏普比5,免费订阅,5积分可查看参数|多智能体的架构设计|akshare的期货MCP代码
人工智能·python
电鱼智能的电小鱼2 小时前
基于电鱼 ARM 工控机的煤矿主控系统高可靠运行方案——让井下控制系统告别“死机与重启”
arm开发·人工智能·嵌入式硬件·深度学习·机器学习