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():
相关推荐
emo猫pro_max1 小时前
openclaw飞书流式回复配置指南
人工智能
FishCoderh1 小时前
被OpenClaw的Session搞晕了?这篇让你彻底搞懂
人工智能
孤烟2 小时前
19 万 + GitHub 星标!OpenClaw 凭什么成为 2026 最火 AI Agent,万字实测告诉你
人工智能
zhl772 小时前
YOLOv5:从0搭建你的第一个目标检测模型
人工智能
TechFind2 小时前
用 OpenClaw 搭建企业微信 AI Agent:从零到自动化客服只需 30 分钟
人工智能·agent
FishCoderh2 小时前
OpenClaw部署后Tools工具权限被禁用?一行配置解决
人工智能
飞哥数智坊4 小时前
openclaw 不是全站第一!但它的爆发,足以引人深思
人工智能
zone77395 小时前
001:LangChain的LCEL语法学习
人工智能·后端·面试
zone77395 小时前
001:简单 RAG 入门
后端·python·面试
F_Quant5 小时前
🚀 Python打包踩坑指南:彻底解决 Nuitka --onefile 配置文件丢失与重启报错问题
python·操作系统