深度学习笔记

Transformer-XL: 在自注意力模型中处理长距离依赖

2020-06-06  本文已影响0人  湘港记者

我的博客: 菱歌's Blog | 听见美好
笔记原文地址:论文阅读笔记(3):Transformer-XL

论文题目:Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context,下载链接

主要观点

问题

建立长期依赖:LSTM-->Transformer,带来问题:

方法

片段级递归机制(segment-level recurrence mechanism)

用途:记忆机制,储存前一片段的信息,然后与当前片段信息拼接,公式如下。

\widetilde{\mathbf{h}}_{\tau+1}^{n-1}=\left[\mathrm{SG}\left(\mathbf{h}_{\tau}^{n-1}\right) \circ \mathbf{h}_{\tau+1}^{n-1}\right] \\ \mathbf{q}_{\tau+1}^{n}, \mathbf{k}_{\tau+1}^{n}, \mathbf{v}_{\tau+1}^{n}=\mathbf{h}_{\tau+1}^{n-1} \mathbf{W}_{q}^{\top}, \widetilde{\mathbf{h}}_{\tau+1}^{n-1} \mathbf{W}_{k}^{\top}, \widetilde{\mathbf{h}}_{\tau+1}^{n-1} \mathbf{W}_{v}^{\top} \\ \mathbf{h}_{\tau+1}^{n}= TRML \left(\mathbf{q}_{\tau+1}^{n}, \mathbf{k}_{\tau+1}^{n}, \mathbf{v}_{\tau+1}^{n}\right)
其中,\widetilde{\mathbf{h}}_{\tau+1}^{n-1}即前一片段的信息(TRM输出向量),SG指stop-gradient,[\circ]指concat,TRML指Transformer Layer.

注意:记忆的状态可以不止前一个segment,在本文实验中,状态的size和attention的length(即segment长度)相同。相当暴力...

相对位置编码机制(relative position embedding scheme)

用途:编码文本在不同片段的相对位置。

传统方法中,因为各个segment是没有联系的,在位置编码时只关注一个segment,采用绝对位置编码对1:L进行编码:

\begin{aligned} \mathbf{h}_{\tau+1} &=f\left(\mathbf{h}_{\tau}, \mathbf{E}_{\mathbf{s}_{\tau+1}}+\mathbf{U}_{1: L}\right) \\ \mathbf{h}_{\tau} &=f\left(\mathbf{h}_{\tau-1}, \mathbf{E}_{\mathbf{s}_{\tau}}+\mathbf{U}_{1: L}\right) \end{aligned}

可以看到,在\tau\tau+1,使用的位置编码是相同的,导致信息混淆。

传统的Attention:

\begin{aligned} \mathbf{A}_{i, j}^{\mathrm{abs}} &=\underbrace{\mathbf{E}_{x_{i}}^{\top} \mathbf{W}_{q}^{\top} \mathbf{W}_{k} \mathbf{E}_{x_{j}}}_{(a)}+\underbrace{\mathbf{E}_{x_{i}}^{\top} \mathbf{W}_{q}^{\top} \mathbf{W}_{k} \mathbf{U}_{j}}_{(b)} \\ &+\underbrace{\mathbf{U}_{i}^{\top} \mathbf{W}_{q}^{\top} \mathbf{W}_{k} \mathbf{E}_{x_{j}}}_{(c)}+\underbrace{\mathbf{U}_{i}^{\top} \mathbf{W}_{q}^{\top} \mathbf{W}_{k} \mathbf{U}_{j}}_{(d)} \end{aligned}

与原Transformer中类似,提出一个\mathbf{R} \in \mathbb{R}^{L} \max \times d,其中来表示\mathbf{R}_{i}表示距离为i的两个位置,表达式改写为:

\begin{aligned} \mathbf{A}_{i, j}^{\mathrm{rel}} &=\underbrace{\mathbf{E}_{x_{i}}^{\top} \mathbf{W}_{q}^{\top} \mathbf{W}_{k, E} \mathbf{E}_{x_{j}}}_{(a)}+\underbrace{\mathbf{E}_{x_{i}}^{\top} \mathbf{W}_{q}^{\top} \mathbf{W}_{k, R} \mathbf{R}_{i-j}}_{(b)} \\ &+\underbrace{u^{\top} \mathbf{W}_{k, E} \mathbf{E}_{x_{j}}}_{(c)}+\underbrace{v^{\top} \mathbf{W}_{k, R} \mathbf{R}_{i-j}}_{(d)} \end{aligned}

其中,主要的变化为:

其中各部分用途为:

其他重要的实验细节

  1. 参数量(由小到大,由于实验数据不同,参数量在不同数据间不一定可比)

    • 12L Transformer-XL: 41M

    • 12L Transformer: 44M

    • 18L Transformer-XL: 88M

    • Transformer-XL Standard:151M

    • Transformer-XL Large: 257M

    • 24L Transformer-XL: 277M

  2. WikiText-103数据集下细节参考

    • 103M training tokens from 28K articles

    • 384 during training and 1600 during evaluation

  3. enwik8数据集下细节参考

    • 模型大小 18-layer and 24-layer Transformer-XLs

    • 注意力长度: 784 during training and 3,800 during evaluation

  4. base模型与large模型的gap

    • 参数量:0.46B~0.8B

    • PPL: 23.5~21.8

    • base模型已经远超LSTM,相对于vanilla Transformer的改进方法Adaptive input则为23.5~23.7

    • large模型相对于Adaptive input则为21.8~23.7

相关论文与代码

  1. Github-transformer-xl,原论文模型

  2. Github-transformer-xl-chinese,中文模型,用于生成文本

  3. Baevski A, Auli M. Adaptive input representations for neural language modeling[J]. arXiv preprint arXiv:1809.10853, 2018.

上一篇 下一篇

猜你喜欢

热点阅读