【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需要更新

举个例子

>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接口计算,现在我们拿第一个数据计算一下验证
公式:
在这里插入图片描述

#先计算第一个通道的均值、方差
>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的情况

#先把结果贴出来
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>)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值