当前位置:网站首页>Pytorch中如何获取某层Module?(方便改变梯度,获取特征图,CAM等)
Pytorch中如何获取某层Module?(方便改变梯度,获取特征图,CAM等)
2022-04-21 23:27:00 【我是一个对称矩阵】
1、引入
我们有时候需要改变某层梯度,或者获取某层梯度图,甚至画CAM可视化效果时,需要定位到模型中某层,但往往模型中层的结构层层嵌套,难。
2、方法
我们以torchvision.models中resnet34为例,来获取其中名"layer1.1.conv2"的层,以下是resnet34结构部分截图:
首先我们先看看所有层的名字:
for n,v in net.named_parameters():
print(n)
Output:
conv1.weight
bn1.weight
bn1.bias
layer1.0.conv1.weight
layer1.0.bn1.weight
layer1.0.bn1.bias
layer1.0.conv2.weight
layer1.0.bn2.weight
layer1.0.bn2.bias
layer1.1.conv1.weight
layer1.1.bn1.weight
layer1.1.bn1.bias
layer1.1.conv2.weight
layer1.1.bn2.weight
layer1.1.bn2.bias
layer1.2.conv1.weight
2.1、方法一:从结构上获取层
根据结果获取层的原则只有一个:非sequential对象的层,直接通过.名字获取,sequential对象的层通过[index]获取sequential对象里的某层
比如"layer1.1.conv2":
1ayer1:1ayer1在一个非sequential对象中,所以直接通过model.layer1获取到该层.1:layer1是一个sequential对象,获取它里面的组成需要通过[index]获取,所以通过model.layer1[1]获取到该层.conv2:conv2是model.layer1[1]中的,而model.layer1[1]不是sequential对象,所以直接通过名字获取,所以通过model.layer1[1].conv2获取到
所以如果你想对layer1.1.conv2做操作,直接通过lay=model.layer1[1].conv2就可以获取到该层,总结就是非纯数字的,直接通过名字,是纯数字的,通过上一层[index]来获取。
2.2、方式二:通过名字直接获取
我们已经知道想要操作的层的名字为"layer1.1.conv2",则通过以下方法获取该层:
name ="layer1.1.conv2"
for name, module in self.model.named_modules():
if name == self.module_name:
#则module 就是该层
版权声明
本文为[我是一个对称矩阵]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_40243750/article/details/124323591
边栏推荐
- Buuctf Netherlands broadband data disclosure
- Implementation of neural network attention mechanism by pytoch code
- 5、网络结构与ISP,分组延时、丢失、吞吐量
- idea 解决项目包出现[wrapper(1)]
- JMeter association parameters
- 经典题目刷一刷
- selenium点击的元素被遮挡无法操作的解决办法
- 6. Protocol hierarchy and service model (key)
- Electronic address book management system based on C
- Its and LPI interrupt of GIC spec 5
猜你喜欢

How does IOT platform realize business configuration center

341 Linux connection database

新独立版抖音口红机全修复版本附视频教程

文件操作和IO

Teach you to easily solve CSRF Cross Site Request Forgery Attack

IJCAI2022录用结果出炉!接收率15%,你中了吗?

Following Huawei Cangjie, it reproduces four domestic programming languages in various forms, including one 0 code

(七)瑞芯微rk3568 buildroot 添加编译好的脚本和二进制程序文件

Custom login successfully processed

【acwing】1125. 牛的旅行***(floyd)
随机推荐
leetcode:440. 字典序的第K小数字
NN in pytoch Brief introduction to adaptive avgpool2d (output_size)
简约易收录的导航网站源码
The pattern should be large and the vision should be broad, and the humanitarian spirit should be upheld [continuous updating, do not delete]
Electronic address book management system based on C
idea 解决项目包出现[wrapper(1)]
Ruffian Heng embedded: talk about the application and influence of system watchdog wdog1 in the startup of i.mxrt1xxx system
6、協議層次化和服務模型(重點)
Technology, products and brand are not problems. For SAIC Audi, these two points may be life and death
SWOOLE高性能内存数据库的使用和配置教程
Bit by bit concentrated and clean, the way to break the situation in the detergent industry
There are Chinese characters in the input parameter, and an error of 500 is reported. There is an internal error in the server
如何构建一个可“持续演进”的可观测体系?| QCon
Sélection et évolution des microservices dans l'architecture native du cloud
【H.264】简单编码器及SPS
IJCAI2022录用结果出炉!接收率15%,你中了吗?
点滴浓缩洁净,洗衣液行业的破局之路
Simple and easy to collect navigation website source code
.101键盘事件
How JMeter sets parameterization