递归神经网络
一 递归神经网络原理
1.工作过程
(1)文本处理(训练)
(2)一个神经元,不同时刻
(3)每个时刻都有输出
2.正向传播
o
t
=
g
(
V
s
t
)
s
t
=
f
(
U
x
t
+
W
s
t
−
1
)
\begin{array}{l} \mathrm{o}_{t}=g\left(V \mathrm{s}_{t}\right) \\ \mathrm{s}_{t}=f\left(U \mathrm{x}_{t}+W \mathrm{s}_{t-1}\right) \end{array}
ot=g(Vst)st=f(Uxt+Wst−1)
其中:g=softmax,f=tanh。
o
t
=
g
(
V
s
t
)
=
V
f
(
U
x
t
+
W
s
t
−
1
)
=
V
f
(
U
x
t
+
W
f
(
U
x
t
−
1
+
W
s
t
−
2
)
)
=
V
f
(
U
x
t
+
W
f
(
U
x
t
−
1
+
W
f
(
U
x
t
−
2
+
W
s
t
−
3
)
)
)
=
V
f
(
U
x
t
+
W
f
(
U
x
t
−
1
+
W
f
(
U
x
t
−
2
+
W
f
(
U
x
t
−
3
+
…
)
)
)
)
\begin{aligned} \mathrm{o}_{t} &=g\left(V \mathrm{s}_{t}\right) \\ &=V f\left(U \mathrm{x}_{t}+W \mathrm{s}_{t-1}\right) \\ &=V f\left(U \mathrm{x}_{t}+W f\left(U \mathrm{x}_{t-1}+W \mathrm{s}_{t-2}\right)\right) \\ &=V f\left(U \mathrm{x}_{t}+W f\left(U \mathrm{x}_{t-1}+W f\left(U \mathrm{x}_{t-2}+W \mathrm{s}_{t-3}\right)\right)\right) \\ &=V f\left(U \mathrm{x}_{t}+W f\left(U \mathrm{x}_{t-1}+W f\left(U \mathrm{x}_{t-2}+W f\left(U \mathrm{x}_{t-3}+\ldots\right)\right)\right)\right) \end{aligned}
ot=g(Vst)=Vf(Uxt+Wst−1)=Vf(Uxt+Wf(Uxt−1+Wst−2))=Vf(Uxt+Wf(Uxt−1+Wf(Uxt−2+Wst−3)))=Vf(Uxt+Wf(Uxt−1+Wf(Uxt−2+Wf(Uxt−3+…))))
3.损失函数
E
t
(
y
t
,
y
^
t
)
=
−
y
t
log
y
^
t
E
(
y
,
y
^
)
=
∑
t
E
t
(
y
t
,
y
^
t
)
=
−
∑
t
y
t
log
y
^
t
\begin{aligned} E_{t}\left(y_{t}, \hat{y}_{t}\right) &=-y_{t} \log \hat{y}_{t} \\ E(y, \hat{y}) &=\sum_{t} E_{t}\left(y_{t}, \hat{y}_{t}\right) \\ &=-\sum_{t} y_{t} \log \hat{y}_{t} \end{aligned}
Et(yt,y^t)E(y,y^)=−ytlogy^t=t∑Et(yt,y^t)=−t∑ytlogy^t
4.反向传播
(1) 参数优化方法:同传统神经网络一样,梯度下降;
(2)计算损失函数对参数的导数;
(3)每个输出都对参数有影响。
(4)计算对w的导数:
∂
E
∂
W
=
∑
t
∂
E
t
∂
W
\frac{\partial E}{\partial W}=\sum_{t} \frac{\partial E_{t}}{\partial W}
∂W∂E=t∑∂W∂Et
E3由t0−t3时刻x,W共同确定,
Δ
s
3
=
∂
E
3
∂
s
3
\Delta s_{3}=\frac{\partial E_{3}}{\partial s_{3}}
Δs3=∂s3∂E3
t3时刻:
∂
E
3
∂
w
=
∂
s
3
∂
w
Δ
s
3
\frac{\partial E_{3}}{\partial w}=\frac{\partial s_{3}}{\partial w} \Delta s_{3}
∂w∂E3=∂w∂s3Δs3
t2时刻:
∂
E
3
∂
w
=
∂
s
3
∂
s
2
∂
s
2
∂
w
Δ
s
3
\frac{\partial E_{3}}{\partial w}=\frac{\partial s_{3}}{\partial s_{2}} \frac{\partial s_{2}}{\partial w} \Delta s_{3}
∂w∂E3=∂s2∂s3∂w∂s2Δs3
t1时刻:
∂
E
3
∂
w
=
∂
s
3
∂
s
2
∂
s
2
∂
s
1
∂
s
1
∂
w
Δ
s
3
\frac{\partial E_{3}}{\partial w}=\frac{\partial s_{3}}{\partial s_{2}} \frac{\partial s_{2}}{\partial s_{1}}\frac{\partial s_{1}}{\partial w} \Delta s_{3}
∂w∂E3=∂s2∂s3∂s1∂s2∂w∂s1Δs3
t0时刻:
∂
E
3
∂
w
=
∂
s
3
∂
s
2
∂
s
2
∂
s
1
∂
s
1
∂
s
0
∂
s
0
∂
w
Δ
s
3
\frac{\partial E_{3}}{\partial w}=\frac{\partial s_{3}}{\partial s_{2}} \frac{\partial s_{2}}{\partial s_{1}} \frac{\partial s_{1}}{\partial s_{0}} \frac{\partial s_{0}}{\partial w} \Delta s_{3}
∂w∂E3=∂s2∂s3∂s1∂s2∂s0∂s1∂w∂s0Δs3
∂
E
3
∂
w
=
∑
k
=
0
3
∂
s
3
∂
s
k
∂
s
k
∂
w
Δ
s
3
\frac{\partial E_{3}}{\partial w}=\sum_{k=0}^{3} \frac{\partial s_{3}}{\partial s_{k}} \frac{\partial s_{k}}{\partial w} \Delta s_{3}
∂w∂E3=k=0∑3∂sk∂s3∂w∂skΔs3
(5)计算对u的导数:同理有
∂
E
3
∂
U
=
∑
k
=
0
3
∂
s
3
∂
s
k
∂
s
k
∂
U
Δ
s
3
\frac{\partial E_{3}}{\partial U}=\sum_{k=0}^{3} \frac{\partial s_{3}}{\partial s_{k}} \frac{\partial s_{k}}{\partial U} \Delta s_{3}
∂U∂E3=k=0∑3∂sk∂s3∂U∂skΔs3
(5)多层网络
类比传统神经网络单层到多层的结构变化,额外添加上层前一状态。
(6)双层网络
输入信息正向、反向来输入RNN。这是由于信息的依赖关系顺序不是一定的。
(7)Vanishing Gradient 问题
注意到因为我们是用向量函数对向量求导数,结果是一个矩阵(称为Jacobian
Matrix),矩阵元素是每个点的导数。
影响:较长的记忆无法产生作用。
解决方式:非线性激励更换;LSTM长短记忆单元。
二 升级版RNN:LSTM
1.RNN局限
2.解决方案:设计Gate,保存重要记忆
3.LSTM生成
Ct是信息流控制的关键,参数决定了ht传递过程中,哪些被保存或者舍弃。该参数被Gate影响。
LSTM 有通过精心设计的称作为“门”的结构来去除或者增加信息到细胞状态的能力。门是一种让信息选择式通过的方法。他们包含一个 sigmoid 神经网络层和一个 pointwise 乘法操作。
4.逐步理解LSTM
(1)对旧的信息进行去除:
新输入
x
t
\mathcal{x}_{t}
xt的前一状态
h
t
−
1
\mathcal{h}_{t-1}
ht−1决定C的哪些信息可以舍弃;
f
t
\mathcal{f}_{t}
ft与
C
t
−
1
\mathcal{C}_{t-1}
Ct−1运算,对部分信息进行去除。
(2)确定什么样的新信息被保存:
(3)更新旧状态信息:
(4)确定输出的信息:
问题: Gate的作用在哪里?有用的信息如何保存?
Gate 输出it,ft,ot,用来引导Ct 的生成;
训练后,Ct 的相关参数为,这样便可以保存有用的信息。
5.LSTM变种
(1)Peephole connection
(2)Gate忘记/更新不再独立
(3)Gated Recurrent Unit (GRU)
遗忘、更新Gate做结合(既不是独立,又不是互补)
控制参数
C
t
C_t
Ct与输出ht结合,直接产生带有长短记忆能力的输出。
其中,
z
t
z_t
zt用于更新,
r
t
r_t
rt用于遗忘,
h
~
t
\tilde{h}_{t}
h~t为临时控制参数的变形。
三 自然语言处理特征提取:Word2Vect
1.建立字典
每个词生成one-hot向量。
如:Word个数为n,产生n维向量,第i个Word的向量为(0,0,…,1,0,0,0,…),1处于第i个位置。
2.训练数据集构建
通过原文本,建立一些词与词之间的词对,这些词对作为后面要继续训练的样本。
3.简单神经网络
简易三层神经网络,各层神经元个数:N-m-N;
学习的是词语到临近词的映射。
4.最终生成vec
训练model进行特征提取;
每个one-hot对应一个300-d的向量;
生成最终的look up word table。
四 LSTM用于语言处理
word形式:Word2Vec;
训练过程:words->word;
LSTM网络只有最后输出有用。
训练目标:生成单词间条件概率