【笔记】BN(Batch Normalization)、正则化:BN是在同一批次中逐个的计算同一channel的均值,方差,来进行归一化;正则化作用是 损失精度去调整样本的不足产生的拟合

本文探讨了批量归一化(BN)在深度学习模型中的作用,指出其可以作为正则化手段,部分替代Dropout。BN层在每个批次的数据上进行操作,调整输入分布,从而稳定训练过程。示例展示了BN层在张量上的应用,并通过实例解释了BN如何影响输出。同时,文章强调了带着批判眼光看待技术的重要性。
摘要由CSDN通过智能技术生成

还是希望大家带着批判的眼光看问题,有的人说L0不是范数。

BN:其能一定程度的起到正则化作用,几乎代替Dropout

 

注意可训练参数\gamma \ \beta 的维度 等于 批次内 每一个张量的 channels。  如果是R、G、B图像,维度就是3。也就是每一个channel都需要一个\gamma \ \beta,所以它俩各是1 X 3的列向量。

summary
OrderedDict([('Conv2d-1',
              OrderedDict([('input_shape', [-1, 3, 418, 418]),
                           ('output_shape', [-1, 64, 209, 209]),
                           ('trainable', True),
                           ('nb_params', tensor(9408))])),
             ('BatchNorm2d-2',
              OrderedDict([('input_shape', [-1, 64, 209, 209]),
                           ('output_shape', [-1, 64, 209, 209]),
                           ('trainable', True)]))])
len(summary)
2

module.weight.requires_grad
True

module.weight
Parameter containing:
tensor([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
        1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
        1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
        1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], device='cuda:0',
       requires_grad=True)

module.weight.size()
torch.Size([64])

module.bias.size()
torch.Size([64])



module.bias 
Parameter containing:
tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       device='cuda:0', requires_grad=True)

module.bias.size()
torch.Size([64])

eg1:

eg2:

import torch.nn as nn
import torch

a = torch.tensor([[
                      [[1., 2]],
                      [[2, 1]],
                      [[2, 2]],
                  ],
                  [
                      [[2., 2]],
                      [[3, 1]],
                      [[1, 2]],
                  ]

                  ])
print(a, type(a), a.dtype)
print(a.shape)
b = nn.BatchNorm2d(3)
d = b(a)
print(d)








tensor([[[[1., 2.]],

         [[2., 1.]],

         [[2., 2.]]],


        [[[2., 2.]],

         [[3., 1.]],

         [[1., 2.]]]]) <class 'torch.Tensor'> torch.float32
torch.Size([2, 3, 1, 2])
tensor([[[[-1.7320,  0.5773]],

         [[ 0.3015, -0.9045]],

         [[ 0.5773,  0.5773]]],


        [[[ 0.5773,  0.5773]],

         [[ 1.5075, -0.9045]],

         [[-1.7320,  0.5773]]]], grad_fn=<NativeBatchNormBackward>)


正则化: 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿的探索之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值