循环神经网络

循环神经网络

下图展示了如何基于循环神经网络实现语言模型。我们的目的是基于当前的输入与过去的输入序列,预测序列的下一个字符。循环神经网络引入一个隐藏变量(H),用(H_{t})表示(H)在时间步(t)的值。(H_{t})的计算基于(X_{t})(H_{t-1}),可以认为(H_{t})记录了到当前字符为止的序列信息,利用(H_{t})对序列的下一个字符进行预测。
Image Name

循环神经网络的构造

我们先看循环神经网络的具体构造。假设(oldsymbol{X}_t in mathbb{R}^{n imes d})是时间步(t)的小批量输入,(oldsymbol{H}_t in mathbb{R}^{n imes h})是该时间步的隐藏变量,则:

[oldsymbol{H}_t = phi(oldsymbol{X}_t oldsymbol{W}_{xh} + oldsymbol{H}_{t-1} oldsymbol{W}_{hh} + oldsymbol{b}_h). ]

其中,(oldsymbol{W}_{xh} in mathbb{R}^{d imes h})(oldsymbol{W}_{hh} in mathbb{R}^{h imes h})(oldsymbol{b}_{h} in mathbb{R}^{1 imes h})(phi)函数是非线性激活函数。由于引入了(oldsymbol{H}_{t-1} oldsymbol{W}_{hh})(H_{t})能够捕捉截至当前时间步的序列的历史信息,就像是神经网络当前时间步的状态或记忆一样。由于(H_{t})的计算基于(H_{t-1}),上式的计算是循环的,使用循环计算的网络即循环神经网络(recurrent neural network)。

在时间步(t),输出层的输出为:

[oldsymbol{O}_t = oldsymbol{H}_t oldsymbol{W}_{hq} + oldsymbol{b}_q. ]

其中(oldsymbol{W}_{hq} in mathbb{R}^{h imes q})(oldsymbol{b}_q in mathbb{R}^{1 imes q})

裁剪梯度

循环神经网络中较容易出现梯度衰减或梯度爆炸,这会导致网络几乎无法训练。裁剪梯度(clip gradient)是一种应对梯度爆炸的方法。假设我们把所有模型参数的梯度拼接成一个向量 (oldsymbol{g}),并设裁剪的阈值是( heta)。裁剪后的梯度

[ minleft(frac{ heta}{|oldsymbol{g}|}, 1 ight)oldsymbol{g} ]

(L_2)范数不超过( heta)

困惑度

我们通常使用困惑度(perplexity)来评价语言模型的好坏。回忆一下“softmax回归”一节中交叉熵损失函数的定义。困惑度是对交叉熵损失函数做指数运算后得到的值。特别地,

  • 最佳情况下,模型总是把标签类别的概率预测为1,此时困惑度为1;
  • 最坏情况下,模型总是把标签类别的概率预测为0,此时困惑度为正无穷;
  • 基线情况下,模型总是预测所有类别的概率都相同,此时困惑度为类别个数。

显然,任何一个有效模型的困惑度必须小于类别个数。

这里的模型训练函数有以下几点不同:

  1. 使用困惑度评价模型。
  2. 在迭代模型参数前裁剪梯度。
  3. 对时序数据采用不同采样方法将导致隐藏状态初始化的不同。

pytorch的RNN

我们使用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.
原文地址:https://www.cnblogs.com/yu212223/p/12309964.html