【论文笔记】没有归一化层的transformer:Transformers without Normalization


前言

归⼀化层在现代神经⽹络中⽆处不在,⻓期以来⼀直被视为必不可少的组件。本研究证明,通过⼀种极其简单的技术,⽆需归⼀化的Transformer模型也能达到相同甚⾄更优的性能。我们引⼊了动态双曲正切(DyT)这⼀逐元素操作,即DyT(x)=tanh(αx),作为Transformer中归⼀化层的直接替代品。DyT的灵感来源于这样⼀个观察:Transformer中的层归⼀化常常产⽣类似双曲正切的S形输⼊输出映射。通过引⼊DyT,⽆需归⼀化的Transformer模型能够与归⼀化模型相媲美甚⾄超越其性能,且⼤多⽆需进⾏超参数调整。我们在从识别到⽣成、从有监督到⾃监督学习、从计算机视觉到语⾔模型等不同场景下验证了DyT在Transformer中的有效性。这些发现挑战了归⼀化层在现代神经⽹络中不可或缺的传统认知,并为深⼊理解其在深度⽹络中的作⽤提供了新的视⻆。


论文地址:https://arxiv.org/pdf/2503.10622
代码:https://github.com/jiachenzhu/DyT


一、简介

本⽂通过在Transformer中引⼊⼀种简单的替代归⼀化层的⽅法来挑战这⼀观点。我们的研究始于这样⼀个观察:层归⼀化(LN)层将其输⼊映射到具有类似双曲正切(tanh)的S形曲线的输出,对输⼊激活进⾏缩放,同时压缩极端值。受此启发,我们提出了⼀种称为动态双曲正切(DyT)的逐元素操作,定义为:DyT(x)=tanh(αx),其中α是⼀个可学习的参数。该操作旨在通过学习适当的缩放因⼦α来模拟LN的⾏为,并通过有界的双曲正切函数压缩极端值。值得注意的是,与归⼀化层不同,它⽆需计算激活统计信息即可实现这两种效果。

二、动态双曲正切(DyT)

1.代码

该模块的代码实现

class DyT(nn.Module):
   def __init__(self,normalized_shape,channels_last,alpha_init_value=0.5):
        super().__init__()
        self.normalized_shape = normalized_shape
        self.alpha_init_value = alpha_init_value
        self.channels_last = channels_last

        self.alpha = nn.Parameter(torch.ones(1) * alpha_init_value)
        self.weight = nn.Parameter(torch.ones(normalized_shape))
        self.bias = nn.Parameter(torch.zeros(normalized_shape))


    def forward(self, x):
        x = torch.tanh(self.alpha * x)
        if self.channels_last:
            x = x * self.weight + self.bias
        else:
            x = x * self.weight[:, None, None] + self.bias[:, None, None]
        return x

2,方法

对于输⼊张量x,DyT层定义如下:
在这里插入图片描述
其中,α是⼀个可学习的标量参数,它可以根据输⼊的范围对其进⾏不同的缩放,以适应不同的x范围。这也是我们将整个操作命名为“动态”Tanh(Dynamic Tanh)的原因。γ和β是可学习的、按通道的向量参数,与所有归⼀化层中使⽤的参数相同——它们允许输出缩放回任何范围。有时这被视为⼀个单独的仿射层;但就我们的⽬的⽽⾔,我们认为它们是DyT层的⼀部分,就像归⼀化层也包含它们⼀样。


三,实验

视觉中的监督学习。我们在ImageNet-1K分类任务(Deng等⼈,2009年)上训练了“基础”和“⼤型”尺⼨的VisionTransformer(ViT)(Dosovitskiy等⼈,2020年)和ConvNeXt(Liu等⼈,2022年)。选择这些模型是因为它们的流⾏度以及各⾃独特的操作:ViT中的注意⼒机制和ConvNeXt中的卷积。表1报告了top-1分类准确率。DyT在两种架构和两种模型尺⼨上的表现均略优于LN。我们进⼀步绘制了ViT-B和ConvNeXt-B的训练损失曲线,⻅图5。这些曲线表明,DyT和基于LN的模型的收敛⾏为⾼度⼀致。
在这里插入图片描述

在这里插入图片描述


四,分析

1,Dyt的效率

通过测量100次前向传播(推理)和100次前向-反向传播(训练)所花费的总时间,对LLaMA7B模型使⽤RMSNorm或DyT进⾏基准测试,测试中使⽤单个4096个标记的序列。表7报告了在NvidiaH100GPU 上以BF16精度运⾏时,所有RMSNorm或DyT层以及整个模型所需的时间。与RMSNorm层相⽐,DyT层显著减少了计算时间,在FP32精度下也观察到了类似的趋势。DyT可能是⾯向效率的⽹络设计的⼀个有前景的选择。
在这里插入图片描述
DyT在推理和训练时间上都实现了⼤幅缩短。

五,总结与局限性

1,结论

在这项⼯作中,我们证明了现代神经⽹络,尤其是Transformer模型,可以在没有归⼀化层的情况下进⾏训练。这是通过动态双曲正切(DyT)实现的,这是⼀种传统归⼀化层的简单替代⽅案。它通过⼀个可学习的缩放因⼦α调整输⼊激活范围,然后通过S形的双曲正切函数压缩极端值。尽管它是⼀个更简单的函数,但能有效地捕捉归⼀化层的⾏为。在各种设置下,采⽤DyT的模型与使⽤归⼀化层的模型相⽐,性能相当甚⾄更优。这些发现挑战了⼈们对于训练现代神经⽹络时归⼀化层必要性的传统认识。我们的研究还有助于理解归⼀化层的机制,归⼀化层是深度神经⽹络中最基本的组成部分之⼀。

2,局限性

在⽹络中使⽤LN或RMSNorm进⾏实验,这是因为它们在Transformer和其他现代架构中颇受欢迎。初步实验(⻅附录C)表明,DyT在像ResNets这样的经典⽹络中难以直接替代BN。DyT是否以及如何适应具有其他类型归⼀化层的模型,还有待更深⼊的研究。
在这里插入图片描述


### Layer Normalization in Transformer Models Explained In the context of deep learning, normalization techniques play a crucial role in stabilizing and accelerating training processes. For Transformer models specifically, layer normalization (LayerNorm) has been an integral component contributing to their effectiveness. #### Definition and Purpose Layer normalization operates across features within each individual example rather than over mini-batches as batch normalization does. This approach ensures that during inference or when using very small batches, performance remains stable because it relies on per-example statistics instead of batch-level ones[^1]. The primary function of LayerNorm is to reduce internal covariate shift by normalizing activations at every hidden layer. By doing so, this technique facilitates smoother gradients flow through layers which can lead to faster convergence rates during optimization procedures. #### Implementation Details For implementing layer normalization inside a Transformer architecture like SimpleTransformer mentioned earlier: At each position \(i\) along sequence length dimension, the mean \(\mu_i\) and variance \(\sigma^2_i\) are computed based only upon values from current token's embedding vector. Then apply standard score transformation followed by learnable affine parameters gamma (\(\gamma\)) & beta (\(\beta\)): \[y = \frac{x-\mu}{\sqrt{\sigma^{2}+\epsilon}}*\gamma + \beta\] Here’s how one might implement such functionality in Python with PyTorch library: ```python import torch.nn as nn class LayerNormalization(nn.Module): "Construct a layernorm module." def __init__(self, features, eps=1e-6): super(LayerNormalization, self).__init__() self.a_2 = nn.Parameter(torch.ones(features)) self.b_2 = nn.Parameter(torch.zeros(features)) self.eps = eps def forward(self, x): mean = x.mean(-1, keepdim=True) std = x.std(-1, keepdim=True) return self.a_2 * (x - mean) / (std + self.eps) + self.b_2 ``` This code snippet defines a custom `LayerNormalization` class inheriting from PyTorch's Module base class. It initializes two trainable parameters (`a_2`, `b_2`) corresponding respectively to scaling factor γ and shifting term β used after applying zero-mean unit-variance normalization operation element-wise over input tensor 'x'. #### Benefits Within Transformers Within Transformer architectures, incorporating layer normalization before residual connections helps mitigate issues related to vanishing/exploding gradient problems often encountered while stacking multiple attention sub-layers sequentially together without proper regularization mechanisms applied throughout network topology design phase[^2]. --related questions-- 1. How does Batch Normalization differ fundamentally from Layer Normalization? 2. Can you explain why Layer Normalization contributes positively towards reducing Internal Covariate Shift? 3. What specific challenges do Deep Neural Networks face due to Vanishing/Exploding Gradients problem? 4. In what ways have advancements beyond basic Layer Norm impacted modern NLP tasks leveraging large-scale pre-trained language models?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值