pytorch钩子函数

一、定义

  1. 定义
  2. 案例1-分析某一层结果
  3. 案例2-挂载到模型上,分析每一层参数
  4. 案例3-挂载到fc层反向传播上,增加噪音
  5. 模块可视化

二、实现

  1. 定义
    钩子函数是指在程序执行过程中预留的一些接口,可以在特定的时间点插入自定义的代码,从而改变程序的行为。在PyTorch中,钩子函数可以在模型训练过程中捕获中间结果,对其进行分析或者可视化,也可以在模型的参数更新过程中进行一些自定义操作。
    在这里插入图片描述
    参考:https://zhuanlan.zhihu.com/p/628354472

  2. 案例1-分析某一层结果

import torch
import torch.nn as nn
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = torch.relu(self.conv1(x))
        x = torch.max_pool2d(x, (2, 2))
        x = torch.relu(self.conv2(x))
        x = torch.max_pool2d(x, 2)
        x = x.view(-1, self.num_flat_features(x))
        x = torch.relu(self.fc1(x))
        x = torch.relu(self.fc2(x))
        x = self.fc3(x)
        return x

    def num_flat_features(self, x):
        size = x.size()[1:]  
        num_features = 1
        for s in size:
            num_features *= s
        return num_features

def hook_fn(module, input, output):
    for name, param in module.named_parameters():
        if param.requires_grad:
            print(f"Parameter {name}: {param.data}")
    print('input:', input)
    print('output:', output)

net = Net()
net.conv1.register_forward_hook(hook_fn)   #执行完conv1 方法即可执行该钩子

input = torch.randn(1, 3, 32, 32)
output = net(input)
  1. 案例2-挂载到模型上,分析每一层参数
import torch
# 定义一个钩子函数,用于记录模型参数
def record_param(module, input, output):
    for name, param in module.named_parameters():
        if param.requires_grad:
            print(f"Parameter {name}: {param.data}")
# 定义一个简单的模型
class Net(torch.nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.fc1 = torch.nn.Linear(10, 5)
        self.fc2 = torch.nn.Linear(5, 2)

    def forward(self, x):
        x = self.fc1(x)
        x = torch.nn.functional.relu(x)
        x = self.fc2(x)
        return x

# 实例化模型和数据
net = Net()
data = torch.randn(1, 10)
# 注册钩子函数
handle = net.register_forward_hook(record_param)
# 前向传播
output = net(data)
# 移除钩子函数
handle.remove()
  1. 案例3-挂载到fc层反向传播上,增加噪音
import torch

# 定义一个钩子函数,用于修改梯度
def add_noise_grad(grad):
    noise = torch.randn_like(grad) * 0.01
    return grad + noise

# 定义一个简单的模型
class Net(torch.nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.fc1 = torch.nn.Linear(10, 5)
        self.fc2 = torch.nn.Linear(5, 2)

    def forward(self, x):
        x = self.fc1(x)
        x = torch.nn.functional.relu(x)
        x = self.fc2(x)
        return x

# 实例化模型和数据
net = Net()
data = torch.randn(1, 10)
label = torch.tensor([0, 1])

# 注册钩子函数
handle = net.fc2.register_backward_hook(add_noise_grad)

# 前向传播和反向传播
output = net(data)
loss = torch.nn.functional.cross_entropy(output, label)
loss.backward()

# 移除钩子函数
handle.remove()
  1. 模块可视化
import torch
from torch import nn
from torchviz import make_dot

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.fc1 = nn.Linear(10, 5)
        self.fc2 = nn.Linear(5, 1)

    def forward(self, x):
        x = self.fc1(x)
        x = torch.relu(x)
        x = self.fc2(x)
        return x

net = Net()

def hook_fn(m, i, o):
    print(m)
    print("------------Input Grad------------")
    print(i)
    print("------------Output Grad-----------")
    print(o)

handle = net.fc1.register_forward_hook(hook_fn)

x = torch.randn(1, 10)
y = net(x)
z=make_dot(y, params=dict(net.named_parameters()))
z.view()

在这里插入图片描述

相关推荐

  1. Pytorch中的钩子函数Hook函数

    2024-07-22 11:32:02       35 阅读
  2. pytorch 钩子函数hook 详解及实战

    2024-07-22 11:32:02       51 阅读
  3. SpringBoot钩子函数

    2024-07-22 11:32:02       29 阅读
  4. vue 钩子函数

    2024-07-22 11:32:02       28 阅读
  5. 钩子函数和副作用

    2024-07-22 11:32:02       33 阅读
  6. vue钩子函数、生命周期

    2024-07-22 11:32:02       26 阅读
  7. Pytest中的钩子函数

    2024-07-22 11:32:02       18 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-22 11:32:02       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-22 11:32:02       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-22 11:32:02       45 阅读
  4. Python语言-面向对象

    2024-07-22 11:32:02       55 阅读

热门阅读

  1. 一线大厂前端vue面试题

    2024-07-22 11:32:02       14 阅读
  2. 【MAUI】动态界面

    2024-07-22 11:32:02       22 阅读
  3. 小白C语言基础详解:C 语言的内存管理

    2024-07-22 11:32:02       16 阅读
  4. 等保测评深度探索:揭秘安全合规的幕后英雄

    2024-07-22 11:32:02       19 阅读
  5. Linux命令更新-文本处理grep

    2024-07-22 11:32:02       20 阅读
  6. 事务

    事务

    2024-07-22 11:32:02      20 阅读
  7. 为什么vue3项目中推荐使用const,而不是let语法

    2024-07-22 11:32:02       21 阅读
  8. NLP专业术语及工具【hanlp、jiolp】

    2024-07-22 11:32:02       18 阅读