当前位置:网站首页>pytorch 23 hook的使用与介绍 及基于hook实现即插即用的DropBlock
pytorch 23 hook的使用与介绍 及基于hook实现即插即用的DropBlock
2022-06-27 01:58:00 【万里鹏程转瞬至】
Hook(钩子):字面意思就是勾住一个函数,在其执行前修改输入数据或其他操作,或者在其执行后修改输出输出或其他操作。通过hook操作可以锁定一个layer对象(model中的模块)的生命周期,监视其执行状态和执行结果。在pytorch中,提供丰富的hook api,让我们可以监听并修改tensor在模型forword中的状态。在本博文中,基于hook操作实现了即插即用的Dropout操作,支持dorpblock、dropout2d等系列Dropout操作。
在pytorch中layer的生命周期可以简单的理解为:定义->初始化->前向传播->反向传播->销毁。其中前向传播和反向传播是其执行流程中最重要的部分,通过对前向传播和反向传播进行hook操作,可以获取数据在模型中的执行状态(比如实现CAM)并进行修改操作(比如实现梯度裁剪)。
Grad-CAM:基于梯度的类别响应特征可视化。通过hook操作获取相应layer的forward流程中feature map的output和backward流程中grad的output,然后将feature_map_output与backward_grad_output中相应的元素相乘,然后实现类别响应特征可视化。
梯度裁剪:对layer的backward流程中grad的output的值进行约束,使其不能大于特定值从而导致梯度爆炸。
1、pytorch中的hook接口
pytorch针对Model、modules.module、ScriptModule和Tensor 4种模块分别提供了相应的hook接口。其中针对于Tensor只提供了一个用于反向传播的hook,因为tensor的前向传播流程对用户是完全可见的。针对于Model、modules.module、ScriptModul
边栏推荐
- Parameter estimation -- Chapter 7 study report of probability theory and mathematical statistics (point estimation)
- Oracle/PLSQL: Rpad Function
- Oracle/PLSQL: Replace Function
- Memcached basics 13
- memcached基础14
- H5 liquid animation JS special effect code
- ConstraintLayout(约束布局)开发指南
- Oracle/PLSQL: CharToRowid Function
- Google began to roll itself, AI architecture pathways was blessed, and 20billion generation models were launched
- Memcached foundation 9
猜你喜欢

Svg drag dress Kitty Cat

Parameter estimation -- Chapter 7 study report of probability theory and mathematical statistics (point estimation)

svg拖拽装扮Kitty猫

Arbre binaire OJ sujet

Flink学习1:简介

Look! In June, 2022, the programming language ranking list was released! The first place is awesome

p5.js死亡星球

Some exception handling for idea plug-in development

Pointer compression for JVM

I encountered some problems when connecting to the database. How can I solve them?
随机推荐
Oracle/PLSQL: Trim Function
Memcached basics 11
memcached基础9
Browser cache
get_ Usage Summary of sequencer
WiFi-IoT 鸿蒙开发套件样例开发
Reading a book in idea is too much!
Summary of config mechanism and methods in UVM (2)
Cvpr2022 | pointdistiller: structured knowledge distillation for efficient and compact 3D detection
C# Tcp服务器如何限制同一个IP的连接数量?
Sample development of WiFi IOT Hongmeng development kit
Oracle/PLSQL: Ltrim Function
Would rather go to 996 than stay at home! 24 years old, unemployed for 7 months, worse than work, no work
I earned 3W yuan a month from my sideline: the industry you despise really makes money!
Config in UVM_ How to use the DB mechanism
Flink学习3:数据处理模式(流批处理)
Oracle/PLSQL: Upper Function
P5.js death planet
Shell脚本系列篇(1) 入门
Oracle/PLSQL: Replace Function