当我们在聊Attention的时候,我们实际在聊什么?

229 阅读7分钟
原文链接: zhuanlan.zhihu.com
最近几年,Attenion 已经逐渐成为了深度学习社区一个相当流行的概念,在很多应用领域充当着一个非常实用的工具

摘选部分翻译自 Attention? Attention!

Attention 如果说的简单点,灵感就是来源于人在观察一张图片或阅读一篇文章的时候,会将注意力去分散到图片上不同的部位或文章中不同的句子。以下图为例,

人类大脑的注意力可以让我们将目光焦距在图片的特定部分,比如上图中黄色框中的耳朵,红色框中的眼睛、鼻子。并且,对于图片上不重要的部位,注意力机制也可以自动忽略,比如上图的背景部分。注意力机制可以让我们在区分一张图片是不是小狗的时候,迅速的去捕捉需要的特征,并且忽略不重要的特征。

One word “attends” to other words in the same sentence differently.

同样,在自然语言处理任务中(如上图),举个栗子,当我们看到 “吃” 这个词的时候,我们的注意力会马上集中在某种食物的名字上,而对其他形容词则相应的注意力会降低。

一言以蔽之,Attention 可以理解成一种对输入进行重新加权分配之后得到的向量:用来表示输入文本中的各个单词或图片中不同的像素点,相对于目标输出的权重。

原始的 Seq2Seq 模型有什么问题

Seq2Seq 是为了解决 language modeling 问题而被提出的 (Sutskever, et al. 2014)。 通俗的说,它的目标是将输入的一个句子(source) 转化成一个新的目标句子(target),同时输入和输出的句子长度可以是任意的。Seq2Seq 的应用领域比较广泛,有机器翻译(文字或者语音)、自动问答和自动摘要等等。。。

传统的 Seq2Seq 都拥有一个 encoder-decoder 的架构:

  • Encoder 负责处理输入,并将输入的信息进行 encode,转化成一个固定长度的 context 向量,这个 context 向量包含了整个输入的信息。
  • Decoder 负责接收 context 向量并将其转化成输出,早起的 Seq2Seq 模型只是利用啦 encoder 最后一个 state 作为 decoder 的初始 state。

Encoder 和 Decoder 通常使用 LSTM 或者 GRU。

The encoder-decoder model, translating the sentence “she is eating a green apple” to Chinese. The visualization of both encoder and decoder is unrolled in time.

将 context 设定为一个固定长度的向量有一个明显的缺点,就是当句子长度增加之后,它并不能很好的记住所有信息,一个典型的栗子就是,通常当他处理完所有的输入之后,输入的前一部分很多信息就已经遗忘啦。。。很显然,想用一个 context 向量来承当编码输入句子的重担,这任务有点重。。

所有,为了解决这个问题,Attention Mechanism (Bahdanau et al., 2015) 应运而生 。

为 Translation 而生

Attention Mechanism 起初为了在机器翻译任务中,帮助更好的记忆长输入的情形。和原始的只是通过 encoder 最后一个 state 构建出 context 向量不同的是,Attention 在 context 向量和 input 之间构建了很多个 shortcuts (可以理解成 Attention 矩阵),这些 shortcut 的权重相对于不同的 output,都是不同的。

现在,在有了 Attention Mechanism 之后,相当于我们有了新的 context 向量,此时,输入和输出之间的对齐关系 (alignment relation) 可以被 context 向量学习到。

本质上来说,context 向量由以下三个部分组成。

  • encoder hidden states;
  • decoder hidden states;
  • alignment between source and target.
The encoder-decoder model with additive attention mechanism in Bahdanau et al., 2015.

定义

下面开始对 attention mechanism 给出一种比较科学的定义。假设我们面临一个 Seq2Seq 任务,source input 为 x ,长度为 n ;target output 为 y ,长度为 m

x = [x_1, x_2, ..., x_n]

y = [y_1, y_2,..., y_m]

Encoder 一般都是 bidirectional RNN。使用双向的原因是可以同时捕捉句子前向和后向之间的联系。

h_i = [h_i^+, h_i^-], i = 1,...,n

Decoder 针对在 t 位置输出的 word 的 hidden state s_t = f(s_{t-1}, y_{t-1}, c_t) ,此时的 context vector 是输入 hidden state 在 alignment scores 下的加权之和。

c_t = \sum_{i=1}^{n}\alpha_{t,i} h_i

输入和输出的两个单词 y_tx_i 的对齐方式:

\alpha_{t,i} = align(y_t, x_i) = \frac{score(s_{t-1},h_i)}{\sum_{i=1}^{n}{score(s_{t-1},h_i)}}

Alignment model 针对 i 位置的输入 和 t 位置的输出,基于它们的匹配程度来计算出一个得分。 输入和输出之间的匹配得分可以构成一个得分矩阵,得分矩阵用来刻画在输出的时候,哪部分的输入需要被重点来考虑。

Bahdanau 的论文中, the alignment score 用一个 feed-forward network with a single hidden layer 来参数化,这一部分联合模型一起进行训练。

The score function 如下所示:

Score(s_t, h_i) = v_a^T tanh (W_a[s_t; h_i])

v_aW_a 都是模型在训练过程中可以学习到的参数。

通过可视化 alignment score 矩阵可以形象的看到输入 word 和 输出 word 相关程度上的对应关系。

Attention 全家桶

各种各样的 attention mechanism,直接贴图了。

Self-Attention

Self-attention,也叫 intra-attention,是一种通过自身和自身相关联的 attention mechanism(像周伯通的左右互搏),从而得到一个更好的 representation 来表达自身。这种方法在机器阅读理解和自动摘要任务中都取得了很不错的效果。

The long short-term memory network 利用 self-attention 来做机器阅读理解。在下面的例子中, self-attention mechanism 使得我们可以学到一个句子中,当前词语和之前出现的词语之间的联系信息。

The current word is in red and the size of the blue shade indicates the activation level

show, attend and tell 这篇论文中,self-attention 被应用在图像描述生成任务中。图像首先经过 CNN 处理,然后输出到一个 self-attention 的 RNN 去来生成对应的 descriptive words。

通过可视化处理的 attention 权重图可以很明显看出图片的不同区域相对于输出 descriptive words 的 match 程度,

Soft vs Hard Attention

soft 和 hard 是两种不同的 attention 方法。同样出自论文 show, attend and tell。它们的区别在于 Attention 矩阵是否和输入的全体有关还是部分有关。

下面一段参考英文说的吧。。。感觉写得更容易懂。

  • Soft Attention: the alignment weights are learned and placed “softly” over all patches in the source image; essentially the same idea as in Bahdanau et al., 2015.
    • Pro: the model is smooth and differentiable.
    • Con: expensive when the source input is large.
  • Hard Attention: only selects one patch of the image to attend to at a time.
    • Pro: less calculation at the inference time.
    • Con: the model is non-differentiable and requires more complicated techniques such as variance reduction or reinforcement learning to train. (Luong, et al., 2015)

Global vs Local Attention

global attention

global attention 在计算 context vector c_{t} 的时候会考虑 encoder 所产生的全部 hidden state。记 decoder 时刻 t 的 target hidden 为 h_{t} ,encoder 的全部 hidden state 为 \bar{h}_{s},s=1,2,...,n ,对于其中任意 \bar{h}_{s} ,其权重 \alpha_{t}(s)

\alpha_{t}(s) = align(h_t, \bar{h}_s) = \frac{score(h_t, h_s)}{\sum_{s'}^{}{score(h_t, \bar{h}_{s'})}}

而其中的 score(h_{t},\bar{h}_{s}) ,文章给出了四种计算方法 (alignment function)

\alpha_t = softmax(W_ah_t)

四种方法都比较直观。在得到这些权重后, c_{t} 的计算是很自然的,即为 \bar{h}_{s} 的加权求和。

local attention

Luong, et al., 2015 提出了 global 和 local 两种 attention. The global attention 和前面说的 soft attention 很像,不再赘述;然而 local attention 是 hard and soft attention 二者有趣的结合。local attention 相比 hard attention,它的计算复杂度要低于 global attention 和 soft attention,而且与 hard attention 不同的是,local attention 几乎处处可微,易与训练。

global attention 或者 soft attention 它们的缺点在于每次都要扫描全部的 source hidden state,计算开销较大,对于长句翻译不利,为了提升效率,提出 local attention,每次只forcus 一小部分的 source position。

这里,context vector c_{t} 的计算只关注窗口 [p_{t}-D,p_{t}+D] 内的 2D+1 个 source hidden states (若发生越界,则忽略界外的source hidden states)。其中 p_{t} 是一个 source position index,可以理解为 attention的焦点,作为模型的参数, D 根据经验来选择。

关于 p_{t} 的计算,文章给出了两种计算方案,

Monotonic alignment (local-m)

p_{t}=t

Predictive alignment (local-p)

p_t = S \cdot sigmoid (v_p^T tanh(W_ph_t))

其中 W_{p}v_{p} 是模型的参数, S 是 source sentence 的长度,易知 p_{t}\in[0,S]

权重 \alpha_{t}(s) 的计算如下:

\alpha_t(s) = align(h_t, \bar{h}_s)exp(-\frac{(s-p_t)^2}{2\sigma^2})

可以看出,距离中心 p_{t} 越远的位置,其位置上的 source hidden state 对应的权重就越小。

总结

先总结到这啦,想到再更。。。