【CNN记录】pytorch中BatchNorm2d

torch.nn.BatchNorm2d(num_features, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True, device=None, dtype=None)

功能 :对输入的四维数组进行批量标准化处理(归一化)

计算公式如下:

对于所有的batch中样本的同一个channel的数据元素进行标准化处理,即如果有C个通道,无论batch中有多少个样本,都会在通道维度上进行标准化处理,一共进行C次

num_features :通道数
eps :分母中添加的值,目的是计算的稳定性(分母不出现0),默认1e-5
momentum :用于运行过程中均值方差的估计参数,默认0.1

affine :设为true时,给定开易学习的系数矩阵r和b
track_running_stats:BN中存储的均值方差是否需要更新,true需要更新

举个例子

python 复制代码
>import torch
>import torch.nn as nn

>input = torch.arange(0, 12, dtype=torch.float32).view(1, 3, 2, 2)
>print(m)
tensor([[[[ 0.,  1.],
          [ 2.,  3.]],

         [[ 4.,  5.],
          [ 6.,  7.]],

         [[ 8.,  9.],
          [10., 11.]]]])
>m= nn.BatchNorm2d(3)
>print(m.weight)
tensor([1., 1., 1.], requires_grad=True)
>print(m.bias)
tensor([0., 0., 0.], requires_grad=True)

>output = m(input)
>print(output)
tensor([[[[-1.3416, -0.4472],
          [ 0.4472,  1.3416]],

         [[-1.3416, -0.4472],
          [ 0.4472,  1.3416]],

         [[-1.3416, -0.4472],
          [ 0.4472,  1.3416]]]], grad_fn=<NativeBatchNormBackward0>)

上面是使用nn接口计算,现在我们拿第一个数据计算一下验证

公式:

python 复制代码
#先计算第一个通道的均值、方差
>first_channel = input[0][0] #第一个通道
tensor([[0., 1.],
        [2., 3.]])

#1、计算均值方差
>mean = torch.Tensor.mean(first_channel)
tensor(1.5000)  #均值
>var=torch.Tensor.var(first_channel,False)
tensor(1.2500) #方差
#2、按照公式计算
>bn_value  =((input[0][0][0][0] -mean)/(torch.pow(var,0.5)+m.eps))*m.weight[0]+m.bias[0]
#这里就是(0-1.5)/sqrt(1.25+1e-5)*1.0 + 1.0

tensor(-1.3416, grad_fn=<AddBackward0>) 

第一个值都是-1.3416,对上了,其他都是一样。

再来个batch_size>1的情况

python 复制代码
#先把结果贴出来
tensor([[[[-1.2288, -1.0650],
          [-0.9012, -0.7373]],

         [[-1.2288, -1.0650],
          [-0.9012, -0.7373]],

         [[-1.2288, -1.0650],
          [-0.9012, -0.7373]]],


        [[[ 0.7373,  0.9012],
          [ 1.0650,  1.2288]],

         [[ 0.7373,  0.9012],
          [ 1.0650,  1.2288]],

         [[ 0.7373,  0.9012],
          [ 1.0650,  1.2288]]]], grad_fn=<NativeBatchNormBackward0>)

>input = torch.arange(0, 24, dtype=torch.float32).view(2, 3, 2, 2)
tensor([[[[ 0.,  1.],
          [ 2.,  3.]],

         [[ 4.,  5.],
          [ 6.,  7.]],

         [[ 8.,  9.],
          [10., 11.]]],


        [[[12., 13.],
          [14., 15.]],

         [[16., 17.],
          [18., 19.]],

         [[20., 21.],
          [22., 23.]]]])

>first_channel =input[:, 0, :, :]
tensor([[[ 0.,  1.],
         [ 2.,  3.]],

        [[12., 13.],
         [14., 15.]]])
>mean = torch.Tensor.mean(first_channel)
tensor(7.5000)
>var=torch.Tensor.var(first_channel,False)
tensor(37.2500)
#第1个batch中的第一个c
>print(((input[0][0][:][:] -mean)/(torch.pow(var,0.5)+m.eps))*m.weight[0]+m.bias[0])
tensor([[-1.2288, -1.0650],
        [-0.9012, -0.7373]], grad_fn=<AddBackward0>)
#第2个batch中的第一个c(共用c的weight、bias、mean、var)
>print(((input[1][0][:][:] -mean)/(torch.pow(var,0.5)+m.eps))*m.weight[0]+m.bias[0])
tensor([[0.7373, 0.9012],
        [1.0650, 1.2288]], grad_fn=<AddBackward0>)
相关推荐
林深现海1 小时前
【刘二大人】PyTorch深度学习实践笔记 —— 第一集:深度学习全景概述(超详细版)
pytorch·笔记·深度学习
阿狸OKay2 小时前
einops 库和 PyTorch 的 einsum 的语法
人工智能·pytorch·python
logic_57 小时前
关于VIT为啥可以用卷积代替第一层嵌入层
人工智能·神经网络·cnn
机器学习之心9 小时前
卷积神经网络(CNN) 与SE(Squeeze-and-Excitation)注意力机制锂电池剩余寿命预测,MATLAB代码
人工智能·matlab·cnn·锂电池剩余寿命预测
工程师老罗10 小时前
Pytorch模型GPU训练
人工智能·pytorch·深度学习
香芋Yu10 小时前
【深度学习教程——01_深度基石(Foundation)】03_计算图是什么?PyTorch动态图机制解密
人工智能·pytorch·深度学习
氵文大师10 小时前
PyTorch 性能分析实战:像手术刀一样精准控制 Nsys Timeline(附自定义颜色教程)
人工智能·pytorch·python
2501_9413220310 小时前
【医疗AI】基于Mask R-CNN的支气管镜内窥镜目标检测系统实现
人工智能·r语言·cnn
林深现海11 小时前
【刘二大人】PyTorch深度学习实践笔记 —— 第二集:线性模型(凝练版)
pytorch·笔记·深度学习
jllllyuz11 小时前
基于卷积神经网络(CNN)的图像融合方法详解
人工智能·神经网络·cnn