论文《Attention is all you need》阅读笔记

Attention is all you need

Transformer模型

Model Architecture

Transformer结构上和传统的翻译模型相同,拥有encoder-decoder structure

encoder:将一系列输入的符号表示\((x_1...,x_n)\)映射到连续表示\(z=(z_1....,z_n)\)
decoder:将\(z\)解码出系列输出序列\((y_i,....,y_n)\)

模型结构图:

202309261042951

上述结构图中:左侧为encoder结构,右侧为decoder结构
1、encoder结构:整体上encoder由两部分构成:1、Multi-head Attention2、Feed Forwarded构成。在每一部分里面都是通过残差进行连接。(论文中对此的描述为:\(LayerNorm(x+Sublayer(x))\)其中\(x+Sublayer(x)\)就是一个残差连接的操作,而\(LayerNorm\)就是一个Norm所进行的一个操作。)

2、decoder结构整体与encoder相似,只是多了一个Masker Multi-Head Attention结构,这部分结构起到的作用为:因为如果是做序列预测(时间序列、翻译等)一般来说都是不去借助未来的数据,也就是说保证我们在预测\(i\)时候只考虑了\(i\)之前的信息。

This masking, combined with fact that the output embeddings are offset by one position, ensures that the predictions for position i can depend only on the known outputs at positions less than i.

Attention模型

202309251434370

在Transformer函数中设计的注意模型:
1、Scaled Dot-Product Attention

\[Attention(Q,K,V)=softmax(\frac{QK^T}{\sqrt{d_k}})V \]

对于上层结构通过翻译的角度进行理解:

202309251434370

下述步骤都是针对关键字Thinking

第一步imputvalues
第二步:计算Score:通过计算QK的点积;通过分数来确定在对某个位置的单词进行编码时,对输入句子其他部分的关注程度。
第三步:从Dividesofmax就是上述Attention公式里面\(softmax\)部分,后续然后乘V就是一层Attention
第四步:对加权值向量求和,这会在该位置(第一个单词)产生自注意力层的输出
QKV理解视频
https://www.youtube.com/watch?v=OyFJWRnt_AY

补充内容

1、encoder-decoder

https://proceedings.neurips.cc/paper_files/paper/2014/hash/09c6c3783b4a70054da74f2538ed47c6-Abstract.html

2、norm

Training state-of-the-art, deep neural networks is computationally expensive. One way to reduce the training time is to normalize the activities of the neurons.

正则化:\(x=\frac{x-x_{min}}{x_{max}- x_{min}}\)将数据都转化到\([0,1]\);以及标准化:\(x=\frac{x- \mu}{\sigma}\)将数据都转化为0均值同方差。在深度学习中常见4类标准化操作:
1、Bath-Norm;2、Layer-Norm;3、Instance-Norm;4、Group-Norm。这4类标准化化唯一区别就在对样本\(\mu,\sigma\)的计算区别。

定义如下计算公式:

\[\mu_i= \frac{1}{m}\sum_{k\in S_i}x_k \]

\[\sigma_i= \sqrt{\frac{1}{m}\sum_{k\in S_i}(x_k- \mu_i)^2+\epsilon} \]

4类标准化区别就在于对于参数\(S_i\)的定义!!!
如:对一组图片定义如下变量:\((N,C,H,W)\)分别代表:batch、channel、height、width
Bath-norm\(S_i=\{k_C=i_C\}\)
Layer-norm\(S_i=\{k_C=i_N\}\)
Instance-norm\(S_i=\{k_C=i_C,K_N=i_N\}\)
Group-norm\(S_i=\{k_N=i_N, \lfloor \frac{k_C}{C/G} \rfloor=\lfloor \frac{i_C}{C/G} \rfloor\}\)
\(G\)代表组的数量,\(C/G\)每个组的通道数量

对于Layer-normbatch-normInstance-normGroup-norm理解直观的用下面一组图片了解:

Group Normalization

图片来源:http://arxiv.org/abs/1803.08494

2.1 Batch-norm详细原理

参考:https://proceedings.mlr.press/v37/ioffe15.html
在原始论文中主要是将Batch-norm运用在图算法(CNN等)中,作者得到结论:通过添加Batch-norm可以加快模型的训练速度,模型效果较以前算法效果更加好

原理:对每一个特征独立的进行归一化

we will normalize each scalar feature independently, by making it have zero mean and unit variance.

对于每一层\(d\)维输入\(x=(x^{(1)},...,x^{(d)})\)通过:

\[\widehat{x}^{(k)}=\frac{x^{k}- E[x^{k}]}{\sqrt{Var[x^{k}]}} \]

问题一:如果只是简单的对每一层输入进行简单的正则化将会改变该层所表示的内容(原文中提到:对于通过sigmoid函数处理的输入进行正则化将会导致原本应该的非线性转化为线性)

Note that simply normalizing each input of a layer may change what the layer can represent. For instance, normalizing the inputs of a sigmoid would constrain them to the linear regime of the nonlinearity.

通过构建\(y^{(k)}=\gamma^{(k)}\widehat{x}^{(k)}+\beta^{(k)}\)其中\(\gamma^{(k)}=\sqrt{Var[x^{(k)}]}、\beta^{(k)}=E[x^{(k)}]\)

问题二:在训练过程中运用的是全部的数据集,因此想要做到随机优化是不可能的,因为通过小批量随机梯度训练,在每一次小梯度过程中都会产生均值以及方差。(Batch Normalizing Transform

2.2 Layer-norm详细原理

参考:http://arxiv.org/abs/1607.06450
针对Batch-norm的部分缺点以及其在序列数据上的表现较差,提出通过使用Layer-norm在序列数据(时间序列、文本数据)上进行使用

原理:通过从一个训练样例中计算用于归一化的所有神经元输入之和的均值和方差(也就是说直接对某个神经元的所有的输入进行标准化)

we transpose batch normalization into layer normalization by computing the mean and variance used for normalization from all of the summed inputs to the neurons in a layer on a single training case

\[\mu^l= \frac{1}{H}\sum_{i=1}^{H}a_i^l \\ \sigma^l=\sqrt{\frac{1}{H}\sum_{i=1}^{H}(a_i^l- \mu^l)^2} \]

其中\(H\)代表隐藏层的数量、\(a^l\)代表一层神经元的输入、\(\mu和\sigma\)则分别代表该层神经元的均值以及方差。

2.3 Group-norm

参考:http://arxiv.org/abs/1803.08494

参考

1、http://arxiv.org/abs/1706.03762

posted @ 2024-02-24 21:33  Big-Yellow-J  阅读(14)  评论(0编辑  收藏  举报