美文网首页
DiSAN: Directional Self-Attentio

DiSAN: Directional Self-Attentio

作者: 哒丑鬼 | 来源:发表于2018-11-22 23:15 被阅读0次

文章主要介绍了两种Multi-dimensional Self-Attention机制和Directional Self-Attention机制。在此基础上设计了Directional Self-Attention Network用于sentence encoding。

背景知识

Multi-dimensional Self-Attention

假设输入序列为X = \left[ { x } _ { 1 } , \cdots , { x } _ { N } \right] \in \mathbb { R } ^ { d \times N },传统的attention机制是计算每一个{x} _ i的权重\alpha_i,然后求\sum_{i=1}^{N}{\alpha_i {x} _ i}。而multi-dimensional是计算每一个{x} _ {ij}的权重\alpha_{ij},令{x}'_ {ij} = \alpha _ {ij}{x}_ {ij},最后求\sum_{i=1}^{N}{x}'_ {i}
下图更具体地描述了传统attention(a)和multi-dimensional attention(b)的区别,其中需要注意的是为了保证在时序维度上,每一维的特征的权重和都为1,multi-dimensional attention在计算权重\alpha的时候,计算softmax的维度是时序维度(红框部分)。

traditional attention VS multi-dimensional attention

token2token

f \left( x _ { i } , x _ { j } \right) = W ^ { T } \sigma \left( W ^ { ( 1 ) } x _ { i } + W ^ { ( 2 ) } x _ { j } + b ^ { ( 1 ) } \right) + b

source2token

f \left( x _ { i } \right) = W ^ { T } \sigma \left( W ^ { ( 1 ) } x _ { i } + b ^ { ( 1 ) } \right) + b

Directional Self-Attention

Directional Self-Attention(DiSA)

上图是Directional Self-Attention(DiSA)的示意图。
DiSA首先通过FC层将输入\boldsymbol{x}=[x_1,x_2,...,x_n]映射到\boldsymbol{h} = [h_1,h_2,...,h_n],即:
\boldsymbol{h} = \sigma _ { h } \left( W ^ { ( h ) } x + b ^ { ( h ) } \right)
然后将\boldsymbol{h}送入multi-dimensional token2token self-attention计算得到\boldsymbol s。在计算\boldsymbol s的过程中,作者对multi-dimensional token2token self-attention做了一些修改,减少了一些参数,同时引入了方向:

  • 将token2token中,score function里面的矩阵W改为一个标量c,同时把\sigma ( \cdot )里面的部分除以c,在实验中c被设置为5。另外,\sigma ( \cdot )使用\tanh ( \cdot )

    关于使用c的motivation,文章并没有说清楚

  • 在score function中引入位置掩码(position mask),掩码M \in \{ 0 , - \infty \} ^ { n \times n }

最后的score function如下:
f \left( h _ { i } , h _ { j } \right) = c \cdot \tanh \left( \left[ W ^ { ( 1 ) } h _ { i } + W ^ { ( 2 ) } h _ { j } + b ^ { ( 1 ) } \right] / c \right) + M _ { i j } \boldsymbol{1}
其中\boldsymbol{1}代表一个全1的矩阵。

positional mask能编码时序的原因
假设M _ { i j } = - \inftyM _ {j i} = 0,由于\alpha_{ij}是通过softmax得到的,因此\alpha_{ij}趋近于0,而\alpha_{ji}则不受影响。

对于self attention而言,通常需要排除自身的attention,因此,这篇文章加了一个diagonal-disable mask:
M _ { i j } ^ { d i a g } = \left\{ \begin{array} { l l } { 0 , } & { i \neq j } \\ { - \infty , } & { i = j } \end{array} \right.
同时,文章使用了两个时序掩码:forward mask和backward mask:
M _ { i j } ^ { f w } = \left\{ \begin{array} { c l } { 0 , } & { i < j } \\ { - \infty , } & { \text { otherwise } } \end{array} \right.
M _ { i j } ^ { b w } = \left\{ \begin{array} { c l } { 0 , } & { i > j } \\ { - \infty , } & { \text { otherwise } } \end{array} \right.

DiSA最后的输出\boldsymbol { u } \in \mathbb { R } ^ { d _ { h } \times n }通过以下计算得到,称为融合门(fusion gate):
F = \operatorname { sigmoid } \left( W ^ { ( f 1 ) } \boldsymbol{s} + W ^ { ( f 2 ) } \boldsymbol{h} + b ^ { ( f ) } \right)
\boldsymbol { u } = F \odot \boldsymbol { h } + ( 1 - F ) \odot \boldsymbol { s }

Directional Self-Attention Network

Directional Self-Attention Network (DiSAN)

文章最后提出了语言编码(Sentence Encoding)模型,如上图。

相关文章

网友评论

      本文标题:DiSAN: Directional Self-Attentio

      本文链接:https://www.haomeiwen.com/subject/flxdqqtx.html