论文地址:Convolutional Sequence to SequenceLearning
代码地址:facebookresearch/fairseq
1. 为什么要选择 CNN 做机器翻译?
相比 RNN:
- CNN 能够并行处理数据,计算更高效;
- CNN 是层级结构,底层的CNN捕捉相聚较近的词之间的依赖关系,高层CNN捕捉较远词之间的依赖关系。
2. 模型
1. Position Embedding
输入为: x = ( x 1 , . . . , x m x_1, ..., x_m x1,...,xm)
词向量表示输入: w = ( w 1 , . . . , w m w_1, ..., w_m w1,...,wm) 其中 w j ∈ R f w_j \in R^f wj∈Rf, 词汇表为 D ∈ R V × f D \in R^{V \times f} D∈RV×f
位置信息: p = ( p 1 , . . . , p m p_1, ..., p_m p1,...,pm) 其中 p j ∈ R f p_j \in R^f pj∈Rf
输入最终表示向量: e = ( w 1 + p 1 , . . . , w m + p m w_1 + p_1, ..., w_m + p_m w1+p1,...,wm+pm) 其中 e j ∈ R f e_j \in R^f ej∈Rf
2. Convolutional Block Structure
encoder:
假设词向量的维数为 d 维
先把 e 做线性映射:
h
i
0
=
W
e
i
+
b
∈
R
h
h_i^0 = We_i + b \in R^h
hi0=Wei+b∈Rh
把一次"卷积计算 + 非线性计算" 看做一个单元 Convolutional Block.
卷积计算: 对原输入进行卷积窗口大小为 k 的 2h个卷积核做卷积运算, 则每次卷积生成两列 h维的向量,以 k = 3 k=3 k=3为例 f i 1 = C o n v ( h i − 1 0 , h i 0 , h i − 1 0 ) ∈ R 2 h f_i^1 = Conv(h_{i-1}^0, h_{i}^0, h_{i-1}^0) \in R^{2h} fi1=Conv(hi−10,hi0,hi−10)∈R2h
注: 卷积方式是"SAME" 需要填充边界
非线性计算: 非线性计算部分采用的是门控制结构 gated linear units(GLM). 计算如下:
G
L
U
(
f
i
1
)
=
f
i
,
1
:
d
0
⊗
δ
(
f
i
,
d
+
1
:
2
d
0
)
∈
R
h
GLU(f_i^1) = f_{i,1:d}^0 \otimes \delta(f_{i, d+1:2d}^0) \in R^h
GLU(fi1)=fi,1:d0⊗δ(fi,d+1:2d0)∈Rh
残差连接: 把输入与输出相加,输入到下一层网络中。
h
i
l
=
G
L
U
(
f
i
l
)
+
h
i
l
−
1
h_i^l = GLU(f_i^l) + h_i^{l-1}
hil=GLU(fil)+hil−1
i
=
1
,
2
,
.
.
m
i=1,2,..m
i=1,2,..m
L层卷积后生成向量可表示为
h
i
L
∈
R
h
h_i^L \in R^h
hiL∈Rh, 再
h
i
L
h_i^L
hiL 做线性映射:
z
i
u
=
W
e
h
i
L
+
b
e
z_i^u = W_eh_i^L + b_e
ziu=WehiL+be 其中
W
e
∈
R
d
×
h
W_e \in R^{d \times h}
We∈Rd×h
decoder:
与encoder类似: 设目标词为 t n t_n tn 表示第 n 步的 decoder,首先在开始位置填充两个 padding: <p>、一个句子开始标记符:<s>, embedded 成 t − 2 , t − 1 , t 0 , t 1 , . . . , t n − 1 t_{-2}, t_{-1}, t_0, t_1, ..., t_{n-1} t−2,t−1,t0,t1,...,tn−1 , 将每个 t j ∈ R d t_j \in R^d tj∈Rd 线性映射到 g j 0 ∈ R h g_j^0 \in R^h gj0∈Rh: g i 0 = W t i + b ∈ R h g_i^0 = Wt_i + b \in R^h gi0=Wti+b∈Rh.
卷积计算 + 非线性计算:
y
j
l
=
G
L
U
(
C
o
n
v
(
g
j
−
3
l
−
1
,
g
j
−
2
l
−
1
,
g
j
−
1
l
−
1
)
)
y_j^l = GLU(Conv(g_{j-3}^{l-1}, g_{j-2}^{l-1}, g_{j-1}^{l-1}))
yjl=GLU(Conv(gj−3l−1,gj−2l−1,gj−1l−1)), 然后将
y
j
l
∈
R
h
y_j^l \in R^h
yjl∈Rh 做线性映射:
r
n
l
=
W
z
y
n
l
+
b
z
+
t
n
−
1
r_n^l = W_zy_n^l + b_z + t_{n-1}
rnl=Wzynl+bz+tn−1
attention:
在每一个卷积层都会进行 attention 的操作,得到的结果输入到下一层卷积层
α n , i l = e x p ( ( z i u ) T r n l ) ∑ k = 1 m e x p ( ( z i u ) T r n l ) \alpha_{n,i}^l = \frac{exp((z_i^u)^Tr_n^l)}{\sum_{k=1}^mexp((z_i^u)^Tr_n^l)} αn,il=∑k=1mexp((ziu)Trnl)exp((ziu)Trnl) 其中 i = 1 , . . . , m i = 1,...,m i=1,...,m
=> x n l = ∑ i = 1 m α n , j l ( z i u + e i ) x_n^l = \sum_{i=1}^m\alpha_{n, j}^l(z_i^u + e_i) xnl=∑i=1mαn,jl(ziu+ei)
=> c n l = W x n l + b ∈ R h c_n^l = Wx_n^l + b \in R^h cnl=Wxnl+b∈Rh 其中 W ∈ R h × d W \in R^{h \times d} W∈Rh×d
=> g n l = y n l + c n l + g n l − 1 g_n^l = y_n^l + c_n^l + g_n^{l-1} gnl=ynl+cnl+gnl−1
output:
=> d n = W g n L + b ∈ R d d_n = Wg_n^L + b \in R^d dn=WgnL+b∈Rd
=>
o
n
=
W
o
d
n
+
b
o
o_n = W_od_n + b_o
on=Wodn+bo 其中
W
o
∈
R
∣
V
t
∣
×
d
,
b
0
∈
R
∣
V
t
∣
W_o \in R^{|V_t| \times d}, b_0 \in R^{|V_t|}
Wo∈R∣Vt∣×d,b0∈R∣Vt∣

8238

被折叠的 条评论
为什么被折叠?



