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():
相关推荐
JoannaJuanCV7 分钟前
自动驾驶—CARLA仿真(19)automatic_control demo
人工智能·机器学习·自动驾驶
热爱生活的五柒11 分钟前
PolSAR Image Registration——极化合成孔径雷达(PolSAR)图像配准
人工智能·计算机视觉·sar
qq_2337727111 分钟前
**给复杂机器“装上行车记录仪”:一篇量子论文如何照亮AI时代的信任之路**
人工智能
美林数据Tempodata11 分钟前
案例分享|西安财经大学打造全覆盖、全链条人工智能通识教育培养体系
人工智能
O561 6O623O7 安徽正华露15 分钟前
露,生物信号采集处理系统一体机 生物机能实验系统 生物信号采集处理系统 生理机能实验
人工智能
AI营销快线18 分钟前
原圈科技如何引领AI营销内容生产升级:行业进化路线与闭环创新洞察
人工智能
AI营销先锋21 分钟前
2025 AI市场舆情分析行业报告:原圈科技如何帮助企业穿越迷雾,寻找增长北极星
大数据·人工智能
阿华田51224 分钟前
如何基于Jupyter内核自研NoteBook
ide·python·jupyter·自研notebook
找方案25 分钟前
hello-agents 学习笔记:智能体发展史 —— 从符号逻辑到 AI 协作的进化之旅
人工智能·笔记·学习·智能体·hello-agents
skywalk816328 分钟前
Auto-Coder用Qwen3-Coder-30B-A3B-Instruct模型写一个学习汉字的项目
人工智能·学习·auto-coder