本文转自《零基础入门深度学习》系列文章,阅读原文请移步这里
一、语言模型
RNN是在自然语言处理领域中最先被用起来的,比如,RNN可以为语言模型来建模。那么,什么是语言模型呢?
我们可以和电脑玩一个游戏,我们写出一个句子前面的一些词,然后,让电脑帮我们写下接下来的一个词。比如下面这句:
我昨天上学迟到了,老师批评了____。
我们给电脑展示了这句话前面这些词,然后,让电脑写下接下来的一个词。在这个例子中,接下来的这个词最有可能是『我』,而不太可能是『小明』,甚至是『吃饭』。
语言模型就是这样的东西:给定一个一句话前面的部分,预测接下来最有可能的一个词是什么。
语言模型是对一种语言的特征进行建模,它有很多很多用处。比如在语音转文本(STT)的应用中,声学模型输出的结果,往往是若干个可能的候选词,这时候就需要语言模型来从这些候选词中选择一个最可能的。当然,它同样也可以用在图像到文本的识别中(OCR)。
使用RNN之前,语言模型主要是采用N-Gram。N可以是一个自然数,比如2或者3。它的含义是,假设一个词出现的概率只与前面N个词相关。我们以2-Gram为例。首先,对前面的一句话进行切词:
我 昨天 上学 迟到 了 ,老师 批评 了 ____。
如果用2-Gram进行建模,那么电脑在预测的时候,只会看到前面的『了』,然后,电脑会在语料库中,搜索『了』后面最可能的一个词。不管最后电脑选的是不是『我』,我们都知道这个模型是不靠谱的,因为『了』前面说了那么一大堆实际上是没有用到的。如果是3-Gram模型呢,会搜索『批评了』后面最可能的词,感觉上比2-Gram靠谱了不少,但还是远远不够的。因为这句话最关键的信息『我』,远在9个词之前!
现在读者可能会想,可以提升继续提升N的值呀,比如4-Gram、5-Gram…。实际上,这个想法是没有实用性的。因为我们想处理任意长度的句子,N设为多少都不合适;另外,模型的大小和N的关系是指数级的,4-Gram模型就会占用海量的存储空间。
所以,该轮到RNN出场了,RNN理论上可以往前看(往后看)任意多个词。
二、循环神经网络是啥
循环神经网络种类繁多,我们先从最简单的基本循环神经网络开始吧。
1、基本循环神经网络
下图是一个简单的循环神经网络如,它由输入层、一个隐藏层和一个输出层组成:
上图为一个抽象的循环神经网络单元,如果把上面带
W
W
W的箭头去掉,就变成了最普通的全连接神经网络。
x
x
x是一个向量,它表示输入层的值,(这里面没有画出来表示神经元节点的圆圈);
s
s
s是一个向量,它表示隐藏层的值(这里隐藏层面画了一个节点,你也可以想象这一层其实是多个节点,节点数与向量
s
s
s的维度相同);
U
U
U是输入层到隐藏层的权重矩阵(读者可以回到《深度学习之神经网络和反向传播算法》,看看我们是怎样用矩阵来表示全连接神经网络的计算的);
o
o
o也是一个向量,它表示输出层的值;
V
V
V是隐藏层到输出层的权重矩阵。那么,现在我们来看看
W
W
W是什么。循环神经网络的隐藏层的值
s
s
s不仅仅取决于当前这次的输入
x
x
x,还取决于上一次隐藏层的值
s
s
s。权重矩阵
W
W
W就是隐藏层上一次的值作为这一次的输入的权重。
如果我们把上面的图展开,循环神经网络也可以画成下面这个样子:
现在看上去就比较清楚了,这个网络在t时刻接收到输入
x
t
x_t
xt之后,隐藏层的值是
s
t
s_t
st,输出值是。关键一点是,的值不仅仅取决于,还取决于。我们可以用下面的公式来表示循环神经网络的计算方法:
o
t
=
g
(
V
s
t
)
(
式
1
)
o_t=g(Vs_t)\space\space\space\space\space\space\space(式1)
ot=g(Vst) (式1)
s
t
=
f
(
U
x
t
+
W
s
t
−
1
)
(
式
2
)
\space\space\space\space\space\space\space \space\space\space\space\space\space\space \space\space\space s_t=f(Ux_t+Ws_{t-1})\space\space\space\space\space\space\space(式2)
st=f(Uxt+Wst−1) (式2)式1是输出层的计算公式,输出层是一个全连接层,也就是它的每个节点都和隐藏层的每个节点相连。
V
V
V是输出层的权重矩阵,
g
g
g是激活函数。式2是隐藏层的计算公式,它是循环层。
U
U
U是输入
x
x
x的权重矩阵,
W
W
W是上一次的值
s
t
−
1
s_{t-1}
st−1作为这一次的输入的权重矩阵,
f
f
f是激活函数。
从上面的公式我们可以看出,循环层和全连接层的区别就是循环层多了一个权重矩阵
W
W
W。
如果反复把式2带入到式1,我们将得到:
o
t
=
g
(
V
s
t
)
o_t=g(Vs_t)\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space
ot=g(Vst)
=
V
f
(
U
x
t
+
W
s
t
−
1
)
=Vf(Ux_t+Ws_{t-1})\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space
=Vf(Uxt+Wst−1)
=
V
f
(
U
x
t
+
W
f
(
U
x
t
−
1
+
W
s
t
−
2
)
)
=Vf(Ux_t+Wf(Ux_{t-1}+Ws_{t-2}))\space\space\space\space\space\space\space
=Vf(Uxt+Wf(Uxt−1+Wst−2))
=
V
f
(
U
x
t
+
W
f
(
U
x
t
−
1
+
W
f
(
U
x
t
−
2
+
W
s
t
−
3
)
)
)
\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=Vf(Ux_t+Wf(Ux_{t-1}+Wf(Ux_{t-2}+Ws_{t-3})))
=Vf(Uxt+Wf(Uxt−1+Wf(Uxt−2+Wst−3)))
=
V
f
(
U
x
t
+
W
f
(
U
x
t
−
1
+
W
f
(
U
x
t
−
2
+
W
f
(
U
x
t
−
3
+
.
.
.
)
)
)
)
\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=Vf(Ux_t+Wf(Ux_{t-1}+Wf(Ux_{t-2}+Wf(Ux_{t-3}+...))))
=Vf(Uxt+Wf(Uxt−1+Wf(Uxt−2+Wf(Uxt−3+...))))从上面可以看出,循环神经网络的输出值
o
t
o_t
ot,是受前面历次输入值
x
t
x_t
xt、
x
t
−
1
x_{t-1}
xt−1、
x
t
−
2
x_{t-2}
xt−2、
x
t
−
3
x_{t-3}
xt−3、…影响的,这就是为什么循环神经网络可以往前看任意多个输入值的原因。
2、双向循环神经网络
对于语言模型来说,很多时候光看前面的词是不够的,比如下面这句话:
我的手机坏了,我打算____一部新手机。
可以想象,如果我们只看横线前面的词,手机坏了,那么我是打算修一修?换一部新的?还是大哭一场?这些都是无法确定的。但如果我们也看到了横线后面的词是『一部新手机』,那么,横线上的词填『买』的概率就大得多了。
在上一小节中的基本循环神经网络是无法对此进行建模的,因此,我们需要双向循环神经网络,如下图所示:
当遇到这种从未来穿越回来的场景时,难免处于懵逼的状态。不过我们还是可以用屡试不爽的老办法:先分析一个特殊场景,然后再总结一般规律。我们先考虑上图中,
y
2
y_2
y2的计算。
从上图可以看出,双向卷积神经网络的隐藏层要保存两个值,一个A参与正向计算,另一个值A’参与反向计算。最终的输出值
y
2
y_2
y2取决于
A
2
A_2
A2和
A
2
′
A_2'
A2′。其计算方法为:
y
2
=
g
(
V
A
2
+
V
′
A
2
′
)
y_2=g(VA_2+V'A_2')
y2=g(VA2+V′A2′)
A
2
A_2
A2和
A
2
′
A_2'
A2′则分别计算:
A
2
=
f
(
W
A
1
+
U
x
2
)
A_2=f(WA_1+Ux_2)
A2=f(WA1+Ux2)
A
2
′
=
f
(
W
′
A
3
′
+
U
′
x
2
)
A_2'=f(W'A_3'+U'x_2)
A2′=f(W′A3′+U′x2)现在,我们已经可以看出一般的规律:正向计算时,隐藏层的值
s
t
s_t
st与
s
t
1
s_{t_1}
st1有关;反向计算时,隐藏层的值
s
t
′
s_t'
st′与
s
t
+
1
′
s_{t+1}'
st+1′有关;最终的输出取决于正向和反向计算的加和。现在,我们仿照式1和式2,写出双向循环神经网络的计算方法:
o
t
=
g
(
V
s
t
+
V
′
s
t
′
)
o_t=g(Vs_t+V's_t')
ot=g(Vst+V′st′)
s
t
=
f
(
U
x
t
+
W
s
t
−
1
)
\space\space\space \space s_t=f(Ux_t+Ws_{t-1})
st=f(Uxt+Wst−1)
s
t
′
=
f
(
U
′
x
t
+
W
′
s
t
+
1
′
)
\space\space\space \space\space\space\space s_t'=f(U'x_t+W's_{t+1}')
st′=f(U′xt+W′st+1′)从上面三个公式我们可以看到,正向计算和反向计算不共享权重,也就是说
U
U
U和
U
′
U'
U′、
W
W
W和
W
′
W'
W′、
V
V
V和
V
′
V'
V′都是不同的权重矩阵。
3、深度循环神经网络
前面我们介绍的循环神经网络只有一个隐藏层,我们当然也可以堆叠两个以上的隐藏层,这样就得到了深度循环神经网络。如下图所示:
我们把第
i
i
i个隐藏层的值表示为
s
t
(
i
)
s_t^{(i)}
st(i)、
s
t
′
(
i
)
s_t^{'(i)}
st′(i),则深度循环神经网络的计算方式可以表示为:
o
t
=
g
(
V
(
i
)
s
t
(
i
)
+
V
′
(
i
)
s
t
′
(
i
)
)
o_t=g(V^{(i)}s_t^{(i)}+V^{'(i)}s_t^{'(i)})
ot=g(V(i)st(i)+V′(i)st′(i))
s
t
(
i
)
=
f
(
U
(
i
)
s
t
(
i
−
1
)
+
W
(
i
)
s
t
−
1
)
\space\space\space\space \space\space s_t^{(i)}=f(U^{(i)}s_t^{(i-1)}+W^{(i)}s_{t-1})
st(i)=f(U(i)st(i−1)+W(i)st−1)
s
t
′
(
i
)
=
f
(
U
′
(
i
)
s
t
′
(
i
−
1
)
+
W
′
(
i
)
s
t
−
1
′
)
\space\space\space\space \space\space\space\space \space\space\space s_t^{'(i)}=f(U^{'(i)}s_t^{'(i-1)}+W'^{(i)}s_{t-1}')
st′(i)=f(U′(i)st′(i−1)+W′(i)st−1′)
.
.
.
...\space\space\space\space \space\space\space\space \space\space\space \space\space\space\space \space\space\space\space \space\space\space \space\space\space\space \space\space\space\space \space\space\space \space\space\space\space
...
s
t
(
1
)
=
f
(
U
(
1
)
x
t
+
W
(
1
)
s
t
−
1
)
\space\space\space\space s_t^{(1)}=f(U^{(1)}x_t+W^{(1)}s_{t-1})
st(1)=f(U(1)xt+W(1)st−1)
s
t
′
(
1
)
=
f
(
U
′
(
1
)
x
t
+
W
′
(
1
)
s
t
+
1
′
)
\space\space\space\space \space\space\space s_t^{'(1)}=f(U^{'(1)}x_t+W'^{(1)}s_{t+1}')
st′(1)=f(U′(1)xt+W′(1)st+1′)
三、循环神经网络的训练
1、循环神经网络的训练算法:BPTT (Back Propagation Through Time)
BPTT算法是针对循环层的训练算法,它的基本原理和BP算法是一样的,也包含同样的三个步骤:
- 前向计算每个神经元的输出值;
- 反向计算每个神经元的误差项 δ j \delta_j δj值,它是误差函数 E E E对神经元 j j j的加权输入 n e t j net_j netj的偏导数;
- 计算每个权重的梯度。
最后再用随机梯度下降算法更新权重。
循环层如下图所示:
前向计算
使用前面的(式2)对循环层进行前向计算:
s
t
=
f
(
U
x
t
+
W
s
t
−
1
)
s_t=f(Ux_t+Ws_{t-1})
st=f(Uxt+Wst−1)注意,上面的
s
t
s_t
st、
x
t
x_t
xt、
s
t
−
1
s_{t-1}
st−1都是向量,用黑体字母表示;而
U
U
U、
V
V
V是矩阵,用大写字母表示。向量的下标表示时刻,例如,
s
t
s_t
st表示在
t
t
t时刻向量
s
s
s的值。
我们假设输入向量
x
x
x的维度是
m
m
m,输出向量
s
s
s的维度是
n
n
n,则矩阵
U
U
U的维度是
n
∗
m
n*m
n∗m,矩阵
W
W
W的维度是
n
∗
n
n*n
n∗n。下面是上式展开成矩阵的样子,看起来更直观一些:
[
s
1
t
s
2
t
.
.
s
n
t
]
=
f
(
[
u
11
u
12
.
.
.
u
1
m
u
21
u
22
.
.
.
u
2
m
.
.
u
n
1
u
n
2
.
.
.
u
n
m
]
[
x
1
x
2
.
.
x
n
]
+
[
w
11
w
12
.
.
.
w
1
n
w
21
w
22
.
.
.
w
2
n
.
.
w
n
1
w
n
2
.
.
.
w
n
n
]
[
s
1
t
−
1
s
2
t
−
1
.
.
s
n
t
−
1
]
)
\begin{bmatrix}s_1^t \\ s_2^t \\ . \\. \\ s_n^t \end{bmatrix}=f(\begin{bmatrix}u_{11}u_{12}...u_{1m} \\ u_{21}u_{22}...u_{2m} \\ . \\. \\ u_{n1}u_{n2}...u_{nm} \end{bmatrix}\begin{bmatrix}x_1 \\ x_2 \\ . \\. \\ x_n \end{bmatrix}+\begin{bmatrix}w_{11}w_{12}...w_{1n} \\ w_{21}w_{22}...w_{2n} \\ . \\. \\ w_{n1}w_{n2}...w_{nn} \end{bmatrix}\begin{bmatrix}s_1^{t-1} \\ s_2^{t-1} \\ . \\. \\ s_n^{t-1} \end{bmatrix})
⎣⎢⎢⎢⎢⎡s1ts2t..snt⎦⎥⎥⎥⎥⎤=f(⎣⎢⎢⎢⎢⎡u11u12...u1mu21u22...u2m..un1un2...unm⎦⎥⎥⎥⎥⎤⎣⎢⎢⎢⎢⎡x1x2..xn⎦⎥⎥⎥⎥⎤+⎣⎢⎢⎢⎢⎡w11w12...w1nw21w22...w2n..wn1wn2...wnn⎦⎥⎥⎥⎥⎤⎣⎢⎢⎢⎢⎡s1t−1s2t−1..snt−1⎦⎥⎥⎥⎥⎤)在这里我们用手写体字母表示向量的一个元素,它的下标表示它是这个向量的第几个元素,它的上标表示第几个时刻。例如,
s
j
t
s_j^t
sjt表示向量
s
s
s的第
j
j
j个元素在
t
t
t时刻的值。
u
j
i
u_{ji}
uji表示输入层第
i
i
i个神经元到循环层第
j
j
j个神经元的权重。
w
j
i
w_{ji}
wji表示循环层第
t
−
1
t-1
t−1时刻的第
i
i
i个神经元到循环层第
t
t
t个时刻的第j个神经元的权重。
误差项的计算
BTPP算法将第 l l l层 t t t时刻的误差项 δ t l \delta_t^l δtl值沿两个方向传播,一个方向是其传递到上一层网络,得到 δ t l − 1 \delta_t^{l-1} δtl−1,这部分只和权重矩阵 U U U有关;另一个是方向是将其沿时间线传递到初始 t 1 t_1 t1时刻,得到 δ 1 l \delta_1^l δ1l,这部分只和权重矩阵 W W W有关。
我们用向量 n e t t net_t nett表示神经元在t时刻的加权输入,因为: n e t t = U x t + W s t − 1 net_t=Ux_t+Ws_{t-1} nett=Uxt+Wst−1 s t − 1 = f ( n e t t − 1 ) s_{t-1}=f(net_{t-1}) st−1=f(nett−1)因此: ∂ n e t t ∂ n e t t − 1 = ∂ n e t t ∂ s t − 1 ∂ s t − 1 ∂ n e t t − 1 \frac{\partial net_t}{\partial net_{t-1}}=\frac{\partial net_t}{\partial s_{t-1}}\frac{\partial s_{t-1}}{\partial net_{t-1}} ∂nett−1∂nett=∂st−1∂nett∂nett−1∂st−1我们用 a a a表示列向量,用 a T a^T aT表示行向量。上式的第一项是向量函数对向量求导,其结果为Jacobian矩阵: ∂ n e t t ∂ s t − 1 = [ ∂ n e t 1 t ∂ s 1 t − 1 ∂ n e t 1 t ∂ s 2 t − 1 . . . ∂ n e t 1 t ∂ s n t − 1 ∂ n e t 2 t ∂ s 1 t − 1 ∂ n e t 2 t ∂ s 2 t − 1 . . . ∂ n e t 2 t ∂ s n t − 1 . . ∂ n e t n t ∂ s 1 t − 1 ∂ n e t n t ∂ s 2 t − 1 . . . ∂ n e t n t ∂ s n t − 1 ] \frac{\partial net_t}{\partial s_{t-1}}=\begin{bmatrix} \frac{\partial net_1^t}{\partial s_1^{t-1}} \space\space \frac{\partial net_1^t}{\partial s_2^{t-1}} \space\space.\space\space. \space\space. \space\space \frac{\partial net_1^t}{\partial s_n^{t-1}} \\ \frac{\partial net_2^t}{\partial s_1^{t-1}} \space\space \frac{\partial net_2^t}{\partial s_2^{t-1}} \space\space.\space\space. \space\space. \space\space \frac{\partial net_2^t}{\partial s_n^{t-1}} \\ . \\. \\ \frac{\partial net_n^t}{\partial s_1^{t-1}} \space\space \frac{\partial net_n^t}{\partial s_2^{t-1}} \space\space.\space\space. \space\space. \space\space \frac{\partial net_n^t}{\partial s_n^{t-1}} \end{bmatrix} ∂st−1∂nett=⎣⎢⎢⎢⎢⎢⎢⎡∂s1t−1∂net1t ∂s2t−1∂net1t . . . ∂snt−1∂net1t∂s1t−1∂net2t ∂s2t−1∂net2t . . . ∂snt−1∂net2t..∂s1t−1∂netnt ∂s2t−1∂netnt . . . ∂snt−1∂netnt⎦⎥⎥⎥⎥⎥⎥⎤ = [ w 11 w 12 . . . w 1 n w 21 w 22 . . . w 2 n . . w n 1 w n 2 . . . w n n ] =\begin{bmatrix}w_{11} \space\space w_{12} \space\space ... \space\space w_{1n} \\ w_{21} \space\space w_{22} \space\space ... \space\space w_{2n} \\ . \\. \\ w_{n1} \space\space w_{n2} \space\space ... \space\space w_{nn} \end{bmatrix} =⎣⎢⎢⎢⎢⎡w11 w12 ... w1nw21 w22 ... w2n..wn1 wn2 ... wnn⎦⎥⎥⎥⎥⎤ = W =W\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space =W 同理,上式第二项也是一个Jacobian矩阵: ∂ s t − 1 ∂ n e t t − 1 = [ ∂ s 1 t − 1 ∂ n e t 1 t − 1 ∂ s 1 t − 1 ∂ n e t 2 t − 1 . . . ∂ s 1 t − 1 ∂ n e t n t − 1 ∂ s 2 t − 1 ∂ n e t 1 t − 1 ∂ s 2 t − 1 ∂ n e t 2 t − 1 . . . ∂ s 2 t − 1 ∂ n e t n t − 1 . . ∂ s n t − 1 ∂ n e t 1 t − 1 ∂ s n t − 1 ∂ n e t 2 t − 1 . . . ∂ s n t − 1 ∂ n e t n t − 1 ] \frac{\partial s_{t-1}}{\partial net_{t-1}}=\begin{bmatrix}\frac{\partial s_1^{t-1}}{\partial net_1^{t-1}} \space\space \frac{\partial s_1^{t-1}}{\partial net_2^{t-1}} \space\space.\space\space. \space\space. \space\space \frac{\partial s_1^{t-1}}{\partial net_n^{t-1}} \\ \frac{\partial s_2^{t-1}}{\partial net_1^{t-1}} \space\space \frac{\partial s_2^{t-1}}{\partial net_2^{t-1}} \space\space.\space\space. \space\space. \space\space \frac{\partial s_2^{t-1}}{\partial net_n^{t-1}} \\.\\.\\ \frac{\partial s_n^{t-1}}{\partial net_1^{t-1}} \space\space \frac{\partial s_n^{t-1}}{\partial net_2^{t-1}} \space\space.\space\space. \space\space. \space\space \frac{\partial s_n^{t-1}}{\partial net_n^{t-1}} \end{bmatrix} ∂nett−1∂st−1=⎣⎢⎢⎢⎢⎢⎢⎢⎡∂net1t−1∂s1t−1 ∂net2t−1∂s1t−1 . . . ∂netnt−1∂s1t−1∂net1t−1∂s2t−1 ∂net2t−1∂s2t−1 . . . ∂netnt−1∂s2t−1..∂net1t−1∂snt−1 ∂net2t−1∂snt−1 . . . ∂netnt−1∂snt−1⎦⎥⎥⎥⎥⎥⎥⎥⎤ = [ f ′ ( n e t 1 t − 1 ) 0 . . . 0 0 f ′ ( n e t 1 t − 1 ) . . . 0 . . 0 0 . . . f ′ ( n e t n t − 1 ) ] \space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=\begin{bmatrix} f'(net_1^{t-1}) \space\space\space\space\space\space\space\space\space0\space\space\space\space\space\space\space\space\space\space\space .\space\space. \space\space.\space\space\space\space\space\space\space\space0\\ \space\space\space\space\space\space\space0 \space\space\space\space\space\space\space\space\space\space\space f'(net_1^{t-1}) \space\space.\space\space. \space\space.\space\space\space\space\space\space\space\space0\\.\\. \\ \space\space\space\space\space\space\space\space\space\space\space\space\space\space\space0 \space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space 0\space\space\space\space\space\space\space\space\space .\space\space. \space\space. f'(net_n^{t-1})\end{bmatrix} =⎣⎢⎢⎢⎢⎡f′(net1t−1) 0 . . . 0 0 f′(net1t−1) . . . 0.. 0 0 . . .f′(netnt−1)⎦⎥⎥⎥⎥⎤ = d i a g [ f ′ ( n e t t − 1 ) ] =diag[f'(net_{t-1})]\space\space\space\space\space\space\space\space =diag[f′(nett−1)] 其中, d i a g [ a ] diag[a] diag[a]表示根据向量 a a a创建一个对角矩阵,即 d i a g ( a ) = [ a 1 0 . . . 0 0 a 2 . . . 0 . . 0 0 . . . a n ] diag(a)=\begin{bmatrix}a_1 \space\space0\space.\space.\space.\space 0 \\0\space\space a_2\space\space.\space.\space.\space0\\.\\.\\0 \space\space0\space.\space.\space.\space\space a_n \end{bmatrix} diag(a)=⎣⎢⎢⎢⎢⎡a1 0 . . . 00 a2 . . . 0..0 0 . . . an⎦⎥⎥⎥⎥⎤最后,将两项合在一起,可得 ∂ n e t t n e t t − 1 = ∂ n e t t ∂ s t − 1 ∂ s t − 1 ∂ n e t t − 1 \frac{\partial net_t}{net_{t-1}} = \frac{\partial net_t}{\partial s_{t-1}}\frac{\partial s_{t-1}}{\partial net_{t-1}}\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space nett−1∂nett=∂st−1∂nett∂nett−1∂st−1 = W d i a g [ f ′ ( n e t t − 1 ) ] =Wdiag[f'(net_{t-1})] =Wdiag[f′(nett−1)] = [ w 11 f ′ ( n e t 1 t − 1 ) w 12 f ′ ( n e t 2 t − 1 ) . . . w 1 n f ′ ( n e t n t − 1 ) w 21 f ′ ( n e t 1 t − 1 ) w 22 f ′ ( n e t 2 t − 1 ) . . . w 2 n f ′ ( n e t n t − 1 ) . . w n 1 f ′ ( n e t 1 t − 1 ) w n 2 f ′ ( n e t 2 t − 1 ) . . . w n n f ′ ( n e t n t − 1 ) ] \space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=\begin{bmatrix} w_{11}f'(net_1^{t-1}) \space\space w_{12}f'(net_2^{t-1}) \space\space.\space\space . \space\space. \space\space w_{1n}f'(net_n^{t-1}) \\ w_{21}f'(net_1^{t-1}) \space\space w_{22}f'(net_2^{t-1}) \space\space.\space\space . \space\space. \space\space w_{2n}f'(net_n^{t-1}) \\.\\.\\w_{n1}f'(net_1^{t-1}) \space\space w_{n2}f'(net_2^{t-1}) \space\space.\space\space . \space\space. \space\space w_{nn}f'(net_n^{t-1}) \end{bmatrix} =⎣⎢⎢⎢⎢⎡w11f′(net1t−1) w12f′(net2t−1) . . . w1nf′(netnt−1)w21f′(net1t−1) w22f′(net2t−1) . . . w2nf′(netnt−1)..wn1f′(net1t−1) wn2f′(net2t−1) . . . wnnf′(netnt−1)⎦⎥⎥⎥⎥⎤上式描述了将 δ \delta δ沿时间往前传递一个时刻的规律,有了这个规律,我们就可以求得任意时刻 k k k的误差项 δ k \delta_k δk: δ k T = ∂ E ∂ n e t k \delta_k^T=\frac{\partial E}{\partial net_k}\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space δkT=∂netk∂E = ∂ E ∂ n e t t ∂ n e t t ∂ n e t k =\frac{\partial E}{\partial net_t}\frac{\partial net_t}{\partial net_k}\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space =∂nett∂E∂netk∂nett = ∂ E ∂ n e t t ∂ n e t t ∂ n e t t − 1 ∂ n e t t − 1 ∂ n e t t − 2 . . . ∂ n e t k + 1 ∂ n e t k =\frac{\partial E}{\partial net_t}\frac{\partial net_t}{\partial net_{t-1}}\frac{\partial net_{t-1}}{\partial net_{t-2}}...\frac{\partial net_{k+1}}{\partial net_k} =∂nett∂E∂nett−1∂nett∂nett−2∂nett−1...∂netk∂netk+1 = W d i a g [ f ′ ( n e t t − 1 ) ] W d i a g [ f ′ ( n e t t − 2 ) ] . . . W d i a g [ f ′ ( n e t k ) ] δ t l \space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=Wdiag[f'(net_{t-1})]Wdiag[f'(net_{t-2})]...Wdiag[f'(net_{k})]\delta_t^l =Wdiag[f′(nett−1)]Wdiag[f′(nett−2)]...Wdiag[f′(netk)]δtl = δ t T ∏ i = k t − 1 W d i a g [ f ′ ( n e t i ) ] ( 式 3 ) =\delta_t^T\prod_{i=k}^{t-1}Wdiag[f'(net_i)]\space\space\space\space\space\space\space\space(式3) =δtTi=k∏t−1Wdiag[f′(neti)] (式3)(式3)就是将误差项沿时间反向传播的算法。
循环层将误差项反向传递到上一层网络,与普通的全连接层是完全一样的,这在《深度学习之神经网络和反向传播算法》中已经详细讲过了,在此仅简要描述一下。
循环层的加权输入
n
e
t
l
net^l
netl与上一层的加权输入
n
e
t
l
−
1
net^{l-1}
netl−1关系如下:
n
e
t
t
l
=
U
a
t
l
−
1
+
W
s
t
−
1
net_t^l=Ua_t^{l-1}+Ws_{t-1}
nettl=Uatl−1+Wst−1
a
t
l
−
1
=
f
l
−
1
(
n
e
t
t
l
−
1
)
a_t^{l-1}=f^{l-1}(net_t^{l-1})
atl−1=fl−1(nettl−1)上式中
n
e
t
l
net^l
netl是第
l
l
l层神经元的加权输入(假设第
l
l
l层是循环层);
n
e
t
l
−
1
net^{l-1}
netl−1是第
l
−
1
l-1
l−1层神经元的加权输入;
a
t
l
−
1
a_t^{l-1}
atl−1是第
l
−
1
l-1
l−1层神经元的输出;
f
l
−
1
f^{l-1}
fl−1是第
l
−
1
l-1
l−1层的激活函数。
∂
n
e
t
t
l
∂
n
e
t
t
l
−
1
=
∂
n
e
t
t
l
∂
a
t
l
−
1
∂
a
t
l
−
1
∂
n
e
t
t
l
−
1
\frac{\partial net_t^l}{\partial net_t^{l-1}}=\frac{\partial net_t^l}{\partial a_t^{l-1}}\frac{\partial a_t^{l-1}}{\partial net_t^{l-1}}
∂nettl−1∂nettl=∂atl−1∂nettl∂nettl−1∂atl−1
=
U
d
i
a
g
[
f
′
l
−
1
(
n
e
t
t
l
−
1
)
]
\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=Udiag[f^{'l-1}(net_t^{l-1})]
=Udiag[f′l−1(nettl−1)]所以,
(
δ
t
l
−
1
)
=
∂
E
∂
n
e
t
t
l
−
1
(\delta_t^{l-1})=\frac{\partial E}{\partial net_t^{l-1}}
(δtl−1)=∂nettl−1∂E
=
∂
E
∂
n
e
t
t
l
∂
n
e
t
t
l
∂
n
e
t
t
l
−
1
\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=\frac{\partial E}{\partial net_t^{l}}\frac{\partial net_t^{l}}{\partial net_t^{l-1}}
=∂nettl∂E∂nettl−1∂nettl
=
(
δ
t
l
)
T
U
d
i
a
g
[
f
′
l
−
1
(
n
e
t
t
l
−
1
)
]
(
式
4
)
\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=(\delta_t^{l})^TUdiag[f^{'l-1}(net_t^{l-1})]\space\space\space\space(式4)
=(δtl)TUdiag[f′l−1(nettl−1)] (式4)(式4)就是将误差项传递到上一层算法。
权重梯度的计算
现在,我们终于来到了BPTT算法的最后一步:计算每个权重的梯度。
首先,我们计算误差函数
E
E
E对权重矩阵
W
W
W的梯度
∂
E
∂
W
\frac{\partial E}{\partial W}
∂W∂E。
上图展示了我们到目前为止,在前两步中已经计算得到的量,包括每个时刻
t
t
t循环层的输出值
s
t
s_t
st,以及误差项
δ
t
\delta_t
δt。
在《深度学习之神经网络和反向传播算法》中介绍的全连接网络的权重梯度计算算法:只要知道了任意一个时刻的误差项
δ
t
\delta_t
δt,以及上一个时刻循环层的输出值
s
t
−
1
s_{t-1}
st−1,就可以按照下面的公式求出权重矩阵在
t
t
t时刻的梯度
∇
W
t
E
\nabla_{W_t}E
∇WtE:
∇
W
t
E
=
[
δ
1
t
s
1
t
−
1
δ
1
t
s
2
t
−
1
.
.
.
δ
1
t
s
n
t
−
1
δ
2
t
s
1
t
−
1
δ
2
t
s
2
t
−
1
.
.
.
δ
2
t
s
n
t
−
1
.
.
δ
n
t
s
1
t
−
1
δ
n
t
s
2
t
−
1
.
.
.
δ
n
t
s
n
t
−
1
]
(
式
5
)
\nabla_{W_t}E= \begin{bmatrix} \delta_1^t s_1^{t-1} \space\space \delta_1^t s_2^{t-1} \space\space .\space.\space. \space\space \delta_1^t s_n^{t-1} \\ \\ \delta_2^t s_1^{t-1} \space\space \delta_2^t s_2^{t-1} \space\space .\space.\space. \space\space \delta_2^t s_n^{t-1} \\ . \\.\\ \delta_n^t s_1^{t-1} \space\space \delta_n^t s_2^{t-1} \space\space .\space.\space. \space\space \delta_n^t s_n^{t-1} \end{bmatrix} \space\space\space\space\space\space(式5)
∇WtE=⎣⎢⎢⎢⎢⎢⎢⎡δ1ts1t−1 δ1ts2t−1 . . . δ1tsnt−1δ2ts1t−1 δ2ts2t−1 . . . δ2tsnt−1..δnts1t−1 δnts2t−1 . . . δntsnt−1⎦⎥⎥⎥⎥⎥⎥⎤ (式5)在(式5)中,
δ
i
t
\delta_i^t
δit表示t时刻误差项向量的第
i
i
i个分量;
s
i
t
−
1
s_i^{t-1}
sit−1表示
t
−
1
t-1
t−1时刻循环层第
i
i
i个神经元的输出值。
我们下面可以简单推导一下(式5)。
n
e
t
t
=
U
x
t
+
W
s
t
−
1
net_t=Ux_t+Ws_{t-1}
nett=Uxt+Wst−1
[
n
e
t
1
t
n
e
t
2
t
.
.
n
e
t
n
t
]
=
U
x
t
+
[
w
11
w
12
.
.
.
w
1
n
w
21
w
22
.
.
.
w
2
n
.
.
w
n
1
w
n
2
.
.
.
w
n
n
]
[
s
1
t
−
1
s
2
t
−
1
.
.
s
n
t
−
1
]
\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space \begin{bmatrix} net_1^t \\net_2^t\\.\\. \\net_n^t \end{bmatrix}=Ux_t+ \begin{bmatrix}w_{11} \space\space w_{12} \space\space ... \space\space w_{1n} \\ w_{21} \space\space w_{22} \space\space ... \space\space w_{2n} \\ . \\. \\ w_{n1} \space\space w_{n2} \space\space ... \space\space w_{nn} \end{bmatrix}\begin{bmatrix} s_1^{t-1} \\s_2^{t-1}\\.\\. \\s_n^{t-1} \end{bmatrix}
⎣⎢⎢⎢⎢⎡net1tnet2t..netnt⎦⎥⎥⎥⎥⎤=Uxt+⎣⎢⎢⎢⎢⎡w11 w12 ... w1nw21 w22 ... w2n..wn1 wn2 ... wnn⎦⎥⎥⎥⎥⎤⎣⎢⎢⎢⎢⎡s1t−1s2t−1..snt−1⎦⎥⎥⎥⎥⎤
=
U
x
t
+
[
w
11
s
1
t
−
1
+
w
12
s
2
t
−
1
+
.
.
.
+
w
1
n
s
n
t
−
1
w
21
s
1
t
−
1
+
w
22
s
2
t
−
1
+
.
.
.
+
w
2
n
s
n
t
−
1
.
.
w
n
1
s
1
t
−
1
+
w
n
2
s
2
t
−
1
+
.
.
.
+
w
n
n
s
n
t
−
1
]
\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space =Ux_t+ \begin{bmatrix} w_{11}s_1^{t-1}+w_{12}s_2^{t-1} +...+w_{1n}s_n^{t-1} \\ w_{21}s_1^{t-1}+w_{22}s_2^{t-1} +...+w_{2n}s_n^{t-1} \\.\\.\\ w_{n1}s_1^{t-1}+w_{n2}s_2^{t-1} +...+w_{nn}s_n^{t-1} \end{bmatrix}
=Uxt+⎣⎢⎢⎢⎢⎡w11s1t−1+w12s2t−1+...+w1nsnt−1w21s1t−1+w22s2t−1+...+w2nsnt−1..wn1s1t−1+wn2s2t−1+...+wnnsnt−1⎦⎥⎥⎥⎥⎤因为对
W
W
W求导与
U
x
t
Ux_t
Uxt无关,我们不再考虑。现在,我们考虑对权重项
w
j
i
w_{ji}
wji求导。通过观察上式我们可以看到
w
j
i
w_{ji}
wji只与
n
e
t
j
t
net_{j}^t
netjt有关,所以:
∂
E
∂
w
j
i
=
∂
E
∂
n
e
t
j
t
∂
n
e
t
j
t
∂
w
j
i
\frac{\partial E}{\partial w_{ji}}=\frac{\partial E}{\partial net_{j}^t}\frac{\partial net_{j}^t}{\partial w_{ji}}
∂wji∂E=∂netjt∂E∂wji∂netjt
=
δ
j
t
s
i
t
−
1
=\delta_j^ts_i^{t-1}
=δjtsit−1按照上面的规律就可以生成(式5)里面的矩阵。
我们已经求得了权重矩阵
W
W
W在
t
t
t时刻的梯度
∇
W
t
E
\nabla_{W_t}E
∇WtE,最终的梯度
∇
W
E
\nabla_{W}E
∇WE是各个时刻的梯度之和:
∇
W
E
=
∑
i
=
1
t
∇
W
t
E
\nabla_{W}E=\sum_{i=1}^t\nabla_{W_t}E\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space
∇WE=i=1∑t∇WtE
=
[
δ
1
t
s
1
t
−
1
δ
1
t
s
2
t
−
1
.
.
.
δ
1
t
s
n
t
−
1
δ
2
t
s
1
t
−
1
δ
2
t
s
2
t
−
1
.
.
.
δ
2
t
s
n
t
−
1
.
.
δ
n
t
s
1
t
−
1
δ
n
t
s
2
t
−
1
.
.
.
δ
n
t
s
n
t
−
1
]
+
.
.
.
+
[
δ
1
1
s
1
0
δ
1
1
s
2
0
.
.
.
δ
1
1
s
n
0
δ
2
1
s
1
0
δ
2
1
s
2
0
.
.
.
δ
2
1
s
n
0
.
.
δ
n
1
s
1
0
δ
n
1
s
2
0
.
.
.
δ
n
1
s
n
0
]
(
式
6
)
=\begin{bmatrix} \delta_1^t s_1^{t-1} \space\space \delta_1^t s_2^{t-1} \space\space .\space.\space. \space\space \delta_1^t s_n^{t-1} \\ \\ \delta_2^t s_1^{t-1} \space\space \delta_2^t s_2^{t-1} \space\space .\space.\space. \space\space \delta_2^t s_n^{t-1} \\ . \\.\\ \delta_n^t s_1^{t-1} \space\space \delta_n^t s_2^{t-1} \space\space .\space.\space. \space\space \delta_n^t s_n^{t-1} \end{bmatrix}+...+\begin{bmatrix} \delta_1^1 s_1^{0} \space\space \delta_1^1 s_2^{0} \space\space .\space.\space. \space\space \delta_1^1 s_n^{0} \\ \\ \delta_2^1 s_1^{0} \space\space \delta_2^1 s_2^{0} \space\space .\space.\space. \space\space \delta_2^1 s_n^{0} \\ . \\.\\ \delta_n^1 s_1^{0} \space\space \delta_n^1 s_2^{0} \space\space .\space.\space. \space\space \delta_n^1 s_n^{0} \end{bmatrix}\space\space\space\space\space\space\space\space(式6)
=⎣⎢⎢⎢⎢⎢⎢⎡δ1ts1t−1 δ1ts2t−1 . . . δ1tsnt−1δ2ts1t−1 δ2ts2t−1 . . . δ2tsnt−1..δnts1t−1 δnts2t−1 . . . δntsnt−1⎦⎥⎥⎥⎥⎥⎥⎤+...+⎣⎢⎢⎢⎢⎢⎢⎡δ11s10 δ11s20 . . . δ11sn0δ21s10 δ21s20 . . . δ21sn0..δn1s10 δn1s20 . . . δn1sn0⎦⎥⎥⎥⎥⎥⎥⎤ (式6)(式6)就是计算循环层权重矩阵W的梯度的公式。
----------数学公式超高能预警----------
前面已经介绍了 ∇ W E \nabla_{W}E ∇WE的计算方法,看上去还是比较直观的。然而,读者也许会困惑,为什么最终的梯度是各个时刻的梯度之和呢?我们前面只是直接用了这个结论,实际上这里面是有道理的,只是这个数学推导比较绕脑子。感兴趣的同学可以仔细阅读接下来这一段,它用到了矩阵对矩阵求导、张量与向量相乘运算的一些法则。
我们还是从这个式子开始: n e t t = U x t + W f ( n e t t − 1 ) net_t=Ux_t+Wf(net_{t-1}) nett=Uxt+Wf(nett−1)因为 U x t Ux_t Uxt与 W W W完全无关,我们把它看做常量。现在,考虑第一个式子加号右边的部分,因为 W W W和 f ( n e t t − 1 ) f(net_{t-1}) f(nett−1)都是 W W W的函数,因此我们要用到大学里面都学过的导数乘法运算: ( u v ) ′ = u ′ v + u v ′ (uv)'=u'v+uv' (uv)′=u′v+uv′因此,上面第一个式子写成: ∂ n e t t ∂ W = ∂ W ∂ W f ( n e t t − 1 ) + W ∂ f ( n e t t − 1 ) ∂ W \frac{\partial net_t}{\partial W}=\frac{\partial W}{\partial W}f(net_{t-1})+W\frac{\partial f(net_{t-1})}{\partial W} ∂W∂nett=∂W∂Wf(nett−1)+W∂W∂f(nett−1)我们最终需要计算的是 ∇ W E \nabla_{W}E ∇WE: ∇ W E = ∂ E ∂ W \nabla_{W}E=\frac{\partial E}{\partial W}\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space ∇WE=∂W∂E = ∂ E ∂ n e t t ∂ n e t t ∂ W =\frac{\partial E}{\partial net_{t}}\frac{\partial net_{t}}{\partial W}\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space =∂nett∂E∂W∂nett = δ t T ∂ W ∂ W f ( n e t t − 1 ) + δ t T W ∂ f ( n e t t − 1 ) ∂ W ( 式 7 ) \space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=\delta_t^T\frac{\partial W}{\partial W}f(net_{t-1})+\delta_t^TW\frac{\partial f(net_{t-1})}{\partial W}\space\space\space\space(式7) =δtT∂W∂Wf(nett−1)+δtTW∂W∂f(nett−1) (式7)我们先计算(式7)加号左边的部分。 ∂ W ∂ W \frac{\partial W}{\partial W} ∂W∂W是矩阵对矩阵求导,其结果是一个四维张量(tensor),如下所示: ∂ W ∂ W = [ ∂ w 11 ∂ W ∂ w 12 ∂ W . . . ∂ w 1 n ∂ W ∂ w 21 ∂ W ∂ w 22 ∂ W . . . ∂ w 2 n ∂ W . . ∂ w n 1 ∂ W ∂ w n 2 ∂ W . . . ∂ w n n ∂ W ] \frac{\partial W}{\partial W}=\begin{bmatrix} \frac{\partial w_{11}}{\partial W} \frac{\partial w_{12}}{\partial W} ... \frac{\partial w_{1n}}{\partial W}\\ \\ \frac{\partial w_{21}}{\partial W} \frac{\partial w_{22}}{\partial W} ... \frac{\partial w_{2n}}{\partial W} \\.\\.\\ \frac{\partial w_{n1}}{\partial W} \frac{\partial w_{n2}}{\partial W} ... \frac{\partial w_{nn}}{\partial W} \end{bmatrix}\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space ∂W∂W=⎣⎢⎢⎢⎢⎢⎢⎡∂W∂w11∂W∂w12...∂W∂w1n∂W∂w21∂W∂w22...∂W∂w2n..∂W∂wn1∂W∂wn2...∂W∂wnn⎦⎥⎥⎥⎥⎥⎥⎤ = [ [ ∂ w 11 ∂ w 11 ∂ w 11 ∂ w 12 . . . ∂ w 11 ∂ w 1 n ∂ w 11 ∂ w 21 ∂ w 11 ∂ w 22 . . . ∂ w 11 ∂ w 2 n . . ∂ w 11 ∂ w n 1 ∂ w 11 ∂ w n 2 . . . ∂ w 11 ∂ w n n ] [ ∂ w 12 ∂ w 11 ∂ w 12 ∂ w 12 . . . ∂ w 12 ∂ w 1 n ∂ w 12 ∂ w 21 ∂ w 12 ∂ w 22 . . . ∂ w 12 ∂ w 2 n . . ∂ w 12 ∂ w n 1 ∂ w 12 ∂ w n 2 . . . ∂ w 12 ∂ w n n ] . . . . . ] \space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=\begin{bmatrix} \begin{bmatrix} \frac{\partial w_{11}}{\partial w_{11}} \frac{\partial w_{11}}{\partial w_{12}} ... \frac{\partial w_{11}}{\partial w_{1n}}\\ \\ \frac{\partial w_{11}}{\partial w_{21}} \frac{\partial w_{11}}{\partial w_{22}} ... \frac{\partial w_{11}}{\partial w_{2n}} \\.\\.\\ \frac{\partial w_{11}}{\partial w_{n1}} \frac{\partial w_{11}}{\partial w_{n2}} ... \frac{\partial w_{11}}{\partial w_{nn}} \end{bmatrix} \begin{bmatrix} \frac{\partial w_{12}}{\partial w_{11}} \frac{\partial w_{12}}{\partial w_{12}} ... \frac{\partial w_{12}}{\partial w_{1n}}\\ \\ \frac{\partial w_{12}}{\partial w_{21}} \frac{\partial w_{12}}{\partial w_{22}} ... \frac{\partial w_{12}}{\partial w_{2n}} \\.\\.\\ \frac{\partial w_{12}}{\partial w_{n1}} \frac{\partial w_{12}}{\partial w_{n2}} ... \frac{\partial w_{12}}{\partial w_{nn}} \end{bmatrix} ... \\.\\. \end{bmatrix} =⎣⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎡⎣⎢⎢⎢⎢⎢⎢⎡∂w11∂w11∂w12∂w11...∂w1n∂w11∂w21∂w11∂w22∂w11...∂w2n∂w11..∂wn1∂w11∂wn2∂w11...∂wnn∂w11⎦⎥⎥⎥⎥⎥⎥⎤⎣⎢⎢⎢⎢⎢⎢⎡∂w11∂w12∂w12∂w12...∂w1n∂w12∂w21∂w12∂w22∂w12...∂w2n∂w12..∂wn1∂w12∂wn2∂w12...∂wnn∂w12⎦⎥⎥⎥⎥⎥⎥⎤.....⎦⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎤ = [ [ 1 0 . . . 0 0 0 . . . 0 . . 0 0 . . . 0 ] [ 0 1 . . . 0 0 0 . . . 0 . . 0 0 . . . 0 ] . . . . . ] \space\space\space\space=\begin{bmatrix} \begin{bmatrix} 1\space0\space...\space0 \\ 0\space0\space...\space0\\.\\. \\ 0\space0\space...\space0 \end{bmatrix} \begin{bmatrix} 0\space1\space...\space0 \\ 0\space0\space...\space0\\.\\. \\ 0\space0\space...\space0 \end{bmatrix} ... \\ .\\ . \end{bmatrix} =⎣⎢⎢⎢⎢⎢⎢⎢⎢⎡⎣⎢⎢⎢⎢⎡1 0 ... 00 0 ... 0..0 0 ... 0⎦⎥⎥⎥⎥⎤⎣⎢⎢⎢⎢⎡0 1 ... 00 0 ... 0..0 0 ... 0⎦⎥⎥⎥⎥⎤.....⎦⎥⎥⎥⎥⎥⎥⎥⎥⎤接下来,我们知道 s t − 1 = f ( n e t t 1 ) s_{t-1}=f(net_{t_1}) st−1=f(nett1),它是一个列向量。我们让上面的四维张量与这个向量相乘,得到了一个三维张量,再左乘行向量 δ t T \delta_t^T δtT,最终得到一个矩阵: δ t T ∂ W ∂ W f ( n e t t − 1 ) = δ t T ∂ W ∂ W s t − 1 \delta_t^T\frac{\partial W}{\partial W}f(net_{t-1})=\delta_t^T\frac{\partial W}{\partial W}s_{t-1}\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space δtT∂W∂Wf(nett−1)=δtT∂W∂Wst−1 = δ t T [ [ 1 0 . . . 0 0 0 . . . 0 . . 0 0 . . . 0 ] [ 0 1 . . . 0 0 0 . . . 0 . . 0 0 . . . 0 ] . . . . . ] [ s 1 t − 1 s 2 t − 1 . . s n t − 1 ] \space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=\delta_t^T\begin{bmatrix} \begin{bmatrix} 1\space0\space...\space0 \\ 0\space0\space...\space0\\.\\. \\ 0\space0\space...\space0 \end{bmatrix} \begin{bmatrix} 0\space1\space...\space0 \\ 0\space0\space...\space0\\.\\. \\ 0\space0\space...\space0 \end{bmatrix} ... \\ .\\ . \end{bmatrix}\begin{bmatrix} s_1^{t-1} \\s_2^{t-1}\\.\\. \\s_n^{t-1} \end{bmatrix} =δtT⎣⎢⎢⎢⎢⎢⎢⎢⎢⎡⎣⎢⎢⎢⎢⎡1 0 ... 00 0 ... 0..0 0 ... 0⎦⎥⎥⎥⎥⎤⎣⎢⎢⎢⎢⎡0 1 ... 00 0 ... 0..0 0 ... 0⎦⎥⎥⎥⎥⎤.....⎦⎥⎥⎥⎥⎥⎥⎥⎥⎤⎣⎢⎢⎢⎢⎡s1t−1s2t−1..snt−1⎦⎥⎥⎥⎥⎤ = δ t T [ [ s 1 t − 1 0 . . 0 ] [ s 2 t − 1 0 . . 0 ] . . . . . ] \space\space\space\space\space\space\space\space\space\space=\delta_t^T\begin{bmatrix} \begin{bmatrix} s_1^{t-1} \\ 0\\.\\.\\0 \end{bmatrix} \begin{bmatrix} s_2^{t-1} \\ 0\\.\\.\\0 \end{bmatrix} ...\\.\\. \end{bmatrix} =δtT⎣⎢⎢⎢⎢⎢⎢⎢⎢⎡⎣⎢⎢⎢⎢⎡s1t−10..0⎦⎥⎥⎥⎥⎤⎣⎢⎢⎢⎢⎡s2t−10..0⎦⎥⎥⎥⎥⎤.....⎦⎥⎥⎥⎥⎥⎥⎥⎥⎤ = [ δ 1 t δ 2 t . . . δ n t ] [ [ s 1 t − 1 0 . . 0 ] [ s 2 t − 1 0 . . 0 ] . . . . . ] \space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space= \begin{bmatrix} \delta_1^t \space\space \delta_2^t \space...\space \delta_n^t \end{bmatrix}\begin{bmatrix} \begin{bmatrix} s_1^{t-1} \\ 0\\.\\.\\0 \end{bmatrix} \begin{bmatrix} s_2^{t-1} \\ 0\\.\\.\\0 \end{bmatrix} ...\\.\\. \end{bmatrix} =[δ1t δ2t ... δnt]⎣⎢⎢⎢⎢⎢⎢⎢⎢⎡⎣⎢⎢⎢⎢⎡s1t−10..0⎦⎥⎥⎥⎥⎤⎣⎢⎢⎢⎢⎡s2t−10..0⎦⎥⎥⎥⎥⎤.....⎦⎥⎥⎥⎥⎥⎥⎥⎥⎤ = [ δ 1 t s 1 t − 1 δ 1 t s 2 t − 1 . . . δ 1 t s n t − 1 δ 2 t s 1 t − 1 δ 2 t s 2 t − 1 . . . δ 2 t s n t − 1 . . δ n t s 1 t − 1 δ n t s 2 t − 1 . . . δ n t s n t − 1 ] \space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=\begin{bmatrix} \delta_1^t s_1^{t-1} \space\space \delta_1^t s_2^{t-1} \space\space .\space.\space. \space\space \delta_1^t s_n^{t-1} \\ \\ \delta_2^t s_1^{t-1} \space\space \delta_2^t s_2^{t-1} \space\space .\space.\space. \space\space \delta_2^t s_n^{t-1} \\ . \\.\\ \delta_n^t s_1^{t-1} \space\space \delta_n^t s_2^{t-1} \space\space .\space.\space. \space\space \delta_n^t s_n^{t-1} \end{bmatrix} =⎣⎢⎢⎢⎢⎢⎢⎡δ1ts1t−1 δ1ts2t−1 . . . δ1tsnt−1δ2ts1t−1 δ2ts2t−1 . . . δ2tsnt−1..δnts1t−1 δnts2t−1 . . . δntsnt−1⎦⎥⎥⎥⎥⎥⎥⎤ = ∇ W t E =\nabla_{W_t}E\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space =∇WtE 接下来,我们计算(式7)加号右边的部分: δ t T W ∂ f ( n e t t − 1 ) ∂ W = δ t T W ∂ f ( n e t t − 1 ) ∂ n e t t − 1 ∂ n e t t − 1 ∂ W \delta_t^TW\frac{\partial f(net_{t-1})}{\partial W}=\delta_t^TW\frac{\partial f(net_{t-1})}{\partial net_{t-1}}\frac{\partial net_{t-1}}{\partial W} δtTW∂W∂f(nett−1)=δtTW∂nett−1∂f(nett−1)∂W∂nett−1 = δ t T W f ′ ( n e t t − 1 ) ∂ n e t t − 1 ∂ W \space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=\delta_t^TWf'(net_{t-1})\frac{\partial net_{t-1}}{\partial W} =δtTWf′(nett−1)∂W∂nett−1 = δ t T ∂ n e t t ∂ n e t t − 1 ∂ n e t t − 1 ∂ W \space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=\delta_t^T\frac{\partial net_{t}}{\partial net_{t-1}}\frac{\partial net_{t-1}}{\partial W} =δtT∂nett−1∂nett∂W∂nett−1 = δ t − 1 T ∂ n e t t − 1 ∂ W \space\space\space\space\space\space\space\space=\delta_{t-1}^T\frac{\partial net_{t-1}}{\partial W} =δt−1T∂W∂nett−1于是,我们得到了如下递推公式: ∇ W E = ∂ E ∂ W \nabla_{W}E=\frac{\partial E}{\partial W} ∇WE=∂W∂E = ∂ E ∂ n e t t ∂ n e t t ∂ W \space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=\frac{\partial E}{\partial net_{t}}\frac{\partial net_{t}}{\partial W} =∂nett∂E∂W∂nett = ∇ W t E + δ t − 1 T ∂ n e t t − 1 ∂ W \space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=\nabla_{W_t}E+\delta_{t-1}^T\frac{\partial net_{t-1}}{\partial W} =∇WtE+δt−1T∂W∂nett−1 = ∇ W t E + ∇ W t − 1 E + δ t − 2 T ∂ n e t t − 2 ∂ W \space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=\nabla_{W_t}E+\nabla_{W_{t-1}}E+\delta_{t-2}^T\frac{\partial net_{t-2}}{\partial W} =∇WtE+∇Wt−1E+δt−2T∂W∂nett−2 = ∇ W t E + ∇ W t − 1 E + . . . + ∇ W 1 E \space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=\nabla_{W_t}E+\nabla_{W_{t-1}}E+...+\nabla_{W_1}E =∇WtE+∇Wt−1E+...+∇W1E = ∑ i = 1 t ∇ W t E \space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=\sum_{i=1}^t\nabla_{W_t}E =i=1∑t∇WtE这样,我们就证明了:最终的梯度 ∇ W E \nabla_{W}E ∇WE是各个时刻的梯度之和。
----------数学公式超高能预警解除----------
同权重矩阵 W W W类似,我们可以得到权重矩阵 U U U的计算方法。 ∇ U t E = [ δ 1 t x 1 t δ 1 t x 2 t . . . δ 1 t x m t δ 2 t x 1 t δ 2 t x 2 t . . . δ 2 t x m t . . δ n t x 1 t δ n t x 2 t . . . δ n t x m t ] ( 式 8 ) \nabla_{U_t}E= \begin{bmatrix} \delta_1^tx_1^t \space\space \delta_1^tx_2^t\space\space...\space\space\delta_1^tx_m^t \\ \delta_2^tx_1^t \space\space \delta_2^tx_2^t\space\space...\space\space\delta_2^tx_m^t \\.\\.\\ \delta_n^tx_1^t \space\space \delta_n^tx_2^t\space\space...\space\space\delta_n^tx_m^t \end{bmatrix}\space\space\space\space\space(式8) ∇UtE=⎣⎢⎢⎢⎢⎡δ1tx1t δ1tx2t ... δ1txmtδ2tx1t δ2tx2t ... δ2txmt..δntx1t δntx2t ... δntxmt⎦⎥⎥⎥⎥⎤ (式8)(式8)是误差函数在t时刻对权重矩阵 U U U的梯度。和权重矩阵 W W W一样,最终的梯度也是各个时刻的梯度之和: ∇ U E = ∑ i = 1 t ∇ U t E \nabla_{U}E=\sum_{i=1}^t\nabla_{U_t}E ∇UE=i=1∑t∇UtE具体的证明这里就不再赘述了,感兴趣的读者可以练习推导一下。
2、RNN的梯度爆炸和消失问题
不幸的是,实践中前面介绍的几种RNNs并不能很好的处理较长的序列。一个主要的原因是,RNN在训练中很容易发生梯度爆炸和梯度消失,这导致训练时梯度不能在较长序列中一直传递下去,从而使RNN无法捕捉到长距离的影响。
为什么RNN会产生梯度爆炸和消失问题呢? 我们接下来将详细分析一下原因。我们根据(式3)可得:
δ
k
T
=
δ
t
T
∏
i
=
k
t
−
1
W
d
i
a
g
[
f
′
(
n
e
t
i
)
]
\delta_k^T=\delta_t^T\prod_{i=k}^{t-1}Wdiag[f'(net_i)]
δkT=δtTi=k∏t−1Wdiag[f′(neti)]
∥
δ
k
T
∥
⩽
∥
δ
t
T
∥
∏
i
=
k
t
−
1
∥
W
∥
∥
d
i
a
g
[
f
′
(
n
e
t
i
)
]
∥
\lVert\delta_k^T\rVert\leqslant\lVert\delta_t^T\rVert\prod_{i=k}^{t-1}\lVert W \rVert\lVert diag[f'(net_i)]\rVert
∥δkT∥⩽∥δtT∥i=k∏t−1∥W∥∥diag[f′(neti)]∥
⩽
∥
δ
t
T
∥
(
β
W
β
f
)
t
−
k
\leqslant\lVert\delta_t^T\rVert(\beta_W\beta_f)^{t-k}
⩽∥δtT∥(βWβf)t−k上式的
β
\beta
β定义为矩阵的模的上界。因为上式是一个指数函数,如果
t
−
k
t-k
t−k很大的话(也就是向前看很远的时候),会导致对应的误差项的值增长或缩小的非常快,这样就会导致相应的梯度爆炸和梯度消失问题(取决于大于1还是小于1)。
通常来说,梯度爆炸更容易处理一些。因为梯度爆炸的时候,我们的程序会收到NaN错误。我们也可以设置一个梯度阈值,当梯度超过这个阈值的时候可以直接截取。
梯度消失更难检测,而且也更难处理一些。总的来说,我们有三种方法应对梯度消失问题:
- 合理的初始化权重值。初始化权重,使每个神经元尽可能不要取极大或极小值,以躲开梯度消失的区域。
- 使用relu代替sigmoid和tanh作为激活函数。
- 使用其他结构的RNNs,比如长短时记忆网络(LTSM)和Gated Recurrent Unit(GRU),这是最流行的做法。我们将在以后的文章中介绍这两种网络。
四、RNN的应用举例——基于RNN的语言模型
现在,我们介绍一下基于RNN语言模型。我们首先把词依次输入到循环神经网络中,每输入一个词,循环神经网络就输出截止到目前为止,下一个最可能的词。例如,当我们依次输入:
我 昨天 上学 迟到 了
神经网络的输出如下图所示:
其中,s和e是两个特殊的词,分别表示一个序列的开始和结束。
1、向量化
我们知道,神经网络的输入和输出都是向量,为了让语言模型能够被神经网络处理,我们必须把词表达为向量的形式,这样神经网络才能处理它。
神经网络的输入是词,我们可以用下面的步骤对输入进行向量化:
- 建立一个包含所有词的词典,每个词在词典里面有一个唯一的编号。
- 任意一个词都可以用一个 N N N维的one-hot向量来表示。其中, N N N是词典中包含的词的个数。假设一个词在词典中的编号是 i i i, v v v是表示这个词的向量, v j v_j vj是向量的第 j j j个元素,则: v j = { 1 if j = i 0 if j ≠ i v_j = \begin{cases} 1 &\text{if } j=i \\ 0 &\text{if } j \not = i \end{cases} vj={10if j=iif j=i
上面这个公式的含义,可以用下面的图来直观的表示:
使用这种向量化方法,我们就得到了一个高维、稀疏的向量(稀疏是指绝大部分元素的值都是0)。处理这样的向量会导致我们的神经网络有很多的参数,带来庞大的计算量。因此,往往会需要使用一些降维方法,将高维的稀疏向量转变为低维的稠密向量。不过这个话题我们就不再这篇文章中讨论了。
语言模型要求的输出是下一个最可能的词,我们可以让循环神经网络计算计算词典中每个词是下一个词的概率,这样,概率最大的词就是下一个最可能的词。因此,神经网络的输出向量也是一个N维向量,向量中的每个元素对应着词典中相应的词是下一个词的概率。如下图所示:
2、Softmax层
前面提到,语言模型是对下一个词出现的概率进行建模。那么,怎样让神经网络输出概率呢?方法就是用softmax层作为神经网络的输出层。
我们先来看一下softmax函数的定义:
g
(
z
i
)
=
e
z
i
∑
k
e
z
k
g(z_i)=\frac{e^{z_i}}{\sum_ke^{z_k}}
g(zi)=∑kezkezi这个公式看起来可能很晕,我们举一个例子。Softmax层如下图所示:
从上图我们可以看到,softmax layer的输入是一个向量,输出也是一个向量,两个向量的维度是一样的(在这个例子里面是4)。输入向量
x
=
[
1
2
3
4
]
x=[1\space\space 2\space\space 3 \space\space4]
x=[1 2 3 4]经过softmax层之后,经过上面的softmax函数计算,转变为输出向量
y
=
[
0.03
0.09
0.24
0.64
]
y=[0.03 \space\space0.09\space\space 0.24\space\space 0.64]
y=[0.03 0.09 0.24 0.64]。计算过程为:
y
1
=
e
x
i
∑
k
e
x
k
y_1=\frac{e^{x_i}}{\sum_ke^{x_k}}
y1=∑kexkexi
=
e
1
e
1
+
e
2
+
e
3
+
e
4
=\frac{e^1}{e^1+e^2+e^3+e^4}
=e1+e2+e3+e4e1
=
0.03
=0.03
=0.03
y
2
=
e
2
e
1
+
e
2
+
e
3
+
e
4
y_2=\frac{e^2}{e^1+e^2+e^3+e^4}
y2=e1+e2+e3+e4e2
=
0.09
=0.09
=0.09
y
3
=
e
3
e
1
+
e
2
+
e
3
+
e
4
y_3=\frac{e^3}{e^1+e^2+e^3+e^4}
y3=e1+e2+e3+e4e3
=
0.24
=0.24
=0.24
y
4
=
e
4
e
1
+
e
2
+
e
3
+
e
4
y_4=\frac{e^4}{e^1+e^2+e^3+e^4}
y4=e1+e2+e3+e4e4
=
0.64
=0.64
=0.64我们来看看输出向量
y
y
y的特征:
- 每一项为取值为0-1之间的正数;
- 所有项的总和是1。
我们不难发现,这些特征和概率的特征是一样的,因此我们可以把它们看做是概率。对于语言模型来说,我们可以认为模型预测下一个词是词典中第一个词的概率是0.03,是词典中第二个词的概率是0.09,以此类推。
3、语言模型的训练
可以使用监督学习的方法对语言模型进行训练,首先,需要准备训练数据集。接下来,我们介绍怎样把语料
我 昨天 上学 迟到 了
转换成语言模型的训练数据集。
首先,我们获取输入-标签对:
输入 | 标签 |
---|---|
s | 我 |
我 | 昨天 |
昨天 | 上学 |
上学 | 迟到 |
迟到 | 了 |
了 | e |
然后,使用前面介绍过的向量化方法,对输入 x x x和标签 y y y进行向量化。这里面有意思的是,对标签 y y y进行向量化,其结果也是一个one-hot向量。例如,我们对标签『我』进行向量化,得到的向量中,只有第2019个元素的值是1,其他位置的元素的值都是0。它的含义就是下一个词是『我』的概率是1,是其它词的概率都是0。
最后,我们使用交叉熵误差函数作为优化目标,对模型进行优化。
在实际工程中,我们可以使用大量的语料来对模型进行训练,获取训练数据和训练的方法都是相同的。
4、交叉熵误差
一般来说,当神经网络的输出层是softmax层时,对应的误差函数E通常选择交叉熵误差函数,其定义如下: L ( y , o ) = − 1 N ∑ n ∈ N y n l o g o n L(y,o)=-\frac{1}{N}\sum_{n\in N}y_nlogo_n L(y,o)=−N1n∈N∑ynlogon在上式中, N N N是训练样本的个数,向量 y n y_n yn是样本的标记,向量 o n o_n on是网络的输出。标记 y n y_n yn是一个one-hot向量,例如 y 1 = [ 1 , 0 , 0 , 0 ] y_1=[1,0,0,0] y1=[1,0,0,0],如果网络的输出 o = [ 0.03 , 0.09 , 0.24 , 0.64 ] o=[0.03,0.09,0.24,0.64] o=[0.03,0.09,0.24,0.64],那么,交叉熵误差是(假设只有一个训练样本,即 N = 1 N=1 N=1): L = − 1 N ∑ n ∈ N y n l o g o n L=-\frac{1}{N}\sum_{n\in N}y_nlogo_n L=−N1n∈N∑ynlogon = − y 1 l o g o 1 =-y_1logo_1 =−y1logo1 = − ( 1 ∗ l o g 0.03 + 0 ∗ l o g 0.09 + 0 ∗ l o g 0.24 + 0 ∗ l o g 0.64 ) =-(1*log0.03+0*log0.09+0*log0.24+0*log0.64) =−(1∗log0.03+0∗log0.09+0∗log0.24+0∗log0.64) = 3.51 =3.51 =3.51我们当然可以选择其他函数作为我们的误差函数,比如最小平方误差函数(MSE)。不过对概率进行建模时,选择交叉熵误差函数更make sense。