首页 > 编程语言 >python hook的使用

python hook的使用

时间:2022-10-29 08:33:42浏览次数:48  
标签:nn python self torch hook 使用 input model

import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as F

class Model(nn.Module):
    def __init__(self):
        super().__init__()
        self.cov1 = nn.Conv2d(1,3,3)
        self.cov2 = nn.Conv2d(3,2,3)
        
    def backword(self , x):
        print("model backword fisrt",x.shape)
        x = F.relu(self.cov1(x))
        x = F.relu(self.cov2(x))
        print("model backword end",x.shape)
        return x
    
def before_hook(model,input):
    print("brefore hook",model," input ",input[0].shape)
    return torch.zeros(1, 1, 7, 7)

model = Model()
hook = model.register_forward_pre_hook(before_hook)
input = torch.zeros(1,1,5,5)
model(input)
hook.remove()

 

标签:nn,python,self,torch,hook,使用,input,model
From: https://www.cnblogs.com/xiaoruirui/p/16838011.html

相关文章

  • ElasticSearch 基础使用
    ElasticSearch使用说明1.索引index,相当于数据库表Table1.1查看所有索引GET_cat/indices?v1.2创建索引字段映射关系PUT/test{"settings":{"number_of......
  • python 中统计文件的行数
     001、[root@pc1test]#lsa.txttest.py[root@pc1test]#cata.txt12345[root@pc1test]#cattest.py#!/usr/bin/pythonin_file=open("a.txt","r")l......
  • 三种常用的Vue表单修饰符使用场景...
    vue的项目开发中须知道的三个vue表单修饰符(.lazy,.number,.trim)1,.lazy简单地说:我们在input输入框输入的时候,标签里的内容会实时变化,加了.lazy修饰符后,只有当鼠标光标离......
  • 多进程与多线程 python
    进程之间的数据传递全局变量在多个进程中不共享,进程之间的数据是独立的,默认情况下互不影响用Queue实现多进程之间的数据传递Queue是多进程安全的队列,可以使用Queue......
  • angular入门篇8----Http模块的使用(2):使用接口
    angular入门篇8----Http模块的使用(2):使用接口1.创建Http服务1.1注册HttpClientModule模块我们需要在model.module.ts中注册HttpClientModule模块:\Store\src\app\mod......
  • 记录第一次使用c++和汇编联合编译
    32位从Ida把目标函数扣出来toUapperprocneararg_0=dwordptr4pushesimovesi,[......
  • [python] Python制作自动填写脚本,100%准确率
    本次案例代码实现思路:打开考试网站selenium-->浏览器驱动-->操作浏览器<模拟人的行为做操作浏览器>获取答案获取答案网站链接获取问题以及答案内容对比题目以......
  • 通过SSH远程使用jupyter notebook
    1.背景一直苦恼于本地机器和服务器上都要配置一些机器学习方面的环境,今天花了点时间研究了下Jupternotebook远程访问服务器,所以记录一下。有些步骤非必须,这里尽量写清楚......
  • python第一次课
    Python学习第一次学习引用第一次写博客没什么好分享的,把我学习python的心得分享出来;一是可以让新入IT行业的同学有一个好的榜样,二是不会写博客的程序员不是一个好的程......
  • python列表套字典去重
    最近在写工具,拿到数据,发现有重复的,想到用set去重,结果报错了,哈哈,重新学习下#例子:data_list=[{'id':1,'name':'user01'},{'id':2,'name':'user02'},{'id':1,'name':'......