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

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,

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

#先把结果贴出来 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, -
#第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], [

GitHub

Gitee