本文共 4056 字,大约阅读时间需要 13 分钟。
对于一个文本中出现的单词 (w_i) 的概率,他更多的依靠的是前 (n) 个单词,而不是这句话中前面所有的单词。具体表达式如下:
[ P\left(w_{1}, \ldots, w_{m}\right)=\prod_{i=1}^{i=m} P\left(w_{i} | w_{1}, \ldots, w_{i-1}\right) \approx \prod_{i=1}^{i=m} P\left(w_{i} | w_{i-n}, \ldots, w_{i-1}\right) ]
在翻译系统中,就是通过对输入的短语进行评分,选择概率最大的那个输出作为预测结果。
n-gram模型是其中最早的语言模型,其核心思想是利用连续单词的频率作为概率。具体表达式如下:
[ \begin{aligned} p\left(w_{2} | w_{1}\right) &= \frac{\operatorname{count}\left(w_{1}, w_{2}\right)}{\operatorname{count}\left(w_{1}\right)} \ p\left(w_{3} | w_{1}, w_{2}\right) &= \frac{\operatorname{count}\left(w_{1}, w_{2}, w_{3}\right)}{\operatorname{count}\left(w_{1}, w_{2}\right)} \end{aligned} ]
这个模型通过条件概率的形式预测下一个单词,但存在一些问题,例如分母分子为零的风险,以及信息稀疏性和存储需求的挑战。
传统的神经语言模型可以用下图表示:
蓝色部分表示输入处理,获取单词向量:
[ \boldsymbol{e}=\left[\boldsymbol{e}^{(1)} ; \boldsymbol{e}^{(2)} ; \boldsymbol{e}^{(3)} ; \boldsymbol{e}^{(4)}\right] ]
红色部分是中间的隐含层:
[ h=f\left(\boldsymbol{W} \boldsymbol{e}+\boldsymbol{b}_{1}\right) ]
最后通过一个 Softmax 层进行分类:
[ \hat{y}=\operatorname{softmax}\left(U h + b_{2}\right) ]
RNN的结构如图所示:
[ h_{t}=\sigma\left(W^{(h h)} h_{t-1} + W^{(h x)} x_{[t]}\right) ]
然后通过 Softmax 层进行分类:
[ \hat{y}{t}=\operatorname{softmax}\left(W^{(S)} h{t}\right) ]
RNN的核心优势在于其具有记忆性,可以处理序列数据。其损失函数通常采用交叉熵损失:
[ J^{(t)}(\theta)=-\sum_{j=1}^{|V|} y_{t, j} \times \log \left(\hat{y}_{t, j}\right) ]
如果语料库大小为 (T),则总损失函数为:
[ J=\frac{1}{T} \sum_{t=1}^{T} J^{(t)}(\theta)=-\frac{1}{T} \sum_{t=1}^{T} \sum_{j=1}^{|V|} y_{t, j} \times \log \left(\hat{y}_{t, j}\right) ]
RNN的另一个重要指标是“困惑度”:
[ Perplexity =2^{J} ]
优点:
缺点:
梯度消失是指在训练过程中,参数梯度的绝对值趋近于零,导致优化过程缓慢甚至无法收敛。具体原因在于:
[ \frac{\partial E}{\partial W}=\sum_{t=1}^{T} \sum_{k=1}^{t} \frac{\partial E_{t}}{\partial y_{t}} \frac{\partial y_{t}}{\partial h_{t}}\left(\prod_{j=k+1}^{t} \frac{\partial h_{j}}{\partial h_{j-1}}\right) \frac{\partial h_{k}}{\partial W} ]
由于链式法则中梯度传播涉及多个矩阵,其范数可能急剧衰减,导致梯度消失。
双向 RNN 结合了正向和逆向 RNN 的优势,能够更好地捕捉上下文信息。其结构如下:
[ \vec{h}{t}=f\left(\vec{W} x{t}+\vec{V} \vec{h}_{t-1}+\vec{b}\right) ]
[ \stackrel{\leftarrow}{h}{t}=f\left(\stackrel{\leftarrow}{W} x{t}+\stackrel{\leftarrow}{V} \stackrel{\leftarrow}h_{t+1}+\stackrel{\leftarrow}{b}\right) ]
最终预测结果结合正向和逆向的隐层信息:
[ \hat{y}{t}=g\left(U h{t}+c\right)=g\left(U\left[\vec{h}{t}^{(L)} ; \stackrel{\leftarrow}{h}{t}^{(L)}\right]+c\right) ]
RNN在机器翻译中的应用主要包括两个阶段:
[ h_{t}=\phi\left(h_{t-1}, x_{t}\right)=f\left(W^{(h h)} h_{t-1} + W^{(h x)} x_{t}\right) ]
[ h_{t}=\phi\left(h_{t-1}\right)=f\left(W^{(h h)} h_{t-1}\right) ]
最终预测单词通过 Softmax 函数:
[ y_{t}=\operatorname{softmax}\left(W^{(S)} h_{t}\right) ]
目标函数为交叉熵损失:
[ \max {\theta} \frac{1}{N} \sum{n=1}^{N} \log p_{\theta}\left(y^{(n)} | x^{(n)}\right) ]
GRU(Gated Recurrent Unit)是一种改进的 RNN,通过引入门控机制解决梯度消失问题。其核心结构包括三个门控单元:
[ \tilde{h}{t}=\tanh \left(r{t} \circ U h_{t-1} + W x_{t}\right) ]
[ r_{t}=\sigma\left(W^{(r)} x_{t} + U^{(r)} h_{t-1}\right) ]
[ z_{t}=\sigma\left(W^{(z)} x_{t} + U^{(z)} h_{t-1}\right) ]
[ h_{t}=\left(1-z_{t}\right) \circ \tilde{h}{t} + z{t} \circ h_{t-1} ]
LSTM(Long Short-Term Memory)是 GRU 的扩展,通过引入记忆单元解决长距离依赖问题。其结构包括四个门控单元:
[ \tilde{c}{t}=\tanh \left(W^{(c)} x{t} + U^{(c)} h_{t-1}\right) ]
[ i_{t}=\sigma\left(W^{(i)} x_{t} + U^{(i)} h_{t-1}\right) ]
[ f_{t}=\sigma\left(W^{(f)} x_{t} + U^{(f)} h_{t-1}\right) ]
[ c_{t}=f_{t} \circ c_{t-1} + i_{t} \circ \tilde{c}_{t} ]
[ o_{t}=\sigma\left(W^{(o)} x_{t} + U^{(o)} h_{t-1}\right) ]
[ h_{t}=o_{t} \circ \tanh \left(c_{t}\right) ]
从 n-gram 到 RNN,再到 GRU 和 LSTM,这些语言模型在捕捉语言依赖关系方面有了显著的进步。RNN 的记忆性使其在复杂语言建模任务中表现优异,但也面临梯度消失等问题。GRU 和 LSTM 的引入有效解决了这些问题,为语言模型的训练和应用提供了更强大的工具。
转载地址:http://qgefk.baihongyu.com/