首页 > 其他分享 >注意力足矣(Attention Is All You Need)

注意力足矣(Attention Is All You Need)

时间:2022-12-11 23:31:56浏览次数:33  
标签:输出 Transformer 编码 Attention 编码器 足矣 Need 注意力 输入


文章目录

  • ​​Transformer​​
  • ​​Transformer架构​​
  • ​​位置编码​​
  • ​​编码器​​
  • ​​多头自注意力​​
  • ​​前馈网络​​
  • ​​解码器​​
  • ​​带掩码的多头自注意力​​
  • ​​以编码器输出作为输入的多头自注意力​​
  • ​​前馈网络​​

本文将介绍一个不使用卷积和循环网络层,而是完全基于注意力机制的模型——Transformer。提出这个模型的论文是Attention Is All You Need

注意力足矣(Attention Is All You Need)_transformer

Transformer

​​一般注意力模型​​

​​自注意力(self-attention)和多头注意力(multi-head attention)​​

Transformer使用的注意力机制是多头自注意力,即将自注意力和多头注意力结合起来:

注意力足矣(Attention Is All You Need)_transformer_02

图片改自:[1]

下面将以Transformer为例展示多头自注意力,所以这里略过。

Transformer架构

Transformer总体架构如图:

注意力足矣(Attention Is All You Need)_机器学习_03

左边为编码器,右边为解码器,注意力足矣(Attention Is All You Need)_深度学习_04表示模块重复N次。

Transformer的编码器将输入注意力足矣(Attention Is All You Need)_Soft_05进行编码,得到输入的表示注意力足矣(Attention Is All You Need)_机器学习_06

Transformer的解码器则是自回归的,即在生成下一符号时,使用先前生成的所有符号作为输入。给定注意力足矣(Attention Is All You Need)_机器学习_06,解码器一次生成一个元素注意力足矣(Attention Is All You Need)_transformer_08直到输出整个序列注意力足矣(Attention Is All You Need)_编码器_09

以机器翻译(中文翻译为英文)为例:

注意力足矣(Attention Is All You Need)_机器学习_10

(1)训练时

编码器

输入:生存还是毁灭
输出:编码Z

解码器

输入:BEGIN To be or not to be 和 Z(中间插入,如架构图所示)
目标:To be or not to be END

BEGIN和END为开始和结束的标识符。

(2)测试时

编码器

输入:生存还是毁灭
输出:编码Z

解码器自回归输入输出:

输入:BEGIN和Z(中间插入)
输出:To

输入:BEGIN To 和Z(中间插入)
输出:be


输入:BEGIN To be 和Z(中间插入)
输出:or

输入:BEGIN To be or和Z(中间插入)
输出: not

输入:BEGIN To be or not和Z(中间插入)
输出: to

输入:BEGIN To be or not to和Z(中间插入)
输出: be

输入:BEGIN To be or not to be和Z(中间插入)
输出:END
结束

下面三个小节将分别讲解Transformer的位置编码,编码器和解码器

位置编码

Transformer不包含卷积和循环模块。为了让模型能够利用输入的顺序,我们可以给输入的特征向量添加位置编码,增加一些位置信息。

注意力足矣(Attention Is All You Need)_Soft_11是输入向量注意力足矣(Attention Is All You Need)_Soft_12的位置,注意力足矣(Attention Is All You Need)_transformer_13是输入向量的维度,注意力足矣(Attention Is All You Need)_深度学习_14表示输入向量的第注意力足矣(Attention Is All You Need)_深度学习_14个维度。Transformer使用不同频率的正弦和余弦函数作为位置编码,输入向量注意力足矣(Attention Is All You Need)_Soft_12的位置编码为:

注意力足矣(Attention Is All You Need)_机器学习_17

注意力足矣(Attention Is All You Need)_Soft_18

其中:注意力足矣(Attention Is All You Need)_Soft_19注意力足矣(Attention Is All You Need)_transformer_20

以维度注意力足矣(Attention Is All You Need)_深度学习_14为横坐标,位置注意力足矣(Attention Is All You Need)_Soft_11为纵坐标,画出位置编码图像:

注意力足矣(Attention Is All You Need)_深度学习_23


图片来源:[7]

每一行为一个位置编码注意力足矣(Attention Is All You Need)_深度学习_24

这种位置编码除了可以表示向量的绝对位置信息,即不同输入向量有不同的位置编码,还包含相对位置信息:

注意力足矣(Attention Is All You Need)_Soft_25

其中

注意力足矣(Attention Is All You Need)_机器学习_26

注意力足矣(Attention Is All You Need)_机器学习_27注意力足矣(Attention Is All You Need)_transformer_28可以由注意力足矣(Attention Is All You Need)_transformer_29注意力足矣(Attention Is All You Need)_Soft_30线性表示。

对于输入注意力足矣(Attention Is All You Need)_编码器_31,位置编码注意力足矣(Attention Is All You Need)_深度学习_24与向量注意力足矣(Attention Is All You Need)_Soft_12具有相同的维度,所以可以将两者逐元素相加:

注意力足矣(Attention Is All You Need)_编码器_34

编码器

多头自注意力

注意力足矣(Attention Is All You Need)_机器学习_35

Transformer总共有注意力足矣(Attention Is All You Need)_transformer_36个并行的自注意力层/头。每个自注意力头都有自己的可学习权重矩阵注意力足矣(Attention Is All You Need)_transformer_37注意力足矣(Attention Is All You Need)_Soft_38注意力足矣(Attention Is All You Need)_编码器_39,自注意力头注意力足矣(Attention Is All You Need)_Soft_40的查询、键和值根据特征矩阵注意力足矣(Attention Is All You Need)_Soft_41计算如下:

键(Key)矩阵:

注意力足矣(Attention Is All You Need)_编码器_42

值(Value)矩阵:

注意力足矣(Attention Is All You Need)_深度学习_43

查询(Query)矩阵:

注意力足矣(Attention Is All You Need)_Soft_44

以缩放点乘(Scaled Dot-Product)作为打分函数计算注意力得分:

注意力足矣(Attention Is All You Need)_transformer_45

以Softmax()作为对齐函数,计算注意力权重:

注意力足矣(Attention Is All You Need)_编码器_46

自注意力头注意力足矣(Attention Is All You Need)_Soft_40输出为:

注意力足矣(Attention Is All You Need)_机器学习_48

将上述每个头部的自注意力计算过程总结为表达式:

注意力足矣(Attention Is All You Need)_机器学习_49

注意力足矣(Attention Is All You Need)_深度学习_50

我们的目标仍然是创建一个上下文向量作为注意力模型的输出。因此,要将各个注意力头产生的上下文向量被连接成一个向量注意力足矣(Attention Is All You Need)_机器学习_51。然后,使用权重矩阵注意力足矣(Attention Is All You Need)_编码器_52对其进行线性变换:

注意力足矣(Attention Is All You Need)_机器学习_53

对于每一个头,可以令注意力足矣(Attention Is All You Need)_深度学习_54。由于每个头部的输出尺寸大小都注意力足矣(Attention Is All You Need)_transformer_55,所以总计算成本与全尺寸注意力足矣(Attention Is All You Need)_transformer_56单头注意的计算成本差不多。

Transformer的多头自注意力层还使用了残差连接和层归一化,最终输出为:

注意力足矣(Attention Is All You Need)_Soft_57

前馈网络

注意力足矣(Attention Is All You Need)_深度学习_58


编码模块最终输出:

注意力足矣(Attention Is All You Need)_机器学习_59

其中

注意力足矣(Attention Is All You Need)_Soft_60

解码器

带掩码的多头自注意力

注意力足矣(Attention Is All You Need)_Soft_61

解码器为了保持自回归性,要求解码器中注意力层的输出注意力足矣(Attention Is All You Need)_Soft_62只与注意力足矣(Attention Is All You Need)_Soft_63有关,这可以通过在打分函数和对齐函数之间加了一个掩码实现。

掩码:

注意力足矣(Attention Is All You Need)_transformer_64

对齐:

注意力足矣(Attention Is All You Need)_Soft_65

得到上下文矩阵,即自注意力层的输出:

注意力足矣(Attention Is All You Need)_机器学习_66

可以看到,使用掩码之后,注意力层的输出注意力足矣(Attention Is All You Need)_Soft_62只与注意力足矣(Attention Is All You Need)_Soft_63有关

其他跟编码器的是一样的。

以编码器输出作为输入的多头自注意力

解码器第二个多头自注意力与编码器的一样,不过它的注意力足矣(Attention Is All You Need)_深度学习_69, 注意力足矣(Attention Is All You Need)_编码器_70矩阵不是使用上一层的输出计算,而是使用编码器的最终编码信息计算。

注意力足矣(Attention Is All You Need)_编码器_71

若编码器的最终输出为注意力足矣(Attention Is All You Need)_深度学习_72;上一层输出为注意力足矣(Attention Is All You Need)_编码器_73,则:

键(Key)矩阵:

注意力足矣(Attention Is All You Need)_transformer_74

值(Value)矩阵:
注意力足矣(Attention Is All You Need)_编码器_75

查询(Query)矩阵:

注意力足矣(Attention Is All You Need)_编码器_76

前馈网络

注意力足矣(Attention Is All You Need)_深度学习_77

与编码器的相同。

暂时介绍到这里,更多细节可能会在后续的博客展开。

参考:

[1] A General Survey on Attention Mechanisms in Deep Learning​​ https://arxiv.org/pdf/2203.14263v1.pdf​​

[2] Attention Is All You Need​​ https://arxiv.org/pdf/1706.03762.pdf​​

[3] 《神经网络与深度学习》 邱锡鹏

位置编码推荐进一步阅读:

[4] https://zhuanlan.zhihu.com/p/454482273


[6] https://zhuanlan.zhihu.com/p/106644634

[7] https://kazemnejad.com/blog/transformer_architecture_positional_encoding/


标签:输出,Transformer,编码,Attention,编码器,足矣,Need,注意力,输入
From: https://blog.51cto.com/u_15278213/5928939

相关文章