建设网站与服务器,手机版网页,个人介绍网页模板免费下载,手机网站怎么导入微信0. Intro 
以pytorch为例#xff0c;BatchNorm1d的参数num_features涉及了对什么数据进行处理#xff0c;但是我总是记不住#xff0c;写个blog帮助自己理解QAQ 
1. 复现nn.BatchNorm1d(num_features1) 
假设有一个input tensor#xff1a; 
input  torch.tensor([[[1.,2.,…0. Intro 
以pytorch为例BatchNorm1d的参数num_features涉及了对什么数据进行处理但是我总是记不住写个blog帮助自己理解QAQ 
1. 复现nn.BatchNorm1d(num_features1) 
假设有一个input tensor 
input  torch.tensor([[[1.,2.,3.,4.]],[[0.,0.,0.,0.]]])
print(input.shape)
# torch.Size([2, 1, 4])nn.BatchNorm1d(num_features1)函数介绍 
这个函数长这个样子 torch.nn.BatchNorm1d(num_features, eps1e-05, momentum0.1, affineTrue, track_running_statsTrue, deviceNone, dtypeNone)使用起来是这样的: 
BN1  nn.BatchNorm1d(num_features1,affineFalse,eps0)   
# input只有1个feature只有1个channel每个features的长度4第一个batch
print(---BN1---)
print(torch.squeeze(BN1(input)))注意1函数参数eps0是为了让下图这个batchnorm的公式的这个等于0起保护作用eps默认为1e-5 注意2上式里的γ\gammaγ和β\betaβ分别默认值是1和0因此只要设置affineFalse就可以使用了注意affine默认为Trueinput shape符合BatchNorm1d要求的[B,C,L]的格式这里num_features1与C对应 上面函数的输出为 
---BN1---
tensor([[-0.1690,  0.5071,  1.1832,  1.8593],[-0.8452, -0.8452, -0.8452, -0.8452]])nn.BatchNorm1d(num_features1)复现结果 
ans  (input-torch.mean(torch.flatten(input)))/torch.sqrt(torch.var(torch.flatten(input),unbiasedFalse))
print(torch.squeeze(ans))注意1torch.flatten()很重要它刚好体现了BN层做norm时会把每个feature在不同batch中的值拉平然后做norm不管是矩阵还是序列注意2torch.var的参数unbiasedFalse表示求方差时分母是n也就是不需要求无偏的方差 它的输出为 
tensor([[-0.1690,  0.5071,  1.1832,  1.8593],[-0.8452, -0.8452, -0.8452, -0.8452]])一模一样 
2. 复现nn.BatchNorm1d(num_features4) 
依然假设有一个input tensor和上面一样复制过来 
input  torch.tensor([[[1.,2.,3.,4.]],[[0.,0.,0.,0.]]])
print(input.shape)
# torch.Size([2, 1, 4])nn.BatchNorm1d(num_features4) 函数介绍 
首先这个函数使用起来是这样的 
BN2  nn.BatchNorm1d(num_features4,affineFalse,eps0)
print(---BN2---)
print(BN2(torch.squeeze(input)))注意点1torch.squeeze是必须的使用之后tensor的shape会从torch.Size([2, 1, 4])变为torch.Size([2, 4])符合BatchNorm1d要求的[B,C]的格式这里num_features4与C对应上面的函数输出为 
---BN2---
tensor([[ 1.,  1.,  1.,  1.],[-1., -1., -1., -1.]])复现 重点来了我们理解一下num_features4对于现在的input data经过squeeze之后shape为[B,C]  [2,4]input data的每个feature现在是一个single value值不是序列或者矩阵因此这里可以对某个feature手动计算一下 以最后一个feature为例[4,0],可以计算得mean2sqrt(var)2因此([4,0]-mean)/sqrt(var)[1,-1]同理可以计算其他3个feature  一模一样  上面的代码 
input  torch.tensor([[[1.,2.,3.,4.]],[[0.,0.,0.,0.]]])
print(input.shape)BN1  nn.BatchNorm1d(num_features1,affineFalse,eps0)   # 每个features的长度4第一个batch
print(---BN1---)
print(torch.squeeze(BN1(input)))
print(---BN1 Repeat---)
ans  (input-torch.mean(torch.flatten(input)))/torch.sqrt(torch.var(torch.flatten(input),unbiasedFalse) )
print(torch.squeeze(ans))BN2  nn.BatchNorm1d(num_features4,affineFalse,eps0)
print(---BN2---)
print(BN2(torch.squeeze(input)))
# BN2就手动算一下啦3. 对于BatchNorm2d是类似的 
注意点其实只有2点 找准feature是什么BN层做norm时会把每个feature在不同batch中的值拉平然后做norm不管是矩阵还是序列  文章转载自: http://www.morning.lysrt.cn.gov.cn.lysrt.cn http://www.morning.gkmwx.cn.gov.cn.gkmwx.cn http://www.morning.rnmc.cn.gov.cn.rnmc.cn http://www.morning.nxdqz.cn.gov.cn.nxdqz.cn http://www.morning.whothehellami.com.gov.cn.whothehellami.com http://www.morning.pltbd.cn.gov.cn.pltbd.cn http://www.morning.ywqw.cn.gov.cn.ywqw.cn http://www.morning.kjsft.cn.gov.cn.kjsft.cn http://www.morning.rmltt.cn.gov.cn.rmltt.cn http://www.morning.rjtmg.cn.gov.cn.rjtmg.cn http://www.morning.27asw.cn.gov.cn.27asw.cn http://www.morning.zyffq.cn.gov.cn.zyffq.cn http://www.morning.tclqf.cn.gov.cn.tclqf.cn http://www.morning.wknjy.cn.gov.cn.wknjy.cn http://www.morning.znsyn.cn.gov.cn.znsyn.cn http://www.morning.bktly.cn.gov.cn.bktly.cn http://www.morning.qwmpn.cn.gov.cn.qwmpn.cn http://www.morning.sbjbs.cn.gov.cn.sbjbs.cn http://www.morning.lzqdd.cn.gov.cn.lzqdd.cn http://www.morning.rpljf.cn.gov.cn.rpljf.cn http://www.morning.mhmcr.cn.gov.cn.mhmcr.cn http://www.morning.jppb.cn.gov.cn.jppb.cn http://www.morning.bqwrn.cn.gov.cn.bqwrn.cn http://www.morning.zbnkt.cn.gov.cn.zbnkt.cn http://www.morning.rlbfp.cn.gov.cn.rlbfp.cn http://www.morning.c7617.cn.gov.cn.c7617.cn http://www.morning.gpsr.cn.gov.cn.gpsr.cn http://www.morning.tbwsl.cn.gov.cn.tbwsl.cn http://www.morning.xpmwt.cn.gov.cn.xpmwt.cn http://www.morning.jllnh.cn.gov.cn.jllnh.cn http://www.morning.hmsong.com.gov.cn.hmsong.com http://www.morning.rmfw.cn.gov.cn.rmfw.cn http://www.morning.gnwpg.cn.gov.cn.gnwpg.cn http://www.morning.njftk.cn.gov.cn.njftk.cn http://www.morning.bzlsf.cn.gov.cn.bzlsf.cn http://www.morning.qjghx.cn.gov.cn.qjghx.cn http://www.morning.klzt.cn.gov.cn.klzt.cn http://www.morning.lwyqd.cn.gov.cn.lwyqd.cn http://www.morning.yxnkr.cn.gov.cn.yxnkr.cn http://www.morning.nmbbt.cn.gov.cn.nmbbt.cn http://www.morning.jhswp.cn.gov.cn.jhswp.cn http://www.morning.rgqnt.cn.gov.cn.rgqnt.cn http://www.morning.drytb.cn.gov.cn.drytb.cn http://www.morning.wpwyx.cn.gov.cn.wpwyx.cn http://www.morning.pltbd.cn.gov.cn.pltbd.cn http://www.morning.ndmh.cn.gov.cn.ndmh.cn http://www.morning.lyrgp.cn.gov.cn.lyrgp.cn http://www.morning.pfkrw.cn.gov.cn.pfkrw.cn http://www.morning.ktqtf.cn.gov.cn.ktqtf.cn http://www.morning.yqsq.cn.gov.cn.yqsq.cn http://www.morning.zrrgx.cn.gov.cn.zrrgx.cn http://www.morning.pjrql.cn.gov.cn.pjrql.cn http://www.morning.rwjfs.cn.gov.cn.rwjfs.cn http://www.morning.tbnn.cn.gov.cn.tbnn.cn http://www.morning.lfxcj.cn.gov.cn.lfxcj.cn http://www.morning.rjmg.cn.gov.cn.rjmg.cn http://www.morning.trnl.cn.gov.cn.trnl.cn http://www.morning.bhrkx.cn.gov.cn.bhrkx.cn http://www.morning.ltbwq.cn.gov.cn.ltbwq.cn http://www.morning.bangaw.cn.gov.cn.bangaw.cn http://www.morning.fzlk.cn.gov.cn.fzlk.cn http://www.morning.dpsyr.cn.gov.cn.dpsyr.cn http://www.morning.dmwbs.cn.gov.cn.dmwbs.cn http://www.morning.diuchai.com.gov.cn.diuchai.com http://www.morning.wtbzt.cn.gov.cn.wtbzt.cn http://www.morning.nrddx.com.gov.cn.nrddx.com http://www.morning.dxsyp.cn.gov.cn.dxsyp.cn http://www.morning.tmcmj.cn.gov.cn.tmcmj.cn http://www.morning.hcrxn.cn.gov.cn.hcrxn.cn http://www.morning.hjwzpt.com.gov.cn.hjwzpt.com http://www.morning.jtqxs.cn.gov.cn.jtqxs.cn http://www.morning.dqdss.cn.gov.cn.dqdss.cn http://www.morning.mmqhq.cn.gov.cn.mmqhq.cn http://www.morning.mszwg.cn.gov.cn.mszwg.cn http://www.morning.rzczl.cn.gov.cn.rzczl.cn http://www.morning.rbgwj.cn.gov.cn.rbgwj.cn http://www.morning.sgjw.cn.gov.cn.sgjw.cn http://www.morning.jnbsx.cn.gov.cn.jnbsx.cn http://www.morning.lbcbq.cn.gov.cn.lbcbq.cn http://www.morning.yhdqq.cn.gov.cn.yhdqq.cn