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():
相关推荐
sleP4o3 分钟前
Python操作MySQL
开发语言·python·mysql
晓星航9 分钟前
Docker本地部署Chatbot Ollama搭建AI聊天机器人并实现远程交互
人工智能·docker·机器人
Kenneth風车26 分钟前
【机器学习(五)】分类和回归任务-AdaBoost算法-Sentosa_DSML社区版
人工智能·算法·低代码·机器学习·数据分析
凌不了云29 分钟前
windows环境下安装python第三方包
开发语言·python
AI小白龙*29 分钟前
大模型团队招人(校招):阿里巴巴智能信息,2025届春招来了!
人工智能·langchain·大模型·llm·transformer
大熊程序猿30 分钟前
python 读取excel数据存储到mysql
数据库·python·mysql
生椰拿铁You32 分钟前
Python
python
鸽芷咕35 分钟前
【Python报错已解决】python setup.py bdist_wheel did not run successfully.
开发语言·python·机器学习·bug
空指针异常Null_Point_Ex1 小时前
大模型LLM之SpringAI:Web+AI(一)
人工智能·chatgpt·nlp
清纯世纪1 小时前
基于深度学习的图像分类或识别系统(含全套项目+PyQt5界面)
开发语言·python·深度学习