.. _sec_rnn: Recurrent Neural Networks ========================= In :numref:`sec_language_model` we introduced :math:`n`-gram models, where the conditional probability of word :math:`x_t` at time step :math:`t` only depends on the :math:`n-1` previous words. If we want to incorporate the possible effect of words earlier than time step :math:`t-(n-1)` on :math:`x_t`, we need to increase :math:`n`. However, the number of model parameters would also increase exponentially with it, as we need to store :math:`|\mathcal{V}|^n` numbers for a vocabulary set :math:`\mathcal{V}`. Hence, rather than modeling :math:`P(x_t \mid x_{t-1}, \ldots, x_{t-n+1})` it is preferable to use a latent variable model: .. math:: P(x_t \mid x_{t-1}, \ldots, x_1) \approx P(x_t \mid h_{t-1}), where :math:`h_{t-1}` is a *hidden state* (also known as a hidden variable) that stores the sequence information up to time step :math:`t-1`. In general, the hidden state at any time step :math:`t` could be computed based on both the current input :math:`x_{t}` and the previous hidden state :math:`h_{t-1}`: .. math:: h_t = f(x_{t}, h_{t-1}). :label: eq_ht_xt For a sufficiently powerful function :math:`f` in :eq:`eq_ht_xt`, the latent variable model is not an approximation. After all, :math:`h_t` may simply store all the data it has observed so far. However, it could potentially make both computation and storage expensive. Recall that we have discussed hidden layers with hidden units in :numref:`chap_perceptrons`. It is noteworthy that hidden layers and hidden states refer to two very different concepts. Hidden layers are, as explained, layers that are hidden from view on the path from input to output. Hidden states are technically speaking *inputs* to whatever we do at a given step, and they can only be computed by looking at data at previous time steps. *Recurrent neural networks* (RNNs) are neural networks with hidden states. Before introducing the RNN model, we first revisit the MLP model introduced in :numref:`sec_mlp`. Neural Networks without Hidden States ------------------------------------- Let us take a look at an MLP with a single hidden layer. Let the hidden layer's activation function be :math:`\phi`. Given a minibatch of examples :math:`\mathbf{X} \in \mathbb{R}^{n \times d}` with batch size :math:`n` and :math:`d` inputs, the hidden layer's output :math:`\mathbf{H} \in \mathbb{R}^{n \times h}` is calculated as .. math:: \mathbf{H} = \phi(\mathbf{X} \mathbf{W}_{xh} + \mathbf{b}_h). :label: rnn_h_without_state In :eq:`rnn_h_without_state`, we have the weight parameter :math:`\mathbf{W}_{xh} \in \mathbb{R}^{d \times h}`, the bias parameter :math:`\mathbf{b}_h \in \mathbb{R}^{1 \times h}`, and the number of hidden units :math:`h`, for the hidden layer. Thus, broadcasting (see :numref:`subsec_broadcasting`) is applied during the summation. Next, the hidden variable :math:`\mathbf{H}` is used as the input of the output layer. The output layer is given by .. math:: \mathbf{O} = \mathbf{H} \mathbf{W}_{hq} + \mathbf{b}_q, where :math:`\mathbf{O} \in \mathbb{R}^{n \times q}` is the output variable, :math:`\mathbf{W}_{hq} \in \mathbb{R}^{h \times q}` is the weight parameter, and :math:`\mathbf{b}_q \in \mathbb{R}^{1 \times q}` is the bias parameter of the output layer. If it is a classification problem, we can use :math:`\text{softmax}(\mathbf{O})` to compute the probability distribution of the output categories. This is entirely analogous to the regression problem we solved previously in :numref:`sec_sequence`, hence we omit details. Suffice it to say that we can pick feature-label pairs at random and learn the parameters of our network via automatic differentiation and stochastic gradient descent. .. _subsec_rnn_w_hidden_states: Recurrent Neural Networks with Hidden States -------------------------------------------- Matters are entirely different when we have hidden states. Let us look at the structure in some more detail. Assume that we have a minibatch of inputs :math:`\mathbf{X}_t \in \mathbb{R}^{n \times d}` at time step :math:`t`. In other words, for a minibatch of :math:`n` sequence examples, each row of :math:`\mathbf{X}_t` corresponds to one example at time step :math:`t` from the sequence. Next, denote by :math:`\mathbf{H}_t \in \mathbb{R}^{n \times h}` the hidden variable of time step :math:`t`. Unlike the MLP, here we save the hidden variable :math:`\mathbf{H}_{t-1}` from the previous time step and introduce a new weight parameter :math:`\mathbf{W}_{hh} \in \mathbb{R}^{h \times h}` to describe how to use the hidden variable of the previous time step in the current time step. Specifically, the calculation of the hidden variable of the current time step is determined by the input of the current time step together with the hidden variable of the previous time step: .. math:: \mathbf{H}_t = \phi(\mathbf{X}_t \mathbf{W}_{xh} + \mathbf{H}_{t-1} \mathbf{W}_{hh} + \mathbf{b}_h). :label: rnn_h_with_state Compared with :eq:`rnn_h_without_state`, :eq:`rnn_h_with_state` adds one more term :math:`\mathbf{H}_{t-1} \mathbf{W}_{hh}` and thus instantiates :eq:`eq_ht_xt`. From the relationship between hidden variables :math:`\mathbf{H}_t` and :math:`\mathbf{H}_{t-1}` of adjacent time steps, we know that these variables captured and retained the sequence's historical information up to their current time step, just like the state or memory of the neural network's current time step. Therefore, such a hidden variable is called a *hidden state*. Since the hidden state uses the same definition of the previous time step in the current time step, the computation of :eq:`rnn_h_with_state` is *recurrent*. Hence, neural networks with hidden states based on recurrent computation are named *recurrent neural networks*. Layers that perform the computation of :eq:`rnn_h_with_state` in RNNs are called *recurrent layers*. There are many different ways for constructing RNNs. RNNs with a hidden state defined by :eq:`rnn_h_with_state` are very common. For time step :math:`t`, the output of the output layer is similar to the computation in the MLP: .. math:: \mathbf{O}_t = \mathbf{H}_t \mathbf{W}_{hq} + \mathbf{b}_q. Parameters of the RNN include the weights :math:`\mathbf{W}_{xh} \in \mathbb{R}^{d \times h}, \mathbf{W}_{hh} \in \mathbb{R}^{h \times h}`, and the bias :math:`\mathbf{b}_h \in \mathbb{R}^{1 \times h}` of the hidden layer, together with the weights :math:`\mathbf{W}_{hq} \in \mathbb{R}^{h \times q}` and the bias :math:`\mathbf{b}_q \in \mathbb{R}^{1 \times q}` of the output layer. It is worth mentioning that even at different time steps, RNNs always use these model parameters. Therefore, the parameterization cost of an RNN does not grow as the number of time steps increases. :numref:`fig_rnn` illustrates the computational logic of an RNN at three adjacent time steps. At any time step :math:`t`, the computation of the hidden state can be treated as: (i) concatenating the input :math:`\mathbf{X}_t` at the current time step :math:`t` and the hidden state :math:`\mathbf{H}_{t-1}` at the previous time step :math:`t-1`; (ii) feeding the concatenation result into a fully-connected layer with the activation function :math:`\phi`. The output of such a fully-connected layer is the hidden state :math:`\mathbf{H}_t` of the current time step :math:`t`. In this case, the model parameters are the concatenation of :math:`\mathbf{W}_{xh}` and :math:`\mathbf{W}_{hh}`, and a bias of :math:`\mathbf{b}_h`, all from :eq:`rnn_h_with_state`. The hidden state of the current time step :math:`t`, :math:`\mathbf{H}_t`, will participate in computing the hidden state :math:`\mathbf{H}_{t+1}` of the next time step :math:`t+1`. What is more, :math:`\mathbf{H}_t` will also be fed into the fully-connected output layer to compute the output :math:`\mathbf{O}_t` of the current time step :math:`t`. .. _fig_rnn: .. figure:: ../img/rnn.svg An RNN with a hidden state. We just mentioned that the calculation of :math:`\mathbf{X}_t \mathbf{W}_{xh} + \mathbf{H}_{t-1} \mathbf{W}_{hh}` for the hidden state is equivalent to matrix multiplication of concatenation of :math:`\mathbf{X}_t` and :math:`\mathbf{H}_{t-1}` and concatenation of :math:`\mathbf{W}_{xh}` and :math:`\mathbf{W}_{hh}`. Though this can be proven in mathematics, in the following we just use a simple code snippet to show this. To begin with, we define matrices ``X``, ``W_xh``, ``H``, and ``W_hh``, whose shapes are (3, 1), (1, 4), (3, 4), and (4, 4), respectively. Multiplying ``X`` by ``W_xh``, and ``H`` by ``W_hh``, respectively, and then adding these two multiplications, we obtain a matrix of shape (3, 4). .. raw:: html
mxnetpytorchtensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python from mxnet import np, npx from d2l import mxnet as d2l npx.set_np() X, W_xh = np.random.normal(0, 1, (3, 1)), np.random.normal(0, 1, (1, 4)) H, W_hh = np.random.normal(0, 1, (3, 4)), np.random.normal(0, 1, (4, 4)) np.dot(X, W_xh) + np.dot(H, W_hh) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output array([[-0.21952915, 4.256434 , 4.5812645 , -5.344988 ], [ 3.447858 , -3.0177274 , -1.6777471 , 7.535347 ], [ 2.2390068 , 1.4199957 , 4.744728 , -8.421293 ]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python import torch from d2l import torch as d2l X, W_xh = torch.normal(0, 1, (3, 1)), torch.normal(0, 1, (1, 4)) H, W_hh = torch.normal(0, 1, (3, 4)), torch.normal(0, 1, (4, 4)) torch.matmul(X, W_xh) + torch.matmul(H, W_hh) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output tensor([[-0.5842, -0.3556, 0.6926, 1.0900], [ 0.9317, 1.3875, -1.3222, 0.0321], [-2.0621, -1.8653, 4.4808, -2.4656]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python import tensorflow as tf from d2l import tensorflow as d2l X, W_xh = tf.random.normal((3, 1), 0, 1), tf.random.normal((1, 4), 0, 1) H, W_hh = tf.random.normal((3, 4), 0, 1), tf.random.normal((4, 4), 0, 1) tf.matmul(X, W_xh) + tf.matmul(H, W_hh) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output .. raw:: html
.. raw:: html
Now we concatenate the matrices ``X`` and ``H`` along columns (axis 1), and the matrices ``W_xh`` and ``W_hh`` along rows (axis 0). These two concatenations result in matrices of shape (3, 5) and of shape (5, 4), respectively. Multiplying these two concatenated matrices, we obtain the same output matrix of shape (3, 4) as above. .. raw:: html
mxnetpytorchtensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python np.dot(np.concatenate((X, H), 1), np.concatenate((W_xh, W_hh), 0)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output array([[-0.21952918, 4.256434 , 4.5812645 , -5.344988 ], [ 3.4478583 , -3.0177271 , -1.677747 , 7.535347 ], [ 2.2390068 , 1.4199957 , 4.744728 , -8.421294 ]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python torch.matmul(torch.cat((X, H), 1), torch.cat((W_xh, W_hh), 0)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output tensor([[-0.5842, -0.3556, 0.6926, 1.0900], [ 0.9317, 1.3875, -1.3222, 0.0321], [-2.0621, -1.8653, 4.4808, -2.4656]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python tf.matmul(tf.concat((X, H), 1), tf.concat((W_xh, W_hh), 0)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output .. raw:: html
.. raw:: html
RNN-based Character-Level Language Models ----------------------------------------- Recall that for language modeling in :numref:`sec_language_model`, we aim to predict the next token based on the current and past tokens, thus we shift the original sequence by one token as the labels. Bengio et al. first proposed to use a neural network for language modeling :cite:`Bengio.Ducharme.Vincent.ea.2003`. In the following we illustrate how RNNs can be used to build a language model. Let the minibatch size be one, and the sequence of the text be "machine". To simplify training in subsequent sections, we tokenize text into characters rather than words and consider a *character-level language model*. :numref:`fig_rnn_train` demonstrates how to predict the next character based on the current and previous characters via an RNN for character-level language modeling. .. _fig_rnn_train: .. figure:: ../img/rnn-train.svg A character-level language model based on the RNN. The input and label sequences are "machin" and "achine", respectively. During the training process, we run a softmax operation on the output from the output layer for each time step, and then use the cross-entropy loss to compute the error between the model output and the label. Due to the recurrent computation of the hidden state in the hidden layer, the output of time step 3 in :numref:`fig_rnn_train`, :math:`\mathbf{O}_3`, is determined by the text sequence "m", "a", and "c". Since the next character of the sequence in the training data is "h", the loss of time step 3 will depend on the probability distribution of the next character generated based on the feature sequence "m", "a", "c" and the label "h" of this time step. In practice, each token is represented by a :math:`d`-dimensional vector, and we use a batch size :math:`n>1`. Therefore, the input :math:`\mathbf X_t` at time step :math:`t` will be a :math:`n\times d` matrix, which is identical to what we discussed in :numref:`subsec_rnn_w_hidden_states`. .. _subsec_perplexity: Perplexity ---------- Last, let us discuss about how to measure the language model quality, which will be used to evaluate our RNN-based models in the subsequent sections. One way is to check how surprising the text is. A good language model is able to predict with high-accuracy tokens that what we will see next. Consider the following continuations of the phrase "It is raining", as proposed by different language models: 1. "It is raining outside" 2. "It is raining banana tree" 3. "It is raining piouw;kcj pwepoiut" In terms of quality, example 1 is clearly the best. The words are sensible and logically coherent. While it might not quite accurately reflect which word follows semantically ("in San Francisco" and "in winter" would have been perfectly reasonable extensions), the model is able to capture which kind of word follows. Example 2 is considerably worse by producing a nonsensical extension. Nonetheless, at least the model has learned how to spell words and some degree of correlation between words. Last, example 3 indicates a poorly trained model that does not fit data properly. We might measure the quality of the model by computing the likelihood of the sequence. Unfortunately this is a number that is hard to understand and difficult to compare. After all, shorter sequences are much more likely to occur than the longer ones, hence evaluating the model on Tolstoy's magnum opus *War and Peace* will inevitably produce a much smaller likelihood than, say, on Saint-Exupery's novella *The Little Prince*. What is missing is the equivalent of an average. Information theory comes handy here. We have defined entropy, surprisal, and cross-entropy when we introduced the softmax regression (:numref:`subsec_info_theory_basics`) and more of information theory is discussed in the `online appendix on information theory `__. If we want to compress text, we can ask about predicting the next token given the current set of tokens. A better language model should allow us to predict the next token more accurately. Thus, it should allow us to spend fewer bits in compressing the sequence. So we can measure it by the cross-entropy loss averaged over all the :math:`n` tokens of a sequence: .. math:: \frac{1}{n} \sum_{t=1}^n -\log P(x_t \mid x_{t-1}, \ldots, x_1), :label: eq_avg_ce_for_lm where :math:`P` is given by a language model and :math:`x_t` is the actual token observed at time step :math:`t` from the sequence. This makes the performance on documents of different lengths comparable. For historical reasons, scientists in natural language processing prefer to use a quantity called *perplexity*. In a nutshell, it is the exponential of :eq:`eq_avg_ce_for_lm`: .. math:: \exp\left(-\frac{1}{n} \sum_{t=1}^n \log P(x_t \mid x_{t-1}, \ldots, x_1)\right). Perplexity can be best understood as the harmonic mean of the number of real choices that we have when deciding which token to pick next. Let us look at a number of cases: - In the best case scenario, the model always perfectly estimates the probability of the label token as 1. In this case the perplexity of the model is 1. - In the worst case scenario, the model always predicts the probability of the label token as 0. In this situation, the perplexity is positive infinity. - At the baseline, the model predicts a uniform distribution over all the available tokens of the vocabulary. In this case, the perplexity equals the number of unique tokens of the vocabulary. In fact, if we were to store the sequence without any compression, this would be the best we could do to encode it. Hence, this provides a nontrivial upper bound that any useful model must beat. In the following sections, we will implement RNNs for character-level language models and use perplexity to evaluate such models. Summary ------- - A neural network that uses recurrent computation for hidden states is called a recurrent neural network (RNN). - The hidden state of an RNN can capture historical information of the sequence up to the current time step. - The number of RNN model parameters does not grow as the number of time steps increases. - We can create character-level language models using an RNN. - We can use perplexity to evaluate the quality of language models. Exercises --------- 1. If we use an RNN to predict the next character in a text sequence, what is the required dimension for any output? 2. Why can RNNs express the conditional probability of a token at some time step based on all the previous tokens in the text sequence? 3. What happens to the gradient if you backpropagate through a long sequence? 4. What are some of the problems associated with the language model described in this section? .. raw:: html
.. raw:: html
`Discussions `__ .. raw:: html
.. raw:: html
`Discussions `__ .. raw:: html
.. raw:: html
`Discussions `__ .. raw:: html
.. raw:: html