{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"origin_pos": 0
},
"source": [
"# Bidirectional Recurrent Neural Networks\n",
":label:`sec_bi_rnn`\n",
"\n",
"In sequence learning,\n",
"so far we assumed that our goal is to model the next output given what we have seen so far, e.g., in the context of a time series or in the context of a language model. While this is a typical scenario, it is not the only one we might encounter. To illustrate the issue, consider the following three tasks of filling in the blank in a text sequence:\n",
"\n",
"* I am `___`.\n",
"* I am `___` hungry.\n",
"* I am `___` hungry, and I can eat half a pig.\n",
"\n",
"Depending on the amount of information available, we might fill in the blanks with very different words such as \"happy\", \"not\", and \"very\".\n",
"Clearly the end of the phrase (if available) conveys significant information about which word to pick.\n",
"A sequence model that is incapable of taking advantage of this will perform poorly on related tasks.\n",
"For instance, to do well in named entity recognition (e.g., to recognize whether \"Green\" refers to \"Mr. Green\" or to the color)\n",
"longer-range context is equally vital.\n",
"To get some inspiration for addressing the problem let us take a detour to probabilistic graphical models.\n",
"\n",
"\n",
"## Dynamic Programming in Hidden Markov Models\n",
"\n",
"This subsection serves to illustrate the dynamic programming problem. The specific technical details do not matter for understanding the deep learning models\n",
"but they help in motivating why one might use deep learning and why one might pick specific architectures.\n",
"\n",
"If we want to solve the problem using probabilistic graphical models we could for instance design a latent variable model as follows.\n",
"At any time step $t$,\n",
"we assume that there exists some latent variable $h_t$ that governs our observed emission $x_t$ via $P(x_t \\mid h_t)$.\n",
"Moreover, any transition $h_t \\to h_{t+1}$ is given by some state transition probability $P(h_{t+1} \\mid h_{t})$. This probabilistic graphical model is then a *hidden Markov model* as in :numref:`fig_hmm`.\n",
"\n",
"![A hidden Markov model.](https://raw.githubusercontent.com/d2l-ai/d2l-en/master/img/hmm.svg)\n",
":label:`fig_hmm`\n",
"\n",
"Thus,\n",
"for a sequence of $T$ observations we have the following joint probability distribution over the observed and hidden states:\n",
"\n",
"$$P(x_1, \\ldots, x_T, h_1, \\ldots, h_T) = \\prod_{t=1}^T P(h_t \\mid h_{t-1}) P(x_t \\mid h_t), \\text{ where } P(h_1 \\mid h_0) = P(h_1).$$\n",
":eqlabel:`eq_hmm_jointP`\n",
"\n",
"\n",
"Now assume that we observe all $x_i$ with the exception of some $x_j$ and it is our goal to compute $P(x_j \\mid x_{-j})$, where $x_{-j} = (x_1, \\ldots, x_{j-1}, x_{j+1}, \\ldots, x_{T})$.\n",
"Since there is no latent variable\n",
"in $P(x_j \\mid x_{-j})$,\n",
"we consider summing over\n",
"all the possible combinations of choices for $h_1, \\ldots, h_T$.\n",
"In case any $h_i$ can take on $k$ distinct values (a finite number of states), this means that we need to sum over $k^T$ terms---usually mission impossible! Fortunately there is an elegant solution for this: *dynamic programming*.\n",
"\n",
"To see how it works,\n",
"consider summing over latent variables\n",
"$h_1, \\ldots, h_T$ in turn.\n",
"According to :eqref:`eq_hmm_jointP`,\n",
"this yields:\n",
"\n",
"$$\\begin{aligned}\n",
" &P(x_1, \\ldots, x_T) \\\\\n",
" =& \\sum_{h_1, \\ldots, h_T} P(x_1, \\ldots, x_T, h_1, \\ldots, h_T) \\\\\n",
" =& \\sum_{h_1, \\ldots, h_T} \\prod_{t=1}^T P(h_t \\mid h_{t-1}) P(x_t \\mid h_t) \\\\\n",
" =& \\sum_{h_2, \\ldots, h_T} \\underbrace{\\left[\\sum_{h_1} P(h_1) P(x_1 \\mid h_1) P(h_2 \\mid h_1)\\right]}_{\\pi_2(h_2) \\stackrel{\\mathrm{def}}{=}}\n",
" P(x_2 \\mid h_2) \\prod_{t=3}^T P(h_t \\mid h_{t-1}) P(x_t \\mid h_t) \\\\\n",
" =& \\sum_{h_3, \\ldots, h_T} \\underbrace{\\left[\\sum_{h_2} \\pi_2(h_2) P(x_2 \\mid h_2) P(h_3 \\mid h_2)\\right]}_{\\pi_3(h_3)\\stackrel{\\mathrm{def}}{=}}\n",
" P(x_3 \\mid h_3) \\prod_{t=4}^T P(h_t \\mid h_{t-1}) P(x_t \\mid h_t)\\\\\n",
" =& \\dots \\\\\n",
" =& \\sum_{h_T} \\pi_T(h_T) P(x_T \\mid h_T).\n",
"\\end{aligned}$$\n",
"\n",
"In general we have the *forward recursion* as\n",
"\n",
"$$\\pi_{t+1}(h_{t+1}) = \\sum_{h_t} \\pi_t(h_t) P(x_t \\mid h_t) P(h_{t+1} \\mid h_t).$$\n",
"\n",
"The recursion is initialized as $\\pi_1(h_1) = P(h_1)$. In abstract terms this can be written as $\\pi_{t+1} = f(\\pi_t, x_t)$, where $f$ is some learnable function. This looks very much like the update equation in the latent variable models we discussed so far in the context of RNNs! \n",
"\n",
"Entirely analogously to the forward recursion,\n",
"we can also \n",
"sum over the same set of latent variables with a backward recursion. This yields:\n",
"\n",
"$$\\begin{aligned}\n",
" & P(x_1, \\ldots, x_T) \\\\\n",
" =& \\sum_{h_1, \\ldots, h_T} P(x_1, \\ldots, x_T, h_1, \\ldots, h_T) \\\\\n",
" =& \\sum_{h_1, \\ldots, h_T} \\prod_{t=1}^{T-1} P(h_t \\mid h_{t-1}) P(x_t \\mid h_t) \\cdot P(h_T \\mid h_{T-1}) P(x_T \\mid h_T) \\\\\n",
" =& \\sum_{h_1, \\ldots, h_{T-1}} \\prod_{t=1}^{T-1} P(h_t \\mid h_{t-1}) P(x_t \\mid h_t) \\cdot\n",
" \\underbrace{\\left[\\sum_{h_T} P(h_T \\mid h_{T-1}) P(x_T \\mid h_T)\\right]}_{\\rho_{T-1}(h_{T-1})\\stackrel{\\mathrm{def}}{=}} \\\\\n",
" =& \\sum_{h_1, \\ldots, h_{T-2}} \\prod_{t=1}^{T-2} P(h_t \\mid h_{t-1}) P(x_t \\mid h_t) \\cdot\n",
" \\underbrace{\\left[\\sum_{h_{T-1}} P(h_{T-1} \\mid h_{T-2}) P(x_{T-1} \\mid h_{T-1}) \\rho_{T-1}(h_{T-1}) \\right]}_{\\rho_{T-2}(h_{T-2})\\stackrel{\\mathrm{def}}{=}} \\\\\n",
" =& \\ldots \\\\\n",
" =& \\sum_{h_1} P(h_1) P(x_1 \\mid h_1)\\rho_{1}(h_{1}).\n",
"\\end{aligned}$$\n",
"\n",
"\n",
"We can thus write the *backward recursion* as\n",
"\n",
"$$\\rho_{t-1}(h_{t-1})= \\sum_{h_{t}} P(h_{t} \\mid h_{t-1}) P(x_{t} \\mid h_{t}) \\rho_{t}(h_{t}),$$\n",
"\n",
"with initialization $\\rho_T(h_T) = 1$. \n",
"Both the forward and backward recursions allow us to sum over $T$ latent variables in $\\mathcal{O}(kT)$ (linear) time over all values of $(h_1, \\ldots, h_T)$ rather than in exponential time.\n",
"This is one of the great benefits of the probabilistic inference with graphical models.\n",
"It is \n",
"also a very special instance of \n",
"a general message passing algorithm :cite:`Aji.McEliece.2000`.\n",
"Combining both forward and backward recursions, we are able to compute\n",
"\n",
"$$P(x_j \\mid x_{-j}) \\propto \\sum_{h_j} \\pi_j(h_j) \\rho_j(h_j) P(x_j \\mid h_j).$$\n",
"\n",
"Note that in abstract terms the backward recursion can be written as $\\rho_{t-1} = g(\\rho_t, x_t)$, where $g$ is a learnable function. Again, this looks very much like an update equation, just running backwards unlike what we have seen so far in RNNs. Indeed, hidden Markov models benefit from knowing future data when it is available. Signal processing scientists distinguish between the two cases of knowing and not knowing future observations as interpolation v.s. extrapolation.\n",
"See the introductory chapter of the book on sequential Monte Carlo algorithms for more details :cite:`Doucet.De-Freitas.Gordon.2001`.\n",
"\n",
"\n",
"## Bidirectional Model\n",
"\n",
"If we want to have a mechanism in RNNs that offers comparable look-ahead ability as in hidden Markov models, we need to modify the RNN design that we have seen so far. Fortunately, this is easy conceptually. Instead of running an RNN only in the forward mode starting from the first token, we start another one from the last token running from back to front. \n",
"*Bidirectional RNNs* add a hidden layer that passes information in a backward direction to more flexibly process such information. :numref:`fig_birnn` illustrates the architecture of a bidirectional RNN with a single hidden layer.\n",
"\n",
"![Architecture of a bidirectional RNN.](https://raw.githubusercontent.com/d2l-ai/d2l-en/master/img/birnn.svg)\n",
":label:`fig_birnn`\n",
"\n",
"In fact, this is not too dissimilar to the forward and backward recursions in the dynamic programing of hidden Markov models. \n",
"The main distinction is that in the previous case these equations had a specific statistical meaning.\n",
"Now they are devoid of such easily accessible interpretations and we can just treat them as \n",
"generic and learnable functions.\n",
"This transition epitomizes many of the principles guiding the design of modern deep networks: first, use the type of functional dependencies of classical statistical models, and then parameterize them in a generic form.\n",
"\n",
"\n",
"### Definition\n",
"\n",
"Bidirectional RNNs were introduced by :cite:`Schuster.Paliwal.1997`. \n",
"For a detailed discussion of the various architectures see also the paper :cite:`Graves.Schmidhuber.2005`.\n",
"Let us look at the specifics of such a network.\n",
"\n",
"\n",
"For any time step $t$, \n",
"given a minibatch input $\\mathbf{X}_t \\in \\mathbb{R}^{n \\times d}$ (number of examples: $n$, number of inputs in each example: $d$) and let the hidden layer activation function be $\\phi$. In the bidirectional architecture, we assume that the forward and backward hidden states for this time step are $\\overrightarrow{\\mathbf{H}}_t \\in \\mathbb{R}^{n \\times h}$ and $\\overleftarrow{\\mathbf{H}}_t \\in \\mathbb{R}^{n \\times h}$, respectively,\n",
"where $h$ is the number of hidden units.\n",
"The forward and backward hidden state updates are as follows:\n",
"\n",
"\n",
"$$\n",
"\\begin{aligned}\n",
"\\overrightarrow{\\mathbf{H}}_t &= \\phi(\\mathbf{X}_t \\mathbf{W}_{xh}^{(f)} + \\overrightarrow{\\mathbf{H}}_{t-1} \\mathbf{W}_{hh}^{(f)} + \\mathbf{b}_h^{(f)}),\\\\\n",
"\\overleftarrow{\\mathbf{H}}_t &= \\phi(\\mathbf{X}_t \\mathbf{W}_{xh}^{(b)} + \\overleftarrow{\\mathbf{H}}_{t+1} \\mathbf{W}_{hh}^{(b)} + \\mathbf{b}_h^{(b)}),\n",
"\\end{aligned}\n",
"$$\n",
"\n",
"where the weights $\\mathbf{W}_{xh}^{(f)} \\in \\mathbb{R}^{d \\times h}, \\mathbf{W}_{hh}^{(f)} \\in \\mathbb{R}^{h \\times h}, \\mathbf{W}_{xh}^{(b)} \\in \\mathbb{R}^{d \\times h}, \\text{ and } \\mathbf{W}_{hh}^{(b)} \\in \\mathbb{R}^{h \\times h}$, and biases $\\mathbf{b}_h^{(f)} \\in \\mathbb{R}^{1 \\times h} \\text{ and } \\mathbf{b}_h^{(b)} \\in \\mathbb{R}^{1 \\times h}$ are all the model parameters.\n",
"\n",
"Next, we concatenate the forward and backward hidden states $\\overrightarrow{\\mathbf{H}}_t$ and $\\overleftarrow{\\mathbf{H}}_t$\n",
"to obtain the hidden state $\\mathbf{H}_t \\in \\mathbb{R}^{n \\times 2h}$ to be fed into the output layer.\n",
"In deep bidirectional RNNs with multiple hidden layers,\n",
"such information\n",
"is passed on as *input* to the next bidirectional layer. Last, the output layer computes the output $\\mathbf{O}_t \\in \\mathbb{R}^{n \\times q}$ (number of outputs: $q$):\n",
"\n",
"$$\\mathbf{O}_t = \\mathbf{H}_t \\mathbf{W}_{hq} + \\mathbf{b}_q.$$\n",
"\n",
"Here, the weight matrix $\\mathbf{W}_{hq} \\in \\mathbb{R}^{2h \\times q}$ and the bias $\\mathbf{b}_q \\in \\mathbb{R}^{1 \\times q}$ are the model parameters of the output layer. In fact, the two directions can have different numbers of hidden units.\n",
"\n",
"### Computational Cost and Applications\n",
"\n",
"One of the key features of a bidirectional RNN is that information from both ends of the sequence is used to estimate the output. That is, we use information from both future and past observations to predict the current one. \n",
"In the case of next token prediction this is not quite what we want.\n",
"After all, we do not have the luxury of knowing the next to next token when predicting the next one. Hence, if we were to use a bidirectional RNN naively we would not get a very good accuracy: during training we have past and future data to estimate the present. During test time we only have past data and thus poor accuracy. We will illustrate this in an experiment below.\n",
"\n",
"To add insult to injury, bidirectional RNNs are also exceedingly slow.\n",
"The main reasons for this are that \n",
"the forward propagation\n",
"requires both forward and backward recursions\n",
"in bidirectional layers\n",
"and that the backpropagation is dependent on the outcomes of the forward propagation. Hence, gradients will have a very long dependency chain.\n",
"\n",
"In practice bidirectional layers are used very sparingly and only for a narrow set of applications, such as filling in missing words, annotating tokens (e.g., for named entity recognition), and encoding sequences wholesale as a step in a sequence processing pipeline (e.g., for machine translation).\n",
"In :numref:`sec_bert` and :numref:`sec_sentiment_rnn`,\n",
"we will introduce how to use bidirectional RNNs\n",
"to encode text sequences.\n",
"\n",
"\n",
"\n",
"## Training a Bidirectional RNN for a Wrong Application\n",
"\n",
"If we were to ignore all advice regarding the fact that bidirectional RNNs use past and future data and simply apply it to language models, \n",
"we will get estimates with acceptable perplexity. Nonetheless, the ability of the model to predict future tokens is severely compromised as the experiment below illustrates. \n",
"Despite reasonable perplexity, it only generates gibberish even after many iterations.\n",
"We include the code below as a cautionary example against using them in the wrong context.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%load ../utils/djl-imports\n",
"%load ../utils/plot-utils\n",
"%load ../utils/Functions.java\n",
"%load ../utils/PlotUtils.java\n",
"\n",
"%load ../utils/StopWatch.java\n",
"%load ../utils/Accumulator.java\n",
"%load ../utils/Animator.java\n",
"%load ../utils/Training.java\n",
"%load ../utils/timemachine/Vocab.java\n",
"%load ../utils/timemachine/RNNModel.java\n",
"%load ../utils/timemachine/RNNModelScratch.java\n",
"%load ../utils/timemachine/TimeMachine.java\n",
"%load ../utils/timemachine/TimeMachineDataset.java"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"NDManager manager = NDManager.newBaseManager();"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"// Load data\n",
"int batchSize = 32;\n",
"int numSteps = 35;\n",
"Device device = manager.getDevice();\n",
"TimeMachineDataset dataset = new TimeMachineDataset.Builder()\n",
" .setManager(manager)\n",
" .setMaxTokens(10000)\n",
" .setSampling(batchSize, false)\n",
" .setSteps(numSteps)\n",
" .build();\n",
"dataset.prepare();\n",
"Vocab vocab = dataset.getVocab();\n",
"\n",
"// Define the bidirectional LSTM model by setting `bidirectional=True`\n",
"int vocabSize = vocab.length();\n",
"int numHiddens = 256;\n",
"int numLayers = 2;\n",
"LSTM lstmLayer =\n",
" LSTM.builder()\n",
" .setNumLayers(numLayers)\n",
" .setStateSize(numHiddens)\n",
" .optReturnState(true)\n",
" .optBatchFirst(false)\n",
" .optBidirectional(true)\n",
" .build();\n",
"\n",
"// Train the model\n",
"RNNModel model = new RNNModel(lstmLayer, vocabSize);\n",
"int numEpochs = Integer.getInteger(\"MAX_EPOCH\", 500);\n",
"\n",
"int lr = 1;\n",
"TimeMachine.trainCh8(model, dataset, vocab, lr, numEpochs, device, false, manager);"
]
},
{
"cell_type": "markdown",
"metadata": {
"origin_pos": 3
},
"source": [
"The output is clearly unsatisfactory for the reasons described above. \n",
"For a\n",
"discussion of more effective uses of bidirectional RNNs, please see the sentiment\n",
"analysis application \n",
"in :numref:`sec_sentiment_rnn`.\n",
"\n",
"## Summary\n",
"\n",
"* In bidirectional RNNs, the hidden state for each time step is simultaneously determined by the data prior to and after the current time step.\n",
"* Bidirectional RNNs bear a striking resemblance with the forward-backward algorithm in probabilistic graphical models.\n",
"* Bidirectional RNNs are mostly useful for sequence encoding and the estimation of observations given bidirectional context.\n",
"* Bidirectional RNNs are very costly to train due to long gradient chains.\n",
"\n",
"## Exercises\n",
"\n",
"1. If the different directions use a different number of hidden units, how will the shape of $\\mathbf{H}_t$ change?\n",
"1. Design a bidirectional RNN with multiple hidden layers.\n",
"1. Polysemy is common in natural languages. For example, the word \"bank\" has different meanings in contexts “i went to the bank to deposit cash” and “i went to the bank to sit down”. How can we design a neural network model such that given a context sequence and a word, a vector representation of the word in the context will be returned? What type of neural architectures is preferred for handling polysemy?\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Java",
"language": "java",
"name": "java"
},
"language_info": {
"codemirror_mode": "java",
"file_extension": ".jshell",
"mimetype": "text/x-java-source",
"name": "Java",
"pygments_lexer": "java",
"version": "14.0.2+12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}