当前位置:网站首页>How to obtain a module of a certain layer in pytorch? (it is convenient to change gradient, obtain feature map, cam, etc.)
How to obtain a module of a certain layer in pytorch? (it is convenient to change gradient, obtain feature map, cam, etc.)
2022-04-21 23:38:00 【I am a symmetric matrix】
1、 introduce
Sometimes we need to change the gradient of a certain layer , Or get a gradient map of a layer , Even painting CAM When visualizing , You need to navigate to a layer in the model , But often the middle-level structure of the model is nested layer by layer , difficult .
2、 Method
We use torchvision.models in resnet34 For example , To get the name "layer1.1.conv2" The layer , Here are resnet34 Screenshot of structural part :
First, let's look at the names of all the layers :
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、 Method 1 : Get the layer from the structure
There is only one principle to obtain the layer according to the result : Not sequential Layers of objects , Directly through . name obtain ,sequential The layer of the object passes through [index] obtain sequential A layer in an object
such as "layer1.1.conv2":
1ayer1:1ayer1 In a non sequential In the object , So straight through model.layer1 Get this layer.1:layer1 It's a sequential object , To get the composition in it, you need to pass [index] obtain , So pass model.layer1[1] Get this layer.conv2:conv2 yes model.layer1[1] Medium , and model.layer1[1] No sequential object , So get directly by name , So pass model.layer1[1].conv2 Get
So if you want to be right layer1.1.conv2 Do something , Directly through lay=model.layer1[1].conv2 You can get the layer , Summary is not pure numbers , Directly by name , It's purely digital , Through the upper layer [index] To get .
2.2、 Mode two : Get... Directly by name
We already know that the name of the layer we want to operate on is "layer1.1.conv2", Then obtain the layer through the following methods :
name ="layer1.1.conv2"
for name, module in self.model.named_modules():
if name == self.module_name:
# be module This is the floor
版权声明
本文为[I am a symmetric matrix]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212327295533.html
边栏推荐
- QT custom control 01 simple timer
- solidworks按住CTRL,拖动复制实体
- Why don't MySQL use select * as query criteria? (continuously updated)
- PHP processing video ffmpeg (conversion)
- BUUCTF 刷题记录
- Solve the error reported by composer: could not find a version of package XXX / YYY
- Textview tilt properties
- [source code] super detailed interpretation of linkedblockingqueue source code
- warning: LF will be replaced by CRLF in composer. json.
- 87 r K-means, hierarchical clustering, implementation of EM clustering
猜你喜欢

There are Chinese characters in the input parameter, and an error of 500 is reported. There is an internal error in the server

C language topic 1: three digits that can be composed of 1,2,3,4

7.10 线程条件变量

Robot OS driven development

BUUCTF 刷题记录

7.3 创建线程

339-Leetcode 单词规律

JDBC方法参数详解 DriverManager,Statement,Connection,ResultSet,PreparedStatement

Man machine verification reCAPTCHA V3 complete instructions

Leetcode:443 Compressed string
随机推荐
2022/4/21
(Reprinted) MySQL ha
leetcode:271.字符串的编码与解码
入参有汉字,报错500,服务器内部错误
Pytorch (V) -- Notes
wireshark区别包的情况
339 leetcode word rules
SolidWorks hold down Ctrl and drag to copy entities
拼多多店铺怎么选择资源位,怎么报名活动,什么活动对店铺利益最大?
340-Leetcode 有效的字母异位词
PP semantic retrieval system
How to set the motion stroke and angle in SolidWorks
Golang force buckle leetcode 380 O (1) time insertion, deletion and acquisition of random elements
BUUCTF 你(竟)然赶我走
Pytorch中如何获取某层Module?(方便改变梯度,获取特征图,CAM等)
Solve the error reported by composer: could not find a version of package XXX / YYY
Amazing, 4 high-quality software full of surprises, feel more comfortable to use
Prompt, can you do it or not?
7.10 thread condition variables
Robot OS驱动开发