Attention Mechanism

Attention

表示个输入信息,给定任务相关的查询向量时,注意力函数为:
\begin{aligned} \operatorname { att } ( ( K , V ) , \mathbf { q } ) & = \sum _ { i = 1 } ^ { N } \alpha _ { i } \mathbf { v } _ { i } \\ & = \sum _ { i = 1 } ^ { N } \frac { \exp \left( s \left( \mathbf { k } _ { i } , \mathbf { q } \right) \right) } { \sum _ { j } \exp \left( s \left( \mathbf { k } _ { j } , \mathbf { q } \right) \right) } \mathbf { v } _ { i } \end{aligned}
其中为score function,表示在查询向量的注意力大小。

在绝大多数场景中,。

下表总结了常用的score function的计算方法

Name score function Citation
Additive/Concat Bahdanau at al.,2015
Location Luong at al.,2015
General Luong at al.,2015
Dot Product Luong at al.,2015
Scaled Dot-Product Vaswani at al.,2017

Self-Attention

假设输入序列为,输出序列为,首先我们可以通过线性变换得到三组向量序列:
\begin{array} { l } { Q = W _ { Q } X \in \mathbb { R } ^ { d _ { 3 } \times N } } \\ { K = W _ { K } X \in \mathbb { R } ^ { d _ { 3 } \times N } } \\ { V = W _ { V } X \in \mathbb { R } ^ { d _ { 2 } \times N } } \end{array}
其中, , 分别为查询向量序列,键向量序列和值向量序列,, , 分别为可学习的参数矩阵。
输出向量可通过以下方法计算得到:
\begin{aligned} \mathbf { h } _ { i } & = \operatorname { att } \left( ( K , V ) , \mathbf { q } _ { i } \right) \\ & = \sum _ { j = 1 } ^ { N } \alpha _ { i j } \mathbf { v } _ { j } \\ & = \sum _ { j = 1 } ^ { N } \operatorname { softmax } \left( s \left( \mathbf { k } _ { j } , \mathbf { q } _ { i } \right) \right) \mathbf { v } _ { j } \end{aligned}
其中为输出和输入向量序列的位置,连接权重 由注意力机制动态生成。由于自注意力模型的权重是动态生成的,因此可以处理变长的信息序列。

自注意力模型计算的权重只依赖和的相关性,而忽略了输入信息的位置信息。因此在单独使用时,自注意力模型一般需要加入位置编码信息来进行修正Vaswani at al., 2017

最新研究

  • DiSAN: Directional Self-Attention Network for RNN/CNN-Free Language Understanding
  • Reinforced Self-Attention Network: a Hybrid of Hard and Soft Attention for Sequence Modeling
  • Convolutional Self-Attention Network
  • Self-Attention with Relative Position Representations
  • Modeling Localness for Self-Attention Networks

你可能感兴趣的:(Attention Mechanism)