[论文阅读]Attention Is All You Need

Transformer是一种基于注意力机制的深度学习模型,抛弃了传统的循环和卷积结构,实现了更高效的并行化训练。在WMT2014的英德、英法翻译任务中,Transformer超越了现有最佳模型,取得了SOTA成绩。模型由Encoder和Decoder堆叠而成,其中包含了多头注意力机制,该模型对后续的BERT等预训练模型发展产生了深远影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


论文名:Attention Is All You Need
论文作者:Ashish Vaswani 等
期刊/会议名:NIPS 2017
本文作者:XMU_MIAO

1) 摘要

  主流的序列转化模型是基于包含一个编码器和一个译码器的复杂循环或卷积神经网络。表现最好的模型也是通过注意力机制连接编码器和译码器。我们提出一种简单的网络架构, Transformer \textbf{Transformer} Transformer,其只基于注意力机制,完全去除了循环和卷积的结构。 在两个机器翻译任务上的实验表明该模型更加能够并行化且需要更少的时间训练的同时,能够在任务质量上有所提升。
  在WMT 2014 English-German \textbf{English-German} English-German任务上取得了 28.4 28.4 28.4 BLEU \textbf{BLEU} BLEU值,超过了现存的最好结果(包括集成) 2 2 2 BLEU \textbf{BLEU} BLEU值 ,在WMT2014 English-French \textbf{English-French} English-French任务上,我们的模型在8块GPU上训练了3.5天(这只是文献中最好模型训练成本的一小部分),得到了单个模型的最先进的 BLEU \textbf{BLEU} BLEU值41.8。我们将 Transformer \textbf{Transformer} Transformer成功地应用于具有大量和有限训练数据的英语选区解析任务上,证明它可以很好地推广到其他任务。

2) 文章贡献

  • 提出了一种全新的只基于 Attention \textbf{Attention} Attention机制的网络结构 Transformer \textbf{Transformer} Transformer
  • Transformer \textbf{Transformer} Transformer在WMT2014的多个任务上达到了SOTA水平并能够推广到多个任务上
  • 该结构造就后来的 BERT \textbf{BERT} BERT等预训练模型

3) 模型架构

  大部分神经序列转换模型都有一个 Encoder-Decoder \textbf{Encoder-Decoder} Encoder-Decoder结构, Encoder \textbf{Encoder} Encoder将词表示序列 ( x 1 , . . . , x n ) (x_1,...,x_n) (x1,...,xn)编码成一个连续的表示序列 z = ( z 1 , . . . , z n ) \textbf{z}=(z_1,...,z_n) z=(z1,...,zn)。给定一个序列 z \textbf{z} z Decoder \textbf{Decoder} Decoder一次一个元素地生成输出序列 ( y 1 , . . . , y m ) (y_1,...,y_m) (y1,...,ym),输出序列中每个元素的生成都是自回归的( a u t o − r e g r e s s i v e auto{-}regressive autoregressive),在生成下一个元素时,使用先前生成的元素作为附加输入。
   Transformer \textbf{Transformer} Transformer遵循了 Encoder-Decoder \textbf{Encoder{-}Decoder} Encoder-Decoder的整体架构,在 Encoder \textbf{Encoder} Encoder端和 Decoder \textbf{Decoder} Decoder端都采用了堆叠式的 self-attention \textbf{self{-}attention} self-attention point-wise \textbf{point{-}wise} point-wise的全连接层,整体架构如下:
在这里插入图片描述
  接下来对每一个部分进行详细的说明和介绍,首先对 Encoder \textbf{Encoder} Encoder Decoder \textbf{Decoder} Decoder进行整体的描述,接下来着重介绍 Scaled    Dot-Product    Attention \textbf{Scaled\,\,Dot{-}Product\,\,Attention} ScaledDot-ProductAttention Mutil-head    Attention \textbf{Mutil{-}head\,\,Attention} Mutil-headAttention以及 Attention \textbf{Attention} Attention机制在该模型的应用,最后介绍该结构中的其他部件和模型的输入和输出。

1   Encoder    and    Decoder    Stacks 1\,\textbf{Encoder\,\,and\,\,Decoder\,\,Stacks} 1EncoderandDecoderStacks

1.2   Encoder 1.2\,\textbf{Encoder} 1.2Encoder

在这里插入图片描述
   Encoder \textbf{Encoder} Encoder N = 6 N=6 N=6个相同层堆叠而成的 Encoder   Stack \textbf{Encoder\,Stack} EncoderStack(如下图所示)。每一层包含两个子层 ( s u b l a y e r ) (sublayer) (sublayer),自下而上分别是 Mutil-Head   Attention \textbf{Mutil-Head Attention} Mutil-Head Attention层和一个简单的 Position-wise \textbf{Position-wise} Position-wise的全连接前馈网络。

  在每个子层中都使用了残差连接 ( r e s i d u a l    c o n n e c t i o n ) (residual\,\,connection) (residualconnection),残差连接之后进行层标准化 ( l a y e r    n o r m a l i z a t i o n ) (layer\,\,normalization) (layernormalization)【子层的残差连接和层标准化即图中的 A d d & N o r m Add\&Norm Add&Norm】,每个子层的输出可以形式化表示为 L a y e r N o r m ( x + S u b l a y e r ( x ) ) LayerNorm(x+Sublayer(x)) LayerNorm(x+Sublayer(x))
其中 S u b l a y e r ( ∗ ) Sublayer(*) Sublayer()表示子层自身实现的函数, x x x表示子层的输入, L a y e r N o r m ( ∗ ) LayerNorm(*) LayerNorm()表示层规范化函数。
  为了便于子层之间的残差连接 ( r e s i d u a l    c o n n e c t i o n ) (residual\,\,connection) (residualconnection) Transformer \textbf{Transformer} Transformer模型中所有的子层 ( s u b l a y e r ) (sublayer) (sublayer)包括嵌入层 ( e m b e d d i n g    l a y e r ) (embedding\,\,layer) (embeddinglayer)输出的维度都为 d m o d e l = 512 d_{model}=512 dmodel=512

1.3   Decoder 1.3\,\textbf{Decoder} 1.3Decoder

在这里插入图片描述

   Decoder \textbf{Decoder} Decoder同样是由于 N = 6 N=6 N=6个相同层堆叠而成的 Encoder   Stack \textbf{Encoder\,Stack} EncoderStack,但除了 Encoder \textbf{Encoder} Encoder中的两个子层外【注: Decoder \textbf{Decoder} Decoder中的 Multi-Head   Attention \textbf{Multi-Head\,Attention} Multi-HeadAttention使用了 Masked \textbf{Masked} Masked的技巧】, Decoder \textbf{Decoder} Decoder中增加了第三个子层(如上图红框中所示),该子层对 Encoder   Stack \textbf{Encoder\,Stack} EncoderStack的输出执行 Multi-Head   Attention \textbf{Multi-Head\,Attention} Multi-HeadAttention操作。 Decoder \textbf{Decoder} Decoder中的残差连接 ( r e s i d u a l    c o n n e c t i o n ) (residual\,\,connection) (residualconnection)和层标准化 ( l a y e r    n o r m a l i z a t i o n ) (layer\,\,normalization) (layernormalization) Encoder中 \textbf{Encoder中} Encoder相同。
   Masked \textbf{Masked} Masked技巧:通过 Masked \textbf{Masked} Masked操作防止 Decoder \textbf{Decoder} Decoder中的输出元素看到其之后的元素。这个掩码 ( m a s k i n g ) (masking) (masking)与上输出偏移一个位置,确保了对位置 i i i元素的预测只能依赖于小于 i i i的已知输出。

2   Attention 2\,\textbf{Attention} 2Attention

   Transformer \textbf{Transformer} Transformer注意力函数可以描述为将查询 ( q u e r y ) (query) (query)和一组键-值对 ( k e y , v a l u e ) (key,value) (key,value)映射到输出,其中查询、键、值和输出都是向量。输出以值的加权和的形式计算,其中分配给每个值的权重由查询与相应键的兼容函数计算。以下介绍在 Transformer \textbf{Transformer} Transformer Attention \textbf{Attention} Attention结构。

2.1   Scaled    Dot-Product    Attention 2.1\,\textbf{Scaled\,\,Dot{-}Product\,\,Attention} 2.1ScaledDot-ProductAttention

在这里插入图片描述
  Scaled    Dot-Product    Attention \,\textbf{Scaled\,\,Dot{-}Product\,\,Attention} ScaledDot-ProductAttention   Mutil-head    Attention \,\textbf{Mutil{-}head\,\,Attention} Mutil-head

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值