本次基于当前输入与过去的输入序列预测序列的下一个字符以展示使用循环神经网络实现语言模型。
循环神经网络引入一个隐藏变量*H*,用*Ht*表示*H*在时间步*t*的值。*Ht* 的计算基于 *Xt* 和 *Ht−1* ,可以认为 *Ht* 记录了到当前字符为止的序列信息,利用 *Ht* 对序列的下一个字符进行预测。
**循环神经网络的构造**
假设 *Xt*∈*Rn×d* 是时间步 *t* 的小批量输入, *Ht*∈*Rn×h* 是该时间步的隐藏变量,则:
*Ht = ϕ(Xt Wxh + Ht−1 Whh + bh).*
其中,*Wxh* ∈ *Rd×h* , *Whh* ∈ *Rh×h* , *bh* ∈ *R1×h* , *ϕ* 函数是非线性激活函数。由于引入了 *Ht−1* *Whh* , *Ht* 能够捕捉截至当前时间步的序列的历史信息,就像是神经网络当前时间步的状态或记忆一样。由于 *Ht* 的计算基于 *Ht−1* ,上式的计算是循环的,使用循环计算的网络即循环神经网络(recurrent neural network)。
在时间步 t ,输出层的输出为:
*Ot = Ht Whq + bq.*
其中 *Whq *∈* Rh×q , bq *∈* R1×q* 。
**定义模型**
使用Pytorch中的nn.RNN来构造循环神经网络。在本节中,我们主要关注nn.RNN的以下几个构造函数参数:
*input_size - The number of expected features in the input x
*hidden_size – The number of features in the hidden state h
*nonlinearity – 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.
现在我们构造一个nn.RNN实例,并用一个简单的例子来看一下输出的形状:
定义一个完整的基于循环神经网络的语言模型:
类似的,我们需要实现一个预测函数,与前面的区别在于前向计算和初始化隐藏状态:
使用权重为随机值的模型来预测一次:
接下来实现训练函数,这里只使用了相邻采样:
训练模型: