【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>)
相关推荐
退休钓鱼选手15 小时前
[ Pytorch教程 ] 神经网络的基本骨架 torch.nn -Neural Network
pytorch·深度学习·神经网络
爱吃泡芙的小白白16 小时前
深入解析CNN中的Dropout层:从基础原理到最新变体实战
人工智能·神经网络·cnn·dropout·防止过拟合
DeniuHe16 小时前
用 PyTorch 库创建了一个随机张量,并演示了多种张量取整和分解操作
pytorch
偷吃的耗子17 小时前
【CNN算法理解】:卷积神经网络 (CNN) 数值计算与传播机制
人工智能·算法·cnn
爱吃泡芙的小白白19 小时前
CNN的FLOPs:从理论计算到实战避坑指南
人工智能·神经网络·cnn·flops
Network_Engineer20 小时前
从零手写LSTM:从门控原理到PyTorch源码级实现
人工智能·pytorch·lstm
JicasdC123asd21 小时前
使用Faster R-CNN模型训练汽车品牌与型号检测数据集 改进C4结构 优化汽车识别系统 多类别检测 VOC格式
r语言·cnn·汽车
机器学习之心21 小时前
基于CNN-GRU(卷积神经网络-门控循环单元)的多变量负荷预测模型MATLAB代码
matlab·cnn·gru
爱吃泡芙的小白白1 天前
深入浅出:卷积神经网络(CNN)池化层全解析——从MaxPool到前沿发展
人工智能·神经网络·cnn·池化层·最大值池化·平均值池化
偷吃的耗子1 天前
【CNN算法理解】:基于训练好的MNIST CNN模型进行预测
人工智能·算法·cnn