【ICLR2020】Transformer Complex-order:一种新的位置编码方式

时间:2022-07-23
本文章向大家介绍【ICLR2020】Transformer Complex-order:一种新的位置编码方式,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

补一下昨天没发完的一篇

文中公式若显示不全可左右滑动~

比较有意思的论文[1],关注的点也是在序列建模的位置信息编码。先前的方法通过引入额外的位置编码,在 embedding 层将词向量和位置向量通过加性编码融合,

f(j, p o s)=f_{w e}(j)+f_{p e}(p o s)

但是该种方式每个位置向量是独立训练得到的,并不能建模序列的order relationship(例如邻接或优先关系),作者将此称为the position independece problem

针对该问题论文提出了一种新的位置编码方式,将独立的词向量替换成自变量为位置的函数,于是单词表示会随着位置的变化而平滑地移动,可以更好地建模单词的绝对位置和顺序信息。

f(j, text { pos })=g_{j}(text { pos }) in mathbb{C}^{D}

其中,

f(j, text { pos })

表示此表中序号为

j

的单词在位置

pos

时的单词向量,

D

表示函数集合,

g_{w e}(cdot):mathbb{N} rightarrow(mathcal{F})^{D}

表示单词到函数的映射,展开即为,

left[g_{j, 1}(mathrm{pos}), g_{j, 2}(mathrm{pos}), ldots, g_{j, D}(mathrm{pos})right] in mathbb{C}^{D}

为了达到上述要求,函数应该满足以下两个条件:

Property 1. Position-free offset transformation

对于任意位置 pos 和

n>1

,存在变换 Transform

_{n}(cdot)=

Transform

(n, cdot)

满足,

g(mathrm{pos}+n)=text { Transform }_{n}(g(mathrm{pos}))

特别地,论文考虑 Transform 为线性变换

Property 2. Boundedness

函数应该是有界的,

exists delta inmathbb{R}^{+}, forall text { pos } in mathbb{N},|g(operatorname{pos})| leq delta

接下去,论文证明了满足上述两个条件的解函数形式为,

g(p o s)=z_{2} z_{1}^{p o s} text { for } z_{1}, z_{2} in mathbb{C} text { with }left|z_{1}right| leq 1

❝贴一下论文给的证明:(看不看无所谓,能用就行 haha) 假设函数

g

满足上述两个条件,则对于任意位置

n_{1}, n_{2} in mathbb{N}

,有

begin{aligned} wleft(n_{1}right) wleft(n_{2}right) g(text { pos }) &=wleft(n_{2}right) gleft(text { pos }+n_{1}right)=gleft(text { pos }+n_{1}+n_{2}right) \ &=text { Transform }_{n_{1}+n_{2}}(g(text { pos }))=wleft(n_{1}+n_{2}right) g(text { pos }) end{aligned}

因此有

wleft(n_{1}+n_{2}right)=wleft(n_{1}right) wleft(n_{2}right)

。我们令

w(1)=z_{1}

以及

g(0)=z_{2}

,由于

n_{1}, n_{2} in mathbb{N}

是任意的,有

w(n)=(w(1))^{n}=z_{1}^{n}
g(text { pos }+n)=w(n) g(text { pos })=z_{1}^{n} g(text { pos })

当pos

geq 1

时,有

g(text { pos })=g(1+text { pos }-1)=w(text { pos }) g(0)=z_{1}^{text {pos }} z_{2}=z_{2} z_{1}^{text {pos }}
g(text { pos })=g(1+text { pos }-1)=w(text { pos }) g(0)=z_{1}^{text {pos }} z_{2}=z_{2} z_{1}^{text {pos }}
g(text { pos })=g(1+text { pos }-1)=w(text { pos }) g(0)=z_{1}^{text {pos }} z_{2}=z_{2} z_{1}^{text {pos }}
g(text { pos })=g(1+text { pos }-1)=w(text { pos }) g(0)=z_{1}^{text {pos }} z_{2}=z_{2} z_{1}^{text {pos }}

当pos =1时,有

g(0)=z_{2}=z_{2} z_{1}^{0}

综上将上述所有情况综合有

g(text { pos })=z_{2} z_{1}^{text {pos }}

发现当

left|z_{1}right|>1

g(text { pos })

就不是有界的了,因此限制

left|z_{1}right| leq 1

后,有

| g(text { pos })|leq| z_{2} z_{1}^{text {pos }}|leq| z_{2}|| z_{1}^{text {pos }}|leq| z_{2} |

这样就满足有界性的条件了; 又由

w(n)=z_{1}^{n}

和 Transform

_{n}(text { pos })=w(n)

pos 得,对所有的位置 pos,

g(text { pos }+n)=z_{2} z_{1}^{mathrm{ps}+n}=z_{2} z_{1}^{mathrm{pos}} z_{1}^{n}=g(text { pos }) z_{1}^{n}=text{Transform}_{n}(g(text { pos }))

得证。

根据欧拉公式,可以将上述解函数转化为,

g(text { pos })=z_{2} z_{1}^{text {pos }}=r_{2} e^{i theta_{2}}left(r_{1} e^{i theta_{1}}right)^{text {pos }}=r_{2} r_{1}^{text {pos }} e^{ileft(theta_{2}+theta_{1} text { pos }right)} text { subject to }left|r_{1}right| leq 1

在实现过程中,由于上述

left|r_{1}right| leq 1

的限制会导致优化问题,因此一种自然而然的做法就是固定

r_{1}=1

,于是上式可以简化为,

g(text { pos })=r e^{i(omega text { pos }+theta)}

最终的 embedding 表示为,

f(j, text { pos })=boldsymbol{g}_{j}(text { pos })=boldsymbol{r}_{j} e^{ileft(boldsymbol{omega}_{j} text { pos }+boldsymbol{theta}_{j}right)}

其中振幅

r*{j}

、角频率

w*{j}

和初相

theta_{j}

是需要学习的参数。

  • 振幅
r_{j}

只和单词本身有关,即原本的词向量;

  • 角频率
w_{j}

决定单词对位置的敏感程度。当角频率非常小时(如下图 p1),单词对于所有位置的词向量基本保持不变,这就与标准词向量一样了;

简单看一下文本分类任务的消融性分析结果:

  • 增加可学习初相之后效果比设置初相为 0 差:
    • "The negative effect of initial phases may be due to periodicity, and cannot be directly regularized with L2-norm penalties."
  • 设置参数共享策略之后(word-sharing scheme和dimension-sharing scheme)效果变差,可能是由于参数变少导致学习表征能力下降;

一些思考:

❝1. 要用的话,计算量以及参数量是不是会很大呀;

❝2.好像也没看到跟其他 relative position 的模型比较

reference

  • Code Here[2]
  • Open Review[3]

本文参考资料

[1]

Encoding Word Order in Complex Embedding: https://openreview.net/pdf?id=Hke-WTVtwr

[2]

Code Here: https://github.com/zhaodongh/Encoding-Word-Order-in-Complex-valued-Embedding

[3]

Open Review: https://openreview.net/forum?id=Hke-WTVtwr

- END -