当前位置: 首页 > news >正文

PyTorch_自动微分模块

自动微分 (Autograd) 模块对张量做了进一步的封装,具有自动求导功能。自动微分模块是构成神经网络训练的必要模块,在神经网络的反向传播过程中,Autograd 模块基于正向计算的结果对当前的参数进行微分计算,从而实现网络权重参数的更新。


梯度基本计算

使用 backward 方法,grad 属性来实现梯度的计算和访问。

import torch 
import numpy as np # 标量的梯度计算
def test01():# 对于需要求导的张量,需设置 requires_grad = Truex = torch.tensor(10, requires_grad=True, dtype=torch.float64)# 对 x 的中间计算f = x ** 2 + 20  # 求导获得 2x# 自动微分f.backward()# 访问梯度print(x.grad)# 向量的梯度计算
def test02():x = torch.tensor([10, 20, 30, 40], requires_grad=True, dtype=torch.float64)# 定义变量的计算过程y1 = x ** 2 + 20 # 注意:自动微分的时候,必须是一个标量y2 = y1.mean()  # 对 y1 / 4 的操作# 自动微分,求导y2.backward()print(x.grad)# 多标量梯度计算
def test03():x1 = torch.tensor(10, requires_grad=True, dtype=torch.float64)x2 = torch.tensor(20, requires_grad=True, dtype=torch.float64)# 中间计算过程y = x1 ** 2 + x2 ** 2 + x1 * x2 # 自动微分y.backward()# 打印梯度值print(x1.grad)print(x2.grad)# 多向量的梯度计算
def test04():x1 = torch.tensor([10, 20], requires_grad=True, dtype=torch.float64)x2 = torch.tensor([30, 40], requires_grad=True, dtype=torch.float64)# 定义中间件计算过程y = x1 ** 2 + x2 **2 + x1 * x2 # 将输出结果变为标量y = y.sum()# 自动微分y.backward()# 打印张量的梯度值print(x1.grad)print(x2.grad)if __name__ == "__main__":test04() 

控制梯度计算

当 requires_grad = True 时,张量在某些时候计算不进行梯度计算。

import torch 
import numpy as np # 控制梯度计算
# 训练时才用到梯度计算
def test01():x = torch.tensor(10, requires_grad=True, dtype=torch.float64)print(x.requires_grad)# 1. 第一钟方法with torch.no_grad():y = x**2print(y.requires_grad)#2. 针对函数# 第二种方式@torch.no_grad()def my_func(x):return x ** 2 y = my_func(x)print(y.requires_grad)#3. 第三种方式: 全局的方式torch.set_grad_enabled(False)y = x ** 2 print(y.requires_grad)# 梯度累加和梯度清零
def test02():x = torch.tensor([10, 20, 30, 40], requires_grad=True, dtype=torch.float64)# 当我们重复对x进行梯度计算的时候,是会将历史的梯度值累加到 x.grad 属性中# 不要取累加历史梯度for _ in range(3):# 对输入x的计算过程f1 = x ** 2 + 20# 将向量转换为标量f2 = f1.mean()# 梯度清零if x.grad is not None:x.grad.data.zero_()# 自动微分f2.backward()print(x.grad)# 梯度下降优化函数
def test03():x = torch.tensor(10, requires_grad=True, dtype=torch.float64)for _ in range(100):# 正向计算y = x ** 2# 梯度清零if x.grad is not None:x.grad.data.zero_()# 自动微分y.backward()# 更新参数x.data = x.data - 0.001 * x.grad # 打印 x 的值print('%.10f' % x.data)if __name__ == "__main__":test03() 

梯度计算注意点

当对设置 requires_grad = True 的张量使用 numpy 函数进行转换时,会出现如下错误:

Can't call numpy()  on Tensor that requires grad. Use tensor.detach().numpy() instead.

此时,需要先使用 detach 函数将张量进行分离,再使用 numpy 函数。

注意:detach 之后会产生一个新的张量,新的张量做为叶子节点并且该张量和原来的张量共享数据,但是分离后的张量不需要计算梯度。

import torch 
import numpy as np # 错误方式
def test01():x = torch.tensor([10, 20], requires_grad=True, dtype=torch.float64)# RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.# print(x.numpy())# 正确的做法print(x.detach().numpy())# 共享数据
def test02():# x 是叶子节点x1 = torch.tensor([10, 20], requires_grad=True, dtype=torch.float64)# 使用 detach 函数来分离出一个新的张量x2 = x1.detach()print(id(x1.data), id(x2.data))# 修改分离后产生的新的张量x2[0] = 100 print(x1)print(x2)# 通过结果我么发现,x2 张量不存在 requires_grad = True # 表示:对 x1 的任何计算都会影响到对 x1 的梯度计算# 但是,对 x2 的任何计算不会影响到 x1 的梯度计算print(x1.requires_grad)print(x2.requires_grad)if __name__ == "__main__":test02() 
http://www.xdnf.cn/news/285805.html

相关文章:

  • 时间同步服务核心知识笔记:原理、配置与故障排除
  • 因为gromacs必须安装cuda(系统自带的NVIDIA驱动不行),这里介绍下如何安装cuda
  • 学习路线(机器人软件架构)
  • Java常用注解大全(基于JDK17+SpringBoot3)
  • 对ubuntu的简单介绍
  • Redis:现代服务端开发的缓存基石与电商实践-优雅草卓伊凡
  • 题目 3321: 蓝桥杯2025年第十六届省赛真题-画展布置
  • SpringMVC 框架核心知识点详解与实战
  • 精益数据分析(41/126):深入解读移动应用商业模式的关键指标与策略
  • linux 高并发 文件句柄数 fs 及 tcp端口数调优
  • 泉州2025年首次网签备案登记的商品住宅并在本年度进行装修、改造及家装物品和材料购置的,在上述补贴额度的基础上上浮2万元,单个产权人补贴最高不超过5万元。
  • VScode中关于Copilot的骚操作
  • ByteArrayOutputStream 类详解
  • 基于yolov11的打电话玩手机检测系统python源码+pytorch模型+评估指标曲线+精美GUI界面
  • 一文说清-什么是强化学习
  • zst-2001 历年真题 程序设计语言
  • 代码随想录算法训练营 Day37 动态规划Ⅴ 完全背包 零钱兑换
  • 【Java ee初阶】多线程(7)
  • C++负载均衡远程调用学习之获取主机信息功能
  • Redis 中简单动态字符串(SDS)的深入解析
  • Vue项目安全实践指南:从输入验证到状态管理的全方位防护
  • 利用WPS创建的Templates目录,快捷生成md文件
  • 【信息系统项目管理师-论文真题】2007下半年论文详解(包括解题思路和写作要点)
  • E-R图作业
  • lambda表达式和方法引用
  • 【Linux】网络基础
  • Python内置函数
  • python打卡day16
  • PyCharm 安装教程
  • 【神经网络与深度学习】深度学习中的生成模型简介