循环神经网络
循环神经网络
下图展示了如何基于循环神经网络实现语言模型。我们的目的是基于当前的输入与过去的输入序列,预测序列的下一个字符。循环神经网络引入一个隐藏变量\(H\),用\(H_{t}\)表示\(H\)在时间步\(t\)的值。\(H_{t}\)的计算基于\(X_{t}\)和\(H_{t-1}\),可以认为\(H_{t}\)记录了到当前字符为止的序列信息,利用\(H_{t}\)对序列的下一个字符进行预测。
循环神经网络的构造
我们先看循环神经网络的具体构造。假设\(\boldsymbol{X}_t \in \mathbb{R}^{n \times d}\)是时间步\(t\)的小批量输入,\(\boldsymbol{H}_t \in \mathbb{R}^{n \times h}\)是该时间步的隐藏变量,则:
其中,\(\boldsymbol{W}_{xh} \in \mathbb{R}^{d \times h}\),\(\boldsymbol{W}_{hh} \in \mathbb{R}^{h \times h}\),\(\boldsymbol{b}_{h} \in \mathbb{R}^{1 \times h}\),\(\phi\)函数是非线性激活函数。由于引入了\(\boldsymbol{H}_{t-1} \boldsymbol{W}_{hh}\),\(H_{t}\)能够捕捉截至当前时间步的序列的历史信息,就像是神经网络当前时间步的状态或记忆一样。由于\(H_{t}\)的计算基于\(H_{t-1}\),上式的计算是循环的,使用循环计算的网络即循环神经网络(recurrent neural network)。
在时间步\(t\),输出层的输出为:
其中\(\boldsymbol{W}_{hq} \in \mathbb{R}^{h \times q}\),\(\boldsymbol{b}_q \in \mathbb{R}^{1 \times q}\)。
裁剪梯度
循环神经网络中较容易出现梯度衰减或梯度爆炸,这会导致网络几乎无法训练。裁剪梯度(clip gradient)是一种应对梯度爆炸的方法。假设我们把所有模型参数的梯度拼接成一个向量 \(\boldsymbol{g}\),并设裁剪的阈值是\(\theta\)。裁剪后的梯度
的\(L_2\)范数不超过\(\theta\)。
困惑度
我们通常使用困惑度(perplexity)来评价语言模型的好坏。回忆一下“softmax回归”一节中交叉熵损失函数的定义。困惑度是对交叉熵损失函数做指数运算后得到的值。特别地,
- 最佳情况下,模型总是把标签类别的概率预测为1,此时困惑度为1;
- 最坏情况下,模型总是把标签类别的概率预测为0,此时困惑度为正无穷;
- 基线情况下,模型总是预测所有类别的概率都相同,此时困惑度为类别个数。
显然,任何一个有效模型的困惑度必须小于类别个数。
这里的模型训练函数有以下几点不同:
- 使用困惑度评价模型。
- 在迭代模型参数前裁剪梯度。
- 对时序数据采用不同采样方法将导致隐藏状态初始化的不同。
pytorch的RNN
我们使用Pytorch中的nn.RNN
来构造循环神经网络。在本节中,我们主要关注nn.RNN
的以下几个构造函数参数:
input_size
- The number of expected features in the input xhidden_size
– The number of features in the hidden state hnonlinearity
– The non-linearity to use. Can be either 'tanh' or 'relu'. Default: 'tanh'batch_first
– If True, then the input and output tensors are provided as (batch_size, num_steps, input_size). Default: False
这里的batch_first
决定了输入的形状,我们使用默认的参数False
,对应的输入形状是 (num_steps, batch_size, input_size)。
forward
函数的参数为:
input
of shape (num_steps, batch_size, input_size): tensor containing the features of the input sequence.h_0
of shape (num_layers * num_directions, batch_size, hidden_size): tensor containing the initial hidden state for each element in the batch. Defaults to zero if not provided. If the RNN is bidirectional, num_directions should be 2, else it should be 1.
forward
函数的返回值是:
output
of shape (num_steps, batch_size, num_directions * hidden_size): tensor containing the output features (h_t) from the last layer of the RNN, for each t.h_n
of shape (num_layers * num_directions, batch_size, hidden_size): tensor containing the hidden state for t = num_steps.