attention model -- Hierarchical Attention Networks for Document Classification

这是2016 ACL的一片论文。本文主要介绍了attention机制在文本分类上的应用。作者提出了分层的attention。

  • sentence level attention:
    每个句子对整篇文章的类别贡献不一样,所以给sentence添加attention机制,可以有权重地选择哪个sentence对文章的贡献。

  • word level attention:
    再细分,每个sentence包含多个word,每个word对sentence的贡献也不一样,所以在word级别再添加一层attention。

  • Hierarchical Attention Networks(HAN)
    attention model -- Hierarchical Attention Networks for Document Classification_第1张图片
    最后softmax的输出即为文章的分类。

  • 公式
    word level的attention公式:
    attention model -- Hierarchical Attention Networks for Document Classification_第2张图片
    uit u i t 指当前位置word的编码表示。 uw u w 是attention机制的共享参数,跟随网络一起训练。 αit α i t 是每个word的attention权重。 si s i 是多个word采用attention机制之后组成的sentence表示。

这里也可以看出,attention的作用只是得到各个位置的权重 αit α i t ,最后attention之后的输出的维度和最开始输入的 hit h i t 的维度一样。只不过是把各个位置的 hit h i t 加权输出了。

sentence level的attention公式:
attention model -- Hierarchical Attention Networks for Document Classification_第3张图片
ui u i 是第i个sentence的编码表示, us u s 是sentence层的attention共享参数,随网络一起训练, αi α i 是第i个sentence的attention权重, v v 是多个sentence采用attention机制加权之后组成的最后的文档表示。

输出:
这里写图片描述
attention后接一层全连接,然后softmax输出。
这里写图片描述
损失函数采用负对数似然。

  • Visualizationattention model -- Hierarchical Attention Networks for Document Classification_第4张图片
    图中蓝色颜色深浅表示word level的attention权重,红色颜色深浅表示sentence level的attention权重。

你可能感兴趣的:(nlp)