Last updated 2 years ago
论文地址:
之前Transformer中使用memory的方式都是当前token和memory中token交互,但是memory中token无法和当前token交互,本文就是对这点进行改进。
符号:
当前token:Xτ=[xτ+1,⋯ ,xτ+N]∈RN×d\mathbf {X}_{\tau}=\left[\mathbf {x}_{\tau+1}, \cdots, \mathbf {x}_{\tau+N}\right] \in \mathbb{R}^{N \times d}Xτ=[xτ+1,⋯,xτ+N]∈RN×d
memory:Xτ−1=[xτ−M+1,⋯ ,xτ]∈RM×d\mathbf {X}_{\tau-1}=\left[\mathbf {x}_{\tau-M+1}, \cdots, \mathbf {x}_{\tau}\right] \in \mathbb{R}^{M \times d}Xτ−1=[xτ−M+1,⋯,xτ]∈RM×d
X~τ−1=[xτ−N+2,⋯ ,xτ+1]∈RN×d\tilde{\mathbf {X}}_{\tau-1}=\left[\mathbf {x}_{\tau-N+2}, \cdots, \mathbf {x}_{\tau+1}\right] \in \mathbb{R}^{N \times d}X~τ−1=[xτ−N+2,⋯,xτ+1]∈RN×d
计算:
Qτ=XτWq,Kτ=XτWk,Vτ=XτWv\mathbf{Q}_{\tau}=\mathbf{X}_{\tau} \mathbf{W}_{q}, \mathbf{K}_{\tau}=\mathbf{X}_{\tau} \mathbf{W}_{k}, \mathbf{V}_{\tau}=\mathbf{X}_{\tau} \mathbf{W}_{v}Qτ=XτWq,Kτ=XτWk,Vτ=XτWv
K~τ−1=X~τ−1Wk,V~τ−1=X~τ−1Wv\tilde{\mathbf{K}}_{\tau-1}=\tilde{\mathbf{X}}_{\tau-1} \mathbf{W}_{k}, \tilde{\mathbf{V}}_{\tau-1}=\tilde{\mathbf{X}}_{\tau-1} \mathbf{W}_{v}K~τ−1=X~τ−1Wk,V~τ−1=X~τ−1Wv
Cτ←=Softmaxlower-triangle(QτK~τ⊤d)V~τ\mathbf{C}_{\tau}^{\leftarrow}=\operatorname{Softmax}_{\text{lower-triangle}} \left(\frac{\mathbf{Q}_{\tau} \tilde{\mathbf{K}}_{\tau}^{\top}}{\sqrt{d}}\right) \tilde{\mathbf{V}}_{\tau}Cτ←=Softmaxlower-triangle(dQτK~τ⊤)V~τ
Cτ→=Softmaxupper-triangle(QτK~τ⊤d)V~τ\mathbf{C}_{\tau}^{\rightarrow}=\operatorname{Softmax}_{\text{upper-triangle}} \left(\frac{\mathbf{Q}_{\tau} \tilde{\mathbf{K}}_{\tau}^{\top}}{\sqrt{d}}\right) \tilde{\mathbf{V}}_{\tau}Cτ→=Softmaxupper-triangle(dQτK~τ⊤)V~τ
Cτ−1↔=ατsg(Cτ→)+(1−ατ)Cτ←\mathbf{C}_{\tau-1}^{\leftrightarrow}=\mathbf{\alpha}_{\tau} \operatorname{sg}\left(\mathbf{C}_{\tau}^{\rightarrow}\right)+\left(1-\mathbf{\alpha}_{\tau}\right) \mathbf{C}_{\tau}^{\leftarrow}Cτ−1↔=ατsg(Cτ→)+(1−ατ)Cτ←
sg\mathrm{sg}sg表示不计算梯度;
ατ=sg(sτ→)sg(sτ→)+sτ←+ε\mathbf{\alpha}_{\tau}=\frac{\operatorname{sg}\left(\mathbf{s}_{\tau}^{\rightarrow}\right)}{\operatorname{sg}\left(\mathbf{s}_{\tau}^{\rightarrow}\right)+\mathbf{s}_{\tau}^{{\leftarrow}}+\varepsilon}ατ=sg(sτ→)+sτ←+εsg(sτ→);
其中sτ→\mathbf{s}_{\tau}^{\rightarrow}sτ→表示Cτ→\mathbf{C}_{\tau}^{\rightarrow}Cτ→中Softmax矩阵归一化之前的元素和;
时间复杂度为O(N(N+M)d)O(N(N+M)d)O(N(N+M)d)。
不变。
适用于encoder和decoder;论文只测试了lm(decoder)场景,获得了一定的提升。
暂无。
提供了一种新的memory交互方式。