搭建Transformer模型

1. 前言

本文使用Attention Layer与Self-Attention Layer搭建深度神经网络——Transformer模型。
本人全部文章请参见:博客文章导航目录
本文归属于:自然语言处理系列
本系列实践代码请参见:我的GitHub
前文:Attention is all you need:剥离RNN,保留Attention
后文:BERT与ERNIE

2. 多头注意力机制(Multi-Head Attention)

2.1 多头自注意力层(Multi-Head Self-Attention Layer)

根据前文可知,Self-Attention层输入为 X = [ x 1 , x 2 , ⋯   , x m ] X=[x_1,x_2,\cdots,x_m] X=[x1,x2,,xm],输出为与输入序列长度相同的序列 C = [ c 1 , c 2 , ⋯   , c m ] C=[c_1,c_2,\cdots,c_m] C=[c1,c2,,cm],即 C = A t t n ( X , X ) C=Attn(X,X) C=Attn(X,X)。前文所述Self-Attention层也可以称为单头Self-Attention层(Single-Head Self-Attention Layer)。
多头Self-Attention层由 l l l个单头Self-Attention组成,每个单头Self-Attention相互独立,不共享参数。每个单头Self-Attention存在3个参数矩阵 W Q , W K , W V W_Q,W_K,W_V WQ,WK,WV,由 l l l个单头Self-Attention组成的多头Self-Attention层共存在3 l l l个参数矩阵 W Q 1 , W Q 2 , ⋯   , W Q l , W K 1 , W K 2 , ⋯   , W K l , W V 1 , W V 2 , ⋯   , W V l W_{Q1},W_{Q2},\cdots,W_{Ql},W_{K1},W_{K2},\cdots,W_{Kl},W_{V1},W_{V2},\cdots,W_{Vl} WQ1,WQ2,,WQl,WK1,WK2,,WKl,WV1,WV2,,WVl
图一
根据上图可知,在多头Self-Attention层中,所有单头Self-Attention层输入均为 X X X,每个单头Self-Attention均会输出一个长度为 m m m,每个元素维度为 d d d的输出序列,多头Self-Attention层输出是由所有单头Self-Attention的输出序列拼接而形成的长度为 m m m,每个元素维度为 l d ld ld的序列。
多头Self-Attention层可用下图表示,与前文所述Self-Attention层相比,多头Self-Attention层输出序列每个元素维度以及参数矩阵数量均是单头Self-Attention层的 l l l倍。
图二

可以将单头Self-Attention比作卷积神经网络卷积层中的一个卷积核。在卷积神经网络中,卷积层一般会使用多个卷积核,从而获取上一层图像中不同的特征。在Transformer模型中,Attention层会使用多头Self-Attention层,从而获取上一层序列中不同的注意力特征。

2.2 多头注意力层(Multi-Head Attention Layer)

与多头Self-Attention层相似,可以使用 l l l个单头Attention构成多头Attention层。所有单头Attention输入均为 X X X X ′ X^\prime X,同时各个单头Attention相互独立,不共享参数。将 l l l个单头Attention的输出 C 1 , C 2 , ⋯   , C l C_1,C_2,\cdots,C_l C1,C2,,Cl拼接,即构成多头Attention层的输出。
图三

3. 搭建Transformer模型

Transformer是一个Seq2Seq模型,有一个Encoder和一个Decoder。Transformer模型是目前机器翻译等NLP问题最好的解决办法,效果比RNN有大幅提高。

3.1 Transformer’s Encoder

Transformer模型的Encoder由多个Encoder Block构成,每个Encoder Block由多头Self-Attention层与全连接层构成。
据2.1可知,多头Self-Attention层输入为 X = [ x 1 , x 2 , ⋯   , x m ] X=[x_1,x_2,\cdots,x_m] X=[x1,x2,,xm],输出为 C = [ c 1 , c 2 , ⋯   , c m ] C=[c_1,c_2,\cdots,c_m] C=[c1,c2,,cm],输出序列 C C C的每个元素维度均为 l d ld ld。将多头Self-Attention层输出序列 C C C的各个元素 c i ,   i = 1 ∼ m c_i,~i=1\sim m ci, i=1m分别输入全连接层,得到Encoder Block输出序列 U = [ u 1 , u 2 , ⋯   , u m ] U=[u_1,u_2,\cdots,u_m] U=[u1,u2,,um] u i = R e L U ( W U c i ) ,   i = 1 ∼ m u_i=ReLU(W_Uc_i),~i=1\sim m ui=ReLU(WUci), i=1m
由多头Self-Attention层与全连接层构成的如图四所示的结构即为Encoder Block。其中将 c i c_i ci变换为 u i u_i ui m m m个Dense模块完全相同,即图四中所有Dense模块参数矩阵均为 W U W_U WU
图四

Encoder Block输入序列 X X X和输出序列 U U U元素个数相同。根据前文可知, c i c_i ci依赖于所有 m m m个输入 x 1 , x 2 , ⋯   , x m x_1,x_2,\cdots,x_m x1,x2,,xm,因此 u i u_i ui同样依赖于所有m个输入 x 1 , x 2 , ⋯   , x m x_1,x_2,\cdots,x_m x1,x2,,xm。改变输入序列中任何一个元素,Encoder Block所有输出序列 U U U中所以元素均会发生改变。

在Transformer’s Encoder中,一个Block包含一个多头Self-Attention层和一个全连接层,Encoder Block按照图四所示方式将序列 X X X映射成序列 U U U。序列 X X X U U U长度均为 m m m,序列中各个元素 x i x_i xi u i u_i ui维度均为512。即在Transformer模型中,Encoder Block输入和输出均是大小为 512 × m 512\times m 512×m的矩阵。
图五
Transformer’s Encoder结构如下图所示,共由6个Encoder Blocks依次堆叠而成,各个Block之间相互独立,不共享参数。Transformer’s Encoder的输入和输出均是大小为 512 × m 512\times m 512×m的矩阵。
图六

Transformer’s Encoder各个Block输入和输出均是大小为 512 × m 512\times m 512×m的矩阵,搭建Transformer’s Encoder可以使用ResNet中的跳层链接(Skip Connection)技巧,以及常见的Batch Normalization等技巧。

3.2 Transformer’s Decoder

Transformer模型的Decoder由多个Decoder Block构成,每个Decoder Block由多头Self-Attention层、多头Attention层和全连接层构成。
如下图所示,Decoder Block第一层为多头Self-Attention层,其输入为 [ x 1 ′ , x 2 ′ , ⋯   , x t ′ ] [x_1^\prime,x_2^\prime,\cdots,x_t^\prime] [x1,x2,,xt],输出为 [ c 1 , c 2 , ⋯   , c t ] [c_1,c_2,\cdots,c_t] [c1,c2,,ct]。第二层是多头Attention层,其输入为Encoder输出 [ u 1 , u 2 , ⋯   , u m ] [u_1,u_2,\cdots,u_m] [u1,u2,,um]和第一层输出 [ c 1 , c 2 , ⋯   , c t ] [c_1,c_2,\cdots,c_t] [c1,c2,,ct],第二层的输出是 [ z 1 , z 2 , ⋯   , z t ] [z_1,z_2,\cdots,z_t] [z1,z2,,zt]。将第二层输出序列 Z Z Z的各个元素 z i ,   i = 1 ∼ t z_i,~i=1\sim t zi, i=1t分别输入全连接层,得到Decoder Block输出序列 S = [ s 1 , s 2 , ⋯   , s t ] S=[s_1,s_2,\cdots,s_t] S=[s1,s2,,st] s i = R e L U ( W S z i ) ,   i = 1 ∼ t s_i=ReLU(W_Sz_i),~i=1\sim t si=ReLU(WSzi), i=1t
与Encoder Block类似,Decoder Block中将 z i z_i zi变换为 s i s_i si t t t个Dense模块完全相同,即图七中所有Dense模块参数矩阵均为 W S W_S WS
图七
在Transformer’s Decoder中,一个Block包含一个多头Self-Attention层、一个Attention层和一个全连接层,Decoder Block按照图七所示方式,输入序列 X X X X ′ X^\prime X,输出序列 S S S。序列 X X X长度为 m m m,输出序列 S S S长度与 X ′ X^\prime X一致,均为 t t t。所有三个序列中各个元素维度均为512。
图八
Transformer’s Decoder共由6个Decoder Block依次堆叠而成,每层Decoder Block的输入序列 X X X均为Encoder的输出, X ′ X^\prime X为上一层Decoder Block的输出。

3.3 Transformer模型结构

Transformer’s Encoder由依次叠加的6个Encoder Block构成,每个Encoder Block有两层,分别是多头Self-Attention层和全连接层。Encoder的输入是大小为 512 × m 512\times m 512×m矩阵 X X X,输出矩阵 U U U也是大小为 512 × m 512\times m 512×m的矩阵,与输入矩阵 X X X大小完全相同。
将Transformer’s Encoder和Transformer’s Decoder连接起来,即构成Transformer模型。
图九

本文讲解Transformer’s Decoder结构时直接将 X ′ X^\prime X整体作为输入。基于Transformer的Seq2Seq模型生成输出序列时,过程如下:

  1. 输入 x t ′ x_t^\prime xt,用三个参数矩阵 W Q j 1 W_{Q_{j1}} WQj1 W K j 1 W_{K_{j1}} WKj1 W V j 1 W_{V_{j1}} WVj1分别对 x t ′ x_t^\prime xt做线性变换,得到 q t q_t qt k t k_t kt v t v_t vt
    j j j是指Multi-Head Self-Attention中第 j j j个Self-Attention,1表示参数矩阵位于Decoder Block第一层(多头Self-Attention层)
  2. 计算向量 k i k_i ki q t q_t qt的内积,得到 α ~ t i \tilde{\alpha}_{ti} α~ti
    α ~ t i = k i T q t ,   f o r   i = 1   t o   t \tilde{\alpha}_{ti}=k_i^Tq_t,~for~i=1~to~t α~ti=kiTqt, for i=1 to t
  3. α ~ t 1 , α ~ t 2 , ⋯   , α ~ t t \tilde{\alpha}_{t1},\tilde{\alpha}_{t2},\cdots,\tilde{\alpha}_{tt} α~t1,α~t2,,α~tt进行 S o f t m a x Softmax Softmax变换,把输出记作 α t 1 , α t 2 , ⋯   , α t t \alpha_{t1},\alpha_{t2},\cdots,\alpha_{tt} αt1,αt2,,αtt
    [ α t 1 , α t 2 , ⋯   , α t t ] = S o f t m a x ( [ α ~ t 1 , α ~ t 2 , ⋯   , α ~ t t ] ) [\alpha_{t1},\alpha_{t2},\cdots,\alpha_{tt}]=Softmax([\tilde{\alpha}_{t1},\tilde{\alpha}_{t2},\cdots,\tilde{\alpha}_{tt}]) [αt1,αt2,,αtt]=Softmax([α~t1,α~t2,,α~tt])
  4. 对所有 v i v_i vi求加权平均,得到Context Vector c t j c_{tj} ctj
    c t j = α t 1 v 1 + α t 2 v 2 + ⋯ + α t t v t c_{tj}=\alpha_{t1}v_1+\alpha_{t2}v_2+\cdots+\alpha_{tt}v_t ctj=αt1v1+αt2v2++αttvt
  5. 对多头Self-Attention层所有Self-Attention执行步骤1-4,将 c t 1 , c t 2 , ⋯   , c t l c_{t1},c_{t2},\cdots,c_{tl} ct1,ct2,,ctl拼接形成 c t c_t ct
  6. 分别用两个参数矩阵 W K j 2 W_{K_{j2}} WKj2 W V j 2 W_{V_{j2}} WVj2对Encoder输出序列每个元素均做线性变换,得到 k i k_i ki v i v_i vi
    k i = W K j 2 ⋅ u i ,   f o r   i = 1   t o   m k_i=W_{K_{j2}}\cdot u_i,~for~i=1~to~m ki=WKj2ui, for i=1 to m
    v i = W V j 2 ⋅ u i ,   f o r   i = 1   t o   m v_i=W_{V_{j2}}\cdot u_i,~for~i=1~to~m vi=WVj2ui, for i=1 to m
    j j j是指Multi-Head Attention中第 j j j个Attention,2表示参数矩阵位于Decoder Block第二层(多头Attention层)
  7. 用参数矩阵 W Q j 2 W_{Q_{j2}} WQj2对步骤5中得到的 c t c_t ct做线性变换,得到 q t q_t qt
  8. 计算向量 k i k_i ki q t q_t qt的内积,得到 α ~ t i \tilde{\alpha}_{ti} α~ti
    α ~ t i = k i T q t ,   f o r   i = 1   t o   m \tilde{\alpha}_{ti}=k_i^Tq_t,~for~i=1~to~m α~ti=kiTqt, for i=1 to m
  9. α ~ t 1 , α ~ t 2 , ⋯   , α ~ t m \tilde{\alpha}_{t1},\tilde{\alpha}_{t2},\cdots,\tilde{\alpha}_{tm} α~t1,α~t2,,α~tm进行 S o f t m a x Softmax Softmax变换,把输出记作 α t 1 , α t 2 , ⋯   , α t m \alpha_{t1},\alpha_{t2},\cdots,\alpha_{tm} αt1,αt2,,αtm
    [ α t 1 , α t 2 , ⋯   , α t m ] = S o f t m a x ( [ α ~ t 1 , α ~ t 2 , ⋯   , α ~ t m ] ) [\alpha_{t1},\alpha_{t2},\cdots,\alpha_{tm}]=Softmax([\tilde{\alpha}_{t1},\tilde{\alpha}_{t2},\cdots,\tilde{\alpha}_{tm}]) [αt1,αt2,,αtm]=Softmax([α~t1,α~t2,,α~tm])
  10. 对所有 v i v_i vi求加权平均,输出 z t j z_{tj} ztj
    z t j = α t 1 v 1 + α t 2 v 2 + ⋯ + α t m v m z_{tj}=\alpha_{t1}v_1+\alpha_{t2}v_2+\cdots+\alpha_{tm}v_m ztj=αt1v1+αt2v2++αtmvm
  11. 对多头Attention层所有Attention执行步骤6-10,将 z t 1 , z t 2 , ⋯   , z t l z_{t1},z_{t2},\cdots,z_{tl} zt1,zt2,,ztl拼接形成 z t z_t zt
  12. z t z_t zt输入Dense模块,得到 s t s_t st
  13. s t s_t st输入 S o f t m a x Softmax Softmax分类器,根据结果确定 x t + 1 ′ x_{t+1}^\prime xt+1

4. 参考资料链接

  1. https://www.youtube.com/watch?v=aJRsr39F4dI&list=PLvOO0btloRntpSWSxFbwPIjIum3Ub4GSC&index=2
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RuizhiHe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值