当前位置:网站首页>PyTorch 12. hook的用法
PyTorch 12. hook的用法
2022-04-23 06:11:00 【DCGJ666】
PyTorch 12. hook的用法
hook
- 由于pytorch会自动舍弃图计算的中间结果,所以想要这些数值就需要使用钩子函数。钩子函数包括Variable的钩子和nn.Module钩子,用法相似。
- 在使用hook函数时,不应该修改它的输入,但是它可以返回一个替代当前梯度的新梯度,即,使用该函数会返回一个梯度值
register_hook
针对Tensor变量的hook函数
import torch
grad_list = []
def print_grad(grad):
grad_list.append(grad)
x = torch.randn(2,1)
y = x+2
y.register_hook(print_grad)
y.backward()
在整个网络进行反向传递时,运行到钩子函数注册的变量后,会保存该变量的梯度,调用print_grad函数,将梯度添加到grad_list中
register_forward_hook
针对网络层的hook函数,具体的可视化博客,可以参考我的另一篇文档,可视化特定层
钩子函数不应该修改输入和输出,并且在使用后应及时删除,以避免每次都运行钩子增加运行负载。钩子函数主要用在获取某些中间结果的情景,如中间某一层的输出或某一层的梯度。
import torch
model = VGG()
features = torch.Tensor()
def hook(module, input, output):
# 把这一层的输出拷贝到features中
features.copy_(output.data)
handle = model.layer8.register_forward_hook(hook)
_ = model(input)
# 用完hook后删除
handle.remove()
register_backward_hook
首先介绍Container的概念:当Module的forward函数中只有一个Function的时候,称为Module,如果Module包含其他Module,称之为Container.
在module上注册一个backward hook。此方法目标只能用在Module上,不能用在Container上。
每次计算module的inputs的梯度时,这个hook会被调用
hook(module,grad_input,grad_output)->Tensor or None
求取模块的梯度,与register_grad类似
版权声明
本文为[DCGJ666]所创,转载请带上原文链接,感谢
https://blog.csdn.net/DCGJ666/article/details/121638159
边栏推荐
- MySQL notes 1_ database
- [2021 book recommendation] practical node red programming
- 【点云系列】Multi-view Neural Human Rendering (NHR)
- [recommendation of new books in 2021] practical IOT hacking
- MySQL notes 4_ Primary key auto_increment
- Chapter 4 pytoch data processing toolbox
- Migrating your native/mobile application to Unified Plan/WebRTC 1.0 API
- SSL/TLS应用示例
- 机器学习 二:基于鸢尾花(iris)数据集的逻辑回归分类
- Exploration of SendMessage principle of advanced handler
猜你喜欢

Chapter 1 numpy Foundation

第2章 Pytorch基础2

【点云系列】Fully-Convolutional geometric features

第5 章 机器学习基础
![[point cloud series] a rotation invariant framework for deep point cloud analysis](/img/43/065d552d216b3e253d25dcfdfaaff4.png)
[point cloud series] a rotation invariant framework for deep point cloud analysis

Chapter 5 fundamentals of machine learning

【點雲系列】SG-GAN: Adversarial Self-Attention GCN for Point Cloud Topological Parts Generation

Machine learning II: logistic regression classification based on Iris data set
![[2021 book recommendation] effortless app development with Oracle visual builder](/img/db/d8802b42d5374e4117db638a0b82b0.png)
[2021 book recommendation] effortless app development with Oracle visual builder

【2021年新书推荐】Red Hat RHCSA 8 Cert Guide: EX200
随机推荐
The Cora dataset was trained and tested using the official torch GCN
face_recognition人脸检测
MySQL数据库安装与配置详解
Binder mechanism principle
Migrating your native/mobile application to Unified Plan/WebRTC 1.0 API
torch_ Geometric learning 1, messagepassing
【2021年新书推荐】Red Hat Certified Engineer (RHCE) Study Guide
【点云系列】Pointfilter: Point Cloud Filtering via Encoder-Decoder Modeling
Some common data type conversion methods in pytorch are similar to list and NP Conversion method of ndarray
[dynamic programming] longest increasing subsequence
Handler进阶之sendMessage原理探索
Recyclerview batch update view: notifyitemrangeinserted, notifyitemrangeremoved, notifyitemrangechanged
【点云系列】SO-Net:Self-Organizing Network for Point Cloud Analysis
HandlerThread原理和实际应用
【2021年新书推荐】Kubernetes in Production Best Practices
Visual Studio 2019安装与使用
PyTorch中的一些常见数据类型转换方法,与list和np.ndarray的转换方法
MySQL notes 3_ Restraint_ Primary key constraint
Pytorch model pruning example tutorial III. multi parameter and global pruning
[Point Cloud Series] SG - Gan: Adversarial Self - attachment GCN for Point Cloud Topological parts Generation