深度学习·经典模型·Transformer

用于博主复习

Transformer

Encoder

  • 输入和输出维度不变
  • 输入维度: ( n , d m o d e l ) (n,d_{model}) (n,dmodel),d m o d e l d_{model} dmodel一般是512,是embedding后的维度
  • 输出维度: ( n , d m o d e l ) (n,d_{model}) (n,dmodel)

自注意力层

注意力分为自注意力交叉注意力

Q,K,V

  • Q,K,V的含义不需要特别分清楚

V在自注意力和交叉注意力中的来源不同

  • Q: ( n , d m o d e l ) (n,d_{model}) (n,dmodel),
  • K: ( n , d m o d e l ) (n,d_{model}) (n,dmodel)
  • V: ( n , d m o d e l ) (n,d_{model}) (n,dmodel):
  • 注:d_model一般都为512,三者的d_model可以不同

d k \sqrt{d_k} dk 的作用

To illustrate why the dot products get large, assume that the components of q and k are independent random variables with mean 0 and variance 1. Then their dot product, q · k has mean 0 and variance d_k.

防止过于softmax过于集中,梯度过小(本质上是因为梯度关系)

  • 数学解释 :Q,K的均值为0,方差为1时,QK的均值为0,方差为 d k d_k dk

多头注意力

多头的动机:多头是为了学习不同的线性映射关系

  • 假设头数h=8,会有8个K,Q,V矩阵,维度为 d m o d e l / 8 d_{model}/8 dmodel/8,这些矩阵都是相对独立的,学习到的映射也不尽相同。

本质上是对 d m o d e l d_{model} dmodel特征维度学习的拆分

  • 最后拼接回来形成 ( n , d m o d e l ) (n,d_{model}) (n,dmodel)的输出即可

注意力输出

对原始token特征向量的基于上下文的一个"修正"或者新的映射

  • (QK)与V的乘积反映在特征空间上的加权,特征向量的某些维度(根据注意力权重)对于形成token新的特征向量更加重要?
  • 维度不变

MASKED掩码

只用于decoder中,由于decoder是基于自回归的方式,所以不能让一个t-1时刻的token与t时刻以后的tokens作注意力

  • Transformer掩码计算: s o f t m a x ( Q K T ∗ M A S K E D ) ∗ V softmax(QK^T*MASKED)*V softmax(QKT∗MASKED)∗V

交叉注意力

  • K,V是来自encoder的信息矩阵,Q是来自Decoder的信息矩阵.

显然是根据encoder的信息,对decoder的特征向量进行专属修正

FFN层

原文描述:In addition to attention sub-layers, each of the layers in our encoder and decoder contains a fully connected feed-forward network, which is applied to each position separately and identically. This consists of two linear transformations with a ReLU activation in between

除了注意力子层之外,我们的编码器和解码器中的每个层都包含一个完全连接的前馈网络,该网络分别且相同地应用于每个位置。这包括两个线性转换,中间有一个 ReLU 激活

  • 两个MLP,一个MLP负责放大维度到 4 ∗ d m o d e l 4*d_{model} 4∗dmodel,两一个负责输出回 d m o d e l d_{model} dmodel。

不要忘记:中间有一个GELU作为激活

Add&Norm

  • 残差连接后 ,再归一化

位置编码

位置编码的动机:Transformer没有序列先后顺序这一概念.

位置编码对于Transformer的作用明显,不加会掉点.

模型Workflow

  • 接受两个句子作为输入
  • 根据Softmax自回归输出token.

参考文献

cpp 复制代码
@misc{vaswaniAttentionAllYou2023,
  title = {Attention {{Is All You Need}}},
  author = {Vaswani, Ashish and Shazeer, Noam and Parmar, Niki and Uszkoreit, Jakob and Jones, Llion and Gomez, Aidan N. and Kaiser, Lukasz and Polosukhin, Illia},
  year = {2023},
  number = {arXiv:1706.03762},
  eprint = {1706.03762},
  primaryclass = {cs},
  publisher = {arXiv},
  doi = {10.48550/arXiv.1706.03762},
  archiveprefix = {arXiv}
}
相关推荐
啊阿狸不会拉杆28 分钟前
人工智能数学基础(四):线性代数
人工智能·python·数学·算法·机器学习
OceanBase数据库官方博客37 分钟前
OceanBase 跻身 Forrester 三大领域代表厂商,全面支撑AI场景
人工智能·oceanbase·分布式数据库
像风一样_1 小时前
机器学习-入门-决策树(1)
人工智能·决策树·机器学习
飞火流星020271 小时前
Weka通过10天的内存指标数据计算内存指标动态阈值
人工智能·机器学习·数据挖掘·weka·计算指标动态阈值·使用统计方法计算动态阈值
xiaoniu6671 小时前
毕业设计-基于预训练语言模型与深度神经网络的Web入侵检测系统
人工智能·语言模型·dnn
豆芽8192 小时前
感受野(Receptive Field)
人工智能·python·深度学习·yolo·计算机视觉
赛卡2 小时前
IPOF方法学应用案例:动态电压频率调整(DVFS)在AIoT芯片中的应用
开发语言·人工智能·python·硬件工程·软件工程·系统工程·ipof
蒙双眼看世界2 小时前
AI应用实战:Excel表的操作工具
人工智能
机器学习之心2 小时前
飞蛾扑火算法优化+Transformer四模型回归打包(内含MFO-Transformer-LSTM及单独模型)
算法·回归·lstm·transformer·飞蛾扑火算法优化
jndingxin2 小时前
OpenCV 图形API(64)图像结构分析和形状描述符------在图像中查找轮廓函数findContours()
人工智能·opencv