当前位置: 首页 > news >正文

大语言模型-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的创作者一样思考

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 如何发一篇顶会论文? 涉及3D高斯,slam,自动驾驶,三维点云等等
  • 学生管理系统(C语言)(Easy-x)
  • [python]pycharm设置清华源
  • WEB前端09-前端服务器搭建(Node.js/nvm/npm)
  • 【引领未来智造新纪元:量化机器人的革命性应用】
  • npm 或者yarn下载依赖卡顿报错 sill idealTree buildDeps
  • [Redis]典型应用——分布式锁
  • BGP笔记的基本概要
  • 开源模型应用落地-FastAPI-助力模型交互-进阶篇-RequestDataclasses(三)
  • HTML5应用的安全防护策略与实践
  • 设计模式11-原型模式
  • 中文诗歌生成
  • 爬取百度图片,想爬谁就爬谁
  • 【Vue3】计算属性
  • Android Studio关于Gradle及JDK问题解决
  • Angular Elements 及其运作原理
  • codis proxy处理流程
  • egg(89)--egg之redis的发布和订阅
  • Git的一些常用操作
  • JS函数式编程 数组部分风格 ES6版
  • spring + angular 实现导出excel
  • 和 || 运算
  • 小程序 setData 学问多
  • ​2021半年盘点,不想你错过的重磅新书
  • ​LeetCode解法汇总307. 区域和检索 - 数组可修改
  • # 日期待t_最值得等的SUV奥迪Q9:空间比MPV还大,或搭4.0T,香
  • #NOIP 2014# day.1 生活大爆炸版 石头剪刀布
  • (173)FPGA约束:单周期时序分析或默认时序分析
  • (33)STM32——485实验笔记
  • (CVPRW,2024)可学习的提示:遥感领域小样本语义分割
  • (C语言)strcpy与strcpy详解,与模拟实现
  • (delphi11最新学习资料) Object Pascal 学习笔记---第13章第6节 (嵌套的Finally代码块)
  • (k8s)Kubernetes 从0到1容器编排之旅
  • (LeetCode 49)Anagrams
  • (php伪随机数生成)[GWCTF 2019]枯燥的抽奖
  • (附源码)流浪动物保护平台的设计与实现 毕业设计 161154
  • (接口自动化)Python3操作MySQL数据库
  • (论文阅读40-45)图像描述1
  • (十二)Flink Table API
  • (太强大了) - Linux 性能监控、测试、优化工具
  • (一)python发送HTTP 请求的两种方式(get和post )
  • (原創) 如何安裝Linux版本的Quartus II? (SOC) (Quartus II) (Linux) (RedHat) (VirtualBox)
  • (转)Sublime Text3配置Lua运行环境
  • (转)关于多人操作数据的处理策略
  • .mat 文件的加载与创建 矩阵变图像? ∈ Matlab 使用笔记
  • .net core docker部署教程和细节问题
  • .NET MVC、 WebAPI、 WebService【ws】、NVVM、WCF、Remoting
  • .net 获取某一天 在当月是 第几周 函数
  • .NET 将多个程序集合并成单一程序集的 4+3 种方法
  • .NET命名规范和开发约定
  • .NET下的多线程编程—1-线程机制概述
  • [ C++ ] 类和对象( 下 )
  • [23] 4K4D: Real-Time 4D View Synthesis at 4K Resolution
  • [3300万人的聊天室] 作为产品的上游公司该如何?
  • [AutoSar]BSW_Com02 PDU详解