当前位置:网站首页>pytorch with torch.no_grad
pytorch with torch.no_grad
2022-07-24 14:41:00 【日和公主】
首先,关于python中的with:
with 语句适用于对资源进行访问的场合,确保不管使用过程中是否发生异常都会执行必要的“清理”操作,释放资源,比如文件使用后自动关闭/线程中锁的自动获取和释放等。
关于 torch.no_grad()
首先从requires_grad讲起:
requires_grad
在pytorch中,tensor有一个requires_grad参数,如果设置为True,则反向传播时,该tensor就会自动求导。tensor的requires_grad的属性默认为False,若一个节点(叶子变量:自己创建的tensor)requires_grad被设置为True,那么所有依赖它的节点requires_grad都为True(即使其他相依赖的tensor的requires_grad = False)
当requires_grad设置为False时,反向传播时就不会自动求导了,因此大大节约了显存或者说内存。
with torch.no_grad的作用
在该模块下,所有计算得出的tensor的requires_grad都自动设置为False。
即使一个tensor(命名为x)的requires_grad = True,在with torch.no_grad计算,由x得到的新tensor(命名为w-标量)requires_grad也为False,且grad_fn也为None,即不会对w求导。
边栏推荐
- VSCode如何调试Nodejs
- C# 多线程锁整理记录
- [matlab] matlab drawing Series II 1. Cell and array conversion 2. Attribute cell 3. delete Nan value 4. Merge multiple figs into the same Fig 5. Merge multiple figs into the same axes
- The server switches between different CONDA environments and views various user processes
- 使用 Fiddler Hook 报错:502 Fiddler - Connection Failed
- Can't remember regular expressions? Here I have sorted out 99 common rules
- AtCoder Beginner Contest 261E // 按位思考 + dp
- SQL的SELF JOIN用法
- spark学习笔记(三)——sparkcore基础知识
- Rasa 3.x 学习系列-Rasa FallbackClassifier源码学习笔记
猜你喜欢

2022 IAA industry category development insight series report - phase II

mysql

The sliding window of Li Kou "step by step" (209. The smallest sub array, 904. Fruit baskets)

IEEE Transaction期刊模板使用注意事项

bibliometrix: 从千万篇论文中挖掘出最值得读的那一篇!

Bibliometrix: dig out the one worth reading from thousands of papers!

北京一卡通以35288.8529万元挂牌出让68.45%股权,溢价率为84%

Spark Learning Notes (III) -- basic knowledge of spark core

深入浅出边缘云 | 2. 架构
![Rasa 3.x 学习系列-Rasa [3.2.4] - 2022-07-21 新版本发布](/img/1e/27f107d514ded6641410cc5a45764b.png)
Rasa 3.x 学习系列-Rasa [3.2.4] - 2022-07-21 新版本发布
随机推荐
Research Summary / programming FAQs
Must use destructuring props assignmenteslint
Detailed explanation of address bus, data bus and control bus
Deep learning 1 perceptron and implementation of simple back propagation network
IntelliSense of Visual Studio: 'no members available'
TypeError: Cannot read property ‘make‘ of undefined
Rasa 3.x 学习系列-Rasa [3.2.3] - 2022-07-18 新版本发布
Spark: specify the date and output the log of the corresponding date (entry level - simple implementation)
2.4. properties of special profile
Time series of machine learning
Decrypt "sea Lotus" organization (domain control detection and defense)
How vscode debug nodejs
C# 多线程锁整理记录
Simple understanding and implementation of unity delegate
Strongly connected component
Video game design report template and
Binlog and iptables prevent nmap scanning, xtrabackup full + incremental backup, and the relationship between redlog and binlog
Where can Huatai Securities open an account? Is it safe to use a mobile phone
C language -- program environment and preprocessing
【机器学习】之 主成分分析PCA