当前位置:网站首页>pytorch使用hook获得特征图
pytorch使用hook获得特征图
2022-07-28 05:17:00 【一知半解百晓生】
更细节的建议参考使用hook获得特征图可视化
定义如下两个hook函数,简单几步就能获得特征图了
fmap_block = dict() # 装feature map
grad_block = dict() # 装梯度
def backward_hook(module, grad_in, grad_out):
grad_block['grad_in'] = grad_in
grad_block['grad_out'] = grad_out
def farward_hook(module, inp, outp):
fmap_block['input'] = inp
fmap_block['output'] = outp
import torch
from torch import nn
#定义网路
class LeNet(nn.Module):
def __init__(self):
super(LeNet, self).__init__()
self.conv1 = nn.Conv2d(3,6,3,1,1)
self.relu1 = nn.ReLU()
self.pool1 = nn.MaxPool2d(2,2)
self.conv2 = nn.Conv2d(6,9,3,1,1)
self.relu2 = nn.ReLU()
self.pool2 = nn.MaxPool2d(2,2)
self.fc1 = nn.Linear(8*8*9, 120)
self.relu3 = nn.ReLU()
self.fc2 = nn.Linear(120,10)
def forward(self, x):
out = self.pool1(self.relu1(self.conv1(x)))
out = self.pool2(self.relu2(self.conv2(out)))
out = out.view(out.shape[0], -1)
out = self.relu3(self.fc1(out))
out = self.fc2(out)
return out
#实例化网络
net = LeNet()
# 注册hook
net.conv2.register_forward_hook(farward_hook)
定义输入
input_img = torch.randn(1,3,32,32).requires_grad_()
fmap_block = dict() # 装feature map
outs = net(input_img)
print(len(fmap_block))
print(fmap_block["output"].shape)
边栏推荐
- 论文模型主图范例
- 2021CSDN博客之星评选,互投
- regular expression
- MySQL practice 45 lectures
- 注册中心服务eureka 切换到 nocas遇到的问题
- Confused, I'm going to start running in the direction of [test]
- Thinking on multi system architecture design
- 凛冬已至,程序员该怎么取暖
- ByteBuffer. Position throws exception illegalargumentexception
- MySQL uses list as a parameter to query
猜你喜欢

After ruoyi generates the code corresponding to the database, what should I do to make the following image look like

Invalid bound statement (not found): com.exam.mapper.UserMapper.findbyid

restFul接口使用个人总结

How practical is the struct module? Learn a knowledge point immediately

How about ink cloud?

11.< tag-动态规划和子序列, 子数组>lt.115. 不同的子序列 + lt. 583. 两个字符串的删除操作 dbc

BigDecimal rounds and retains two decimal places

论文写作用词

The solution after the samesite by default cookies of Chrome browser 91 version are removed, and the solution that cross domain post requests in chrome cannot carry cookies

FusionGAN代码学习(一)
随机推荐
C language: addition and deletion of linked list in structure
JSON in JS (launch object deep copy)
Duoyu security browser will improve the security mode and make users browse more safely
项目中问题合集
Thinking on multi system architecture design
类和对象【中】
Interpreting the source code of cfrunloopref
Mysql数据库索引(innodb引擎)
JVM篇 笔记3:类加载与字节码技术
MySQL adds sequence number to query results
Antd setfieldsvalue warning problem cannot use 'setfieldsvalue' until you use 'getfielddecorator' or
Flask Development & get/post request
New modularity in ES6
Class class added in ES6
论文模型主图范例
After ruoyi generates the code corresponding to the database, what should I do to make the following image look like
分享几种管理C程序中标志位的方法
2021csdn blog star selection, mutual investment
BigDecimal rounds and retains two decimal places
[slam] lvi-sam analysis - Overview