{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "---\n",
    "execute:\n",
    "  enabled: false\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=lstm-md-1 fw=all -->\n",
    "\n",
    "# Gated Recurrence\n",
    "\n",
    "that section ended with a diagnosis. The\n",
    "gradient that travels $k$ steps back through a vanilla RNN is multiplied by\n",
    "the $k$-th power of the recurrent Jacobian, so it shrinks or grows\n",
    "geometrically; clipping tames the growth, but no amount of arithmetic can\n",
    "restore a signal that has decayed into noise. The cure has to change the\n",
    "dynamics of the recurrence itself. This section builds that cure:\n",
    "*multiplicative gating*, a mechanism that lets the network learn what to\n",
    "write into its memory, what to erase, and what to reveal. The analysis\n",
    "behind it goes back to Hochreiter's 1991 diploma thesis and to\n",
    "@bengio1994learning (see also\n",
    "[@Hochreiter.Bengio.Frasconi.ea.2001]), and its most famous product is\n",
    "the *long short-term memory* (LSTM) cell of\n",
    "@Hochreiter.Schmidhuber.1997, which powered speech recognition,\n",
    "translation, and language modeling for most of the 2010s.\n",
    "\n",
    "Gates are worth understanding in 2026 for a reason beyond history. The\n",
    "architectures that displaced the LSTM kept its central trick: the\n",
    "transformer's gated feed-forward networks multiply one branch of\n",
    "activations by another, a design whose value that section\n",
    "measured directly, and the modern linear recurrences that occupy the rest\n",
    "of this chapter are, at heart, a forget gate wrapped around a linear\n",
    "state. We develop the idea once, carefully, here: first the gate itself,\n",
    "then the LSTM from scratch and with framework layers, then the gated\n",
    "recurrent unit (GRU) as its streamlined descendant, then a brief look at\n",
    "two structural axes (depth and bidirectionality), and finally a survey of\n",
    "where gating lives today.\n",
    "\n",
    "*Prerequisites: the RNN language-modeling pipeline of\n",
    "that section (data, scaffold, and training recipe are reused\n",
    "verbatim) and the gradient analysis of\n",
    "that section.*"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "lstm-gated-recurrent-networks"
   },
   "source": [
    "%matplotlib inline\n",
    "from d2l import torch as d2l\n",
    "import math\n",
    "import torch\n",
    "from torch import nn"
   ],
   "execution_count": 1,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=lstm-md-2 fw=all -->\n",
    "\n",
    "## Memory Needs a Controller\n",
    "\n",
    "Start from what the gradient analysis demands. As motivation, take the\n",
    "simplest caricature: if the state-to-state Jacobian were one fixed\n",
    "matrix, the contribution of a dependency $k$ steps back would scale\n",
    "asymptotically as $\\rho^k$, where $\\rho$ is the spectral radius, and the\n",
    "only value of $\\rho$ that neither buries nor explodes distant information\n",
    "is $1$. (The full story is subtler — a product of time-varying,\n",
    "generally non-normal Jacobians can grow for many steps even with every\n",
    "eigenvalue inside the unit circle, a phenomenon\n",
    "that section develops — but the caricature\n",
    "points at the right target.) The simplest\n",
    "recurrence with that property is an *accumulator*,\n",
    "\n",
    "$$\n",
    "\\mathbf{S}_t = \\mathbf{S}_{t-1} + (\\textrm{new content}),\n",
    "$$\n",
    "\n",
    "whose Jacobian $\\partial \\mathbf{S}_t / \\partial \\mathbf{S}_{t-1}$ is\n",
    "exactly the identity: gradients pass backwards through it unattenuated, no\n",
    "matter how many steps they travel. Additive updates, not repeated matrix\n",
    "multiplication, are the memory-friendly primitive.\n",
    "\n",
    "But a pure accumulator is a terrible memory. It adds *everything*, forever:\n",
    "irrelevant tokens pile onto relevant ones, the sum typically grows with\n",
    "sequence length, and there is no way to discard a fact once it has stopped\n",
    "mattering. A\n",
    "useful memory must make decisions. Should this input be written down?\n",
    "Should this stored value be kept or cleared? Should the memory influence\n",
    "the output right now, or stay silent? What turns these discrete-sounding\n",
    "decisions into something a network can learn is the *gate*: a small\n",
    "sigmoid-activated layer of the current input and the previous state,\n",
    "\n",
    "$$\n",
    "\\mathbf{G}_t = \\sigma(\\mathbf{X}_t \\mathbf{W}_{\\textrm{xg}} + \\mathbf{H}_{t-1} \\mathbf{W}_{\\textrm{hg}} + \\mathbf{b}_\\textrm{g}),\n",
    "$$\n",
    "\n",
    "whose entries lie in $(0, 1)$ and multiply a signal *elementwise*. A gate\n",
    "value near $1$ lets its coordinate pass; a value near $0$ blocks it; values\n",
    "in between attenuate. Because the gate is computed from context, the\n",
    "decision is different at every time step and for every unit, and because\n",
    "$\\sigma$ is smooth, the decision is differentiable and trainable by the\n",
    "same backpropagation as everything else. Every architecture in this section\n",
    "is a particular arrangement of a few such gates around an additive state.\n",
    "\n",
    "The gate also resolves the stability question of\n",
    "that section in a satisfying way. A vanilla\n",
    "RNN has one shared matrix $\\mathbf{W}_{\\textrm{hh}}$ whose spectrum decides\n",
    "the fate of every perturbation in every direction at every step, and\n",
    "training must keep that global object near the knife-edge $\\rho = 1$ by\n",
    "luck. In a gated cell, the state coordinate $i$ is scaled by the gate value\n",
    "$g_{t,i} \\in (0, 1)$ at step $t$, so a perturbation from $k$ steps ago is\n",
    "scaled by the product $\\prod_{j} g_{j,i} \\leq 1$: growth is impossible\n",
    "along the memory path, while holding the gate near $1$ makes the product,\n",
    "and hence the preserved memory, as close to lossless as the task requires.\n",
    "In dynamical-systems terms, each unit gets its own learned, input-dependent\n",
    "rate of decay in place of one hard-wired global one. Some units learn to\n",
    "forget in three steps; others hold a value across an entire sequence.\n",
    "\n",
    "## Long Short-Term Memory\n",
    "\n",
    "### The Memory Cell and Its Gates\n",
    "\n",
    "The LSTM replaces the vanilla RNN's hidden node with a *memory cell*. The\n",
    "name \"long short-term memory\" describes its role: a network's weights are\n",
    "its long-term memory, changing slowly across training, while its\n",
    "activations are short-term memory, overwritten at every step; the cell is\n",
    "an intermediate store, an activation engineered to persist. Concretely,\n",
    "the cell keeps an *internal state* $\\mathbf{C}_t$, the additive accumulator\n",
    "of the previous section, and controls it with three gates computed exactly\n",
    "in the form of the equation. With a minibatch of $n$ examples,\n",
    "$d$-dimensional inputs $\\mathbf{X}_t \\in \\mathbb{R}^{n \\times d}$, and $h$\n",
    "hidden units carrying the previous state\n",
    "$\\mathbf{H}_{t-1} \\in \\mathbb{R}^{n \\times h}$, the *input gate*\n",
    "$\\mathbf{I}_t$, *forget gate* $\\mathbf{F}_t$, and *output gate*\n",
    "$\\mathbf{O}_t$, each in $\\mathbb{R}^{n \\times h}$, are\n",
    "\n",
    "$$\n",
    "\\begin{aligned}\n",
    "\\mathbf{I}_t &= \\sigma(\\mathbf{X}_t \\mathbf{W}_{\\textrm{xi}} + \\mathbf{H}_{t-1} \\mathbf{W}_{\\textrm{hi}} + \\mathbf{b}_\\textrm{i}),\\\\\n",
    "\\mathbf{F}_t &= \\sigma(\\mathbf{X}_t \\mathbf{W}_{\\textrm{xf}} + \\mathbf{H}_{t-1} \\mathbf{W}_{\\textrm{hf}} + \\mathbf{b}_\\textrm{f}),\\\\\n",
    "\\mathbf{O}_t &= \\sigma(\\mathbf{X}_t \\mathbf{W}_{\\textrm{xo}} + \\mathbf{H}_{t-1} \\mathbf{W}_{\\textrm{ho}} + \\mathbf{b}_\\textrm{o}),\n",
    "\\end{aligned}\n",
    "$$\n",
    "\n",
    "where the $\\mathbf{W}_{\\textrm{x}\\cdot} \\in \\mathbb{R}^{d \\times h}$ and\n",
    "$\\mathbf{W}_{\\textrm{h}\\cdot} \\in \\mathbb{R}^{h \\times h}$ are weights and\n",
    "the $\\mathbf{b}_\\cdot \\in \\mathbb{R}^{1 \\times h}$ are biases (broadcast\n",
    "over the batch as in that section). A fourth head, the\n",
    "*input node*, proposes the content that might be written. It uses $\\tanh$\n",
    "rather than $\\sigma$, since content should be able to be negative:\n",
    "\n",
    "$$\n",
    "\\tilde{\\mathbf{C}}_t = \\tanh(\\mathbf{X}_t \\mathbf{W}_{\\textrm{xc}} + \\mathbf{H}_{t-1} \\mathbf{W}_{\\textrm{hc}} + \\mathbf{b}_\\textrm{c}).\n",
    "$$\n",
    "\n",
    "The four heads are the same computation with different nonlinearities and\n",
    "different jobs. What distinguishes the LSTM is how they combine. The forget\n",
    "gate scales the old cell state, the input gate scales the proposed content,\n",
    "and the two are *added*; the output gate then decides how much of the\n",
    "(squashed) cell is exposed as the hidden state:\n",
    "\n",
    "$$\n",
    "\\mathbf{C}_t = \\mathbf{F}_t \\odot \\mathbf{C}_{t-1} + \\mathbf{I}_t \\odot \\tilde{\\mathbf{C}}_t,\n",
    "\\qquad\n",
    "\\mathbf{H}_t = \\mathbf{O}_t \\odot \\tanh(\\mathbf{C}_t),\n",
    "$$\n",
    "\n",
    "where $\\odot$ is the elementwise (Hadamard) product.\n",
    "the figure traces the full data flow.\n",
    "\n",
    "![The LSTM memory cell. The previous state $\\mathbf{H}_{t-1}$ and the input $\\mathbf{X}_t$ feed four heads: three sigmoid gates and a $\\tanh$ input node. The cell state $\\mathbf{C}_t$ is updated additively, scaled by the forget gate, and the output gate controls what the rest of the network sees.](../img/mdl-modernrnn-lstm-cell.svg)\n",
    "\n",
    "Follow the top path of the figure from left to right. The cell\n",
    "state is touched only by an elementwise product and an elementwise sum; no\n",
    "matrix multiplication and no saturating nonlinearity stands between\n",
    "$\\mathbf{C}_{t-1}$ and $\\mathbf{C}_t$. Along this *direct* path — gates\n",
    "and candidate held fixed — the Jacobian is\n",
    "$\\partial \\mathbf{C}_t / \\partial \\mathbf{C}_{t-1} =\n",
    "\\textrm{diag}(\\mathbf{F}_t)$, so with the forget gate near $1$ and the\n",
    "input gate near $0$ the cell holds its value, and the gradient holds with\n",
    "it, across arbitrarily many steps. To be precise about what is and is not\n",
    "guaranteed: $\\mathbf{F}_t$, $\\mathbf{I}_t$, and $\\tilde{\\mathbf{C}}_t$\n",
    "all depend on $\\mathbf{H}_{t-1}$, which depends on $\\mathbf{C}_{t-1}$, so\n",
    "the *total* derivative of $\\mathbf{C}_t$ with respect to\n",
    "$\\mathbf{C}_{t-1}$ carries additional terms that flow through the gates\n",
    "and reintroduce exactly the matrix products the direct path avoids\n",
    "(that section analyzes such Jacobian products).\n",
    "The architecture supplies a well-conditioned additive route and lets\n",
    "learning control it; it does not guarantee that every gradient is\n",
    "preserved. This protected additive path is the\n",
    "*constant error carousel* that gives the LSTM its resistance to vanishing\n",
    "gradients: it is precisely the controlled accumulator we asked for. The\n",
    "output gate adds a subtler ability: a cell can accumulate evidence silently\n",
    "for many steps ($\\mathbf{O}_t \\approx 0$), invisible to the layers above,\n",
    "and release it at the moment it becomes relevant. Only $\\mathbf{H}_t$ is\n",
    "passed to output layers and to the next stacked layer; the cell state stays\n",
    "internal to the cell.\n",
    "\n",
    "### Implementation from Scratch\n",
    "\n",
    "We now implement the cell, following the template of\n",
    "that section: the cell class holds parameters and steps the\n",
    "recurrence, and `d2l.RNNLMScratch` wraps it with an embedding and an output\n",
    "head to form a language model. As there, `num_inputs` is the width of the\n",
    "vectors the cell consumes at each step, which will be the embedding\n",
    "dimension. Each of the four heads needs the same triple of parameters, so\n",
    "we build them with a small factory."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "lstm-implementation-from-scratch-1"
   },
   "source": [
    "class LSTMScratch(d2l.Module):\n",
    "    \"\"\"The long short-term memory (LSTM) cell implemented from scratch.\"\"\"\n",
    "    def __init__(self, num_inputs, num_hiddens, sigma=0.01):\n",
    "        super().__init__()\n",
    "        self.save_hyperparameters()\n",
    "\n",
    "        init_weight = lambda *shape: nn.Parameter(d2l.randn(*shape) * sigma)\n",
    "        triple = lambda: (init_weight(num_inputs, num_hiddens),\n",
    "                          init_weight(num_hiddens, num_hiddens),\n",
    "                          nn.Parameter(d2l.zeros(num_hiddens)))\n",
    "        self.W_xi, self.W_hi, self.b_i = triple()  # Input gate\n",
    "        self.W_xf, self.W_hf, self.b_f = triple()  # Forget gate\n",
    "        self.W_xo, self.W_ho, self.b_o = triple()  # Output gate\n",
    "        self.W_xc, self.W_hc, self.b_c = triple()  # Input node"
   ],
   "execution_count": 2,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=lstm-md-3 fw=all -->\n",
    "\n",
    "The forward computation walks the time axis just as the vanilla cell did,\n",
    "except that each step evaluates the equation, the equation,\n",
    "and the equation, and the carried state is now the pair\n",
    "$(\\mathbf{H}, \\mathbf{C})$."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "lstm-implementation-from-scratch-2"
   },
   "source": [
    "@d2l.add_to_class(LSTMScratch)\n",
    "def forward(self, inputs, H_C=None):\n",
    "    if H_C is None:\n",
    "        # Initial state with shape: (batch_size, num_hiddens)\n",
    "        H = d2l.zeros((inputs.shape[1], self.num_hiddens),\n",
    "                      device=inputs.device)\n",
    "        C = d2l.zeros((inputs.shape[1], self.num_hiddens),\n",
    "                      device=inputs.device)\n",
    "    else:\n",
    "        H, C = H_C\n",
    "    outputs = []\n",
    "    for X in inputs:\n",
    "        I = d2l.sigmoid(d2l.matmul(X, self.W_xi) +\n",
    "                        d2l.matmul(H, self.W_hi) + self.b_i)\n",
    "        F = d2l.sigmoid(d2l.matmul(X, self.W_xf) +\n",
    "                        d2l.matmul(H, self.W_hf) + self.b_f)\n",
    "        O = d2l.sigmoid(d2l.matmul(X, self.W_xo) +\n",
    "                        d2l.matmul(H, self.W_ho) + self.b_o)\n",
    "        C_tilde = d2l.tanh(d2l.matmul(X, self.W_xc) +\n",
    "                           d2l.matmul(H, self.W_hc) + self.b_c)\n",
    "        C = F * C + I * C_tilde\n",
    "        H = O * d2l.tanh(C)\n",
    "        outputs.append(H)\n",
    "    return outputs, (H, C)"
   ],
   "execution_count": 3,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=lstm-md-4 fw=all -->\n",
    "\n",
    "A quick shape check confirms that the cell consumes a\n",
    "(`num_steps`, `batch_size`, `num_inputs`) tensor and that both pieces of\n",
    "state keep the (`batch_size`, `num_hiddens`) shape from step to step."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "lstm-implementation-from-scratch-3"
   },
   "source": [
    "lstm = LSTMScratch(num_inputs=16, num_hiddens=32)\n",
    "X = d2l.ones((9, 4, 16))  # (num_steps, batch_size, num_inputs)\n",
    "outputs, (H, C) = lstm(X)\n",
    "d2l.check_shape(outputs[-1], (4, 32))\n",
    "d2l.check_shape(H, (4, 32))\n",
    "d2l.check_shape(C, (4, 32))"
   ],
   "execution_count": 4,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=lstm-md-5 fw=all -->\n",
    "\n",
    "### Training\n",
    "\n",
    "Does the gated cell actually beat the vanilla RNN? To make the comparison\n",
    "mean something we reuse the recipe of that section unchanged:\n",
    "*The Time Machine* under the 1,024-token BPE tokenizer, 50,000 training\n",
    "windows of 32 tokens, embedding dimension 64, 128 hidden units, ten epochs\n",
    "with gradients clipped to norm 1."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "lstm-training-1"
   },
   "source": [
    "data = d2l.TimeMachine(batch_size=1024, num_steps=32,\n",
    "                       num_train=50000, num_val=5000)"
   ],
   "execution_count": 5,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=lstm-md-6 fw=all -->\n",
    "\n",
    "The language-model scaffold from that section accepts the new\n",
    "cell without modification, because the cell keeps the same interface:\n",
    "embeddings in, per-step hidden states out. Only the recurrence changed."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "lstm-training-2"
   },
   "source": [
    "lstm = LSTMScratch(num_inputs=64, num_hiddens=128)\n",
    "model = d2l.RNNLMScratch(lstm, vocab_size=len(data.vocab), lr=4)"
   ],
   "execution_count": 6,
   "outputs": []
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "lstm-training-3"
   },
   "source": [
    "trainer = d2l.Trainer(max_epochs=10, gradient_clip_val=1, num_gpus=1)\n",
    "model.board.yscale = 'log'\n",
    "trainer.fit(model, data)"
   ],
   "execution_count": 7,
   "outputs": [
    {
     "output_type": "display_data",
     "data": {
      "image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"240.6625pt\" height=\"183.35625pt\" viewBox=\"0 0 240.6625 183.35625\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n <metadata>\n  <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n   <cc:Work>\n    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n    <dc:date>2026-07-19T20:54:54.521565</dc:date>\n    <dc:format>image/svg+xml</dc:format>\n    <dc:creator>\n     <cc:Agent>\n      <dc:title>Matplotlib v3.10.8, https://matplotlib.org/</dc:title>\n     </cc:Agent>\n    </dc:creator>\n   </cc:Work>\n  </rdf:RDF>\n </metadata>\n <defs>\n  <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n </defs>\n <g id=\"figure_1\">\n  <g id=\"patch_1\">\n   <path d=\"M 0 183.35625 \nL 240.6625 183.35625 \nL 240.6625 0 \nL 0 0 \nz\n\" style=\"fill: #ffffff\"/>\n  </g>\n  <g id=\"axes_1\">\n   <g id=\"patch_2\">\n    <path d=\"M 31.8 145.8 \nL 227.1 145.8 \nL 227.1 7.2 \nL 31.8 7.2 \nz\n\" style=\"fill: #ffffff\"/>\n   </g>\n   <g id=\"matplotlib.axis_1\">\n    <g id=\"xtick_1\">\n     <g id=\"line2d_1\">\n      <defs>\n       <path id=\"md9c93725d9\" d=\"M 0 0 \nL 0 3.5 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#md9c93725d9\" x=\"31.8\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_1\">\n      <!-- 0 -->\n      <g transform=\"translate(28.61875 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-30\" d=\"M 2034 4250 \nQ 1547 4250 1301 3770 \nQ 1056 3291 1056 2328 \nQ 1056 1369 1301 889 \nQ 1547 409 2034 409 \nQ 2525 409 2770 889 \nQ 3016 1369 3016 2328 \nQ 3016 3291 2770 3770 \nQ 2525 4250 2034 4250 \nz\nM 2034 4750 \nQ 2819 4750 3233 4129 \nQ 3647 3509 3647 2328 \nQ 3647 1150 3233 529 \nQ 2819 -91 2034 -91 \nQ 1250 -91 836 529 \nQ 422 1150 422 2328 \nQ 422 3509 836 4129 \nQ 1250 4750 2034 4750 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-30\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_2\">\n     <g id=\"line2d_2\">\n      <g>\n       <use xlink:href=\"#md9c93725d9\" x=\"70.86\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_2\">\n      <!-- 2 -->\n      <g transform=\"translate(67.67875 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-32\" d=\"M 1228 531 \nL 3431 531 \nL 3431 0 \nL 469 0 \nL 469 531 \nQ 828 903 1448 1529 \nQ 2069 2156 2228 2338 \nQ 2531 2678 2651 2914 \nQ 2772 3150 2772 3378 \nQ 2772 3750 2511 3984 \nQ 2250 4219 1831 4219 \nQ 1534 4219 1204 4116 \nQ 875 4013 500 3803 \nL 500 4441 \nQ 881 4594 1212 4672 \nQ 1544 4750 1819 4750 \nQ 2544 4750 2975 4387 \nQ 3406 4025 3406 3419 \nQ 3406 3131 3298 2873 \nQ 3191 2616 2906 2266 \nQ 2828 2175 2409 1742 \nQ 1991 1309 1228 531 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-32\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_3\">\n     <g id=\"line2d_3\">\n      <g>\n       <use xlink:href=\"#md9c93725d9\" x=\"109.92\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_3\">\n      <!-- 4 -->\n      <g transform=\"translate(106.73875 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-34\" d=\"M 2419 4116 \nL 825 1625 \nL 2419 1625 \nL 2419 4116 \nz\nM 2253 4666 \nL 3047 4666 \nL 3047 1625 \nL 3713 1625 \nL 3713 1100 \nL 3047 1100 \nL 3047 0 \nL 2419 0 \nL 2419 1100 \nL 313 1100 \nL 313 1709 \nL 2253 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-34\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_4\">\n     <g id=\"line2d_4\">\n      <g>\n       <use xlink:href=\"#md9c93725d9\" x=\"148.98\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_4\">\n      <!-- 6 -->\n      <g transform=\"translate(145.79875 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-36\" d=\"M 2113 2584 \nQ 1688 2584 1439 2293 \nQ 1191 2003 1191 1497 \nQ 1191 994 1439 701 \nQ 1688 409 2113 409 \nQ 2538 409 2786 701 \nQ 3034 994 3034 1497 \nQ 3034 2003 2786 2293 \nQ 2538 2584 2113 2584 \nz\nM 3366 4563 \nL 3366 3988 \nQ 3128 4100 2886 4159 \nQ 2644 4219 2406 4219 \nQ 1781 4219 1451 3797 \nQ 1122 3375 1075 2522 \nQ 1259 2794 1537 2939 \nQ 1816 3084 2150 3084 \nQ 2853 3084 3261 2657 \nQ 3669 2231 3669 1497 \nQ 3669 778 3244 343 \nQ 2819 -91 2113 -91 \nQ 1303 -91 875 529 \nQ 447 1150 447 2328 \nQ 447 3434 972 4092 \nQ 1497 4750 2381 4750 \nQ 2619 4750 2861 4703 \nQ 3103 4656 3366 4563 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-36\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_5\">\n     <g id=\"line2d_5\">\n      <g>\n       <use xlink:href=\"#md9c93725d9\" x=\"188.04\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_5\">\n      <!-- 8 -->\n      <g transform=\"translate(184.85875 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-38\" d=\"M 2034 2216 \nQ 1584 2216 1326 1975 \nQ 1069 1734 1069 1313 \nQ 1069 891 1326 650 \nQ 1584 409 2034 409 \nQ 2484 409 2743 651 \nQ 3003 894 3003 1313 \nQ 3003 1734 2745 1975 \nQ 2488 2216 2034 2216 \nz\nM 1403 2484 \nQ 997 2584 770 2862 \nQ 544 3141 544 3541 \nQ 544 4100 942 4425 \nQ 1341 4750 2034 4750 \nQ 2731 4750 3128 4425 \nQ 3525 4100 3525 3541 \nQ 3525 3141 3298 2862 \nQ 3072 2584 2669 2484 \nQ 3125 2378 3379 2068 \nQ 3634 1759 3634 1313 \nQ 3634 634 3220 271 \nQ 2806 -91 2034 -91 \nQ 1263 -91 848 271 \nQ 434 634 434 1313 \nQ 434 1759 690 2068 \nQ 947 2378 1403 2484 \nz\nM 1172 3481 \nQ 1172 3119 1398 2916 \nQ 1625 2713 2034 2713 \nQ 2441 2713 2670 2916 \nQ 2900 3119 2900 3481 \nQ 2900 3844 2670 4047 \nQ 2441 4250 2034 4250 \nQ 1625 4250 1398 4047 \nQ 1172 3844 1172 3481 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-38\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_6\">\n     <g id=\"line2d_6\">\n      <g>\n       <use xlink:href=\"#md9c93725d9\" x=\"227.1\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_6\">\n      <!-- 10 -->\n      <g transform=\"translate(220.7375 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-31\" d=\"M 794 531 \nL 1825 531 \nL 1825 4091 \nL 703 3866 \nL 703 4441 \nL 1819 4666 \nL 2450 4666 \nL 2450 531 \nL 3481 531 \nL 3481 0 \nL 794 0 \nL 794 531 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-31\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"text_7\">\n     <!-- epoch -->\n     <g transform=\"translate(114.221875 174.076563) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-65\" d=\"M 3597 1894 \nL 3597 1613 \nL 953 1613 \nQ 991 1019 1311 708 \nQ 1631 397 2203 397 \nQ 2534 397 2845 478 \nQ 3156 559 3463 722 \nL 3463 178 \nQ 3153 47 2828 -22 \nQ 2503 -91 2169 -91 \nQ 1331 -91 842 396 \nQ 353 884 353 1716 \nQ 353 2575 817 3079 \nQ 1281 3584 2069 3584 \nQ 2775 3584 3186 3129 \nQ 3597 2675 3597 1894 \nz\nM 3022 2063 \nQ 3016 2534 2758 2815 \nQ 2500 3097 2075 3097 \nQ 1594 3097 1305 2825 \nQ 1016 2553 972 2059 \nL 3022 2063 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-70\" d=\"M 1159 525 \nL 1159 -1331 \nL 581 -1331 \nL 581 3500 \nL 1159 3500 \nL 1159 2969 \nQ 1341 3281 1617 3432 \nQ 1894 3584 2278 3584 \nQ 2916 3584 3314 3078 \nQ 3713 2572 3713 1747 \nQ 3713 922 3314 415 \nQ 2916 -91 2278 -91 \nQ 1894 -91 1617 61 \nQ 1341 213 1159 525 \nz\nM 3116 1747 \nQ 3116 2381 2855 2742 \nQ 2594 3103 2138 3103 \nQ 1681 3103 1420 2742 \nQ 1159 2381 1159 1747 \nQ 1159 1113 1420 752 \nQ 1681 391 2138 391 \nQ 2594 391 2855 752 \nQ 3116 1113 3116 1747 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-6f\" d=\"M 1959 3097 \nQ 1497 3097 1228 2736 \nQ 959 2375 959 1747 \nQ 959 1119 1226 758 \nQ 1494 397 1959 397 \nQ 2419 397 2687 759 \nQ 2956 1122 2956 1747 \nQ 2956 2369 2687 2733 \nQ 2419 3097 1959 3097 \nz\nM 1959 3584 \nQ 2709 3584 3137 3096 \nQ 3566 2609 3566 1747 \nQ 3566 888 3137 398 \nQ 2709 -91 1959 -91 \nQ 1206 -91 779 398 \nQ 353 888 353 1747 \nQ 353 2609 779 3096 \nQ 1206 3584 1959 3584 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-63\" d=\"M 3122 3366 \nL 3122 2828 \nQ 2878 2963 2633 3030 \nQ 2388 3097 2138 3097 \nQ 1578 3097 1268 2742 \nQ 959 2388 959 1747 \nQ 959 1106 1268 751 \nQ 1578 397 2138 397 \nQ 2388 397 2633 464 \nQ 2878 531 3122 666 \nL 3122 134 \nQ 2881 22 2623 -34 \nQ 2366 -91 2075 -91 \nQ 1284 -91 818 406 \nQ 353 903 353 1747 \nQ 353 2603 823 3093 \nQ 1294 3584 2113 3584 \nQ 2378 3584 2631 3529 \nQ 2884 3475 3122 3366 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-68\" d=\"M 3513 2113 \nL 3513 0 \nL 2938 0 \nL 2938 2094 \nQ 2938 2591 2744 2837 \nQ 2550 3084 2163 3084 \nQ 1697 3084 1428 2787 \nQ 1159 2491 1159 1978 \nL 1159 0 \nL 581 0 \nL 581 4863 \nL 1159 4863 \nL 1159 2956 \nQ 1366 3272 1645 3428 \nQ 1925 3584 2291 3584 \nQ 2894 3584 3203 3211 \nQ 3513 2838 3513 2113 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-65\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(61.523438 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6f\" transform=\"translate(125 0)\"/>\n      <use xlink:href=\"#DejaVuSans-63\" transform=\"translate(186.181641 0)\"/>\n      <use xlink:href=\"#DejaVuSans-68\" transform=\"translate(241.162109 0)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"matplotlib.axis_2\">\n    <g id=\"ytick_1\">\n     <g id=\"line2d_7\">\n      <defs>\n       <path id=\"md0c9b0c3d2\" d=\"M 0 0 \nL -3.5 0 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#md0c9b0c3d2\" x=\"31.8\" y=\"126.809723\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_8\">\n      <!-- $\\mathdefault{10^{2}}$ -->\n      <g transform=\"translate(7.2 130.608941) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-31\" transform=\"translate(0 0.765625)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0.765625)\"/>\n       <use xlink:href=\"#DejaVuSans-32\" transform=\"translate(128.203125 39.046875) scale(0.7)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_2\">\n     <g id=\"line2d_8\">\n      <defs>\n       <path id=\"mbb81e0fb33\" d=\"M 0 0 \nL -2 0 \n\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#mbb81e0fb33\" x=\"31.8\" y=\"139.278121\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_3\">\n     <g id=\"line2d_9\">\n      <g>\n       <use xlink:href=\"#mbb81e0fb33\" x=\"31.8\" y=\"132.69686\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_4\">\n     <g id=\"line2d_10\">\n      <g>\n       <use xlink:href=\"#mbb81e0fb33\" x=\"31.8\" y=\"88.079341\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_5\">\n     <g id=\"line2d_11\">\n      <g>\n       <use xlink:href=\"#mbb81e0fb33\" x=\"31.8\" y=\"65.42352\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_6\">\n     <g id=\"line2d_12\">\n      <g>\n       <use xlink:href=\"#mbb81e0fb33\" x=\"31.8\" y=\"49.348959\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_7\">\n     <g id=\"line2d_13\">\n      <g>\n       <use xlink:href=\"#mbb81e0fb33\" x=\"31.8\" y=\"36.880561\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_8\">\n     <g id=\"line2d_14\">\n      <g>\n       <use xlink:href=\"#mbb81e0fb33\" x=\"31.8\" y=\"26.693138\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_9\">\n     <g id=\"line2d_15\">\n      <g>\n       <use xlink:href=\"#mbb81e0fb33\" x=\"31.8\" y=\"18.079794\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_10\">\n     <g id=\"line2d_16\">\n      <g>\n       <use xlink:href=\"#mbb81e0fb33\" x=\"31.8\" y=\"10.618577\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n   </g>\n   <g id=\"line2d_17\">\n    <path d=\"M 36.383571 13.5 \nL 45.949286 43.435435 \nL 55.515 56.391038 \nL 65.080714 62.294548 \nL 74.646429 67.269171 \nL 84.212143 72.526309 \nL 93.777857 77.770764 \nL 103.343571 83.213499 \nL 112.909286 88.0169 \nL 122.475 93.328865 \nL 132.040714 98.561265 \nL 141.606429 103.675309 \nL 151.172143 108.57758 \nL 160.737857 113.610416 \nL 170.303571 118.326021 \nL 179.869286 122.94547 \nL 189.435 127.612454 \nL 199.000714 131.5286 \nL 208.566429 135.544459 \nL 218.132143 139.5 \n\" clip-path=\"url(#p7df3d8e359)\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_18\">\n    <path d=\"M 51.33 57.985893 \nL 70.86 69.660521 \nL 90.39 77.973911 \nL 109.92 88.683483 \nL 129.45 96.99195 \nL 148.98 104.564086 \nL 168.51 110.259017 \nL 188.04 116.308015 \nL 207.57 120.210208 \nL 227.1 124.802022 \n\" clip-path=\"url(#p7df3d8e359)\" style=\"fill: none; stroke-dasharray: 5.55,2.4; stroke-dashoffset: 0; stroke: #ff7f0e; stroke-width: 1.5\"/>\n   </g>\n   <g id=\"patch_3\">\n    <path d=\"M 31.8 145.8 \nL 31.8 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_4\">\n    <path d=\"M 227.1 145.8 \nL 227.1 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_5\">\n    <path d=\"M 31.8 145.8 \nL 227.1 145.8 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_6\">\n    <path d=\"M 31.8 7.2 \nL 227.1 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"legend_1\">\n    <g id=\"patch_7\">\n     <path d=\"M 144.35 45.1125 \nL 220.1 45.1125 \nQ 222.1 45.1125 222.1 43.1125 \nL 222.1 14.2 \nQ 222.1 12.2 220.1 12.2 \nL 144.35 12.2 \nQ 142.35 12.2 142.35 14.2 \nL 142.35 43.1125 \nQ 142.35 45.1125 144.35 45.1125 \nz\n\" style=\"fill: #ffffff; opacity: 0.8; stroke: #cccccc; stroke-linejoin: miter\"/>\n    </g>\n    <g id=\"line2d_19\">\n     <path d=\"M 146.35 20.298438 \nL 156.35 20.298438 \nL 166.35 20.298438 \n\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n    </g>\n    <g id=\"text_9\">\n     <!-- train_ppl -->\n     <g transform=\"translate(174.35 23.798438) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-74\" d=\"M 1172 4494 \nL 1172 3500 \nL 2356 3500 \nL 2356 3053 \nL 1172 3053 \nL 1172 1153 \nQ 1172 725 1289 603 \nQ 1406 481 1766 481 \nL 2356 481 \nL 2356 0 \nL 1766 0 \nQ 1100 0 847 248 \nQ 594 497 594 1153 \nL 594 3053 \nL 172 3053 \nL 172 3500 \nL 594 3500 \nL 594 4494 \nL 1172 4494 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-72\" d=\"M 2631 2963 \nQ 2534 3019 2420 3045 \nQ 2306 3072 2169 3072 \nQ 1681 3072 1420 2755 \nQ 1159 2438 1159 1844 \nL 1159 0 \nL 581 0 \nL 581 3500 \nL 1159 3500 \nL 1159 2956 \nQ 1341 3275 1631 3429 \nQ 1922 3584 2338 3584 \nQ 2397 3584 2469 3576 \nQ 2541 3569 2628 3553 \nL 2631 2963 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-61\" d=\"M 2194 1759 \nQ 1497 1759 1228 1600 \nQ 959 1441 959 1056 \nQ 959 750 1161 570 \nQ 1363 391 1709 391 \nQ 2188 391 2477 730 \nQ 2766 1069 2766 1631 \nL 2766 1759 \nL 2194 1759 \nz\nM 3341 1997 \nL 3341 0 \nL 2766 0 \nL 2766 531 \nQ 2569 213 2275 61 \nQ 1981 -91 1556 -91 \nQ 1019 -91 701 211 \nQ 384 513 384 1019 \nQ 384 1609 779 1909 \nQ 1175 2209 1959 2209 \nL 2766 2209 \nL 2766 2266 \nQ 2766 2663 2505 2880 \nQ 2244 3097 1772 3097 \nQ 1472 3097 1187 3025 \nQ 903 2953 641 2809 \nL 641 3341 \nQ 956 3463 1253 3523 \nQ 1550 3584 1831 3584 \nQ 2591 3584 2966 3190 \nQ 3341 2797 3341 1997 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-69\" d=\"M 603 3500 \nL 1178 3500 \nL 1178 0 \nL 603 0 \nL 603 3500 \nz\nM 603 4863 \nL 1178 4863 \nL 1178 4134 \nL 603 4134 \nL 603 4863 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-6e\" d=\"M 3513 2113 \nL 3513 0 \nL 2938 0 \nL 2938 2094 \nQ 2938 2591 2744 2837 \nQ 2550 3084 2163 3084 \nQ 1697 3084 1428 2787 \nQ 1159 2491 1159 1978 \nL 1159 0 \nL 581 0 \nL 581 3500 \nL 1159 3500 \nL 1159 2956 \nQ 1366 3272 1645 3428 \nQ 1925 3584 2291 3584 \nQ 2894 3584 3203 3211 \nQ 3513 2838 3513 2113 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-5f\" d=\"M 3263 -1063 \nL 3263 -1509 \nL -63 -1509 \nL -63 -1063 \nL 3263 -1063 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-6c\" d=\"M 603 4863 \nL 1178 4863 \nL 1178 0 \nL 603 0 \nL 603 4863 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-74\"/>\n      <use xlink:href=\"#DejaVuSans-72\" transform=\"translate(39.208984 0)\"/>\n      <use xlink:href=\"#DejaVuSans-61\" transform=\"translate(80.322266 0)\"/>\n      <use xlink:href=\"#DejaVuSans-69\" transform=\"translate(141.601562 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6e\" transform=\"translate(169.384766 0)\"/>\n      <use xlink:href=\"#DejaVuSans-5f\" transform=\"translate(232.763672 0)\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(282.763672 0)\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(346.240234 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6c\" transform=\"translate(409.716797 0)\"/>\n     </g>\n    </g>\n    <g id=\"line2d_20\">\n     <path d=\"M 146.35 35.254688 \nL 156.35 35.254688 \nL 166.35 35.254688 \n\" style=\"fill: none; stroke-dasharray: 5.55,2.4; stroke-dashoffset: 0; stroke: #ff7f0e; stroke-width: 1.5\"/>\n    </g>\n    <g id=\"text_10\">\n     <!-- val_ppl -->\n     <g transform=\"translate(174.35 38.754688) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-76\" d=\"M 191 3500 \nL 800 3500 \nL 1894 563 \nL 2988 3500 \nL 3597 3500 \nL 2284 0 \nL 1503 0 \nL 191 3500 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-76\"/>\n      <use xlink:href=\"#DejaVuSans-61\" transform=\"translate(59.179688 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6c\" transform=\"translate(120.458984 0)\"/>\n      <use xlink:href=\"#DejaVuSans-5f\" transform=\"translate(148.242188 0)\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(198.242188 0)\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(261.71875 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6c\" transform=\"translate(325.195312 0)\"/>\n     </g>\n    </g>\n   </g>\n  </g>\n </g>\n <defs>\n  <clipPath id=\"p7df3d8e359\">\n   <rect x=\"31.8\" y=\"7.2\" width=\"195.3\" height=\"138.6\"/>\n  </clipPath>\n </defs>\n</svg>\n"
     },
     "metadata": {}
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=lstm-md-7 fw=all -->\n",
    "\n",
    "We will train several models in this section, so we wrap the perplexity\n",
    "readout in a helper and start a small scoreboard."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "lstm-training-4"
   },
   "source": [
    "def val_ppl(model):\n",
    "    return float(model.board.data['val_ppl'][-1].y)\n",
    "\n",
    "ppls = {'LSTM (scratch)': val_ppl(model)}\n",
    "print(f\"validation perplexity {ppls['LSTM (scratch)']:.1f}\")"
   ],
   "execution_count": 8,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "validation perplexity 103.7\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=lstm-md-8 fw=all -->\n",
    "\n",
    "Under the identical recipe, the vanilla RNN of that section\n",
    "reached a validation perplexity in the neighborhood of 90 to 110, and our\n",
    "from-scratch LSTM lands in the same range. That may disappoint: where is\n",
    "the payoff? Two things hold this run back, and both are worth\n",
    "understanding. First, ten epochs is a short budget for a cell whose\n",
    "recurrent core has four heads and $4h(d + h + 1)$ weights instead of\n",
    "$h(d + h + 1)$: the curves are still moving when the budget ends, so the\n",
    "model is undertrained rather than out of capacity. Second, our naive\n",
    "initialization starts every gate half-open: with weights near zero,\n",
    "$\\sigma(0) = 0.5$, so the untrained forget gate halves the cell state at\n",
    "every step, and the carousel only starts carrying once training pushes the\n",
    "forget-gate biases up. Practitioners initialize\n",
    "$\\mathbf{b}_\\textrm{f}$ to 1 for exactly this reason, and an exercise asks\n",
    "you to try both remedies. Within this section's fixed budget, the payoff\n",
    "of gating will show most clearly in the streamlined GRU below.\n",
    "\n",
    "### Concise Implementation\n",
    "\n",
    "Every framework ships an LSTM layer that fuses the cell and the time loop\n",
    "into one optimized call; as that section measured for the\n",
    "vanilla cell, the fused kernel is what you use for any model you train at\n",
    "length, and for the LSTM the gap is larger because each hand-rolled step\n",
    "launches eight matrix products. The `LSTM` class below keeps the interface\n",
    "of `LSTMScratch` and adds a `num_layers` argument, which stacks recurrent\n",
    "layers on top of one another; we use it later in this section, and the\n",
    "following sections of this chapter reuse it as their recurrent baseline.\n",
    "As before, `num_inputs` is the width of the per-step input vectors."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "lstm-concise-implementation-1"
   },
   "source": [
    "class LSTM(d2l.RNN):\n",
    "    \"\"\"The multilayer LSTM model implemented with high-level APIs.\"\"\"\n",
    "    def __init__(self, num_inputs, num_hiddens, num_layers=1, dropout=0):\n",
    "        d2l.Module.__init__(self)\n",
    "        self.save_hyperparameters()\n",
    "        self.rnn = nn.LSTM(num_inputs, num_hiddens, num_layers,\n",
    "                           dropout=dropout)\n",
    "\n",
    "    def forward(self, inputs, H_C=None):\n",
    "        return self.rnn(inputs, H_C)"
   ],
   "execution_count": 9,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=lstm-md-9 fw=all -->\n",
    "\n",
    "The concise language model `d2l.RNNLM` wraps it exactly as its scratch\n",
    "counterpart did, and training is the same call with the same\n",
    "hyperparameters."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "lstm-concise-implementation-2"
   },
   "source": [
    "lstm = LSTM(num_inputs=64, num_hiddens=128)\n",
    "model = d2l.RNNLM(lstm, vocab_size=len(data.vocab), lr=4)"
   ],
   "execution_count": 10,
   "outputs": []
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "lstm-concise-implementation-3"
   },
   "source": [
    "trainer = d2l.Trainer(max_epochs=10, gradient_clip_val=1, num_gpus=1)\n",
    "model.board.yscale = 'log'\n",
    "trainer.fit(model, data)"
   ],
   "execution_count": 11,
   "outputs": [
    {
     "output_type": "display_data",
     "data": {
      "image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"240.6625pt\" height=\"183.35625pt\" viewBox=\"0 0 240.6625 183.35625\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n <metadata>\n  <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n   <cc:Work>\n    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n    <dc:date>2026-07-19T20:55:01.500440</dc:date>\n    <dc:format>image/svg+xml</dc:format>\n    <dc:creator>\n     <cc:Agent>\n      <dc:title>Matplotlib v3.10.8, https://matplotlib.org/</dc:title>\n     </cc:Agent>\n    </dc:creator>\n   </cc:Work>\n  </rdf:RDF>\n </metadata>\n <defs>\n  <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n </defs>\n <g id=\"figure_1\">\n  <g id=\"patch_1\">\n   <path d=\"M 0 183.35625 \nL 240.6625 183.35625 \nL 240.6625 0 \nL 0 0 \nz\n\" style=\"fill: #ffffff\"/>\n  </g>\n  <g id=\"axes_1\">\n   <g id=\"patch_2\">\n    <path d=\"M 31.8 145.8 \nL 227.1 145.8 \nL 227.1 7.2 \nL 31.8 7.2 \nz\n\" style=\"fill: #ffffff\"/>\n   </g>\n   <g id=\"matplotlib.axis_1\">\n    <g id=\"xtick_1\">\n     <g id=\"line2d_1\">\n      <defs>\n       <path id=\"m5d5bde2585\" d=\"M 0 0 \nL 0 3.5 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#m5d5bde2585\" x=\"31.8\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_1\">\n      <!-- 0 -->\n      <g transform=\"translate(28.61875 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-30\" d=\"M 2034 4250 \nQ 1547 4250 1301 3770 \nQ 1056 3291 1056 2328 \nQ 1056 1369 1301 889 \nQ 1547 409 2034 409 \nQ 2525 409 2770 889 \nQ 3016 1369 3016 2328 \nQ 3016 3291 2770 3770 \nQ 2525 4250 2034 4250 \nz\nM 2034 4750 \nQ 2819 4750 3233 4129 \nQ 3647 3509 3647 2328 \nQ 3647 1150 3233 529 \nQ 2819 -91 2034 -91 \nQ 1250 -91 836 529 \nQ 422 1150 422 2328 \nQ 422 3509 836 4129 \nQ 1250 4750 2034 4750 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-30\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_2\">\n     <g id=\"line2d_2\">\n      <g>\n       <use xlink:href=\"#m5d5bde2585\" x=\"70.86\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_2\">\n      <!-- 2 -->\n      <g transform=\"translate(67.67875 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-32\" d=\"M 1228 531 \nL 3431 531 \nL 3431 0 \nL 469 0 \nL 469 531 \nQ 828 903 1448 1529 \nQ 2069 2156 2228 2338 \nQ 2531 2678 2651 2914 \nQ 2772 3150 2772 3378 \nQ 2772 3750 2511 3984 \nQ 2250 4219 1831 4219 \nQ 1534 4219 1204 4116 \nQ 875 4013 500 3803 \nL 500 4441 \nQ 881 4594 1212 4672 \nQ 1544 4750 1819 4750 \nQ 2544 4750 2975 4387 \nQ 3406 4025 3406 3419 \nQ 3406 3131 3298 2873 \nQ 3191 2616 2906 2266 \nQ 2828 2175 2409 1742 \nQ 1991 1309 1228 531 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-32\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_3\">\n     <g id=\"line2d_3\">\n      <g>\n       <use xlink:href=\"#m5d5bde2585\" x=\"109.92\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_3\">\n      <!-- 4 -->\n      <g transform=\"translate(106.73875 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-34\" d=\"M 2419 4116 \nL 825 1625 \nL 2419 1625 \nL 2419 4116 \nz\nM 2253 4666 \nL 3047 4666 \nL 3047 1625 \nL 3713 1625 \nL 3713 1100 \nL 3047 1100 \nL 3047 0 \nL 2419 0 \nL 2419 1100 \nL 313 1100 \nL 313 1709 \nL 2253 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-34\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_4\">\n     <g id=\"line2d_4\">\n      <g>\n       <use xlink:href=\"#m5d5bde2585\" x=\"148.98\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_4\">\n      <!-- 6 -->\n      <g transform=\"translate(145.79875 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-36\" d=\"M 2113 2584 \nQ 1688 2584 1439 2293 \nQ 1191 2003 1191 1497 \nQ 1191 994 1439 701 \nQ 1688 409 2113 409 \nQ 2538 409 2786 701 \nQ 3034 994 3034 1497 \nQ 3034 2003 2786 2293 \nQ 2538 2584 2113 2584 \nz\nM 3366 4563 \nL 3366 3988 \nQ 3128 4100 2886 4159 \nQ 2644 4219 2406 4219 \nQ 1781 4219 1451 3797 \nQ 1122 3375 1075 2522 \nQ 1259 2794 1537 2939 \nQ 1816 3084 2150 3084 \nQ 2853 3084 3261 2657 \nQ 3669 2231 3669 1497 \nQ 3669 778 3244 343 \nQ 2819 -91 2113 -91 \nQ 1303 -91 875 529 \nQ 447 1150 447 2328 \nQ 447 3434 972 4092 \nQ 1497 4750 2381 4750 \nQ 2619 4750 2861 4703 \nQ 3103 4656 3366 4563 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-36\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_5\">\n     <g id=\"line2d_5\">\n      <g>\n       <use xlink:href=\"#m5d5bde2585\" x=\"188.04\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_5\">\n      <!-- 8 -->\n      <g transform=\"translate(184.85875 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-38\" d=\"M 2034 2216 \nQ 1584 2216 1326 1975 \nQ 1069 1734 1069 1313 \nQ 1069 891 1326 650 \nQ 1584 409 2034 409 \nQ 2484 409 2743 651 \nQ 3003 894 3003 1313 \nQ 3003 1734 2745 1975 \nQ 2488 2216 2034 2216 \nz\nM 1403 2484 \nQ 997 2584 770 2862 \nQ 544 3141 544 3541 \nQ 544 4100 942 4425 \nQ 1341 4750 2034 4750 \nQ 2731 4750 3128 4425 \nQ 3525 4100 3525 3541 \nQ 3525 3141 3298 2862 \nQ 3072 2584 2669 2484 \nQ 3125 2378 3379 2068 \nQ 3634 1759 3634 1313 \nQ 3634 634 3220 271 \nQ 2806 -91 2034 -91 \nQ 1263 -91 848 271 \nQ 434 634 434 1313 \nQ 434 1759 690 2068 \nQ 947 2378 1403 2484 \nz\nM 1172 3481 \nQ 1172 3119 1398 2916 \nQ 1625 2713 2034 2713 \nQ 2441 2713 2670 2916 \nQ 2900 3119 2900 3481 \nQ 2900 3844 2670 4047 \nQ 2441 4250 2034 4250 \nQ 1625 4250 1398 4047 \nQ 1172 3844 1172 3481 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-38\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_6\">\n     <g id=\"line2d_6\">\n      <g>\n       <use xlink:href=\"#m5d5bde2585\" x=\"227.1\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_6\">\n      <!-- 10 -->\n      <g transform=\"translate(220.7375 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-31\" d=\"M 794 531 \nL 1825 531 \nL 1825 4091 \nL 703 3866 \nL 703 4441 \nL 1819 4666 \nL 2450 4666 \nL 2450 531 \nL 3481 531 \nL 3481 0 \nL 794 0 \nL 794 531 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-31\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"text_7\">\n     <!-- epoch -->\n     <g transform=\"translate(114.221875 174.076563) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-65\" d=\"M 3597 1894 \nL 3597 1613 \nL 953 1613 \nQ 991 1019 1311 708 \nQ 1631 397 2203 397 \nQ 2534 397 2845 478 \nQ 3156 559 3463 722 \nL 3463 178 \nQ 3153 47 2828 -22 \nQ 2503 -91 2169 -91 \nQ 1331 -91 842 396 \nQ 353 884 353 1716 \nQ 353 2575 817 3079 \nQ 1281 3584 2069 3584 \nQ 2775 3584 3186 3129 \nQ 3597 2675 3597 1894 \nz\nM 3022 2063 \nQ 3016 2534 2758 2815 \nQ 2500 3097 2075 3097 \nQ 1594 3097 1305 2825 \nQ 1016 2553 972 2059 \nL 3022 2063 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-70\" d=\"M 1159 525 \nL 1159 -1331 \nL 581 -1331 \nL 581 3500 \nL 1159 3500 \nL 1159 2969 \nQ 1341 3281 1617 3432 \nQ 1894 3584 2278 3584 \nQ 2916 3584 3314 3078 \nQ 3713 2572 3713 1747 \nQ 3713 922 3314 415 \nQ 2916 -91 2278 -91 \nQ 1894 -91 1617 61 \nQ 1341 213 1159 525 \nz\nM 3116 1747 \nQ 3116 2381 2855 2742 \nQ 2594 3103 2138 3103 \nQ 1681 3103 1420 2742 \nQ 1159 2381 1159 1747 \nQ 1159 1113 1420 752 \nQ 1681 391 2138 391 \nQ 2594 391 2855 752 \nQ 3116 1113 3116 1747 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-6f\" d=\"M 1959 3097 \nQ 1497 3097 1228 2736 \nQ 959 2375 959 1747 \nQ 959 1119 1226 758 \nQ 1494 397 1959 397 \nQ 2419 397 2687 759 \nQ 2956 1122 2956 1747 \nQ 2956 2369 2687 2733 \nQ 2419 3097 1959 3097 \nz\nM 1959 3584 \nQ 2709 3584 3137 3096 \nQ 3566 2609 3566 1747 \nQ 3566 888 3137 398 \nQ 2709 -91 1959 -91 \nQ 1206 -91 779 398 \nQ 353 888 353 1747 \nQ 353 2609 779 3096 \nQ 1206 3584 1959 3584 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-63\" d=\"M 3122 3366 \nL 3122 2828 \nQ 2878 2963 2633 3030 \nQ 2388 3097 2138 3097 \nQ 1578 3097 1268 2742 \nQ 959 2388 959 1747 \nQ 959 1106 1268 751 \nQ 1578 397 2138 397 \nQ 2388 397 2633 464 \nQ 2878 531 3122 666 \nL 3122 134 \nQ 2881 22 2623 -34 \nQ 2366 -91 2075 -91 \nQ 1284 -91 818 406 \nQ 353 903 353 1747 \nQ 353 2603 823 3093 \nQ 1294 3584 2113 3584 \nQ 2378 3584 2631 3529 \nQ 2884 3475 3122 3366 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-68\" d=\"M 3513 2113 \nL 3513 0 \nL 2938 0 \nL 2938 2094 \nQ 2938 2591 2744 2837 \nQ 2550 3084 2163 3084 \nQ 1697 3084 1428 2787 \nQ 1159 2491 1159 1978 \nL 1159 0 \nL 581 0 \nL 581 4863 \nL 1159 4863 \nL 1159 2956 \nQ 1366 3272 1645 3428 \nQ 1925 3584 2291 3584 \nQ 2894 3584 3203 3211 \nQ 3513 2838 3513 2113 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-65\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(61.523438 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6f\" transform=\"translate(125 0)\"/>\n      <use xlink:href=\"#DejaVuSans-63\" transform=\"translate(186.181641 0)\"/>\n      <use xlink:href=\"#DejaVuSans-68\" transform=\"translate(241.162109 0)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"matplotlib.axis_2\">\n    <g id=\"ytick_1\">\n     <g id=\"line2d_7\">\n      <defs>\n       <path id=\"m82a28daf77\" d=\"M 0 0 \nL -3.5 0 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#m82a28daf77\" x=\"31.8\" y=\"106.783329\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_8\">\n      <!-- $\\mathdefault{10^{2}}$ -->\n      <g transform=\"translate(7.2 110.582547) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-31\" transform=\"translate(0 0.765625)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0.765625)\"/>\n       <use xlink:href=\"#DejaVuSans-32\" transform=\"translate(128.203125 39.046875) scale(0.7)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_2\">\n     <g id=\"line2d_8\">\n      <defs>\n       <path id=\"mef2576301c\" d=\"M 0 0 \nL -2 0 \n\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#mef2576301c\" x=\"31.8\" y=\"142.715366\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_3\">\n     <g id=\"line2d_9\">\n      <g>\n       <use xlink:href=\"#mef2576301c\" x=\"31.8\" y=\"133.264004\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_4\">\n     <g id=\"line2d_10\">\n      <g>\n       <use xlink:href=\"#mef2576301c\" x=\"31.8\" y=\"125.272991\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_5\">\n     <g id=\"line2d_11\">\n      <g>\n       <use xlink:href=\"#mef2576301c\" x=\"31.8\" y=\"118.350861\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_6\">\n     <g id=\"line2d_12\">\n      <g>\n       <use xlink:href=\"#mef2576301c\" x=\"31.8\" y=\"112.245109\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_7\">\n     <g id=\"line2d_13\">\n      <g>\n       <use xlink:href=\"#mef2576301c\" x=\"31.8\" y=\"70.851291\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_8\">\n     <g id=\"line2d_14\">\n      <g>\n       <use xlink:href=\"#mef2576301c\" x=\"31.8\" y=\"49.832396\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_9\">\n     <g id=\"line2d_15\">\n      <g>\n       <use xlink:href=\"#mef2576301c\" x=\"31.8\" y=\"34.919253\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_10\">\n     <g id=\"line2d_16\">\n      <g>\n       <use xlink:href=\"#mef2576301c\" x=\"31.8\" y=\"23.351721\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_11\">\n     <g id=\"line2d_17\">\n      <g>\n       <use xlink:href=\"#mef2576301c\" x=\"31.8\" y=\"13.900359\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n   </g>\n   <g id=\"line2d_18\">\n    <path d=\"M 36.383571 13.5 \nL 45.949286 41.737305 \nL 55.515 49.784957 \nL 65.080714 57.325282 \nL 74.646429 64.837726 \nL 84.212143 71.482306 \nL 93.777857 78.35679 \nL 103.343571 84.529749 \nL 112.909286 90.823462 \nL 122.475 96.571707 \nL 132.040714 102.344812 \nL 141.606429 107.564871 \nL 151.172143 112.618606 \nL 160.737857 117.164009 \nL 170.303571 121.563316 \nL 179.869286 125.587836 \nL 189.435 129.408106 \nL 199.000714 132.867938 \nL 208.566429 136.359246 \nL 218.132143 139.5 \n\" clip-path=\"url(#p0b8f9a4918)\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_19\">\n    <path d=\"M 51.33 51.300667 \nL 70.86 59.477224 \nL 90.39 73.810289 \nL 109.92 81.886637 \nL 129.45 89.994949 \nL 148.98 96.255146 \nL 168.51 101.409178 \nL 188.04 106.517163 \nL 207.57 108.676371 \nL 227.1 111.838394 \n\" clip-path=\"url(#p0b8f9a4918)\" style=\"fill: none; stroke-dasharray: 5.55,2.4; stroke-dashoffset: 0; stroke: #ff7f0e; stroke-width: 1.5\"/>\n   </g>\n   <g id=\"patch_3\">\n    <path d=\"M 31.8 145.8 \nL 31.8 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_4\">\n    <path d=\"M 227.1 145.8 \nL 227.1 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_5\">\n    <path d=\"M 31.8 145.8 \nL 227.1 145.8 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_6\">\n    <path d=\"M 31.8 7.2 \nL 227.1 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"legend_1\">\n    <g id=\"patch_7\">\n     <path d=\"M 144.35 45.1125 \nL 220.1 45.1125 \nQ 222.1 45.1125 222.1 43.1125 \nL 222.1 14.2 \nQ 222.1 12.2 220.1 12.2 \nL 144.35 12.2 \nQ 142.35 12.2 142.35 14.2 \nL 142.35 43.1125 \nQ 142.35 45.1125 144.35 45.1125 \nz\n\" style=\"fill: #ffffff; opacity: 0.8; stroke: #cccccc; stroke-linejoin: miter\"/>\n    </g>\n    <g id=\"line2d_20\">\n     <path d=\"M 146.35 20.298438 \nL 156.35 20.298438 \nL 166.35 20.298438 \n\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n    </g>\n    <g id=\"text_9\">\n     <!-- train_ppl -->\n     <g transform=\"translate(174.35 23.798438) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-74\" d=\"M 1172 4494 \nL 1172 3500 \nL 2356 3500 \nL 2356 3053 \nL 1172 3053 \nL 1172 1153 \nQ 1172 725 1289 603 \nQ 1406 481 1766 481 \nL 2356 481 \nL 2356 0 \nL 1766 0 \nQ 1100 0 847 248 \nQ 594 497 594 1153 \nL 594 3053 \nL 172 3053 \nL 172 3500 \nL 594 3500 \nL 594 4494 \nL 1172 4494 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-72\" d=\"M 2631 2963 \nQ 2534 3019 2420 3045 \nQ 2306 3072 2169 3072 \nQ 1681 3072 1420 2755 \nQ 1159 2438 1159 1844 \nL 1159 0 \nL 581 0 \nL 581 3500 \nL 1159 3500 \nL 1159 2956 \nQ 1341 3275 1631 3429 \nQ 1922 3584 2338 3584 \nQ 2397 3584 2469 3576 \nQ 2541 3569 2628 3553 \nL 2631 2963 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-61\" d=\"M 2194 1759 \nQ 1497 1759 1228 1600 \nQ 959 1441 959 1056 \nQ 959 750 1161 570 \nQ 1363 391 1709 391 \nQ 2188 391 2477 730 \nQ 2766 1069 2766 1631 \nL 2766 1759 \nL 2194 1759 \nz\nM 3341 1997 \nL 3341 0 \nL 2766 0 \nL 2766 531 \nQ 2569 213 2275 61 \nQ 1981 -91 1556 -91 \nQ 1019 -91 701 211 \nQ 384 513 384 1019 \nQ 384 1609 779 1909 \nQ 1175 2209 1959 2209 \nL 2766 2209 \nL 2766 2266 \nQ 2766 2663 2505 2880 \nQ 2244 3097 1772 3097 \nQ 1472 3097 1187 3025 \nQ 903 2953 641 2809 \nL 641 3341 \nQ 956 3463 1253 3523 \nQ 1550 3584 1831 3584 \nQ 2591 3584 2966 3190 \nQ 3341 2797 3341 1997 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-69\" d=\"M 603 3500 \nL 1178 3500 \nL 1178 0 \nL 603 0 \nL 603 3500 \nz\nM 603 4863 \nL 1178 4863 \nL 1178 4134 \nL 603 4134 \nL 603 4863 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-6e\" d=\"M 3513 2113 \nL 3513 0 \nL 2938 0 \nL 2938 2094 \nQ 2938 2591 2744 2837 \nQ 2550 3084 2163 3084 \nQ 1697 3084 1428 2787 \nQ 1159 2491 1159 1978 \nL 1159 0 \nL 581 0 \nL 581 3500 \nL 1159 3500 \nL 1159 2956 \nQ 1366 3272 1645 3428 \nQ 1925 3584 2291 3584 \nQ 2894 3584 3203 3211 \nQ 3513 2838 3513 2113 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-5f\" d=\"M 3263 -1063 \nL 3263 -1509 \nL -63 -1509 \nL -63 -1063 \nL 3263 -1063 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-6c\" d=\"M 603 4863 \nL 1178 4863 \nL 1178 0 \nL 603 0 \nL 603 4863 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-74\"/>\n      <use xlink:href=\"#DejaVuSans-72\" transform=\"translate(39.208984 0)\"/>\n      <use xlink:href=\"#DejaVuSans-61\" transform=\"translate(80.322266 0)\"/>\n      <use xlink:href=\"#DejaVuSans-69\" transform=\"translate(141.601562 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6e\" transform=\"translate(169.384766 0)\"/>\n      <use xlink:href=\"#DejaVuSans-5f\" transform=\"translate(232.763672 0)\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(282.763672 0)\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(346.240234 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6c\" transform=\"translate(409.716797 0)\"/>\n     </g>\n    </g>\n    <g id=\"line2d_21\">\n     <path d=\"M 146.35 35.254688 \nL 156.35 35.254688 \nL 166.35 35.254688 \n\" style=\"fill: none; stroke-dasharray: 5.55,2.4; stroke-dashoffset: 0; stroke: #ff7f0e; stroke-width: 1.5\"/>\n    </g>\n    <g id=\"text_10\">\n     <!-- val_ppl -->\n     <g transform=\"translate(174.35 38.754688) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-76\" d=\"M 191 3500 \nL 800 3500 \nL 1894 563 \nL 2988 3500 \nL 3597 3500 \nL 2284 0 \nL 1503 0 \nL 191 3500 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-76\"/>\n      <use xlink:href=\"#DejaVuSans-61\" transform=\"translate(59.179688 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6c\" transform=\"translate(120.458984 0)\"/>\n      <use xlink:href=\"#DejaVuSans-5f\" transform=\"translate(148.242188 0)\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(198.242188 0)\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(261.71875 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6c\" transform=\"translate(325.195312 0)\"/>\n     </g>\n    </g>\n   </g>\n  </g>\n </g>\n <defs>\n  <clipPath id=\"p0b8f9a4918\">\n   <rect x=\"31.8\" y=\"7.2\" width=\"195.3\" height=\"138.6\"/>\n  </clipPath>\n </defs>\n</svg>\n"
     },
     "metadata": {}
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=lstm-md-10 fw=all -->\n",
    "\n",
    "The trained model reaches a somewhat better perplexity than our\n",
    "from-scratch cell. The difference is not the arithmetic, which is\n",
    "identical, but the starting point, and it is worth being precise about\n",
    "what each framework actually does. PyTorch's `nn.LSTM` draws every\n",
    "weight *and bias* uniformly at scale $1/\\sqrt{h}$; Flax's `LSTMCell`\n",
    "pairs fan-in-scaled input weights with orthogonal recurrent matrices and\n",
    "zero biases. Both are better starting points than our teaching code's\n",
    "small Gaussian, and they differ from each other — one reason the\n",
    "two tabs' numbers do not match. Note what neither default includes: the\n",
    "forget-gate bias of 1 recommended above remains yours to set.\n",
    "At a fixed training budget, how you start matters.\n",
    "\n",
    "Reading the perplexity off the scoreboard, we let the model continue\n",
    "*the time traveller*. As that section explained, greedy\n",
    "(argmax) decoding of a model this lightly trained soon drifts into a\n",
    "repetition loop, so we sample at a low temperature: the continuation stays\n",
    "close to the model's preferences yet keeps moving."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "lstm-concise-implementation-4"
   },
   "source": [
    "ppls['LSTM'] = val_ppl(model)\n",
    "pred = model.predict('the time traveller', 30, data.tokenizer, d2l.try_gpu(),\n",
    "                     temperature=0.5)\n",
    "print(f\"perplexity {ppls['LSTM']:.1f}, {pred!r}\")"
   ],
   "execution_count": 12,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "perplexity 90.7, \"the time travellering, and\\npresently\\ntism.\\n\\n'I had a viola, and\\ntowing\\ns\"\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=lstm-md-11 fw=all -->\n",
    "\n",
    "## Gated Recurrent Units\n",
    "\n",
    "As LSTMs spread through the 2010s, researchers looked for cheaper cells\n",
    "that kept the two essential ingredients, an additive state path and\n",
    "multiplicative gates. The most successful answer is the *gated recurrent\n",
    "unit* (GRU) of @Cho.Van-Merrienboer.Bahdanau.ea.2014: it drops the\n",
    "separate cell state, letting the hidden state itself be the memory, and\n",
    "merges four heads into three, often at no cost in quality\n",
    "[@Chung.Gulcehre.Cho.ea.2014].\n",
    "\n",
    "### Reset and Update Gates\n",
    "\n",
    "The GRU computes two gates in the by-now-familiar form of\n",
    "the equation, a *reset gate* $\\mathbf{R}_t$ and an *update gate*\n",
    "$\\mathbf{Z}_t$:\n",
    "\n",
    "$$\n",
    "\\mathbf{R}_t = \\sigma(\\mathbf{X}_t \\mathbf{W}_{\\textrm{xr}} + \\mathbf{H}_{t-1} \\mathbf{W}_{\\textrm{hr}} + \\mathbf{b}_\\textrm{r}),\n",
    "\\qquad\n",
    "\\mathbf{Z}_t = \\sigma(\\mathbf{X}_t \\mathbf{W}_{\\textrm{xz}} + \\mathbf{H}_{t-1} \\mathbf{W}_{\\textrm{hz}} + \\mathbf{b}_\\textrm{z}).\n",
    "$$\n",
    "\n",
    "The reset gate acts *inside* the candidate state: it filters how much of\n",
    "the previous state may enter the vanilla-RNN-style proposal,\n",
    "\n",
    "$$\n",
    "\\tilde{\\mathbf{H}}_t = \\tanh(\\mathbf{X}_t \\mathbf{W}_{\\textrm{xh}} + \\left(\\mathbf{R}_t \\odot \\mathbf{H}_{t-1}\\right) \\mathbf{W}_{\\textrm{hh}} + \\mathbf{b}_\\textrm{h}),\n",
    "$$\n",
    "\n",
    "so $\\mathbf{R}_t \\approx 1$ recovers the vanilla recurrence while\n",
    "$\\mathbf{R}_t \\approx 0$ makes the proposal a function of the current input\n",
    "alone, as if the sequence restarted. The update gate then blends old state\n",
    "and proposal as a convex combination,\n",
    "\n",
    "$$\n",
    "\\mathbf{H}_t = \\mathbf{Z}_t \\odot \\mathbf{H}_{t-1} + (1 - \\mathbf{Z}_t) \\odot \\tilde{\\mathbf{H}}_t.\n",
    "$$\n",
    "\n",
    "Here the additive memory path is plain to see: wherever\n",
    "$\\mathbf{Z}_t \\approx 1$ the new state is a copy of the old one and the\n",
    "step is effectively skipped, giving the same near-identity direct-path\n",
    "Jacobian as the LSTM's carousel (with the same caveat: the gates\n",
    "themselves depend on $\\mathbf{H}_{t-1}$, so this is the protected route,\n",
    "not the total derivative); wherever $\\mathbf{Z}_t \\approx 0$ the state is\n",
    "overwritten by the proposal. Because the equation is a convex combination of two\n",
    "bounded quantities, the state stays bounded by construction, and the GRU\n",
    "needs neither a separate protected accumulator nor an output squashing: one\n",
    "vector serves as memory and output at once. The division of labor is that\n",
    "reset gates capture short-range decisions (what to drop from the proposal)\n",
    "and update gates capture long-range ones (how long to hold state).\n",
    "the figure shows the flow.\n",
    "\n",
    "![The GRU cell. The reset gate filters the previous state inside the candidate $\\tilde{\\mathbf{H}}_t$; the update gate blends old state and candidate as a convex combination.](../img/mdl-modernrnn-gru-cell.svg)\n",
    "\n",
    "### Implementation and Comparison\n",
    "\n",
    "After the LSTM, a from-scratch GRU offers no new idea, only one fewer\n",
    "head, so we skip straight to the framework layer (an exercise asks you to\n",
    "write the scratch version by editing `LSTMScratch`). The class mirrors\n",
    "`LSTM`, including the `num_layers` argument; the GRU is also the cell\n",
    "that the next section (that section) strips down to its minimal\n",
    "linear core."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "lstm-implementation-and-comparison-1"
   },
   "source": [
    "class GRU(d2l.RNN):\n",
    "    \"\"\"The multilayer GRU model implemented with high-level APIs.\"\"\"\n",
    "    def __init__(self, num_inputs, num_hiddens, num_layers=1, dropout=0):\n",
    "        d2l.Module.__init__(self)\n",
    "        self.save_hyperparameters()\n",
    "        self.rnn = nn.GRU(num_inputs, num_hiddens, num_layers,\n",
    "                          dropout=dropout)"
   ],
   "execution_count": 13,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=lstm-md-12 fw=all -->\n",
    "\n",
    "One training run under the same recipe puts the GRU on our scoreboard."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "lstm-implementation-and-comparison-2"
   },
   "source": [
    "gru = GRU(num_inputs=64, num_hiddens=128)\n",
    "model = d2l.RNNLM(gru, vocab_size=len(data.vocab), lr=4)"
   ],
   "execution_count": 14,
   "outputs": []
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "lstm-implementation-and-comparison-3"
   },
   "source": [
    "trainer = d2l.Trainer(max_epochs=10, gradient_clip_val=1, num_gpus=1)\n",
    "model.board.yscale = 'log'\n",
    "trainer.fit(model, data)"
   ],
   "execution_count": 15,
   "outputs": [
    {
     "output_type": "display_data",
     "data": {
      "image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"240.6625pt\" height=\"183.35625pt\" viewBox=\"0 0 240.6625 183.35625\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n <metadata>\n  <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n   <cc:Work>\n    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n    <dc:date>2026-07-19T20:55:07.560707</dc:date>\n    <dc:format>image/svg+xml</dc:format>\n    <dc:creator>\n     <cc:Agent>\n      <dc:title>Matplotlib v3.10.8, https://matplotlib.org/</dc:title>\n     </cc:Agent>\n    </dc:creator>\n   </cc:Work>\n  </rdf:RDF>\n </metadata>\n <defs>\n  <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n </defs>\n <g id=\"figure_1\">\n  <g id=\"patch_1\">\n   <path d=\"M 0 183.35625 \nL 240.6625 183.35625 \nL 240.6625 0 \nL 0 0 \nz\n\" style=\"fill: #ffffff\"/>\n  </g>\n  <g id=\"axes_1\">\n   <g id=\"patch_2\">\n    <path d=\"M 31.8 145.8 \nL 227.1 145.8 \nL 227.1 7.2 \nL 31.8 7.2 \nz\n\" style=\"fill: #ffffff\"/>\n   </g>\n   <g id=\"matplotlib.axis_1\">\n    <g id=\"xtick_1\">\n     <g id=\"line2d_1\">\n      <defs>\n       <path id=\"m93f6b2cfc9\" d=\"M 0 0 \nL 0 3.5 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#m93f6b2cfc9\" x=\"31.8\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_1\">\n      <!-- 0 -->\n      <g transform=\"translate(28.61875 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-30\" d=\"M 2034 4250 \nQ 1547 4250 1301 3770 \nQ 1056 3291 1056 2328 \nQ 1056 1369 1301 889 \nQ 1547 409 2034 409 \nQ 2525 409 2770 889 \nQ 3016 1369 3016 2328 \nQ 3016 3291 2770 3770 \nQ 2525 4250 2034 4250 \nz\nM 2034 4750 \nQ 2819 4750 3233 4129 \nQ 3647 3509 3647 2328 \nQ 3647 1150 3233 529 \nQ 2819 -91 2034 -91 \nQ 1250 -91 836 529 \nQ 422 1150 422 2328 \nQ 422 3509 836 4129 \nQ 1250 4750 2034 4750 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-30\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_2\">\n     <g id=\"line2d_2\">\n      <g>\n       <use xlink:href=\"#m93f6b2cfc9\" x=\"70.86\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_2\">\n      <!-- 2 -->\n      <g transform=\"translate(67.67875 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-32\" d=\"M 1228 531 \nL 3431 531 \nL 3431 0 \nL 469 0 \nL 469 531 \nQ 828 903 1448 1529 \nQ 2069 2156 2228 2338 \nQ 2531 2678 2651 2914 \nQ 2772 3150 2772 3378 \nQ 2772 3750 2511 3984 \nQ 2250 4219 1831 4219 \nQ 1534 4219 1204 4116 \nQ 875 4013 500 3803 \nL 500 4441 \nQ 881 4594 1212 4672 \nQ 1544 4750 1819 4750 \nQ 2544 4750 2975 4387 \nQ 3406 4025 3406 3419 \nQ 3406 3131 3298 2873 \nQ 3191 2616 2906 2266 \nQ 2828 2175 2409 1742 \nQ 1991 1309 1228 531 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-32\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_3\">\n     <g id=\"line2d_3\">\n      <g>\n       <use xlink:href=\"#m93f6b2cfc9\" x=\"109.92\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_3\">\n      <!-- 4 -->\n      <g transform=\"translate(106.73875 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-34\" d=\"M 2419 4116 \nL 825 1625 \nL 2419 1625 \nL 2419 4116 \nz\nM 2253 4666 \nL 3047 4666 \nL 3047 1625 \nL 3713 1625 \nL 3713 1100 \nL 3047 1100 \nL 3047 0 \nL 2419 0 \nL 2419 1100 \nL 313 1100 \nL 313 1709 \nL 2253 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-34\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_4\">\n     <g id=\"line2d_4\">\n      <g>\n       <use xlink:href=\"#m93f6b2cfc9\" x=\"148.98\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_4\">\n      <!-- 6 -->\n      <g transform=\"translate(145.79875 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-36\" d=\"M 2113 2584 \nQ 1688 2584 1439 2293 \nQ 1191 2003 1191 1497 \nQ 1191 994 1439 701 \nQ 1688 409 2113 409 \nQ 2538 409 2786 701 \nQ 3034 994 3034 1497 \nQ 3034 2003 2786 2293 \nQ 2538 2584 2113 2584 \nz\nM 3366 4563 \nL 3366 3988 \nQ 3128 4100 2886 4159 \nQ 2644 4219 2406 4219 \nQ 1781 4219 1451 3797 \nQ 1122 3375 1075 2522 \nQ 1259 2794 1537 2939 \nQ 1816 3084 2150 3084 \nQ 2853 3084 3261 2657 \nQ 3669 2231 3669 1497 \nQ 3669 778 3244 343 \nQ 2819 -91 2113 -91 \nQ 1303 -91 875 529 \nQ 447 1150 447 2328 \nQ 447 3434 972 4092 \nQ 1497 4750 2381 4750 \nQ 2619 4750 2861 4703 \nQ 3103 4656 3366 4563 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-36\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_5\">\n     <g id=\"line2d_5\">\n      <g>\n       <use xlink:href=\"#m93f6b2cfc9\" x=\"188.04\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_5\">\n      <!-- 8 -->\n      <g transform=\"translate(184.85875 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-38\" d=\"M 2034 2216 \nQ 1584 2216 1326 1975 \nQ 1069 1734 1069 1313 \nQ 1069 891 1326 650 \nQ 1584 409 2034 409 \nQ 2484 409 2743 651 \nQ 3003 894 3003 1313 \nQ 3003 1734 2745 1975 \nQ 2488 2216 2034 2216 \nz\nM 1403 2484 \nQ 997 2584 770 2862 \nQ 544 3141 544 3541 \nQ 544 4100 942 4425 \nQ 1341 4750 2034 4750 \nQ 2731 4750 3128 4425 \nQ 3525 4100 3525 3541 \nQ 3525 3141 3298 2862 \nQ 3072 2584 2669 2484 \nQ 3125 2378 3379 2068 \nQ 3634 1759 3634 1313 \nQ 3634 634 3220 271 \nQ 2806 -91 2034 -91 \nQ 1263 -91 848 271 \nQ 434 634 434 1313 \nQ 434 1759 690 2068 \nQ 947 2378 1403 2484 \nz\nM 1172 3481 \nQ 1172 3119 1398 2916 \nQ 1625 2713 2034 2713 \nQ 2441 2713 2670 2916 \nQ 2900 3119 2900 3481 \nQ 2900 3844 2670 4047 \nQ 2441 4250 2034 4250 \nQ 1625 4250 1398 4047 \nQ 1172 3844 1172 3481 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-38\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_6\">\n     <g id=\"line2d_6\">\n      <g>\n       <use xlink:href=\"#m93f6b2cfc9\" x=\"227.1\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_6\">\n      <!-- 10 -->\n      <g transform=\"translate(220.7375 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-31\" d=\"M 794 531 \nL 1825 531 \nL 1825 4091 \nL 703 3866 \nL 703 4441 \nL 1819 4666 \nL 2450 4666 \nL 2450 531 \nL 3481 531 \nL 3481 0 \nL 794 0 \nL 794 531 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-31\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"text_7\">\n     <!-- epoch -->\n     <g transform=\"translate(114.221875 174.076563) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-65\" d=\"M 3597 1894 \nL 3597 1613 \nL 953 1613 \nQ 991 1019 1311 708 \nQ 1631 397 2203 397 \nQ 2534 397 2845 478 \nQ 3156 559 3463 722 \nL 3463 178 \nQ 3153 47 2828 -22 \nQ 2503 -91 2169 -91 \nQ 1331 -91 842 396 \nQ 353 884 353 1716 \nQ 353 2575 817 3079 \nQ 1281 3584 2069 3584 \nQ 2775 3584 3186 3129 \nQ 3597 2675 3597 1894 \nz\nM 3022 2063 \nQ 3016 2534 2758 2815 \nQ 2500 3097 2075 3097 \nQ 1594 3097 1305 2825 \nQ 1016 2553 972 2059 \nL 3022 2063 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-70\" d=\"M 1159 525 \nL 1159 -1331 \nL 581 -1331 \nL 581 3500 \nL 1159 3500 \nL 1159 2969 \nQ 1341 3281 1617 3432 \nQ 1894 3584 2278 3584 \nQ 2916 3584 3314 3078 \nQ 3713 2572 3713 1747 \nQ 3713 922 3314 415 \nQ 2916 -91 2278 -91 \nQ 1894 -91 1617 61 \nQ 1341 213 1159 525 \nz\nM 3116 1747 \nQ 3116 2381 2855 2742 \nQ 2594 3103 2138 3103 \nQ 1681 3103 1420 2742 \nQ 1159 2381 1159 1747 \nQ 1159 1113 1420 752 \nQ 1681 391 2138 391 \nQ 2594 391 2855 752 \nQ 3116 1113 3116 1747 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-6f\" d=\"M 1959 3097 \nQ 1497 3097 1228 2736 \nQ 959 2375 959 1747 \nQ 959 1119 1226 758 \nQ 1494 397 1959 397 \nQ 2419 397 2687 759 \nQ 2956 1122 2956 1747 \nQ 2956 2369 2687 2733 \nQ 2419 3097 1959 3097 \nz\nM 1959 3584 \nQ 2709 3584 3137 3096 \nQ 3566 2609 3566 1747 \nQ 3566 888 3137 398 \nQ 2709 -91 1959 -91 \nQ 1206 -91 779 398 \nQ 353 888 353 1747 \nQ 353 2609 779 3096 \nQ 1206 3584 1959 3584 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-63\" d=\"M 3122 3366 \nL 3122 2828 \nQ 2878 2963 2633 3030 \nQ 2388 3097 2138 3097 \nQ 1578 3097 1268 2742 \nQ 959 2388 959 1747 \nQ 959 1106 1268 751 \nQ 1578 397 2138 397 \nQ 2388 397 2633 464 \nQ 2878 531 3122 666 \nL 3122 134 \nQ 2881 22 2623 -34 \nQ 2366 -91 2075 -91 \nQ 1284 -91 818 406 \nQ 353 903 353 1747 \nQ 353 2603 823 3093 \nQ 1294 3584 2113 3584 \nQ 2378 3584 2631 3529 \nQ 2884 3475 3122 3366 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-68\" d=\"M 3513 2113 \nL 3513 0 \nL 2938 0 \nL 2938 2094 \nQ 2938 2591 2744 2837 \nQ 2550 3084 2163 3084 \nQ 1697 3084 1428 2787 \nQ 1159 2491 1159 1978 \nL 1159 0 \nL 581 0 \nL 581 4863 \nL 1159 4863 \nL 1159 2956 \nQ 1366 3272 1645 3428 \nQ 1925 3584 2291 3584 \nQ 2894 3584 3203 3211 \nQ 3513 2838 3513 2113 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-65\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(61.523438 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6f\" transform=\"translate(125 0)\"/>\n      <use xlink:href=\"#DejaVuSans-63\" transform=\"translate(186.181641 0)\"/>\n      <use xlink:href=\"#DejaVuSans-68\" transform=\"translate(241.162109 0)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"matplotlib.axis_2\">\n    <g id=\"ytick_1\">\n     <g id=\"line2d_7\">\n      <defs>\n       <path id=\"m3c03453bcc\" d=\"M 0 0 \nL -3.5 0 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#m3c03453bcc\" x=\"31.8\" y=\"87.233967\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_8\">\n      <!-- $\\mathdefault{10^{2}}$ -->\n      <g transform=\"translate(7.2 91.033186) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-31\" transform=\"translate(0 0.765625)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0.765625)\"/>\n       <use xlink:href=\"#DejaVuSans-32\" transform=\"translate(128.203125 39.046875) scale(0.7)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_2\">\n     <g id=\"line2d_8\">\n      <defs>\n       <path id=\"m090944e2d8\" d=\"M 0 0 \nL -2 0 \n\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#m090944e2d8\" x=\"31.8\" y=\"140.537708\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_3\">\n     <g id=\"line2d_9\">\n      <g>\n       <use xlink:href=\"#m090944e2d8\" x=\"31.8\" y=\"127.8011\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_4\">\n     <g id=\"line2d_10\">\n      <g>\n       <use xlink:href=\"#m090944e2d8\" x=\"31.8\" y=\"117.921818\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_5\">\n     <g id=\"line2d_11\">\n      <g>\n       <use xlink:href=\"#m090944e2d8\" x=\"31.8\" y=\"109.849858\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_6\">\n     <g id=\"line2d_12\">\n      <g>\n       <use xlink:href=\"#m090944e2d8\" x=\"31.8\" y=\"103.025112\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_7\">\n     <g id=\"line2d_13\">\n      <g>\n       <use xlink:href=\"#m090944e2d8\" x=\"31.8\" y=\"97.113249\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_8\">\n     <g id=\"line2d_14\">\n      <g>\n       <use xlink:href=\"#m090944e2d8\" x=\"31.8\" y=\"91.898616\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_9\">\n     <g id=\"line2d_15\">\n      <g>\n       <use xlink:href=\"#m090944e2d8\" x=\"31.8\" y=\"56.546117\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_10\">\n     <g id=\"line2d_16\">\n      <g>\n       <use xlink:href=\"#m090944e2d8\" x=\"31.8\" y=\"38.594875\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_11\">\n     <g id=\"line2d_17\">\n      <g>\n       <use xlink:href=\"#m090944e2d8\" x=\"31.8\" y=\"25.858266\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_12\">\n     <g id=\"line2d_18\">\n      <g>\n       <use xlink:href=\"#m090944e2d8\" x=\"31.8\" y=\"15.978984\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_13\">\n     <g id=\"line2d_19\">\n      <g>\n       <use xlink:href=\"#m090944e2d8\" x=\"31.8\" y=\"7.907024\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n   </g>\n   <g id=\"line2d_20\">\n    <path d=\"M 36.383571 13.5 \nL 45.949286 40.495409 \nL 55.515 52.442124 \nL 65.080714 62.700608 \nL 74.646429 71.835568 \nL 84.212143 80.199776 \nL 93.777857 87.510511 \nL 103.343571 93.476934 \nL 112.909286 99.247866 \nL 122.475 104.326776 \nL 132.040714 109.265524 \nL 141.606429 113.061442 \nL 151.172143 117.163033 \nL 160.737857 120.787222 \nL 170.303571 124.161126 \nL 179.869286 127.693544 \nL 189.435 130.573096 \nL 199.000714 133.379753 \nL 208.566429 136.725707 \nL 218.132143 139.5 \n\" clip-path=\"url(#p64213cfba3)\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_21\">\n    <path d=\"M 51.33 45.308586 \nL 70.86 64.304722 \nL 90.39 74.317637 \nL 109.92 81.316523 \nL 129.45 88.763789 \nL 148.98 91.975669 \nL 168.51 94.255911 \nL 188.04 96.212633 \nL 207.57 97.155569 \nL 227.1 97.556369 \n\" clip-path=\"url(#p64213cfba3)\" style=\"fill: none; stroke-dasharray: 5.55,2.4; stroke-dashoffset: 0; stroke: #ff7f0e; stroke-width: 1.5\"/>\n   </g>\n   <g id=\"patch_3\">\n    <path d=\"M 31.8 145.8 \nL 31.8 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_4\">\n    <path d=\"M 227.1 145.8 \nL 227.1 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_5\">\n    <path d=\"M 31.8 145.8 \nL 227.1 145.8 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_6\">\n    <path d=\"M 31.8 7.2 \nL 227.1 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"legend_1\">\n    <g id=\"patch_7\">\n     <path d=\"M 144.35 45.1125 \nL 220.1 45.1125 \nQ 222.1 45.1125 222.1 43.1125 \nL 222.1 14.2 \nQ 222.1 12.2 220.1 12.2 \nL 144.35 12.2 \nQ 142.35 12.2 142.35 14.2 \nL 142.35 43.1125 \nQ 142.35 45.1125 144.35 45.1125 \nz\n\" style=\"fill: #ffffff; opacity: 0.8; stroke: #cccccc; stroke-linejoin: miter\"/>\n    </g>\n    <g id=\"line2d_22\">\n     <path d=\"M 146.35 20.298438 \nL 156.35 20.298438 \nL 166.35 20.298438 \n\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n    </g>\n    <g id=\"text_9\">\n     <!-- train_ppl -->\n     <g transform=\"translate(174.35 23.798438) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-74\" d=\"M 1172 4494 \nL 1172 3500 \nL 2356 3500 \nL 2356 3053 \nL 1172 3053 \nL 1172 1153 \nQ 1172 725 1289 603 \nQ 1406 481 1766 481 \nL 2356 481 \nL 2356 0 \nL 1766 0 \nQ 1100 0 847 248 \nQ 594 497 594 1153 \nL 594 3053 \nL 172 3053 \nL 172 3500 \nL 594 3500 \nL 594 4494 \nL 1172 4494 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-72\" d=\"M 2631 2963 \nQ 2534 3019 2420 3045 \nQ 2306 3072 2169 3072 \nQ 1681 3072 1420 2755 \nQ 1159 2438 1159 1844 \nL 1159 0 \nL 581 0 \nL 581 3500 \nL 1159 3500 \nL 1159 2956 \nQ 1341 3275 1631 3429 \nQ 1922 3584 2338 3584 \nQ 2397 3584 2469 3576 \nQ 2541 3569 2628 3553 \nL 2631 2963 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-61\" d=\"M 2194 1759 \nQ 1497 1759 1228 1600 \nQ 959 1441 959 1056 \nQ 959 750 1161 570 \nQ 1363 391 1709 391 \nQ 2188 391 2477 730 \nQ 2766 1069 2766 1631 \nL 2766 1759 \nL 2194 1759 \nz\nM 3341 1997 \nL 3341 0 \nL 2766 0 \nL 2766 531 \nQ 2569 213 2275 61 \nQ 1981 -91 1556 -91 \nQ 1019 -91 701 211 \nQ 384 513 384 1019 \nQ 384 1609 779 1909 \nQ 1175 2209 1959 2209 \nL 2766 2209 \nL 2766 2266 \nQ 2766 2663 2505 2880 \nQ 2244 3097 1772 3097 \nQ 1472 3097 1187 3025 \nQ 903 2953 641 2809 \nL 641 3341 \nQ 956 3463 1253 3523 \nQ 1550 3584 1831 3584 \nQ 2591 3584 2966 3190 \nQ 3341 2797 3341 1997 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-69\" d=\"M 603 3500 \nL 1178 3500 \nL 1178 0 \nL 603 0 \nL 603 3500 \nz\nM 603 4863 \nL 1178 4863 \nL 1178 4134 \nL 603 4134 \nL 603 4863 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-6e\" d=\"M 3513 2113 \nL 3513 0 \nL 2938 0 \nL 2938 2094 \nQ 2938 2591 2744 2837 \nQ 2550 3084 2163 3084 \nQ 1697 3084 1428 2787 \nQ 1159 2491 1159 1978 \nL 1159 0 \nL 581 0 \nL 581 3500 \nL 1159 3500 \nL 1159 2956 \nQ 1366 3272 1645 3428 \nQ 1925 3584 2291 3584 \nQ 2894 3584 3203 3211 \nQ 3513 2838 3513 2113 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-5f\" d=\"M 3263 -1063 \nL 3263 -1509 \nL -63 -1509 \nL -63 -1063 \nL 3263 -1063 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-6c\" d=\"M 603 4863 \nL 1178 4863 \nL 1178 0 \nL 603 0 \nL 603 4863 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-74\"/>\n      <use xlink:href=\"#DejaVuSans-72\" transform=\"translate(39.208984 0)\"/>\n      <use xlink:href=\"#DejaVuSans-61\" transform=\"translate(80.322266 0)\"/>\n      <use xlink:href=\"#DejaVuSans-69\" transform=\"translate(141.601562 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6e\" transform=\"translate(169.384766 0)\"/>\n      <use xlink:href=\"#DejaVuSans-5f\" transform=\"translate(232.763672 0)\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(282.763672 0)\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(346.240234 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6c\" transform=\"translate(409.716797 0)\"/>\n     </g>\n    </g>\n    <g id=\"line2d_23\">\n     <path d=\"M 146.35 35.254688 \nL 156.35 35.254688 \nL 166.35 35.254688 \n\" style=\"fill: none; stroke-dasharray: 5.55,2.4; stroke-dashoffset: 0; stroke: #ff7f0e; stroke-width: 1.5\"/>\n    </g>\n    <g id=\"text_10\">\n     <!-- val_ppl -->\n     <g transform=\"translate(174.35 38.754688) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-76\" d=\"M 191 3500 \nL 800 3500 \nL 1894 563 \nL 2988 3500 \nL 3597 3500 \nL 2284 0 \nL 1503 0 \nL 191 3500 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-76\"/>\n      <use xlink:href=\"#DejaVuSans-61\" transform=\"translate(59.179688 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6c\" transform=\"translate(120.458984 0)\"/>\n      <use xlink:href=\"#DejaVuSans-5f\" transform=\"translate(148.242188 0)\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(198.242188 0)\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(261.71875 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6c\" transform=\"translate(325.195312 0)\"/>\n     </g>\n    </g>\n   </g>\n  </g>\n </g>\n <defs>\n  <clipPath id=\"p64213cfba3\">\n   <rect x=\"31.8\" y=\"7.2\" width=\"195.3\" height=\"138.6\"/>\n  </clipPath>\n </defs>\n</svg>\n"
     },
     "metadata": {}
    }
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "lstm-implementation-and-comparison-4"
   },
   "source": [
    "ppls['GRU'] = val_ppl(model)\n",
    "print(f\"validation perplexity {ppls['GRU']:.1f}\")"
   ],
   "execution_count": 16,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "validation perplexity 79.2\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=lstm-md-13 fw=all -->\n",
    "\n",
    "At the same width the GRU's recurrent core has $3h(d + h + 1)$ weights to\n",
    "the LSTM's $4h(d + h + 1)$ and computes three matmul heads per step\n",
    "instead of four, yet in our runs it posts the best perplexity of the\n",
    "section, clearly ahead of the vanilla RNN and ahead of the LSTM at this\n",
    "budget. The advantage is convergence speed, not capacity: with fewer gates\n",
    "to coordinate and a state that the convex blend keeps well scaled from the\n",
    "first epoch, the GRU makes good use of a short schedule. This mirrors the\n",
    "early empirical comparisons [@Chung.Gulcehre.Cho.ea.2014], which\n",
    "found the GRU at least the LSTM's equal on small and mid-sized tasks; with\n",
    "longer training and careful initialization the LSTM closes the gap (see\n",
    "the exercises).\n",
    "\n",
    "The GRU also earns a place in this book for where it led. Look again at\n",
    "the equation: the *only* nonlinearity standing between\n",
    "$\\mathbf{H}_{t-1}$ and $\\mathbf{H}_t$ enters through\n",
    "$\\tilde{\\mathbf{H}}_t$ and the gates. If one makes the gates functions of\n",
    "the input alone and lets the candidate ignore the previous state, the\n",
    "update becomes *linear* in $\\mathbf{H}_{t-1}$, and a linear recurrence can\n",
    "be trained in parallel across the whole sequence rather than step by step.\n",
    "That observation, worked out in that section, is the seed of minimal\n",
    "gated cells such as minGRU [@Feng.Tung.Ahmed.ea.2024], of linear\n",
    "recurrent units [@Orvieto.Smith.Gu.ea.2023], and of the state space\n",
    "models that now appear inside production language models. The GRU is not\n",
    "just a smaller LSTM; it is the halfway point to the linear recurrences that\n",
    "follow.\n",
    "\n",
    "## Depth and Direction\n",
    "\n",
    "Gating changes what happens *inside* a cell; two further RNN-era design\n",
    "axes change how cells are wired together. We compress both to the lessons\n",
    "that outlived them.\n",
    "\n",
    "### Deep Recurrent Networks\n",
    "\n",
    "A deep RNN stacks recurrent layers, each reading the hidden-state\n",
    "sequence of the layer below as its input sequence: function composition\n",
    "within a time step, on top of recurrence across steps. Gated cells handle\n",
    "the time direction; the layer direction borrows the feedforward remedies\n",
    "of residual connections (that section) and between-layer dropout,\n",
    "and stacks beyond four to eight layers rarely paid off for recurrent\n",
    "models. Since our `LSTM` class already accepts `num_layers`, a two-layer\n",
    "model is a one-argument change, and it completes our scoreboard."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "lstm-deep-recurrent-networks-1"
   },
   "source": [
    "lstm2 = LSTM(num_inputs=64, num_hiddens=128, num_layers=2)\n",
    "model = d2l.RNNLM(lstm2, vocab_size=len(data.vocab), lr=4)\n",
    "trainer = d2l.Trainer(max_epochs=10, gradient_clip_val=1, num_gpus=1)\n",
    "model.board.yscale = 'log'\n",
    "trainer.fit(model, data)"
   ],
   "execution_count": 17,
   "outputs": [
    {
     "output_type": "display_data",
     "data": {
      "image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"257.6625pt\" height=\"183.35625pt\" viewBox=\"0 0 257.6625 183.35625\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n <metadata>\n  <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n   <cc:Work>\n    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n    <dc:date>2026-07-19T20:55:15.901594</dc:date>\n    <dc:format>image/svg+xml</dc:format>\n    <dc:creator>\n     <cc:Agent>\n      <dc:title>Matplotlib v3.10.8, https://matplotlib.org/</dc:title>\n     </cc:Agent>\n    </dc:creator>\n   </cc:Work>\n  </rdf:RDF>\n </metadata>\n <defs>\n  <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n </defs>\n <g id=\"figure_1\">\n  <g id=\"patch_1\">\n   <path d=\"M 0 183.35625 \nL 257.6625 183.35625 \nL 257.6625 0 \nL 0 0 \nz\n\" style=\"fill: #ffffff\"/>\n  </g>\n  <g id=\"axes_1\">\n   <g id=\"patch_2\">\n    <path d=\"M 48.8 145.8 \nL 244.1 145.8 \nL 244.1 7.2 \nL 48.8 7.2 \nz\n\" style=\"fill: #ffffff\"/>\n   </g>\n   <g id=\"matplotlib.axis_1\">\n    <g id=\"xtick_1\">\n     <g id=\"line2d_1\">\n      <defs>\n       <path id=\"m4ecd96abba\" d=\"M 0 0 \nL 0 3.5 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#m4ecd96abba\" x=\"48.8\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_1\">\n      <!-- 0 -->\n      <g transform=\"translate(45.61875 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-30\" d=\"M 2034 4250 \nQ 1547 4250 1301 3770 \nQ 1056 3291 1056 2328 \nQ 1056 1369 1301 889 \nQ 1547 409 2034 409 \nQ 2525 409 2770 889 \nQ 3016 1369 3016 2328 \nQ 3016 3291 2770 3770 \nQ 2525 4250 2034 4250 \nz\nM 2034 4750 \nQ 2819 4750 3233 4129 \nQ 3647 3509 3647 2328 \nQ 3647 1150 3233 529 \nQ 2819 -91 2034 -91 \nQ 1250 -91 836 529 \nQ 422 1150 422 2328 \nQ 422 3509 836 4129 \nQ 1250 4750 2034 4750 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-30\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_2\">\n     <g id=\"line2d_2\">\n      <g>\n       <use xlink:href=\"#m4ecd96abba\" x=\"87.86\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_2\">\n      <!-- 2 -->\n      <g transform=\"translate(84.67875 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-32\" d=\"M 1228 531 \nL 3431 531 \nL 3431 0 \nL 469 0 \nL 469 531 \nQ 828 903 1448 1529 \nQ 2069 2156 2228 2338 \nQ 2531 2678 2651 2914 \nQ 2772 3150 2772 3378 \nQ 2772 3750 2511 3984 \nQ 2250 4219 1831 4219 \nQ 1534 4219 1204 4116 \nQ 875 4013 500 3803 \nL 500 4441 \nQ 881 4594 1212 4672 \nQ 1544 4750 1819 4750 \nQ 2544 4750 2975 4387 \nQ 3406 4025 3406 3419 \nQ 3406 3131 3298 2873 \nQ 3191 2616 2906 2266 \nQ 2828 2175 2409 1742 \nQ 1991 1309 1228 531 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-32\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_3\">\n     <g id=\"line2d_3\">\n      <g>\n       <use xlink:href=\"#m4ecd96abba\" x=\"126.92\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_3\">\n      <!-- 4 -->\n      <g transform=\"translate(123.73875 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-34\" d=\"M 2419 4116 \nL 825 1625 \nL 2419 1625 \nL 2419 4116 \nz\nM 2253 4666 \nL 3047 4666 \nL 3047 1625 \nL 3713 1625 \nL 3713 1100 \nL 3047 1100 \nL 3047 0 \nL 2419 0 \nL 2419 1100 \nL 313 1100 \nL 313 1709 \nL 2253 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-34\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_4\">\n     <g id=\"line2d_4\">\n      <g>\n       <use xlink:href=\"#m4ecd96abba\" x=\"165.98\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_4\">\n      <!-- 6 -->\n      <g transform=\"translate(162.79875 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-36\" d=\"M 2113 2584 \nQ 1688 2584 1439 2293 \nQ 1191 2003 1191 1497 \nQ 1191 994 1439 701 \nQ 1688 409 2113 409 \nQ 2538 409 2786 701 \nQ 3034 994 3034 1497 \nQ 3034 2003 2786 2293 \nQ 2538 2584 2113 2584 \nz\nM 3366 4563 \nL 3366 3988 \nQ 3128 4100 2886 4159 \nQ 2644 4219 2406 4219 \nQ 1781 4219 1451 3797 \nQ 1122 3375 1075 2522 \nQ 1259 2794 1537 2939 \nQ 1816 3084 2150 3084 \nQ 2853 3084 3261 2657 \nQ 3669 2231 3669 1497 \nQ 3669 778 3244 343 \nQ 2819 -91 2113 -91 \nQ 1303 -91 875 529 \nQ 447 1150 447 2328 \nQ 447 3434 972 4092 \nQ 1497 4750 2381 4750 \nQ 2619 4750 2861 4703 \nQ 3103 4656 3366 4563 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-36\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_5\">\n     <g id=\"line2d_5\">\n      <g>\n       <use xlink:href=\"#m4ecd96abba\" x=\"205.04\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_5\">\n      <!-- 8 -->\n      <g transform=\"translate(201.85875 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-38\" d=\"M 2034 2216 \nQ 1584 2216 1326 1975 \nQ 1069 1734 1069 1313 \nQ 1069 891 1326 650 \nQ 1584 409 2034 409 \nQ 2484 409 2743 651 \nQ 3003 894 3003 1313 \nQ 3003 1734 2745 1975 \nQ 2488 2216 2034 2216 \nz\nM 1403 2484 \nQ 997 2584 770 2862 \nQ 544 3141 544 3541 \nQ 544 4100 942 4425 \nQ 1341 4750 2034 4750 \nQ 2731 4750 3128 4425 \nQ 3525 4100 3525 3541 \nQ 3525 3141 3298 2862 \nQ 3072 2584 2669 2484 \nQ 3125 2378 3379 2068 \nQ 3634 1759 3634 1313 \nQ 3634 634 3220 271 \nQ 2806 -91 2034 -91 \nQ 1263 -91 848 271 \nQ 434 634 434 1313 \nQ 434 1759 690 2068 \nQ 947 2378 1403 2484 \nz\nM 1172 3481 \nQ 1172 3119 1398 2916 \nQ 1625 2713 2034 2713 \nQ 2441 2713 2670 2916 \nQ 2900 3119 2900 3481 \nQ 2900 3844 2670 4047 \nQ 2441 4250 2034 4250 \nQ 1625 4250 1398 4047 \nQ 1172 3844 1172 3481 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-38\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_6\">\n     <g id=\"line2d_6\">\n      <g>\n       <use xlink:href=\"#m4ecd96abba\" x=\"244.1\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_6\">\n      <!-- 10 -->\n      <g transform=\"translate(237.7375 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-31\" d=\"M 794 531 \nL 1825 531 \nL 1825 4091 \nL 703 3866 \nL 703 4441 \nL 1819 4666 \nL 2450 4666 \nL 2450 531 \nL 3481 531 \nL 3481 0 \nL 794 0 \nL 794 531 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-31\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"text_7\">\n     <!-- epoch -->\n     <g transform=\"translate(131.221875 174.076563) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-65\" d=\"M 3597 1894 \nL 3597 1613 \nL 953 1613 \nQ 991 1019 1311 708 \nQ 1631 397 2203 397 \nQ 2534 397 2845 478 \nQ 3156 559 3463 722 \nL 3463 178 \nQ 3153 47 2828 -22 \nQ 2503 -91 2169 -91 \nQ 1331 -91 842 396 \nQ 353 884 353 1716 \nQ 353 2575 817 3079 \nQ 1281 3584 2069 3584 \nQ 2775 3584 3186 3129 \nQ 3597 2675 3597 1894 \nz\nM 3022 2063 \nQ 3016 2534 2758 2815 \nQ 2500 3097 2075 3097 \nQ 1594 3097 1305 2825 \nQ 1016 2553 972 2059 \nL 3022 2063 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-70\" d=\"M 1159 525 \nL 1159 -1331 \nL 581 -1331 \nL 581 3500 \nL 1159 3500 \nL 1159 2969 \nQ 1341 3281 1617 3432 \nQ 1894 3584 2278 3584 \nQ 2916 3584 3314 3078 \nQ 3713 2572 3713 1747 \nQ 3713 922 3314 415 \nQ 2916 -91 2278 -91 \nQ 1894 -91 1617 61 \nQ 1341 213 1159 525 \nz\nM 3116 1747 \nQ 3116 2381 2855 2742 \nQ 2594 3103 2138 3103 \nQ 1681 3103 1420 2742 \nQ 1159 2381 1159 1747 \nQ 1159 1113 1420 752 \nQ 1681 391 2138 391 \nQ 2594 391 2855 752 \nQ 3116 1113 3116 1747 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-6f\" d=\"M 1959 3097 \nQ 1497 3097 1228 2736 \nQ 959 2375 959 1747 \nQ 959 1119 1226 758 \nQ 1494 397 1959 397 \nQ 2419 397 2687 759 \nQ 2956 1122 2956 1747 \nQ 2956 2369 2687 2733 \nQ 2419 3097 1959 3097 \nz\nM 1959 3584 \nQ 2709 3584 3137 3096 \nQ 3566 2609 3566 1747 \nQ 3566 888 3137 398 \nQ 2709 -91 1959 -91 \nQ 1206 -91 779 398 \nQ 353 888 353 1747 \nQ 353 2609 779 3096 \nQ 1206 3584 1959 3584 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-63\" d=\"M 3122 3366 \nL 3122 2828 \nQ 2878 2963 2633 3030 \nQ 2388 3097 2138 3097 \nQ 1578 3097 1268 2742 \nQ 959 2388 959 1747 \nQ 959 1106 1268 751 \nQ 1578 397 2138 397 \nQ 2388 397 2633 464 \nQ 2878 531 3122 666 \nL 3122 134 \nQ 2881 22 2623 -34 \nQ 2366 -91 2075 -91 \nQ 1284 -91 818 406 \nQ 353 903 353 1747 \nQ 353 2603 823 3093 \nQ 1294 3584 2113 3584 \nQ 2378 3584 2631 3529 \nQ 2884 3475 3122 3366 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-68\" d=\"M 3513 2113 \nL 3513 0 \nL 2938 0 \nL 2938 2094 \nQ 2938 2591 2744 2837 \nQ 2550 3084 2163 3084 \nQ 1697 3084 1428 2787 \nQ 1159 2491 1159 1978 \nL 1159 0 \nL 581 0 \nL 581 4863 \nL 1159 4863 \nL 1159 2956 \nQ 1366 3272 1645 3428 \nQ 1925 3584 2291 3584 \nQ 2894 3584 3203 3211 \nQ 3513 2838 3513 2113 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-65\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(61.523438 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6f\" transform=\"translate(125 0)\"/>\n      <use xlink:href=\"#DejaVuSans-63\" transform=\"translate(186.181641 0)\"/>\n      <use xlink:href=\"#DejaVuSans-68\" transform=\"translate(241.162109 0)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"matplotlib.axis_2\">\n    <g id=\"ytick_1\">\n     <g id=\"line2d_7\">\n      <defs>\n       <path id=\"m07106ecf73\" d=\"M 0 0 \nL -2 0 \n\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#m07106ecf73\" x=\"48.8\" y=\"94.830837\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n     <g id=\"text_8\">\n      <!-- $\\mathdefault{2\\times10^{2}}$ -->\n      <g transform=\"translate(7.2 98.630055) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-d7\" d=\"M 4488 3438 \nL 3059 2003 \nL 4488 575 \nL 4116 197 \nL 2681 1631 \nL 1247 197 \nL 878 575 \nL 2303 2003 \nL 878 3438 \nL 1247 3816 \nL 2681 2381 \nL 4116 3816 \nL 4488 3438 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-32\" transform=\"translate(0 0.765625)\"/>\n       <use xlink:href=\"#DejaVuSans-d7\" transform=\"translate(83.105469 0.765625)\"/>\n       <use xlink:href=\"#DejaVuSans-31\" transform=\"translate(186.376953 0.765625)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(250 0.765625)\"/>\n       <use xlink:href=\"#DejaVuSans-32\" transform=\"translate(314.580078 39.046875) scale(0.7)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_2\">\n     <g id=\"line2d_8\">\n      <g>\n       <use xlink:href=\"#m07106ecf73\" x=\"48.8\" y=\"64.859318\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n     <g id=\"text_9\">\n      <!-- $\\mathdefault{3\\times10^{2}}$ -->\n      <g transform=\"translate(7.2 68.658537) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-33\" d=\"M 2597 2516 \nQ 3050 2419 3304 2112 \nQ 3559 1806 3559 1356 \nQ 3559 666 3084 287 \nQ 2609 -91 1734 -91 \nQ 1441 -91 1130 -33 \nQ 819 25 488 141 \nL 488 750 \nQ 750 597 1062 519 \nQ 1375 441 1716 441 \nQ 2309 441 2620 675 \nQ 2931 909 2931 1356 \nQ 2931 1769 2642 2001 \nQ 2353 2234 1838 2234 \nL 1294 2234 \nL 1294 2753 \nL 1863 2753 \nQ 2328 2753 2575 2939 \nQ 2822 3125 2822 3475 \nQ 2822 3834 2567 4026 \nQ 2313 4219 1838 4219 \nQ 1578 4219 1281 4162 \nQ 984 4106 628 3988 \nL 628 4550 \nQ 988 4650 1302 4700 \nQ 1616 4750 1894 4750 \nQ 2613 4750 3031 4423 \nQ 3450 4097 3450 3541 \nQ 3450 3153 3228 2886 \nQ 3006 2619 2597 2516 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-33\" transform=\"translate(0 0.765625)\"/>\n       <use xlink:href=\"#DejaVuSans-d7\" transform=\"translate(83.105469 0.765625)\"/>\n       <use xlink:href=\"#DejaVuSans-31\" transform=\"translate(186.376953 0.765625)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(250 0.765625)\"/>\n       <use xlink:href=\"#DejaVuSans-32\" transform=\"translate(314.580078 39.046875) scale(0.7)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_3\">\n     <g id=\"line2d_9\">\n      <g>\n       <use xlink:href=\"#m07106ecf73\" x=\"48.8\" y=\"43.594187\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n     <g id=\"text_10\">\n      <!-- $\\mathdefault{4\\times10^{2}}$ -->\n      <g transform=\"translate(7.2 47.393406) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-34\" transform=\"translate(0 0.765625)\"/>\n       <use xlink:href=\"#DejaVuSans-d7\" transform=\"translate(83.105469 0.765625)\"/>\n       <use xlink:href=\"#DejaVuSans-31\" transform=\"translate(186.376953 0.765625)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(250 0.765625)\"/>\n       <use xlink:href=\"#DejaVuSans-32\" transform=\"translate(314.580078 39.046875) scale(0.7)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_4\">\n     <g id=\"line2d_10\">\n      <g>\n       <use xlink:href=\"#m07106ecf73\" x=\"48.8\" y=\"27.09967\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_5\">\n     <g id=\"line2d_11\">\n      <g>\n       <use xlink:href=\"#m07106ecf73\" x=\"48.8\" y=\"13.622669\" style=\"stroke: #000000; stroke-width: 0.6\"/>\n      </g>\n     </g>\n     <g id=\"text_11\">\n      <!-- $\\mathdefault{6\\times10^{2}}$ -->\n      <g transform=\"translate(7.2 17.421887) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-36\" transform=\"translate(0 0.765625)\"/>\n       <use xlink:href=\"#DejaVuSans-d7\" transform=\"translate(83.105469 0.765625)\"/>\n       <use xlink:href=\"#DejaVuSans-31\" transform=\"translate(186.376953 0.765625)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(250 0.765625)\"/>\n       <use xlink:href=\"#DejaVuSans-32\" transform=\"translate(314.580078 39.046875) scale(0.7)\"/>\n      </g>\n     </g>\n    </g>\n   </g>\n   <g id=\"line2d_12\">\n    <path d=\"M 53.383571 13.5 \nL 62.949286 42.360898 \nL 72.515 46.87407 \nL 82.080714 49.856393 \nL 91.646429 53.144345 \nL 101.212143 56.089532 \nL 110.777857 62.084191 \nL 120.343571 67.113312 \nL 129.909286 73.680191 \nL 139.475 80.701274 \nL 149.040714 87.594735 \nL 158.606429 94.676509 \nL 168.172143 101.921588 \nL 177.737857 107.578576 \nL 187.303571 112.8046 \nL 196.869286 119.951943 \nL 206.435 124.094395 \nL 216.000714 130.075985 \nL 225.566429 134.441195 \nL 235.132143 139.5 \n\" clip-path=\"url(#p7847c46dc5)\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_13\">\n    <path d=\"M 68.33 52.425448 \nL 87.86 55.345532 \nL 107.39 66.728263 \nL 126.92 74.516907 \nL 146.45 90.933065 \nL 165.98 97.713458 \nL 185.51 114.095849 \nL 205.04 114.157803 \nL 224.57 128.236507 \nL 244.1 130.963378 \n\" clip-path=\"url(#p7847c46dc5)\" style=\"fill: none; stroke-dasharray: 5.55,2.4; stroke-dashoffset: 0; stroke: #ff7f0e; stroke-width: 1.5\"/>\n   </g>\n   <g id=\"patch_3\">\n    <path d=\"M 48.8 145.8 \nL 48.8 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_4\">\n    <path d=\"M 244.1 145.8 \nL 244.1 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_5\">\n    <path d=\"M 48.8 145.8 \nL 244.1 145.8 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_6\">\n    <path d=\"M 48.8 7.2 \nL 244.1 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"legend_1\">\n    <g id=\"patch_7\">\n     <path d=\"M 161.35 45.1125 \nL 237.1 45.1125 \nQ 239.1 45.1125 239.1 43.1125 \nL 239.1 14.2 \nQ 239.1 12.2 237.1 12.2 \nL 161.35 12.2 \nQ 159.35 12.2 159.35 14.2 \nL 159.35 43.1125 \nQ 159.35 45.1125 161.35 45.1125 \nz\n\" style=\"fill: #ffffff; opacity: 0.8; stroke: #cccccc; stroke-linejoin: miter\"/>\n    </g>\n    <g id=\"line2d_14\">\n     <path d=\"M 163.35 20.298438 \nL 173.35 20.298438 \nL 183.35 20.298438 \n\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n    </g>\n    <g id=\"text_12\">\n     <!-- train_ppl -->\n     <g transform=\"translate(191.35 23.798438) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-74\" d=\"M 1172 4494 \nL 1172 3500 \nL 2356 3500 \nL 2356 3053 \nL 1172 3053 \nL 1172 1153 \nQ 1172 725 1289 603 \nQ 1406 481 1766 481 \nL 2356 481 \nL 2356 0 \nL 1766 0 \nQ 1100 0 847 248 \nQ 594 497 594 1153 \nL 594 3053 \nL 172 3053 \nL 172 3500 \nL 594 3500 \nL 594 4494 \nL 1172 4494 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-72\" d=\"M 2631 2963 \nQ 2534 3019 2420 3045 \nQ 2306 3072 2169 3072 \nQ 1681 3072 1420 2755 \nQ 1159 2438 1159 1844 \nL 1159 0 \nL 581 0 \nL 581 3500 \nL 1159 3500 \nL 1159 2956 \nQ 1341 3275 1631 3429 \nQ 1922 3584 2338 3584 \nQ 2397 3584 2469 3576 \nQ 2541 3569 2628 3553 \nL 2631 2963 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-61\" d=\"M 2194 1759 \nQ 1497 1759 1228 1600 \nQ 959 1441 959 1056 \nQ 959 750 1161 570 \nQ 1363 391 1709 391 \nQ 2188 391 2477 730 \nQ 2766 1069 2766 1631 \nL 2766 1759 \nL 2194 1759 \nz\nM 3341 1997 \nL 3341 0 \nL 2766 0 \nL 2766 531 \nQ 2569 213 2275 61 \nQ 1981 -91 1556 -91 \nQ 1019 -91 701 211 \nQ 384 513 384 1019 \nQ 384 1609 779 1909 \nQ 1175 2209 1959 2209 \nL 2766 2209 \nL 2766 2266 \nQ 2766 2663 2505 2880 \nQ 2244 3097 1772 3097 \nQ 1472 3097 1187 3025 \nQ 903 2953 641 2809 \nL 641 3341 \nQ 956 3463 1253 3523 \nQ 1550 3584 1831 3584 \nQ 2591 3584 2966 3190 \nQ 3341 2797 3341 1997 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-69\" d=\"M 603 3500 \nL 1178 3500 \nL 1178 0 \nL 603 0 \nL 603 3500 \nz\nM 603 4863 \nL 1178 4863 \nL 1178 4134 \nL 603 4134 \nL 603 4863 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-6e\" d=\"M 3513 2113 \nL 3513 0 \nL 2938 0 \nL 2938 2094 \nQ 2938 2591 2744 2837 \nQ 2550 3084 2163 3084 \nQ 1697 3084 1428 2787 \nQ 1159 2491 1159 1978 \nL 1159 0 \nL 581 0 \nL 581 3500 \nL 1159 3500 \nL 1159 2956 \nQ 1366 3272 1645 3428 \nQ 1925 3584 2291 3584 \nQ 2894 3584 3203 3211 \nQ 3513 2838 3513 2113 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-5f\" d=\"M 3263 -1063 \nL 3263 -1509 \nL -63 -1509 \nL -63 -1063 \nL 3263 -1063 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-6c\" d=\"M 603 4863 \nL 1178 4863 \nL 1178 0 \nL 603 0 \nL 603 4863 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-74\"/>\n      <use xlink:href=\"#DejaVuSans-72\" transform=\"translate(39.208984 0)\"/>\n      <use xlink:href=\"#DejaVuSans-61\" transform=\"translate(80.322266 0)\"/>\n      <use xlink:href=\"#DejaVuSans-69\" transform=\"translate(141.601562 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6e\" transform=\"translate(169.384766 0)\"/>\n      <use xlink:href=\"#DejaVuSans-5f\" transform=\"translate(232.763672 0)\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(282.763672 0)\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(346.240234 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6c\" transform=\"translate(409.716797 0)\"/>\n     </g>\n    </g>\n    <g id=\"line2d_15\">\n     <path d=\"M 163.35 35.254688 \nL 173.35 35.254688 \nL 183.35 35.254688 \n\" style=\"fill: none; stroke-dasharray: 5.55,2.4; stroke-dashoffset: 0; stroke: #ff7f0e; stroke-width: 1.5\"/>\n    </g>\n    <g id=\"text_13\">\n     <!-- val_ppl -->\n     <g transform=\"translate(191.35 38.754688) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-76\" d=\"M 191 3500 \nL 800 3500 \nL 1894 563 \nL 2988 3500 \nL 3597 3500 \nL 2284 0 \nL 1503 0 \nL 191 3500 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-76\"/>\n      <use xlink:href=\"#DejaVuSans-61\" transform=\"translate(59.179688 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6c\" transform=\"translate(120.458984 0)\"/>\n      <use xlink:href=\"#DejaVuSans-5f\" transform=\"translate(148.242188 0)\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(198.242188 0)\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(261.71875 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6c\" transform=\"translate(325.195312 0)\"/>\n     </g>\n    </g>\n   </g>\n  </g>\n </g>\n <defs>\n  <clipPath id=\"p7847c46dc5\">\n   <rect x=\"48.8\" y=\"7.2\" width=\"195.3\" height=\"138.6\"/>\n  </clipPath>\n </defs>\n</svg>\n"
     },
     "metadata": {}
    }
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "lstm-deep-recurrent-networks-3"
   },
   "source": [
    "ppls['LSTM (2 layers)'] = val_ppl(model)\n",
    "print(f'{\"model\":>16} {\"val ppl\":>8}')\n",
    "for name, p in ppls.items():\n",
    "    print(f'{name:>16} {p:>8.1f}')"
   ],
   "execution_count": 18,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "           model  val ppl\n  LSTM (scratch)    103.7\n            LSTM     90.7\n             GRU     79.2\n LSTM (2 layers)    122.7\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=lstm-md-14 fw=all -->\n",
    "\n",
    "The scoreboard repays a careful read. The GRU beats the vanilla\n",
    "RNN of that section clearly at the same recipe; the\n",
    "single-layer LSTM roughly matches the vanilla model within this short\n",
    "budget, for the initialization and budget reasons discussed above; and the\n",
    "second layer does not pay at all, adding optimization difficulty and\n",
    "capacity that a corpus this small cannot fill. Depth pays on large\n",
    "corpora; knowing when it will not is part of the craft.\n",
    "\n",
    "### Bidirectional Recurrent Networks\n",
    "\n",
    "Language modeling forces left-to-right reading, but a model that\n",
    "*analyzes* a complete, given sequence, a part-of-speech tagger say, has\n",
    "the future context in hand, and it is informative. A *bidirectional RNN*\n",
    "[@Schuster.Paliwal.1997] runs a second, independent recurrent layer\n",
    "backward over the same input and concatenates the two states at each\n",
    "step into\n",
    "$\\mathbf{H}_t = [\\overrightarrow{\\mathbf{H}}_t, \\overleftarrow{\\mathbf{H}}_t]\n",
    "\\in \\mathbb{R}^{n \\times 2h}$: every output conditions on the entire\n",
    "sequence, at twice the compute. In frameworks this is a flag or a\n",
    "wrapper, and the shape check shows exactly what changed, the per-step\n",
    "output width doubling to $2h$."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "lstm-bidirectional-recurrent-networks"
   },
   "source": [
    "birnn = nn.LSTM(64, 128, bidirectional=True)\n",
    "X = d2l.randn(32, 8, 64)  # (num_steps, batch_size, num_inputs)\n",
    "outputs, (H, C) = birnn(X)\n",
    "outputs.shape, H.shape"
   ],
   "execution_count": 19,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "(torch.Size([32, 8, 256]), torch.Size([2, 8, 128]))"
     },
     "metadata": {},
     "execution_count": 19
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=lstm-md-15 fw=all -->\n",
    "\n",
    "One warning matters more than the mechanics: **a bidirectional network\n",
    "cannot serve unchanged as a causal decoder**. Every $\\mathbf{H}_t$\n",
    "conditions on tokens after $t$, so the model cannot emit tokens\n",
    "left-to-right autoregressively; at generation time the future it expects\n",
    "does not exist. Worse, training one to \"predict the\n",
    "next token\" is self-deception: the backward pass hands the network the\n",
    "very token it must predict, training loss collapses, and the model is\n",
    "useless at inference, a common and expensive mistake in the RNN era.\n",
    "The scope of the warning is the causal role, not generation wholesale:\n",
    "bidirectional encoders live inside generative systems (the encoder half\n",
    "of a translation model conditions a causal decoder), and\n",
    "non-autoregressive decoders generate whole sequences by iterative\n",
    "refinement rather than left to right. Use\n",
    "bidirectional layers where the full input is given up front and\n",
    "per-position representations are wanted: tagging, span labeling, and\n",
    "encoding. Their most consequential success was ELMo\n",
    "[@Peters.Neumann.Iyyer.ea.2018], deep bidirectional LSTMs trained to\n",
    "produce contextual word embeddings (see also\n",
    "[@Graves.Schmidhuber.2005]); that role, and the entire\n",
    "encode-both-directions idea, passed to the bidirectional attention of\n",
    "BERT [@Devlin.Chang.Lee.ea.2018], which we study in the pretraining\n",
    "chapters.\n",
    "\n",
    "## Gates beyond Recurrent Networks\n",
    "\n",
    "The cells above are, by the standards of this book's earlier chapters, old\n",
    "technology. The gate is not. Once you know its shape, a sigmoid (or other\n",
    "bounded) branch elementwise-multiplying a signal branch, you will find it\n",
    "across modern deep learning, doing the same job of content-dependent\n",
    "routing that it does inside the LSTM.\n",
    "\n",
    "Highway networks [@srivastava2015highway] applied the LSTM's gating\n",
    "to *depth*: a transform gate blends each layer's output with its\n",
    "unmodified input, letting very deep feedforward networks train years\n",
    "before normalization made it routine, and prefiguring the residual\n",
    "connections of that section. Transformer blocks carry gates in\n",
    "their MLPs: the gated linear unit family, whose SwiGLU variant\n",
    "[@Shazeer.2020] computes\n",
    "$\\textrm{Swish}(\\mathbf{x}\\mathbf{W}_1) \\odot \\mathbf{x}\\mathbf{W}_2$, is\n",
    "the standard MLP in most contemporary open language models, and\n",
    "that section measured what that gate buys in a\n",
    "matched-parameter sweep. And the recurrence story of this chapter\n",
    "continues the pattern most directly: the selective state space model\n",
    "Mamba [@Gu.Dao.2023] modulates a linear state with an\n",
    "input-dependent step size $\\Delta_t$ that plays precisely the role of a\n",
    "forget gate, and Griffin [@De.Smith.Fernando.ea.2024] builds\n",
    "production-scale hybrids from gated linear recurrences.\n",
    "\n",
    "The gate even circled back to the cell it came from. xLSTM\n",
    "[@Beck.Poppel.Spanring.ea.2024] revisits the LSTM with *exponential*\n",
    "gating: its sLSTM cell replaces the sigmoid's $(0,1)$ range with $\\exp$,\n",
    "so a confident new write can overwhelm the accumulated state rather than\n",
    "merely dilute it, and a running maximum carried in log space keeps the\n",
    "unbounded gates numerically stable. The result revives the classic\n",
    "recurrent, state-dependent cell at modern scale. Its sibling, the\n",
    "matrix-memory mLSTM, gives up state-dependent gating to regain parallel\n",
    "training, and appears where it belongs, in the family table of\n",
    "that section. One idea, many wardrobes:\n",
    "\n",
    "| Where | The gate | What it controls |\n",
    "| :-- | :-- | :-- |\n",
    "| LSTM (1997) | forget gate $\\mathbf{F}_t$ | decay of the cell state $\\mathbf{C}_t$ |\n",
    "| GRU (2014) | update gate $\\mathbf{Z}_t$ | copy-vs-overwrite blend of $\\mathbf{H}_t$ |\n",
    "| Highway nets (2015) | transform gate | skip-vs-transform blend across depth |\n",
    "| GLU, SwiGLU MLPs (that section) | activation branch | which channels pass through the MLP |\n",
    "| Mamba (2023) | step size $\\Delta_t$ | decay of a linear state, from input only |\n",
    "| Griffin, mLSTM (2024) | recurrence gates | decay and write strength of linear cells |\n",
    "\n",
    "The last three rows share a further twist: their gates depend on the\n",
    "*input only*, not on the state. Give up state-dependent gating and the\n",
    "recurrence becomes linear, which unlocks parallel training across the\n",
    "sequence while keeping the learned forgetting that made the LSTM work.\n",
    "that section takes exactly that step, and the rest of the chapter\n",
    "rides it: linear state space models, made selective in\n",
    "that section, reconciled with attention in\n",
    "that section, taught to edit their memory in\n",
    "that section, reread as learning at test time in\n",
    "that section, and finally hybridized with the\n",
    "transformer in that section. The gate you now understand is the\n",
    "first rung of that ladder.\n",
    "\n",
    "## Summary\n",
    "\n",
    "Vanishing gradients are an architectural problem, and gating is the\n",
    "architectural answer: keep an additive state path whose direct-path\n",
    "Jacobian stays\n",
    "near the identity, and control reads, writes, and erasures with learned\n",
    "sigmoid gates that multiply elementwise. The LSTM realizes this with a\n",
    "protected cell state and three gates; its cell update\n",
    "$\\mathbf{C}_t = \\mathbf{F}_t \\odot \\mathbf{C}_{t-1} + \\mathbf{I}_t \\odot\n",
    "\\tilde{\\mathbf{C}}_t$ is the constant error carousel that lets both\n",
    "information and gradients cross long horizons. The GRU reaches nearly the\n",
    "same behavior with two gates and no separate cell, at three quarters of\n",
    "the recurrent parameters. On an identical short training recipe the GRU\n",
    "clearly outperforms the vanilla RNN from that section, while\n",
    "the LSTM needs a longer budget or a friendlier initialization before its\n",
    "extra machinery pays: architecture and optimization cannot be judged\n",
    "separately. Depth (stacked layers) and bidirectionality (a backward pass\n",
    "concatenated to the forward one) are orthogonal wiring choices; the\n",
    "latter yields encoders, which cannot double unchanged as causal\n",
    "decoders. The gate itself outlived these\n",
    "architectures: it routes channels in transformer MLPs\n",
    "(that section), revives the LSTM in exponential form\n",
    "(xLSTM), and, made input-dependent only, powers the linear recurrences\n",
    "and state space models that the rest of this chapter builds.\n",
    "\n",
    "**What the experiments show, and what they do not.** Under one fixed\n",
    "small-corpus recipe, gate-equipped cells train reliably where the vanilla\n",
    "RNN plateaus, and initialization can matter as much as architecture at a\n",
    "ten-epoch budget. These are single seeded runs on one small corpus: they\n",
    "do not rank LSTM against GRU in general, and they say nothing about\n",
    "behavior at scale, where budgets, tuning, and data change the ordering.\n",
    "\n",
    "## Exercises\n",
    "\n",
    "1. [extended] Adjust the hyperparameters (width, embedding dimension,\n",
    "   learning rate,\n",
    "   number of epochs) and study their effect on training time and\n",
    "   perplexity. How far below the section's result can you push the LSTM at\n",
    "   this data budget?\n",
    "1. [short-code] Ablate the forget gate: modify `LSTMScratch` so that\n",
    "   $\\mathbf{F}_t = 1$ always (the original 1997 design, which lacked a\n",
    "   forget gate). Retrain with the section's recipe and report the\n",
    "   perplexity. Explain the result using the equation: what happens\n",
    "   to the cell state over a 32-step window when nothing is ever erased?\n",
    "1. [short-code] Initialize the forget-gate bias $\\mathbf{b}_\\textrm{f}$ to 1 instead of\n",
    "   0 in `LSTMScratch` and retrain. Why does starting from\n",
    "   $\\mathbf{F}_t \\approx 0.73$ rather than $0.5$ help at short training\n",
    "   budgets? Then double the number of epochs: does the LSTM now match or\n",
    "   beat the section's GRU?\n",
    "1. [short-code] Parameter-matched comparison: compute the number of recurrent-core\n",
    "   parameters of the LSTM at $h = 128$ and find the GRU width $h'$ with\n",
    "   approximately the same count. Train that GRU and compare perplexities.\n",
    "   Does the GRU's advantage survive the handicap being removed?\n",
    "1. [conceptual] Compare the per-step computational cost of vanilla RNN, GRU, and LSTM\n",
    "   cells for a given $d$ and $h$, counting matrix products. How do\n",
    "   training cost and single-token inference cost differ?\n",
    "1. [conceptual] The cell state is already squashed once in the equation. Why\n",
    "   does the equation apply $\\tanh$ to $\\mathbf{C}_t$ again before\n",
    "   the output gate, rather than using $\\mathbf{O}_t \\odot \\mathbf{C}_t$?\n",
    "   What could go wrong after many steps of accumulation without it?\n",
    "1. [extended] Implement a GRU variant with only a reset gate, and one with only an\n",
    "   update gate. Which one still trains well on this section's task, and\n",
    "   why would you expect that from the equation?\n",
    "1. [conceptual] Suppose the input at step $t'$ must determine the output at a much\n",
    "   later step $t$. For each gate of the GRU (and of the LSTM), state the\n",
    "   ideal values in the interval from $t'$ to $t$.\n",
    "1. [extended] Sweep `num_layers` from 1 to 4 for the concise LSTM. Report perplexity\n",
    "   and wall-clock time per configuration. At what depth does this corpus\n",
    "   stop rewarding extra layers, and what would you change to make depth\n",
    "   pay?\n",
    "1. [conceptual] Why can a bidirectional RNN not be used for language modeling or any\n",
    "   other autoregressive generation? Argue from the factorization\n",
    "   $P(x_1, \\ldots, x_T) = \\prod_t P(x_t \\mid x_{<t})$, then describe what\n",
    "   concretely goes wrong at sampling time if you try."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=lstm-md-16 fw=pytorch -->\n",
    "\n",
    "[Discussions](https://d2l.discourse.group/t/1057)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "d2l-pytorch",
   "display_name": "d2l (PyTorch)",
   "language": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
