百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术分类 > 正文

批归一化原理及Pytorch实现【Batch Normalization】

ztj100 2025-07-24 23:22 6 浏览 0 评论

推荐:用 NSDT设计器 快速搭建可编程3D场景。

批归一化是完整 CNN 架构中的另一个重要元素,其作用如下:

  • 将不同范围的值压缩成近似N~(0, 1),即平均值为0,标准差为1
  • 对于 sigmoid 激活,如果值太大,梯度接近 0,存在梯度消失的风险

批量归一化的优点如下:

  • 使收敛速度更快
  • 梯度下降更稳定
  • 可以使用更大的学习率
  • 可能获得更好的损失函数最小值

图像或特征块批归一化的详细信息

  • 找到每个通道的平均值 μ 和标准 σ
  • 例如。 一批(N 个图像)形状为 [N, 3, 28, 28] 的 RGB 图像,在 3 个通道上完成批归一化。
  • 因此批处理层将具有shape=[3]的μ、σ。
  • 还有另外两个可学习参数:γ 和 β,用于微调平均值和标准差(γ 进行缩放,β 进行平移 N~(0, 1))
  • 这意味着最终的输出不会完全落入 N~(0, 1) 而是 N~(β, γ)

1、nn.BatchNorm1d()

用于扁平化网络。

例如。 CNN 末尾的全连接 nn,其中 x.shape=[100, 16, 784]

import torch
import torch.nn as nn

# nn.BatchNorm1d

x = torch.rand(100, 16, 784)  # here imgs are flattened from 28x28

layer = nn.BatchNorm1d(16)  # batch norm is done on channels
out = layer(x)

print(layer.running_mean)
print(layer.running_var)

2、nn.BatchNorm2d()

  • 用于具有 4 维特征的卷积层。
  • 例如。 x.shape = [1, 16, 7, 7]
  • 批归一化层中的权重为 γ,可在反向传播过程中学习
  • 批归一化层中的偏差为 β,可在反向传播过程中学习
  • 由于 γ 和 β 是可学习的,因此从 vars(layer) 结果中你可以看到 require_grad=True
  • 使用layer.train()和layer.eval()改变模式来判断是否需要计算梯度、μ和σ
# nn.BatchNorm2d
x = torch.rand(1, 16, 7, 7)  # here image or features are not flattened 
print(x.shape)

layer = nn.BatchNorm2d(16)  # batch norm is done on channels

out = layer(x)

print(layer.weight)  # weight here is gamma, was learned to adjust the batch norm to N(beta, gamma) from N(0, 1)
print(layer.bias)  # bias here is beta, was learned to adjust the batch norm to N(beta, gamma)

print(vars(layer))

print(layer.eval())
print(layer.train())

原文链接:
http://www.bimant.com/blog/pytorch-batchnorm/

相关推荐

10条军规:电商API从数据泄露到高可用的全链路防护

电商API接口避坑指南:数据安全、版本兼容与成本控制的10个教训在电商行业数字化转型中,API接口已成为连接平台、商家、用户与第三方服务的核心枢纽。然而,从数据泄露到版本冲突,从成本超支到系统崩溃,A...

Python 文件处理在实际项目中的困难与应对策略

在Python项目开发,文件处理是一项基础且关键的任务。然而,在实际项目中,Python文件处理往往会面临各种各样的困难和挑战,从文件格式兼容性、编码问题,到性能瓶颈、并发访问冲突等。本文将深入...

The Future of Manufacturing with Custom CNC Parts

ThefutureofmanufacturingisincreasinglybeingshapedbytheintegrationofcustomCNC(ComputerNumericalContro...

Innovative Solutions in Custom CNC Machining

Inrecentyears,thelandscapeofcustomCNCmachininghasevolvedrapidly,drivenbyincreasingdemandsforprecisio...

C#.NET serilog 详解(c# repository)

简介Serilog是...

Custom CNC Machining for Small Batch Production

Inmodernmanufacturing,producingsmallbatchesofcustomizedpartshasbecomeanincreasinglycommondemandacros...

Custom CNC Machining for Customized Solutions

Thedemandforcustomizedsolutionsinmanufacturinghasgrownsignificantly,drivenbydiverseindustryneedsandt...

Revolutionizing Manufacturing with Custom CNC Parts

Understandinghowmanufacturingisevolving,especiallythroughtheuseofcustomCNCparts,canseemcomplex.Thisa...

Breaking Boundaries with Custom CNC Parts

BreakingboundarieswithcustomCNCpartsinvolvesexploringhowadvancedmanufacturingtechniquesaretransformi...

Custom CNC Parts for Aerospace Industry

Intherealmofaerospacemanufacturing,precisionandreliabilityareparamount.Thecomponentsthatmakeupaircra...

Cnc machining for custom parts and components

UnderstandingCNCmachiningforcustompartsandcomponentsinvolvesexploringitsprocesses,advantages,andcomm...

洞察宇宙(十八):深入理解C语言内存管理

分享乐趣,传播快乐,增长见识,留下美好。亲爱的您,这里是LearingYard学苑!今天小编为大家带来“深入理解C语言内存管理”...

The Art of Crafting Custom CNC Parts

UnderstandingtheprocessofcreatingcustomCNCpartscanoftenbeconfusingforbeginnersandevensomeexperienced...

Tailored Custom CNC Solutions for Automotive

Intheautomotiveindustry,precisionandefficiencyarecrucialforproducinghigh-qualityvehiclecomponents.Ta...

关于WEB服务器(.NET)一些经验累积(一)

以前做过技术支持,把一些遇到的问题累积保存起来,现在发出了。1.问题:未能加载文件或程序集“System.EnterpriseServices.Wrapper.dll”或它的某一个依赖项。拒绝访问。解...

取消回复欢迎 发表评论: