A Structured Self-attentive Sentence Embedding 论文笔记

笔记原文地址:http://chenhao.space/post/e76a9f9c.html

摘要

论文地址:https://arxiv.org/abs/1703.03130

本篇论文提出了一种基于自注意力机制(自注意力机制是注意力机制的改进,其减少了对外部信息的依赖,更擅长捕捉数据或特征的内部相关性)的sentence embedding

sentence embedding模型用 2-D matrix 来表示一句话的embedding(而不是传统的vector),matrix中的每一行表示了句子中的不同的部分 。基于sentence embedding模型,论文还提出自注意力机制和一种特殊的正则化项的方法。对于该模型,一个附带的作用就是可以用一种简单的方法把句子中被encode到embedding的特殊的部分可视化出来。

作者在三个公开数据集上进行实验,并且取得了不错的效果。


Self-attention机制

For some tasks people propose to use attention mechanism on top of the CNN or LSTM model to introduce extra source of information to guide the extraction of sentence embedding. However, for some other tasks like sentiment classification, this is not directly applicable since there is no such extra information: the model is only given one single sentence as input.

意思是,对于一些任务,我们经常会使用CNN或者LSTM模型再加上注意力机制去引入额外的信息用来辅助sentence embedding的抽取。

但是,对于一些其他的任务,比如情感分类任务,上面说的模型就不能很好的应用了,因为这类任务输入的仅仅是一句话,没有额外的信息可以抽取。

这种情况下,最常用的解决方法是add a max pooling or averaging step across all time steps,或者提取the hidden representation at the last time step as the encoded embedding。

我们假设在 recurrent model 的所有时间步长上携带语义是相对困难和不必要的,所以为这些序列模型提出了一种自注意力机制(self-attention mechanism)来代替 max pooling or averaging step 。

self-attention 的好处

不同于之前的方法,这种自注意力机制可以提取句子中的不同方面,用多个向量来表示。

In addition, due to its direct access to hidden representations from previous time steps, it relieves some long-term memorization burden from LSTM.

As a side effect coming together with our proposed self-attentive sentence embedding, interpreting the extracted embedding becomes very easy and explicit.


模型的详细介绍

论文提出的模型由两部分组成,一部分是biLSTM,另一部分是self-attention mechanism(它为LSTM的隐藏层状态提供了一组summation weight vectors)。这些summation weight vectors与LSTM的隐藏状态进行点乘,得到的加权后的隐藏状态被视为句子的embedding。

A Structured Self-attentive Sentence Embedding 论文笔记_第1张图片

Figure 1 展示了sentence model应用与情感分析任务,模型中结合了全连接层和softmax层。

假设我们有一个包含n个词的句子,它被表示为一个序列的word embedding:
S = ( w 1 , w 2 , . . . , w n ) S=(w_1,w_2,...,w_n) S=(w1,w2,...,wn)
其中, w i w_i wi为一个向量(vector)表示为一个d维word embedding( s i z e = ( 1 , d ) size=(1,d) size=(1,d)), i i i表示它是句子中的第 i i i个词。因此 S S S可表示为一个2-D matrix( s i z e = ( n , d ) size=(n,d) size=(n,d))。

为了获得两个相邻词之间的依赖,我们将 S S S送到biLSTM中进行处理:

设隐藏层的个数为 u u u,则我们得到一个新的隐藏层状态 H H H,它的 s i z e = ( n , 2 u ) size=(n,2u) size=(n,2u),这个 2 u 2u 2u是两个双向的LSTM的隐藏状态合并而来的。
H = ( h 1 , h 2 , . . . , h n ) H=(h_1,h_2,...,h_n) H=(h1,h2,...,hn)
我们的最终目的是将一个句子变成一个固定size的embedding。We achieve that by choosing a linear combination of the n LSTM hidden vectors in H. Computing the linear combination requires the self-attention mechanism.

自注意力机制的任务就是将所有的LSTM隐藏状态合并成的 H H H作为一个输入,然后输出一个权重向量 a a a
a = s o f t m a x ( w s 2 t a n h ( W s 1 H T ) ) a=softmax(w_{s2}tanh(W_{s1}H^T)) a=softmax(ws2tanh(Ws1HT))
这里的 W s 1 W_{s1} Ws1是一个权重矩阵, s i z e = ( d a , 2 u ) size=(d_a,2u) size=(da,2u) w s 2 w_{s2} ws2是一个参数向量, s i z e = ( 1 , d a ) size=(1,d_a) size=(1,da)

d a d_a da是一个超参数,我们可以自由设置。

计算详细流程:
s i z e ( W s 1 ∗ H T ) = ( d a , 2 u ) ∗ ( 2 u , n ) = ( d a , n ) s i z e ( W s 2 ) ∗ ( d a , n ) = ( 1 , d a ) ∗ ( d a , n ) = ( 1 , n ) size(W_{s1}*H^T)=(d_a,2u)*(2u,n)=(d_a,n) \\ size(W_{s_2})*(d_a,n)=(1,d_a)*(d_a,n)=(1,n) size(Ws1HT)=(da,2u)(2u,n)=(da,n)size(Ws2)(da,n)=(1,da)(da,n)=(1,n)
所以 s i z e ( a ) = ( 1 , n ) size(a)=(1,n) size(a)=(1,n) a a a就是我们要找的一组linear combination参数。最后我们将 a a a H H H进行点乘,我们也就得到了最终的句向量表达。而这中vector representation往往会关注句子的一个特殊的部分(如一组特殊的相关单词或短语),但是对于长句子,我们需要关注多个部分来反应整个句子的语义。

因此我们需要进行多次关注。具体做法是将上面的 W s 2 W_{s2} Ws2的维度进行扩展,如扩展成 s i z e = ( r , d a ) size=(r,d_a) size=(r,da)的矩阵,这就相当于对句子进行了 r r r次不同部分的关注操作。最后经过运算,我们得到一个权重矩阵 A A A s i z e = ( r , n ) size=(r,n) size=(r,n)
A = s o f t m a x ( w s 2 t a n h ( W s 1 H T ) ) A=softmax(w_{s2}tanh(W_{s1}H^T)) A=softmax(ws2tanh(Ws1HT))
最后得到的 sentence embedding 为 M M M s i z e = ( r , 2 u ) size=(r,2u) size=(r,2u)
M = A H M=AH M=AH

注:该模型是有监督的:

As a downside of our proposed model, the current training method heavily relies on downstream applications, thus we are not able to train it in an unsupervised way. The major obstacle towards enabling unsupervised learning in this model is that during decoding, we don’t know as prior how the different rows in the embedding should be divided and reorganized. Exploring all those possible divisions by using a neural network could easily end up with overfitting. Although we can still do unsupervised learning on the proposed model by using a sequential decoder on top of the sentence embedding, it merits more to find some other structures as a decoder.


惩罚项

如果r次的关注操作得到的是相同或者相似的权重,那么就会产生冗余问题。因此我们需要一个惩罚项去保证每次的关注操作得到的是不同的权重向量。

惩罚项使用的是:
P = ∣ ∣ ( A A T − I ) ∣ ∣ F 2 P=||(AA^T-I)||_{F}^2 P=(AATI)F2
其中 I I I是单位矩阵, F F F表示的是Frobenius 范数, ∣ ∣ R ∣ ∣ F ||R||_F RF表示为矩阵A的各项元素的绝对值平方的总和,再开方。

如果权重参数学习得一样,那么 A A T AA^T AAT的值会变大,整个惩罚项就会变大,这样就起到了防止权重参数一样的结果。

A Structured Self-attentive Sentence Embedding 论文笔记_第2张图片


总结

Self-Attention在本论文模型的作用就是获得一个权重矩阵参数,然后将这个参数与所有隐藏层的集合H相乘,就得到了一个sentence embedding,而sentence embedding中包含了对句子中一些比较重要的部分聚焦(或者说关注)。惩罚项的部分是为了多次聚焦(关注)得到的向量不能相同或者相似。

你可能感兴趣的:(NLP)