(Paper Weekly01) Do Transformer Modifications Transfer?

背景

当前深度学习中“经验性的成功”(emperical success)大多来自于对网络架构的改进与训练过程的改进。包括优化器、正则化方法和模型结构等等。在这些成功的工作背后隐藏的一个原则是,在将某个机器学习pipeline上的改进迁移到其他task上时,表现至少不会变差。

例如CNN中的残差连接,被广泛应用于图像分类、语义分割等任务上。当然在实际工作中研究者并不可能尝试所有的改进方法,因此研究者必须通过一些有代表性的任务对改进进行评估。

Transformer结构是一个很好的例子。在三年前被提出以来,学界出现了许多对transformer结构的mod,然而只有很少的mods被真正应用于实践。Transformers的各种mods似乎出现了在不同task上的“泛化”问题。

Transformer结构回顾

(这篇总结的不错,包括各种符号用的也比较统一)

input token sequences: $x[1],x[2],x[3]…,x[T]$

Target sequence: $y[1],y[2],y[3]…,y[T]$

经过emb层和position encoding p之后得到隐状态 $h_0$

$$ h_{e,0}[t]=\mathbf{E}[x[t]]+p[t] $$

这里的 $p[t,i] \in \mathbf{R} ^{d_{model}}$

$$ p[t,i]= \begin{cases} sin(\frac{i}{10000^{2i/d_{model}}})& \text{i even} \ cos(\frac{i}{10000^{2i/d_{model}}})& \text{i odd} \end{cases} \tag{0} $$

Layer normalization被定义为在序列$h[1],h[2],…,h[T]$上的一系列操作,

$$ \mu[t]=\frac{1}{d_{model}}\Sigma^{d_{model}}_{i=1}h[t,i] \tag{1} \ $$

$$ \sigma[t]=\sqrt{\frac{1}{d_{model}}\Sigma^{d_{model}}_{i=1}(h[t,i]-\mu[t])^2} \tag{2} $$

$$ LayerNorm(h)[t]=\frac{\gamma}{\sigma[t]}\odot(h[t,i]-\mu[t])+\beta \tag{3} $$

$$\odot$$代表elementwise multiplication,$$\gamma,\beta\in\mathbf{R}^{d_{model}}$$ 是可学习的参数。q,k,v的计算方式如下: $$ q_{e,l,h}[t]=h_{e,l-1}[t]Q_{e,l,h}\tag{2} $$

$$ k_{e,l,h}[t]=h_{e,l-1}[t]K_{e,l,h} \tag{3} $$

$$ v_{e,l,h}[t]=h_{e,l-1}[t]V_{e,l.h} \tag{4} $$

$$ a_{e,l,h}=softmax(\frac{q_{e,l,h[t]}k_{e,l,h}[t]^{T}}{\sqrt{d_k}})v_{e,l,h}[t] \tag{5} $$

Q,K,V的维度都是 $$ Q_{e,l,h},K_{e,l,h},V_{e,l,h} \in \mathbb{R}^{d_{model}\times d_k} $$ 将这H个注意力头进行拼接,并通过仿射变换加上残差连接,最后利用LayerNorm得到中间结果,

2

最后再通过两层前馈网络得到最终的输出 $$ f_{e,l}[t]=max(0,s_{e,l}[t]W_{e,l,1}+b_{e,l,1})W_{e,l,2}+b_{e,l,2}, W_{e,l,1}\in \mathbb{R}^{d_{ff}\times d_{model}} $$

$$ h_{e,l}=\mathbf{LayerNorm}(s_{e,l}+f_{e,l}) $$

对transformers的各种修改

针对激活函数(Activations)

包括:

  • GeLU
  • Swish
  • Exponential Linear Units(ELU)
  • Sigmoid
  • Softplus
  • Gated Linear Unit

归一化(Normalization)

  • RMS (root-mean-square)
  • Rezero
  • Fixup : initialization scheme which tries to solve the vanishing/exploding gradient pro

深度

在比较不同深度时对feedfoward block块数量和模型宽度进行缩放,以保证运算量大致相同。

嵌入(embeddings)

将embedding矩阵分解成两个矩阵的乘积 $$ d_{model}\times d_{inner} $$

参数共享(Parameter sharing)

Softmax

  • Adaptive softmax
  • Mixture of Softmax(MoS)

结果

1

针对实验结果的猜想

  1. Mesh TensorFlow不靠谱? ✖️
  2. Task不够general?✖️
  3. 超参数调节不到位?✖️
  4. Re-implement有问题?✖️
  5. Modifications to the Transformer architecture often do not transfer across implementations and applications. ✅
陈沁宇
陈沁宇
Master Student@PKU

My research interests include natural language processing, machine learning and recommender systems.