BN体系理解——类封装复现

python 复制代码
from pathlib import Path
from typing import Optional

import torch
import torch.nn as nn
from torch import Tensor


class BN(nn.Module):
    def __init__(self,num_features,momentum=0.1,eps=1e-8):##num_features是通道数
        """
        初始化方法
        :param num_features:特征属性的数量,也就是通道数目C
        """
        super(BN, self).__init__()
        ##register_buffer:将属性当成parameter进行处理,唯一的区别就是不参与反向传播的梯度求解
        self.register_buffer('running_mean', torch.zeros(1, num_features, 1, 1))
        self.register_buffer('running_var', torch.zeros(1, num_features, 1, 1))
        self.running_mean: Optional[Tensor]
        self.running_var: Optional[Tensor]
        self.running_mean=torch.zeros([1,num_features,1,1])
        self.running_var=torch.zeros([1,num_features,1,1])
        self.gamma=nn.Parameter(torch.ones([1,num_features,1,1]))
        self.beta=nn.Parameter(torch.zeros(1,num_features,1,1))
        self.eps=eps
        self.momentum=momentum


    def forward(self,x):
        """
        前向过程
        output=(x-μ)/α*γ+β
        :param x: [N,C,H,W]
        :return: [N,C,H,W]
        """
        if self.training:
            #训练阶段--》使用当前批次的数据
            _mean=torch.mean(x,dim=(0,2,3),keepdim=True)
            _var = torch.var(x, dim=(0, 2, 3), keepdim=True)
            #将训练过程中的均值和方差保存下来--方便推理的时候使用--》滑动平均
            self.running_mean=self.momentum*self.running_mean+(1.0-self.momentum)*_mean
            self.running_var=self.momentum*self.running_var+(1.0-self.momentum)*_var
        else:
            #推理阶段-->使用的是训练过程中的累积数据
            _mean=self.running_mean
            _var=self.running_var
        z=(x-_mean)/torch.sqrt(_var+self.eps)*self.gamma+self.beta
        return z

if __name__ == '__main__':
    torch.manual_seed(28)
    path_dir=Path("./output/models")
    path_dir.mkdir(parents=True,exist_ok=True)
    device=torch.device("cuda" if torch.cuda.is_available() else "cpu")
    bn=BN(num_features=12)
    bn.to(device)#只针对子模块和参数进行转换



    #模拟训练过程
    bn.train()
    xs=[torch.randn(8,12,32,32).to(device) for _ in range(10)]
    for _x in xs:
        bn(_x)

    print(bn.running_mean.view(-1))
    print(bn.running_var.view(-1))

    #模拟推理过程
    bn.eval()
    _r=bn(xs[0])
    print(_r.shape)

    bn=bn.cpu()#保存都是以cpu保存,恢复再自己转回GPU上
    #模拟模型保存
    torch.save(bn,str(path_dir/'bn_model.pkl'))
    #state_dict:获取当前模块的所有参数(Parameter+register_buffer)
    torch.save(bn.state_dict(),str(path_dir/"bn_params.pkl"))

    #pt结构的保存
    traced_script_module=torch.jit.trace(bn.eval(),xs[0].cpu())
    traced_script_module.save("./output/bn_model.pt")


    #模拟模型恢复
    bn_model=torch.load(str(path_dir/"bn_model.pkl"),map_location='cpu')
    bn_params=torch.load(str(path_dir/"bn_params.pkl"),map_location='cpu')
    print(len(bn_params))
相关推荐
维克兜率天几秒前
【维克】布林带:通道类指标的“量化基石“
人工智能·python·算法
CoderIsArt6 分钟前
逐层拆解:从 (B,1,28,28) 到 (B,10) 的完整数据流
人工智能·深度学习·机器学习
向科→向总工→空间动态计系统35 分钟前
机器人大脑系统
开发语言·人工智能·python·机器人
染指111036 分钟前
194.DeepAgents-在DeepAgents中使用自定义沙盒环境OpenSandBox
网络·人工智能·python·langchain·deepagents
m0_646429972 小时前
LangGraph 学习笔记(一):StateGraph 与状态管理
python·langgraph
Dmz丶2 小时前
前端转 AI 100 天 Day 57:RAG 项目实战(六)——上线后迭代,从“能用”到“越用越好”
python
小小-猿2 小时前
用 Python 写昇腾算子:pyasc 是怎么回事
开发语言·python·昇腾·cann·pyasc
蓝蓝的代码人3 小时前
用 Python、Qt 和 Vue 做一个 Windows 股票看盘工具,观潮的行情小窗与桌面提醒
vue.js·python·qt·开源·桌面应用
老唐7773 小时前
具身智能视觉感知详解:从图像识别到机器人环境理解,核心技术、算法架构与应用实践
人工智能·深度学习·算法·机器人·大模型·具身智能
富贵儿9983 小时前
NL2SQL 实践手记 10:从命令行到网页查询——把整个系列串起来
数据库·python·sql·ai·自然语言处理·langchain