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

【Pytorch】AutoGrad个人理解

前提知识:[Pytorch] 前向传播和反向传播示例_友人小A的博客-CSDN博客

目录

简介

叶子节点

Tensor AutoGrad Functions


简介

torch.autograd是PyTorch的自动微分引擎(自动求导),为神经网络训练提供动力。torch.autograd需要对现有代码进行最少的更改——声明需要计算梯度的Tensor的属性requires_grad=True。截至目前,PyTorch仅支持 FloatTensor类型(half、float、double和bfloat16)和 ComplexTensor(cfloat、cdouble)的autograd。【信息来自官网】

叶子节点

叶子结点是离散数学中的概念。一棵树当中没有子结点(即度为0)的结点称为叶子结点,简称“叶子”。 叶子是指出度为0的结点,又称为终端结点。

在pytorch中,什么是叶子节点?根据官方定义理解如下。

  • 所有requires_grad为False的张量,都约定俗成地归结为叶子张量
  • requires_grad为True的张量, 如果他们是由用户创建的,则它们是叶张量(leaf Tensor), 表明不是运算的结果,因此grad_fn=None

示例1

def test_training_pipeline2():input_data = [[4, 4, 4, 4],[9, 9, 9, 9]]  # 2x4input = torch.tensor(input_data, dtype=torch.float32)  # requires_grad=Falseoutput = torch.sqrt(input)target_data = [1, 2, 3, 4]target = torch.tensor(target_data, dtype=torch.float32)  # requires_grad=Falseloss_fn = torch.nn.MSELoss()loss = loss_fn(input=output, target=target)print("\ninput.is_leaf:", input.is_leaf)print("output.requires_grad:", output.requires_grad)print("output.is_leaf:", output.is_leaf)print("target.is_leaf:", target.is_leaf)print("loss.requires_grad:", loss.requires_grad)print("loss.is_leaf:", loss.is_leaf)

样例2

def test_training_pipeline2():input_data = [[4, 4, 4, 4],[9, 9, 9, 9]]  # 2x4input = torch.tensor(input_data, dtype=torch.float32)  # requires_grad=Falseoutput = torch.sqrt(input)output.requires_grad_(True) # requires_grad=Truetarget_data = [1, 2, 3, 4]target = torch.tensor(target_data, dtype=torch.float32)  # requires_grad=Falseloss_fn = torch.nn.MSELoss()loss = loss_fn(input=output, target=target)print("\ninput.is_leaf:", input.is_leaf)print("output.requires_grad:", output.requires_grad)print("output.is_leaf:", output.is_leaf)print("target.is_leaf:", target.is_leaf)print("loss.requires_grad:", loss.requires_grad)print("loss.is_leaf:", loss.is_leaf)

样例3

 

def test_training_pipeline5():input = torch.rand(1, requires_grad=True)output = torch.unique(input=input, sorted=True, return_inverse=False, return_counts=False, dim=None)print("\ninput.is_leaf:", input.is_leaf)print("output.requires_grad:", output.requires_grad)print("output.is_leaf:", output.is_leaf)output.backward()

样例4

def test_training_pipeline3():input_data = [[4, 4, 4, 4],[9, 9, 9, 9]]  # 2x4input_a = torch.tensor(input_data, dtype=torch.float32, requires_grad=True)input_b = torch.tensor(input_data, dtype=torch.float32, requires_grad=True)output = torch.ne(input_a, input_b)print("\ninput_a.is_leaf:", input_a.is_leaf)print("input_b.is_leaf:", input_b.is_leaf)print("output.dtype:", output.dtype)print("output.requires_grad:", output.requires_grad)print("output.is_leaf:", output.is_leaf)output.backward()   # 报错

 

 

样例5

def test_training_pipeline7():input_data = [[4, 4, 4, 4],[9, 9, 9, 9]]  # 2x4input_a = torch.tensor(input_data, dtype=torch.float32, requires_grad=True)input_b = torch.tensor(input_data, dtype=torch.float32)    output = torch.add(input_a, input_b)print("\ninput_a.requires_grad:", input_a.requires_grad)print("input_b.requires_grad:", input_b.requires_grad)print("output.requires_grad:", output.requires_grad)print("output.is_leaf:", output.is_leaf)grad = torch.ones_like(output)input_b[0][0] = 10 input_a[0][0] = 10 output.backward(grad)

 样例6

def test_training_pipeline9():x = torch.tensor([1.0], requires_grad=True)y = x + 2z = 2 * y		# <-- dz/dy=2y[0] = -2.0print("\nx.is_leaf:", x.is_leaf)print("y.is_leaf:", y.is_leaf)print("z.is_leaf:", z.is_leaf)print("\nx.requires_grad:", x.requires_grad)print("y.requires_grad:", y.requires_grad)print("z.requires_grad:", z.requires_grad)z.backward()def test_training_pipeline9():x = torch.tensor([1.0], requires_grad=True)y = x + 2z = y * y  # <-- dz/dy= 2*yy[0] = -2.0print("\nx.is_leaf:", x.is_leaf)print("y.is_leaf:", y.is_leaf)print("z.is_leaf:", z.is_leaf)print("\nx.requires_grad:", x.requires_grad)print("y.requires_grad:", y.requires_grad)print("z.requires_grad:", z.requires_grad)z.backward()

 

Tensor AutoGrad Functions

  1. Tensor.grad

  2. Tensor.requires_grad

  3. Tensor.is_leaf

  4. Tensor.backward(gradient=None, reqain_graph=None, create_graph=False)

  5. Tensor.detach()

  6. Tensor.detach_()

  7. Tensor.retain_grad()

相关文章:

【Pytorch】AutoGrad个人理解

前提知识&#xff1a;[Pytorch] 前向传播和反向传播示例_友人小A的博客-CSDN博客 目录 简介 叶子节点 Tensor AutoGrad Functions 简介 torch.autograd是PyTorch的自动微分引擎&#xff08;自动求导&#xff09;&#xff0c;为神经网络训练提供动力。torch.autograd需要对…...

华硕z790让独显和集显同时工作

系统用了一段时间&#xff0c;现在想让显卡主要做深度学习训练&#xff0c;集显用来连接显示器。却发现显示器接到集显接口无信号。 打售后客服也没有解决&#xff0c;现在把解决方案记录一下。 这是客服给的方案&#xff1a; 请开机后进BIOS---Advanced---System Agent (SA)…...

提高编程思维的python代码

1.通过函数取差。举例&#xff1a;返回有差别的列表元素 from math import floordef difference_by(a,b,fn):b set(map(fn, b))return [i for i in a if fn(i) not in b] print(difference_by([2.1, 1.2], [2.3, 3.4], floor))2.一行代码调用多个函数 def add(a, b):return …...

CSS背景background属性整理

1.background-color background-color属性&#xff1a;设置元素的背景颜色 2.background-position background-position属性&#xff1a;设置背景图像的起始位置&#xff0c;需要把 background-attachment 属性设置为 "fixed"&#xff0c;才能保证该属性在 Firefo…...

AQS底层源码深度剖析-Lock锁

目录 AQS底层源码深度剖析-Lock锁 ReentrantLock底层原理 为什么把获取锁失败的线程加入到阻塞队列中&#xff0c;而不是采取其它方法&#xff1f; 总结&#xff1a;三大核心原理 CAS是啥&#xff1f; 代码模拟一个CAS&#xff1a; 公平锁与非公平锁 可重入锁的应用场景&…...

网络编程(二)

6. TCP 三次握手四次挥手 HTTP 协议是 Hype Transfer Protocol&#xff08;超文本传输协议&#xff09;的缩写&#xff0c;是用于从万维网&#xff08;WWW&#xff1a;World Wide Web&#xff09;服务器&#xff08;sever&#xff09;传输超文本到客户端&#xff08;本地浏览器…...

访问学者进入美国哪些东西不能带?

随着疫情的稳定&#xff0c;各国签证的逐步放开&#xff0c;成功申请到国外访问学者、博士后如何顺利的进入国外&#xff0c;哪些东西不能带&#xff0c;下面就随知识人网小编一起看一看。一、畜禽肉类(Meats, Livestock and Poultry)不论是新鲜的、干燥的、罐头的、真空包装的…...

灵巧手抓持<分类><仿真>

获取灵巧手抓取物体时的抓持类型&#xff0c;需要考虑&#xff1a;手本身的结构、被抓取物体的形状尺寸、抓持操作任务的条件。 研究方法&#xff1a;基于模型的方法、基于数据驱动的方法 基于模型的方法&#xff1a;建立灵巧手抓持相关的运动学和动力学模型建立目标函数求解…...

CENTO OS上的网络安全工具(十九)ClickHouse集群部署

一、VMware上集群部署ClickHouse &#xff08;一&#xff09;网络设置 1. 通过修改文件设置网络参数 &#xff08;1&#xff09;CentOS 在CENTOS上的网络安全工具&#xff08;十六&#xff09;容器特色的Linux操作_lhyzws的博客-CSDN博客中我们提到过可以使用更改配置文件的方式…...

tesseract -图像识别

下载链接&#xff1a;https://digi.bib.uni-mannheim.de/tesseract/如下选择最新的版本&#xff0c;这里我选择tesseract-ocr-w64-setup-5.3.0.20221222.exe有如下python模块操作tesseractpyocr 国内源&#xff1a;pip install -i https://pypi.mirrors.ustc.edu.cn/simple/ py…...

JavaScript Math 算数对象

文章目录JavaScript Math 算数对象Math 对象Math 对象属性Math 对象方法算数值算数方法JavaScript Math 算数对象 Math&#xff08;算数&#xff09;对象的作用是&#xff1a;执行常见的算数任务。 Math 对象 Math&#xff08;算数&#xff09;对象的作用是&#xff1a;执行普…...

一体机HDATA节点添加和删除

瀚高数据库 目录 文档用途 详细信息 文档用途 一体机可在线添加、删除数据库集群节点。 详细信息 一体机可在线添加、删除数据库集群节点。具体操作步骤如下 一、节点添加 集群可以在其他机器上通过配置hghac.yaml文件&#xff0c;将新节点加入集群。 集群操作 1&#xf…...

关于 interface{} 会有啥注意事项?上

学习 golang &#xff0c;对于 interface{} 接口类型&#xff0c;我们一定绕不过&#xff0c;咱们一起来看看 使用 interface{} 的时候&#xff0c;都有哪些注意事项吧 interface {} 可以用于模拟多态 xdm 咱们写一个简单的例子&#xff0c;就举动物的例子 写一个 Animal 的…...

Matlab中旧版modem.qammod与新版不兼容

最近&#xff0c;因为课题需要&#xff0c;在研究通信。在网上下了一个2015年左右的代码&#xff0c;其中用的是matlab旧版中的modem.qammod函数&#xff0c;但是旧版中的函数已经被删除了&#xff0c;&#xff08;这里必须得吐槽一下&#xff0c;直接该函数内部运行机制就行呀…...

通达信指标公式颜色代码的四种写法(COLOR/RGB)

通达信指标公式颜色代码有四种写法&#xff0c;分别为COLOR颜色的英文、COLOR十六进制、RGBX十六进制、RGB(R,G,B)。标题有点尴尬&#xff0c;让我想到孔乙己“茴”字的四种写法&#xff0c;哈哈。 一、COLOR颜色的英文 “COLOR颜色的英文”这种写法比较简单&#xff0c;函数库…...

小程序面试题收集(持续更新中...)

小程序面试题收集 1.请谈谈微信小程序主要目录和文件的作用 project.config.json&#xff1a;项目配置文件&#xff0c;用的最多的就是配置是否开启https校验App.js&#xff1a;设置一些全局的基础数据等App.json&#xff1a;底部tab&#xff0c;标题栏和路由等设置App.wxss&…...

最深情的告白——郁金香(Python实现)

目录 1 最深情的告白 2 即兴赞之 2.1 李小白言郁金香 2.2 郁金香般的姑娘 2.3 荷兰的郁金香 3 Python代码实现 3.1 郁金香的芬芳 3.2 我俩绚丽多姿的风景 1 最深情的告白 曾经以为&#xff0c;她爱玫瑰&#xff0c;然后我画了好几种&#xff1a; 花仙子——玫瑰&a…...

代码随想录算法训练营第六天|242.有效的字母异位词 、349. 两个数组的交集 、 202. 快乐数、1. 两数之和

当我们遇到了要快速判断一个元素是否出现集合里的时候&#xff0c;就要考虑哈希法。哈希法是牺牲了空间换取了时间&#xff0c;要使用额外的数组&#xff0c;set或者是map来存放数据&#xff0c;才能实现快速的查找。当我们要使用集合来解决哈希问题的时候&#xff0c;优先使用…...

【STL】模拟实现list

目录 1、list介绍 所要实现类及其成员函数接口总览 2、结点类的模拟实现 基本框架 构造函数 3、迭代器类的模拟实现 迭代器类存在的意义 3.1、正向迭代器 基本框架 默认成员函数 构造函数 运算符重载 --运算符重载 !运算符重载 运算符重载 *运算符重载 …...

Spring Cloud Alibaba全家桶(五)——微服务组件Nacos配置中心

前言 本文小新为大家带来 微服务组件Nacos配置中心 相关知识&#xff0c;具体内容包括Nacos Config快速开始指引&#xff0c;搭建nacos-config服务&#xff0c;Config相关配置&#xff0c;配置的优先级&#xff0c;RefreshScope注解等进行详尽介绍~ 不积跬步&#xff0c;无以至…...

手游刚开服就被攻击怎么办?如何防御DDoS?

开服初期是手游最脆弱的阶段&#xff0c;极易成为DDoS攻击的目标。一旦遭遇攻击&#xff0c;可能导致服务器瘫痪、玩家流失&#xff0c;甚至造成巨大经济损失。本文为开发者提供一套简洁有效的应急与防御方案&#xff0c;帮助快速应对并构建长期防护体系。 一、遭遇攻击的紧急应…...

循环冗余码校验CRC码 算法步骤+详细实例计算

通信过程&#xff1a;&#xff08;白话解释&#xff09; 我们将原始待发送的消息称为 M M M&#xff0c;依据发送接收消息双方约定的生成多项式 G ( x ) G(x) G(x)&#xff08;意思就是 G &#xff08; x ) G&#xff08;x) G&#xff08;x) 是已知的&#xff09;&#xff0…...

centos 7 部署awstats 网站访问检测

一、基础环境准备&#xff08;两种安装方式都要做&#xff09; bash # 安装必要依赖 yum install -y httpd perl mod_perl perl-Time-HiRes perl-DateTime systemctl enable httpd # 设置 Apache 开机自启 systemctl start httpd # 启动 Apache二、安装 AWStats&#xff0…...

Python爬虫实战:研究feedparser库相关技术

1. 引言 1.1 研究背景与意义 在当今信息爆炸的时代,互联网上存在着海量的信息资源。RSS(Really Simple Syndication)作为一种标准化的信息聚合技术,被广泛用于网站内容的发布和订阅。通过 RSS,用户可以方便地获取网站更新的内容,而无需频繁访问各个网站。 然而,互联网…...

高频面试之3Zookeeper

高频面试之3Zookeeper 文章目录 高频面试之3Zookeeper3.1 常用命令3.2 选举机制3.3 Zookeeper符合法则中哪两个&#xff1f;3.4 Zookeeper脑裂3.5 Zookeeper用来干嘛了 3.1 常用命令 ls、get、create、delete、deleteall3.2 选举机制 半数机制&#xff08;过半机制&#xff0…...

Leetcode 3577. Count the Number of Computer Unlocking Permutations

Leetcode 3577. Count the Number of Computer Unlocking Permutations 1. 解题思路2. 代码实现 题目链接&#xff1a;3577. Count the Number of Computer Unlocking Permutations 1. 解题思路 这一题其实就是一个脑筋急转弯&#xff0c;要想要能够将所有的电脑解锁&#x…...

学校招生小程序源码介绍

基于ThinkPHPFastAdminUniApp开发的学校招生小程序源码&#xff0c;专为学校招生场景量身打造&#xff0c;功能实用且操作便捷。 从技术架构来看&#xff0c;ThinkPHP提供稳定可靠的后台服务&#xff0c;FastAdmin加速开发流程&#xff0c;UniApp则保障小程序在多端有良好的兼…...

自然语言处理——循环神经网络

自然语言处理——循环神经网络 循环神经网络应用到基于机器学习的自然语言处理任务序列到类别同步的序列到序列模式异步的序列到序列模式 参数学习和长程依赖问题基于门控的循环神经网络门控循环单元&#xff08;GRU&#xff09;长短期记忆神经网络&#xff08;LSTM&#xff09…...

浅谈不同二分算法的查找情况

二分算法原理比较简单&#xff0c;但是实际的算法模板却有很多&#xff0c;这一切都源于二分查找问题中的复杂情况和二分算法的边界处理&#xff0c;以下是博主对一些二分算法查找的情况分析。 需要说明的是&#xff0c;以下二分算法都是基于有序序列为升序有序的情况&#xf…...

Kafka入门-生产者

生产者 生产者发送流程&#xff1a; 延迟时间为0ms时&#xff0c;也就意味着每当有数据就会直接发送 异步发送API 异步发送和同步发送的不同在于&#xff1a;异步发送不需要等待结果&#xff0c;同步发送必须等待结果才能进行下一步发送。 普通异步发送 首先导入所需的k…...