LayerNorm 相比 BatchNorm 有以下两个优点:

  1. LN 针对单个训练样本进行,不依赖于其他数据,因此可以避免 BN 中受 mini-batch 数据分布影响的问题,可以用于 小mini-batch场景、动态网络场景和 RNN,特别是自然语言处理领域。
  2. LN 不需要保存 mini-batch 的均值和方差,节省了额外的存储空间。

更具体介绍参考模型优化之Layer Normalization

y=x−E[x]Var⁡[x]+ϵ∗γ+βy=\frac{x-\mathrm{E}[x]}{\sqrt{\operatorname{Var}[x]+\epsilon}} * \gamma+\beta y=Var[x]+ϵ​x−E[x]​∗γ+β

公式看上去和BN一致,但是这里统计的样本和方差是在同一个样本的不同属性上

torch.nn.LayerNorm

  • normalized_shape – 指定需要进行LayerNorm的维度,可以是int,list和 torch.Size
  • eps – 上述公式中的ϵ,防止分母为0,默认为1e-5
  • elementwise_affine – 是否使用可训练参数的β和γ ,默认为True
import torch
import torch.nn as nn
import numpy as np
import mathdef validation(x):"""验证函数:param x::return:"""x = np.array(x)avg = np.mean(x, axis=1)#维度 3*1std2 = np.var(x, axis=1)#维度3*1x_avg = [[item for item in avg] for _ in range(x.shape[1])]x_std = [[math.pow(item, 1 / 2) for item in std2] for _ in range(x.shape[1])]x_ = (x - np.array(x_avg).T) / np.array(x_std).Treturn x_x = [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]]
# 维度:3*4
input = torch.tensor(x, dtype=torch.float)
m = nn.LayerNorm(4)
output = m(input)
print(output)val = validation(x)
print(val)

结果:

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=<NativeLayerNormBackward>)[[-1.34164079 -0.4472136   0.4472136   1.34164079][-1.34164079 -0.4472136   0.4472136   1.34164079][-1.34164079 -0.4472136   0.4472136   1.34164079]]

normalized_shape 输入为torch.size处理更高的维度

x = [[[1, 1], [2, 2], [3, 3], [4, 4]], [[2, 2], [3, 3], [4, 4], [5, 5]], [[3, 3], [4, 4], [5, 5], [6, 6]]]
input = torch.tensor(x, dtype=torch.float)
normalized_shape = input.size()[1:]
print(normalized_shape)
m = nn.LayerNorm(normalized_shape)
output = m(input)
print(output)

输出:

torch.Size([4, 2])
tensor([[[-1.3416, -1.3416],[-0.4472, -0.4472],[ 0.4472,  0.4472],[ 1.3416,  1.3416]],[[-1.3416, -1.3416],[-0.4472, -0.4472],[ 0.4472,  0.4472],[ 1.3416,  1.3416]],[[-1.3416, -1.3416],[-0.4472, -0.4472],[ 0.4472,  0.4472],[ 1.3416,  1.3416]]], grad_fn=<NativeLayerNormBackward>)

相当于两个3*4的矩阵单独处理。

pytorch之LayerNorm相关推荐

  1. 深度学习笔记016:BatchNorm批量归一化+nn.LayerNorm暂记

    实现 pytorch 实现 Layernorm的官方文档 import torch import torch.nn as nn# NLP Example batch, sentence_length, ...

  2. BatchNorm和LayerNorm

    一.BatchNorm 论文:Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariat ...

  3. CV【5】:Layer normalization

    系列文章目录 Normalization 系列方法(一):CV[4]:Batch normalization Normalization 系列方法(二):CV[5]:Layer normalizati ...

  4. Pytorch归一化方法讲解与实战:BatchNormalization、LayerNormalization、nn.BatchNorm1d和LayerNorm()和F.normalize()

    文章目录 LayerNormalization BatchNormalization F.normalize 这些Normalization的作用都是让数据保持一个比较稳定的分布,从而加速收敛.Bat ...

  5. pytorch中的BatchNorm和LayerNorm

    参考文章 https://blog.csdn.net/weixin_39228381/article/details/107896863 https://blog.csdn.net/weixin_39 ...

  6. pytorch 层标准化 LayerNorm 的用法

    目录 1.为什么要标准化(理解的直接跳过到这部分) 2.LayerNorm 解释 3.举例-只对最后 1 个维度进行标准化 4.举例-对最后 D 个维度进行标准化 1.为什么要标准化(理解的直接跳过到 ...

  7. PyTorch学习之归一化层(BatchNorm、LayerNorm、InstanceNorm、GroupNorm)

    BN,LN,IN,GN从学术化上解释差异: BatchNorm:batch方向做归一化,算NHW的均值,对小batchsize效果不好:BN主要缺点是对batchsize的大小比较敏感,由于每次计算均 ...

  8. Transformer的PyTorch实现

    Google 2017年的论文 Attention is all you need 阐释了什么叫做大道至简!该论文提出了Transformer模型,完全基于Attention mechanism,抛弃 ...

  9. [Pytorch]基于混和精度的模型加速

    这篇博客是在pytorch中基于apex使用混合精度加速的一个偏工程的描述,原理层面的解释并不是这篇博客的目的,不过在参考部分提供了非常有价值的资料,可以进一步研究. 一个关键原则:"仅仅在 ...

最新文章

  1. 2021 年度热门技术书单提前公开,这些好书藏不住了
  2. 详解基于朴素贝叶斯的情感分析及 Python 实现
  3. 无线 在linux叫什么地方,请问有知道atheros无线网卡Linux驱动官方下载地址是什么吗?...
  4. [材料力学]弯扭组合梁实验报告
  5. Python的闭包的后期绑定问题
  6. iOS interface适配
  7. LeetCode--41.缺失的第一个正数(C)
  8. Spring中父子容器的实现实例
  9. 【完美】SpringBoot中使用注解来实现 Redis 分布式锁
  10. tomcat处理html流程,Tomcat 简单配置使用,基本工作原理(流程图)
  11. atos命令符化号crash文件
  12. httprunner接口测试脚本自动生成
  13. ijkplayer-音视频变速播放实现
  14. 是时候考虑怎么用好云了
  15. 汇编语言王爽第四版 第六章内容解读(附实验五答案解析)
  16. 同居mm_倾向于同居
  17. if函数多个条件php,excelif函数的多个条件使用方法
  18. vue3的组件传递之子传父(项目实操)
  19. pandas数据日期函数之date_range()、resample()与to_period()
  20. java ieee_Java IEEE754 工具类

热门文章

  1. 等级链与跳板原则_管理学原理重点
  2. 1000部电影艺术的刺杀和横行-1柴系媛
  3. Windows 远程桌面安全配置 mstsc 连接后要求输入账号密码
  4. Parallels 16已可在支持Windows的M1 Mac上运行
  5. 我在阿里巴巴上联系了近20个口罩供应商,全是假的!
  6. 姿态解算知识点3——空鼠原理和实现
  7. 尚硅谷 周阳老师 SpringCloud 学习笔记
  8. Win7/Win10安装AutoCAD+CASS
  9. 【站长SEO助手】浏览器插件 - seo工具
  10. TVU云导播全新亮相BIRTV 2018