大语言模型-Transformer-Attention Is All You Need

一、背景信息:

Transformer是一种由谷歌在2017年提出的深度学习模型。

主要用于自然语言处理(NLP)任务,特别是序列到序列(Sequence-to-Sequence)的学习问题,如机器翻译、文本生成等。Transformer彻底改变了之前基于循环神经网络(RNNs)和长短期记忆网络(LSTMs)的序列建模范式,并且在性能上取得了显著提升。

二、整体结构:

Transformer 由 Encoder 和 Decoder 两个部分组成,Encoder 和 Decoder 都包含 6 个 block。

Transformer 的输入
Transformer 的输入由 x的 词向量位置向量 相加得到。
其中Transformer 在位置向量中保存单词在序列中的相对或绝对位置信息,位置向量由PE(Positional Encoding)表示:

eg:假设n为序列长度,d为表示向量维度,原始输入为 X o r i − i n p u t X_{ori-input} Xoriinput [ x 1 , x 2 . . . x n ] [x_{1},x_{2}...x_{n} ] [x1,x2...xn]
则,原始输入 X o r i − i n p u t X_{ori-input} Xoriinput的词向量矩阵为 X W E X_{WE} XWE其维度为(n, d),
原始输入 X o r i − i n p u t X_{ori-input} Xoriinput的位置向量矩阵 X P E X_{PE} XPE维度也为(n, d),
最终 Transformer 的输入矩阵 X i n p u t X_{input} Xinput = X W E X_{WE} XWE + X P E X_{PE} XPE维度也是(n, d)。

三、 Encoder

Encoder 部分由6个Encoder block 组成。
Encoder block 由Multi-Head Attention结合Add & Norm、Feed Forward结合 Add & Norm 组成。
即由下面两部分组成:
X = L a y d e r N o r m ( X i n p u t + M u l t i H e a d A t t e n t i o n ( X i n p u t ) ) X = LayderNorm(X_{input} + MultiHeadAttention(X_{input})) X=LayderNorm(Xinput+MultiHeadAttention(Xinput))
X = L a y d e r N o r m ( X + F e e d F o r w o r d ( X ) ) X = LayderNorm(X + FeedForword(X)) X=LayderNorm(X+FeedForword(X))

MultiHeadAttention部分
其中MultiHeadAttention为多个Self-Attention进行Concat后linear而成:
Q = X i n p u t × W q Q = X_{input} \times W_{q} Q=Xinput×Wq
K = X i n p u t × W k K = X_{input} \times W_{k} K=Xinput×Wk
V = X i n p u t × W v V = X_{input} \times W_{v} V=Xinput×Wv
Z = A t t e n t i o n ( Q , K , V ) = s o f t m a x ( Q K T d k ) V Z = Attention(Q, K, V) = softmax( \frac{QK^{T} }{\sqrt{d_{k}} } )V Z=Attention(Q,K,V)=softmax(dk QKT)V
其中, Z 1 . . . . Z 8 Z_{1}....Z_{8} Z1....Z8为X_{input} 经过8个不同Self-Attention得到的结果
X = M u l t i H e a d A t t e n t i o n ( X i n p u t ) = L i n e a r ( C o n c a t ( Z 1 , Z 2 . . . . Z 8 ) ) X =MultiHeadAttention(X_{input} ) = Linear(Concat(Z_{1},Z_{2}....Z_{8})) X=MultiHeadAttention(Xinput)=Linear(Concat(Z1,Z2....Z8))

FeedForword部分
Feed Forward 层,是一个两层的全连接层,第一层的激活函数为 Relu,第二层不使用激活函数,公式如下。

F e e d F o r w o r d ( X ) = m a x ( 0 , X W 1 + b 1 ) W 2 + b 2 FeedForword(X) = max(0, XW_{1} + b_{1})W_{2} + b_{2} FeedForword(X)=max(0,XW1+b1)W2+b2

四、 Decoder

Decoder 由 6个Decoder block 以及最后的一个linear组成。
Decoder block 由 一个带有 Masked的Multi-Head Attention结合Add & Norm和一个Multi-Head Attention结合Add & Norm以及一个Feed Forward结合 Add & Norm 组成。

X o u t p u t = X o u p u t − o r i ⊗ X M a s k X_{output}=X_{ouput-ori }\otimes X_{Mask} Xoutput=XouputoriXMask
X = L a y d e r N o r m ( X o u t p u t + M a s k M u l t i H e a d A t t e n t i o n ( X o u p u t ) ) X = LayderNorm(X_{output} + MaskMultiHeadAttention(X_{ouput})) X=LayderNorm(Xoutput+MaskMultiHeadAttention(Xouput))

X = L a y d e r N o r m ( X + M u l t i H e a d A t t e n t i o n ( [ X a s Q , E C a s K , E C a s V ] ) X = LayderNorm(X + MultiHeadAttention([X_{as Q}, EC_{as K}, EC_{as V}]) X=LayderNorm(X+MultiHeadAttention([XasQ,ECasK,ECasV])
X r e s u l t = S o f t m a x ( X ) X_{result} = Softmax(X) Xresult=Softmax(X)

带有 Masked的Multi-Head Attention层
其中带有 Masked的Multi-Head Attention中 X o u p u t X_{ouput} Xouput为Transformer 标签对应输出向量; X o u p u t − o r i X_{ouput-ori} Xouputori需要先 ⊗ \otimes X M a s k X_{Mask} XMask得到 X o u p u t X_{ouput} Xouput
Q = X o u p u t × W q Q = X_{ouput} \times W_{q} Q=Xouput×Wq
K = X o u p u t × W k K = X_{ouput} \times W_{k} K=Xouput×Wk
V = X o u p u t × W v V = X_{ouput} \times W_{v} V=Xouput×Wv
Z = A t t e n t i o n ( Q , K , V ) = s o f t m a x ( Q K T d k ⊗ X M a s k ) V Z = Attention(Q, K, V) = softmax( \frac{QK^{T} }{\sqrt{d_{k}} } \otimes X_{Mask} )V Z=Attention(Q,K,V)=softmax(dk QKTXMask)V

其中第二个 Multi-Head Attention层
Self-Attention 的 K, V矩阵使用的是根据Encoder编码的输出矩阵C计算得到 K, V; Self-Attention 的 Q矩阵是根据Decoder block中的Masked Multi-Head Attention层输出矩阵 Z 计算得到 Q。

Reference

1.Attention Is All You Need
2.Transformer模型详解(图解最完整版)
3.Self-Attention & Transformer完全指南:像Transformer的创作者一样思考

相关推荐

  1. 语言模型系列-Transformer

    2024-07-21 23:08:01       19 阅读
  2. 语言模型系列-Transformer

    2024-07-21 23:08:01       22 阅读
  3. 语言模型系列-Transformer

    2024-07-21 23:08:01       17 阅读
  4. 语言模型系列:Transformer

    2024-07-21 23:08:01       18 阅读
  5. 语言模型系列-Transformer

    2024-07-21 23:08:01       22 阅读
  6. 语言模型系列-Transformer介绍

    2024-07-21 23:08:01       25 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-21 23:08:01       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-21 23:08:01       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-21 23:08:01       45 阅读
  4. Python语言-面向对象

    2024-07-21 23:08:01       55 阅读

热门阅读

  1. 接到需求后的开发步骤

    2024-07-21 23:08:01       18 阅读
  2. C#WPF九宫格图片背景实例

    2024-07-21 23:08:01       17 阅读
  3. 算法学习4——动态规划

    2024-07-21 23:08:01       20 阅读
  4. Mysql-多表查询

    2024-07-21 23:08:01       20 阅读
  5. lodash将对象转换成http参数

    2024-07-21 23:08:01       17 阅读
  6. 链表的返回中点问题

    2024-07-21 23:08:01       17 阅读
  7. python实战(输出会动的爱心)*

    2024-07-21 23:08:01       15 阅读
  8. 42、PHP 实现把二叉树打印成多行

    2024-07-21 23:08:01       15 阅读