{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "---\n",
    "execute:\n",
    "  enabled: false\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=seq2seq-md-1 fw=all -->\n",
    "\n",
    "# Encoder-Decoder Models for Sequence Transduction\n",
    "\n",
    "The language models of that section read a sequence and continue it. Many\n",
    "of the most useful problems in machine learning have a different shape: read one\n",
    "sequence and emit a *different* one. Translate an English sentence into French,\n",
    "transcribe an audio clip into words, summarize a document, answer a question.\n",
    "The input and the output are both sequences, they usually have different lengths,\n",
    "and their tokens need not line up position by position. Problems of this kind are\n",
    "called *sequence transduction*, or *sequence-to-sequence* (seq2seq) learning, and\n",
    "they were the arena in which recurrent networks first beat classical pipelines at\n",
    "scale [@Sutskever.Vinyals.Le.2014; @Cho.Van-Merrienboer.Gulcehre.ea.2014].\n",
    "\n",
    "This section is deliberately historical: it builds the 2014 system that taught\n",
    "the field what sequence transduction demands, using the recurrent tools of\n",
    "that section. You have already met both of its successors — the\n",
    "attention of that section grew directly out of this model's central\n",
    "weakness, and the transformer encoder-decoders of that section\n",
    "displaced it outright — so read it as the baseline that the rest of this part,\n",
    "and much of the modern field, is measured against. We introduce the\n",
    "*encoder-decoder* abstraction that organizes almost every transduction model\n",
    "built since, assemble a small English-to-French translator from two GRUs, train\n",
    "it with the teacher forcing of that section and a masked loss, translate\n",
    "with the decoding strategies of that section, and score the results\n",
    "with a character-level metric. We end by *measuring* the weakness itself: the\n",
    "encoder squeezes the entire source through a single fixed-size vector, and that\n",
    "bottleneck is where attention began."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "seq2seq-imports"
   },
   "source": [
    "import collections\n",
    "import math\n",
    "from d2l import jax as d2l\n",
    "from flax import nnx\n",
    "import jax\n",
    "from jax import numpy as jnp\n",
    "import optax"
   ],
   "execution_count": 1,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=seq2seq-md-2 fw=all -->\n",
    "\n",
    "## Sequence Transduction and the Abstraction\n",
    "\n",
    "The trick that makes transduction tractable is to split the model in two\n",
    "(the figure). An *encoder* consumes the variable-length source\n",
    "sequence and compresses it into an intermediate state of fixed shape. A *decoder*\n",
    "consumes that state and produces the variable-length target, one token at a time.\n",
    "The state is the sole channel between them: everything the decoder ever learns\n",
    "about the source, it learns through that one object.\n",
    "\n",
    "![The encoder-decoder architecture: the encoder maps a variable-length source into a fixed-shape state, and the decoder maps the state back into a variable-length target.](../img/mdl-modernrnn-encoder-decoder.svg)\n",
    "\n",
    "The decoder half should look familiar: it *is* a language model in the sense of\n",
    "that section, factorizing the target probability autoregressively,\n",
    "only now every factor is additionally conditioned on the encoder state\n",
    "$\\mathbf{c}$,\n",
    "\n",
    "$$P(y_1, \\ldots, y_{T'} \\mid x_1, \\ldots, x_T) = \\prod_{t'=1}^{T'} P(y_{t'} \\mid y_1, \\ldots, y_{t'-1}, \\mathbf{c}).$$\n",
    "\n",
    "A decoder is a *conditional* language model, and conditioning is the whole game:\n",
    "the same next-token machinery, pointed at a source. This is why the abstraction\n",
    "long outlived the RNN that first filled it. Swap the encoder for a convolutional\n",
    "audio front-end and you have Whisper transcribing speech; swap it for a vision\n",
    "backbone and you have an image-captioning model; feed it text and images together\n",
    "and you have the multimodal front-ends of current assistants. The shape in\n",
    "the figure survives; only the encoder changes.\n",
    "\n",
    "Because the abstraction is reused so widely, it pays to fix it in code as a pair\n",
    "of interfaces before committing to any particular network. The encoder exposes a\n",
    "single method that maps an input to outputs. Later stages may need side\n",
    "information, such as the length of the source excluding padding, so we allow\n",
    "extra positional arguments."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "seq2seq-abstraction-encoder"
   },
   "source": [
    "class Encoder(nnx.Module):\n",
    "    \"\"\"The base encoder interface for the encoder-decoder architecture.\"\"\"\n",
    "    # Later there can be additional arguments (e.g., length excluding padding)\n",
    "    def __call__(self, X, *args):\n",
    "        raise NotImplementedError"
   ],
   "execution_count": 2,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=seq2seq-md-3 fw=all -->\n",
    "\n",
    "The decoder adds one method, `init_state`, that turns the encoder output into\n",
    "whatever the decoder wants to carry as its running state, plus a `forward` that\n",
    "maps the next input token and the current state to an output and an updated state."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "seq2seq-abstraction-decoder"
   },
   "source": [
    "class Decoder(nnx.Module):\n",
    "    \"\"\"The base decoder interface for the encoder-decoder architecture.\"\"\"\n",
    "    def init_state(self, enc_all_outputs, *args):\n",
    "        raise NotImplementedError\n",
    "\n",
    "    def __call__(self, X, state):\n",
    "        raise NotImplementedError"
   ],
   "execution_count": 3,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=seq2seq-md-4 fw=all -->\n",
    "\n",
    "Wiring them together is a three-line class: run the encoder, hand its output to\n",
    "`init_state`, then run the decoder. Subclassing `d2l.Classifier` gives us the\n",
    "training loop and the softmax cross-entropy loss for free, so a concrete\n",
    "model needs to specify only the encoder, the decoder, and how the two\n",
    "connect."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "seq2seq-abstraction-together"
   },
   "source": [
    "class EncoderDecoder(d2l.Classifier):\n",
    "    \"\"\"The base class for the encoder-decoder architecture.\"\"\"\n",
    "    def __init__(self, encoder, decoder):\n",
    "        super().__init__()\n",
    "        self.encoder = encoder\n",
    "        self.decoder = decoder\n",
    "\n",
    "    def forward(self, enc_X, dec_X, *args):\n",
    "        enc_all_outputs = self.encoder(enc_X, *args)\n",
    "        dec_state = self.decoder.init_state(enc_all_outputs, *args)\n",
    "        # Return decoder output only\n",
    "        return self.decoder(dec_X, dec_state)[0]"
   ],
   "execution_count": 4,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=seq2seq-md-5 fw=all -->\n",
    "\n",
    "## The Machine Translation Dataset\n",
    "\n",
    "To make the abstraction concrete we need data. We use a small English-French\n",
    "corpus of sentence pairs from the [Tatoeba Project](http://www.manythings.org/anki/):\n",
    "each line is a tab-separated pair, an English *source* and its French *target*.\n",
    "Statistical machine translation on corpora like this\n",
    "[@Brown.Cocke.Della-Pietra.ea.1990] was, for decades, the flagship\n",
    "application that drove sequence modeling forward, and it remains the cleanest\n",
    "task on which to see an encoder-decoder learn.\n",
    "\n",
    "Tokenization follows that section exactly: a byte-level BPE\n",
    "tokenizer, trained here on both languages at once to yield a *single shared\n",
    "vocabulary*. Sharing one vocabulary across source and target is standard practice\n",
    "in translation [@Sennrich.Haddow.Birch.2015]. English and French overlap\n",
    "heavily in the Latin alphabet, in punctuation, and in outright shared words\n",
    "(\"nation\", \"possible\"), so a joint vocabulary spends its budget where it is\n",
    "actually needed and lets a token learned on one side transfer to the other; and\n",
    "because BPE starts from raw bytes, no word in either language is ever out of\n",
    "vocabulary. Our tokenizer reserves the three special ids the batching below\n",
    "needs: `<pad>`, `<bos>`, and `<eos>`.\n",
    "\n",
    "Batching pads or truncates every sentence to a fixed length `num_steps`. We append\n",
    "`<eos>` to mark where a sequence ends (so the decoder can learn to stop), pad\n",
    "short sentences with `<pad>` to a common length, and truncate long ones. Targets\n",
    "additionally get a `<bos>` prefix: the decoder is fed the target shifted right (by\n",
    "one, starting from `<bos>`), and is trained to predict the target shifted left\n",
    "(ending in `<eos>`), which is exactly the teacher forcing of that section.\n",
    "We also record each source's length excluding padding, the bookkeeping an\n",
    "attention-based decoder uses to mask padded positions\n",
    "(that section); our recurrent model here never\n",
    "reads it."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "seq2seq-mtfraeng"
   },
   "source": [
    "class MTFraEng(d2l.DataModule):\n",
    "    \"\"\"The English-French dataset, tokenized with a shared byte-level BPE.\"\"\"\n",
    "    def __init__(self, batch_size, num_steps=20, num_train=1024, num_val=128,\n",
    "                 vocab_size=4000):\n",
    "        super().__init__()\n",
    "        self.save_hyperparameters()\n",
    "        pairs = self._pairs(self._preprocess(self._download()))\n",
    "        # Train ONE shared byte-level BPE over both languages.\n",
    "        m = min(5000, len(pairs))\n",
    "        self.tokenizer = d2l.BPETokenizer(\n",
    "            vocab_size, pattern=d2l.BPETokenizer.GPT2_PATTERN)\n",
    "        self.tokenizer.train('\\n'.join([p[0] for p in pairs[:m]] +\n",
    "                                       [p[1] for p in pairs[:m]]))\n",
    "        # src_vocab / tgt_vocab both refer to the shared tokenizer.\n",
    "        self.src_vocab = self.tgt_vocab = self.tokenizer\n",
    "        pairs = pairs[:num_train + num_val]\n",
    "        self.src_sents = [p[0] for p in pairs]\n",
    "        self.tgt_sents = [p[1] for p in pairs]\n",
    "        self.arrays = self._encode(pairs)\n",
    "\n",
    "    def _download(self):\n",
    "        d2l.extract(d2l.download(\n",
    "            d2l.DATA_URL + 'fra-eng.zip', self.root,\n",
    "            '94646ad1522d915e7b0f9296181140edcf86a4f5'))\n",
    "        with open(self.root + '/fra-eng/fra.txt', encoding='utf-8') as f:\n",
    "            return f.read()\n",
    "\n",
    "    def _preprocess(self, text):\n",
    "        # Normalize spaces, lowercase, and put a space before punctuation.\n",
    "        text = text.replace(' ', ' ').replace('\\xa0', ' ').lower()\n",
    "        no_space = lambda c, prev: c in ',.!?' and prev != ' '\n",
    "        return ''.join([' ' + c if i > 0 and no_space(c, text[i - 1]) else c\n",
    "                        for i, c in enumerate(text)])\n",
    "\n",
    "    def _pairs(self, text):\n",
    "        return [ln.split('\\t') for ln in text.split('\\n') if ln.count('\\t') == 1]"
   ],
   "execution_count": 5,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=seq2seq-md-6 fw=all -->\n",
    "\n",
    "The `_encode` method turns a list of sentence pairs into the four arrays every\n",
    "batch carries: the padded source, the decoder input (target with `<bos>`), the\n",
    "source length excluding padding, and the label (target with `<eos>`). Because it\n",
    "takes an explicit list of pairs, we reuse it both to build the dataset and,\n",
    "later, to encode arbitrary sentences we want to translate."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "seq2seq-mtfraeng-encode"
   },
   "source": [
    "@d2l.add_to_class(MTFraEng)\n",
    "def _encode(self, pairs):\n",
    "    tok, t = self.tokenizer, self.num_steps\n",
    "    def row(sent, is_tgt):\n",
    "        ids = tok.encode(sent)\n",
    "        ids = (ids[:t - 1] + [tok.eos] if len(ids) >= t else\n",
    "               ids + [tok.eos] + [tok.pad] * (t - 1 - len(ids)))\n",
    "        return [tok.bos] + ids if is_tgt else ids\n",
    "    src = d2l.tensor([row(s, False) for s, _ in pairs])\n",
    "    tgt = d2l.tensor([row(t, True) for _, t in pairs])\n",
    "    valid_len = d2l.reduce_sum(d2l.astype(src != tok.pad, d2l.int32), 1)\n",
    "    return src, tgt[:, :-1], valid_len, tgt[:, 1:]\n",
    "\n",
    "@d2l.add_to_class(MTFraEng)\n",
    "def build(self, src_sentences, tgt_sentences):\n",
    "    return self._encode([(self._preprocess(s), self._preprocess(t))\n",
    "                         for s, t in zip(src_sentences, tgt_sentences)])\n",
    "\n",
    "@d2l.add_to_class(MTFraEng)\n",
    "def get_dataloader(self, train):\n",
    "    idx = slice(0, self.num_train) if train else slice(self.num_train, None)\n",
    "    return self.get_tensorloader(self.arrays, train, idx)"
   ],
   "execution_count": 6,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=seq2seq-md-7 fw=all -->\n",
    "\n",
    "Two small shims let the shared tokenizer stand in for the `src_vocab` /\n",
    "`tgt_vocab` interface that later chapters expect, mapping a special token to its\n",
    "id and a list of ids back to token strings."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "seq2seq-vocab-shims"
   },
   "source": [
    "@d2l.add_to_class(d2l.BPETokenizer)\n",
    "def __getitem__(self, token):\n",
    "    return self.specials[token]\n",
    "\n",
    "@d2l.add_to_class(d2l.BPETokenizer)\n",
    "def to_tokens(self, ids):\n",
    "    return [self.decode([int(i)]) for i in ids]"
   ],
   "execution_count": 7,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=seq2seq-md-8 fw=all -->\n",
    "\n",
    "Let us instantiate the dataset and read one small batch. The four tensors are the\n",
    "source, the decoder input (note the leading `<bos>` id), the source valid length,\n",
    "and the label (the target shifted one step left)."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "seq2seq-mtfraeng-batch"
   },
   "source": [
    "data = MTFraEng(batch_size=3)\n",
    "src, dec_in, src_valid_len, label = next(iter(data.train_dataloader()))\n",
    "print('source:', d2l.astype(src, d2l.int32))\n",
    "print('decoder input:', d2l.astype(dec_in, d2l.int32))\n",
    "print('source valid length:', d2l.astype(src_valid_len, d2l.int32))\n",
    "print('shared vocabulary size:', len(data.tokenizer))"
   ],
   "execution_count": 8,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "source: [[ 524  735  256 4002 4000 4000 4000 4000 4000 4000 4000 4000 4000 4000\n  4000 4000 4000 4000 4000 4000]\n [ 641 1346  111  466  262 4002 4000 4000 4000 4000 4000 4000 4000 4000\n  4000 4000 4000 4000 4000 4000]\n [ 623  262 4002 4000 4000 4000 4000 4000 4000 4000 4000 4000 4000 4000\n  4000 4000 4000 4000 4000 4000]]\ndecoder input: [[4001  815  361 1819  256 4002 4000 4000 4000 4000 4000 4000 4000 4000\n  4000 4000 4000 4000 4000 4000]\n [4001 1118  338 1830  371 3663  262 4002 4000 4000 4000 4000 4000 4000\n  4000 4000 4000 4000 4000 4000]\n [4001  740   45  507  262 4002 4000 4000 4000 4000 4000 4000 4000 4000\n  4000 4000 4000 4000 4000 4000]]\nsource valid length: [4 6 3]\nshared vocabulary size: 4003\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=seq2seq-md-9 fw=all -->\n",
    "\n",
    "Padding is a real cost, so it is worth seeing how much of it we do. The histogram\n",
    "below plots the distribution of tokenized sentence lengths for the two languages.\n",
    "Most sentences are short, which is why a modest `num_steps` suffices, and it is\n",
    "this same skew that makes a masked loss (below) worthwhile: without masking, the\n",
    "padding on short sentences would dominate the gradient."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "seq2seq-length-hist-fn"
   },
   "source": [
    "def show_list_len_pair_hist(legend, xlabel, ylabel, xlist, ylist):\n",
    "    \"\"\"Plot the histogram for list length pairs.\"\"\"\n",
    "    d2l.set_figsize()\n",
    "    _, _, patches = d2l.plt.hist(\n",
    "        [[len(l) for l in xlist], [len(l) for l in ylist]])\n",
    "    d2l.plt.xlabel(xlabel)\n",
    "    d2l.plt.ylabel(ylabel)\n",
    "    for patch in patches[1].patches:\n",
    "        patch.set_hatch('/')\n",
    "    d2l.plt.legend(legend)"
   ],
   "execution_count": 9,
   "outputs": []
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "seq2seq-length-hist"
   },
   "source": [
    "src_ids = [data.tokenizer.encode(s) for s in data.src_sents]\n",
    "tgt_ids = [data.tokenizer.encode(s) for s in data.tgt_sents]\n",
    "show_list_len_pair_hist(['source', 'target'], '# tokens per sentence',\n",
    "                        'count', src_ids, tgt_ids);"
   ],
   "execution_count": 10,
   "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=\"249.465625pt\" height=\"183.35625pt\" viewBox=\"0 0 249.465625 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:58:45.090579</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 249.465625 183.35625 \nL 249.465625 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 46.965625 145.8 \nL 242.265625 145.8 \nL 242.265625 7.2 \nL 46.965625 7.2 \nz\n\" style=\"fill: #ffffff\"/>\n   </g>\n   <g id=\"patch_3\">\n    <path d=\"M 55.842898 145.8 \nL 63.089651 145.8 \nL 63.089651 13.8 \nL 55.842898 13.8 \nz\n\" clip-path=\"url(#p0b9ebb167d)\" style=\"fill: #1f77b4\"/>\n   </g>\n   <g id=\"patch_4\">\n    <path d=\"M 73.959781 145.8 \nL 81.206534 145.8 \nL 81.206534 47.766102 \nL 73.959781 47.766102 \nz\n\" clip-path=\"url(#p0b9ebb167d)\" style=\"fill: #1f77b4\"/>\n   </g>\n   <g id=\"patch_5\">\n    <path d=\"M 92.076664 145.8 \nL 99.323417 145.8 \nL 99.323417 142.952542 \nL 92.076664 142.952542 \nz\n\" clip-path=\"url(#p0b9ebb167d)\" style=\"fill: #1f77b4\"/>\n   </g>\n   <g id=\"patch_6\">\n    <path d=\"M 110.193547 145.8 \nL 117.4403 145.8 \nL 117.4403 144.579661 \nL 110.193547 144.579661 \nz\n\" clip-path=\"url(#p0b9ebb167d)\" style=\"fill: #1f77b4\"/>\n   </g>\n   <g id=\"patch_7\">\n    <path d=\"M 128.31043 145.8 \nL 135.557183 145.8 \nL 135.557183 145.59661 \nL 128.31043 145.59661 \nz\n\" clip-path=\"url(#p0b9ebb167d)\" style=\"fill: #1f77b4\"/>\n   </g>\n   <g id=\"patch_8\">\n    <path d=\"M 146.427313 145.8 \nL 153.674067 145.8 \nL 153.674067 145.8 \nL 146.427313 145.8 \nz\n\" clip-path=\"url(#p0b9ebb167d)\" style=\"fill: #1f77b4\"/>\n   </g>\n   <g id=\"patch_9\">\n    <path d=\"M 164.544196 145.8 \nL 171.79095 145.8 \nL 171.79095 145.8 \nL 164.544196 145.8 \nz\n\" clip-path=\"url(#p0b9ebb167d)\" style=\"fill: #1f77b4\"/>\n   </g>\n   <g id=\"patch_10\">\n    <path d=\"M 182.66108 145.8 \nL 189.907833 145.8 \nL 189.907833 145.8 \nL 182.66108 145.8 \nz\n\" clip-path=\"url(#p0b9ebb167d)\" style=\"fill: #1f77b4\"/>\n   </g>\n   <g id=\"patch_11\">\n    <path d=\"M 200.777963 145.8 \nL 208.024716 145.8 \nL 208.024716 145.8 \nL 200.777963 145.8 \nz\n\" clip-path=\"url(#p0b9ebb167d)\" style=\"fill: #1f77b4\"/>\n   </g>\n   <g id=\"patch_12\">\n    <path d=\"M 218.894846 145.8 \nL 226.141599 145.8 \nL 226.141599 145.8 \nL 218.894846 145.8 \nz\n\" clip-path=\"url(#p0b9ebb167d)\" style=\"fill: #1f77b4\"/>\n   </g>\n   <g id=\"patch_13\">\n    <path d=\"M 63.089651 145.8 \nL 70.336404 145.8 \nL 70.336404 87.223729 \nL 63.089651 87.223729 \nz\n\" clip-path=\"url(#p0b9ebb167d)\" style=\"fill: url(#hb6a2a9f17c)\"/>\n   </g>\n   <g id=\"patch_14\">\n    <path d=\"M 81.206534 145.8 \nL 88.453287 145.8 \nL 88.453287 64.444068 \nL 81.206534 64.444068 \nz\n\" clip-path=\"url(#p0b9ebb167d)\" style=\"fill: url(#hb6a2a9f17c)\"/>\n   </g>\n   <g id=\"patch_15\">\n    <path d=\"M 99.323417 145.8 \nL 106.57017 145.8 \nL 106.57017 95.155932 \nL 99.323417 95.155932 \nz\n\" clip-path=\"url(#p0b9ebb167d)\" style=\"fill: url(#hb6a2a9f17c)\"/>\n   </g>\n   <g id=\"patch_16\">\n    <path d=\"M 117.4403 145.8 \nL 124.687054 145.8 \nL 124.687054 122.00339 \nL 117.4403 122.00339 \nz\n\" clip-path=\"url(#p0b9ebb167d)\" style=\"fill: url(#hb6a2a9f17c)\"/>\n   </g>\n   <g id=\"patch_17\">\n    <path d=\"M 135.557183 145.8 \nL 142.803937 145.8 \nL 142.803937 136.647458 \nL 135.557183 136.647458 \nz\n\" clip-path=\"url(#p0b9ebb167d)\" style=\"fill: url(#hb6a2a9f17c)\"/>\n   </g>\n   <g id=\"patch_18\">\n    <path d=\"M 153.674067 145.8 \nL 160.92082 145.8 \nL 160.92082 136.037288 \nL 153.674067 136.037288 \nz\n\" clip-path=\"url(#p0b9ebb167d)\" style=\"fill: url(#hb6a2a9f17c)\"/>\n   </g>\n   <g id=\"patch_19\">\n    <path d=\"M 171.79095 145.8 \nL 179.037703 145.8 \nL 179.037703 145.39322 \nL 171.79095 145.39322 \nz\n\" clip-path=\"url(#p0b9ebb167d)\" style=\"fill: url(#hb6a2a9f17c)\"/>\n   </g>\n   <g id=\"patch_20\">\n    <path d=\"M 189.907833 145.8 \nL 197.154586 145.8 \nL 197.154586 145.59661 \nL 189.907833 145.59661 \nz\n\" clip-path=\"url(#p0b9ebb167d)\" style=\"fill: url(#hb6a2a9f17c)\"/>\n   </g>\n   <g id=\"patch_21\">\n    <path d=\"M 208.024716 145.8 \nL 215.271469 145.8 \nL 215.271469 145.8 \nL 208.024716 145.8 \nz\n\" clip-path=\"url(#p0b9ebb167d)\" style=\"fill: url(#hb6a2a9f17c)\"/>\n   </g>\n   <g id=\"patch_22\">\n    <path d=\"M 226.141599 145.8 \nL 233.388352 145.8 \nL 233.388352 145.39322 \nL 226.141599 145.39322 \nz\n\" clip-path=\"url(#p0b9ebb167d)\" style=\"fill: url(#hb6a2a9f17c)\"/>\n   </g>\n   <g id=\"matplotlib.axis_1\">\n    <g id=\"xtick_1\">\n     <g id=\"line2d_1\">\n      <defs>\n       <path id=\"mdfa2151060\" d=\"M 0 0 \nL 0 3.5 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#mdfa2151060\" x=\"61.579911\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_1\">\n      <!-- 2.5 -->\n      <g transform=\"translate(53.628348 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        <path id=\"DejaVuSans-2e\" d=\"M 684 794 \nL 1344 794 \nL 1344 0 \nL 684 0 \nL 684 794 \nz\n\" transform=\"scale(0.015625)\"/>\n        <path id=\"DejaVuSans-35\" d=\"M 691 4666 \nL 3169 4666 \nL 3169 4134 \nL 1269 4134 \nL 1269 2991 \nQ 1406 3038 1543 3061 \nQ 1681 3084 1819 3084 \nQ 2600 3084 3056 2656 \nQ 3513 2228 3513 1497 \nQ 3513 744 3044 326 \nQ 2575 -91 1722 -91 \nQ 1428 -91 1123 -41 \nQ 819 9 494 109 \nL 494 744 \nQ 775 591 1075 516 \nQ 1375 441 1709 441 \nQ 2250 441 2565 725 \nQ 2881 1009 2881 1497 \nQ 2881 1984 2565 2268 \nQ 2250 2553 1709 2553 \nQ 1456 2553 1204 2497 \nQ 953 2441 691 2322 \nL 691 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-32\"/>\n       <use xlink:href=\"#DejaVuSans-2e\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-35\" transform=\"translate(95.410156 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_2\">\n     <g id=\"line2d_2\">\n      <g>\n       <use xlink:href=\"#mdfa2151060\" x=\"99.323417\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_2\">\n      <!-- 5.0 -->\n      <g transform=\"translate(91.371855 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-35\"/>\n       <use xlink:href=\"#DejaVuSans-2e\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(95.410156 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_3\">\n     <g id=\"line2d_3\">\n      <g>\n       <use xlink:href=\"#mdfa2151060\" x=\"137.066924\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_3\">\n      <!-- 7.5 -->\n      <g transform=\"translate(129.115361 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-37\" d=\"M 525 4666 \nL 3525 4666 \nL 3525 4397 \nL 1831 0 \nL 1172 0 \nL 2766 4134 \nL 525 4134 \nL 525 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-37\"/>\n       <use xlink:href=\"#DejaVuSans-2e\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-35\" transform=\"translate(95.410156 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_4\">\n     <g id=\"line2d_4\">\n      <g>\n       <use xlink:href=\"#mdfa2151060\" x=\"174.81043\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_4\">\n      <!-- 10.0 -->\n      <g transform=\"translate(163.677618 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       <use xlink:href=\"#DejaVuSans-2e\" transform=\"translate(127.246094 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(159.033203 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_5\">\n     <g id=\"line2d_5\">\n      <g>\n       <use xlink:href=\"#mdfa2151060\" x=\"212.553937\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_5\">\n      <!-- 12.5 -->\n      <g transform=\"translate(201.421124 160.398438) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-31\"/>\n       <use xlink:href=\"#DejaVuSans-32\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-2e\" transform=\"translate(127.246094 0)\"/>\n       <use xlink:href=\"#DejaVuSans-35\" transform=\"translate(159.033203 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"text_6\">\n     <!-- # tokens per sentence -->\n     <g transform=\"translate(87.8875 174.076563) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-23\" d=\"M 3272 2816 \nL 2363 2816 \nL 2100 1772 \nL 3016 1772 \nL 3272 2816 \nz\nM 2803 4594 \nL 2478 3297 \nL 3391 3297 \nL 3719 4594 \nL 4219 4594 \nL 3897 3297 \nL 4872 3297 \nL 4872 2816 \nL 3775 2816 \nL 3519 1772 \nL 4513 1772 \nL 4513 1294 \nL 3397 1294 \nL 3072 0 \nL 2572 0 \nL 2894 1294 \nL 1978 1294 \nL 1656 0 \nL 1153 0 \nL 1478 1294 \nL 494 1294 \nL 494 1772 \nL 1594 1772 \nL 1856 2816 \nL 850 2816 \nL 850 3297 \nL 1978 3297 \nL 2297 4594 \nL 2803 4594 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-20\" transform=\"scale(0.015625)\"/>\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-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-6b\" d=\"M 581 4863 \nL 1159 4863 \nL 1159 1991 \nL 2875 3500 \nL 3609 3500 \nL 1753 1863 \nL 3688 0 \nL 2938 0 \nL 1159 1709 \nL 1159 0 \nL 581 0 \nL 581 4863 \nz\n\" transform=\"scale(0.015625)\"/>\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-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-73\" d=\"M 2834 3397 \nL 2834 2853 \nQ 2591 2978 2328 3040 \nQ 2066 3103 1784 3103 \nQ 1356 3103 1142 2972 \nQ 928 2841 928 2578 \nQ 928 2378 1081 2264 \nQ 1234 2150 1697 2047 \nL 1894 2003 \nQ 2506 1872 2764 1633 \nQ 3022 1394 3022 966 \nQ 3022 478 2636 193 \nQ 2250 -91 1575 -91 \nQ 1294 -91 989 -36 \nQ 684 19 347 128 \nL 347 722 \nQ 666 556 975 473 \nQ 1284 391 1588 391 \nQ 1994 391 2212 530 \nQ 2431 669 2431 922 \nQ 2431 1156 2273 1281 \nQ 2116 1406 1581 1522 \nL 1381 1569 \nQ 847 1681 609 1914 \nQ 372 2147 372 2553 \nQ 372 3047 722 3315 \nQ 1072 3584 1716 3584 \nQ 2034 3584 2315 3537 \nQ 2597 3491 2834 3397 \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-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-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      </defs>\n      <use xlink:href=\"#DejaVuSans-23\"/>\n      <use xlink:href=\"#DejaVuSans-20\" transform=\"translate(83.789062 0)\"/>\n      <use xlink:href=\"#DejaVuSans-74\" transform=\"translate(115.576172 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6f\" transform=\"translate(154.785156 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6b\" transform=\"translate(215.966797 0)\"/>\n      <use xlink:href=\"#DejaVuSans-65\" transform=\"translate(270.251953 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6e\" transform=\"translate(331.775391 0)\"/>\n      <use xlink:href=\"#DejaVuSans-73\" transform=\"translate(395.154297 0)\"/>\n      <use xlink:href=\"#DejaVuSans-20\" transform=\"translate(447.253906 0)\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(479.041016 0)\"/>\n      <use xlink:href=\"#DejaVuSans-65\" transform=\"translate(542.517578 0)\"/>\n      <use xlink:href=\"#DejaVuSans-72\" transform=\"translate(604.041016 0)\"/>\n      <use xlink:href=\"#DejaVuSans-20\" transform=\"translate(645.154297 0)\"/>\n      <use xlink:href=\"#DejaVuSans-73\" transform=\"translate(676.941406 0)\"/>\n      <use xlink:href=\"#DejaVuSans-65\" transform=\"translate(729.041016 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6e\" transform=\"translate(790.564453 0)\"/>\n      <use xlink:href=\"#DejaVuSans-74\" transform=\"translate(853.943359 0)\"/>\n      <use xlink:href=\"#DejaVuSans-65\" transform=\"translate(893.152344 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6e\" transform=\"translate(954.675781 0)\"/>\n      <use xlink:href=\"#DejaVuSans-63\" transform=\"translate(1018.054688 0)\"/>\n      <use xlink:href=\"#DejaVuSans-65\" transform=\"translate(1073.035156 0)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"matplotlib.axis_2\">\n    <g id=\"ytick_1\">\n     <g id=\"line2d_6\">\n      <defs>\n       <path id=\"m2e65b9f0ef\" d=\"M 0 0 \nL -3.5 0 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#m2e65b9f0ef\" x=\"46.965625\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_7\">\n      <!-- 0 -->\n      <g transform=\"translate(33.603125 149.599219) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-30\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_2\">\n     <g id=\"line2d_7\">\n      <g>\n       <use xlink:href=\"#m2e65b9f0ef\" x=\"46.965625\" y=\"105.122034\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_8\">\n      <!-- 200 -->\n      <g transform=\"translate(20.878125 108.921253) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-32\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(127.246094 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_3\">\n     <g id=\"line2d_8\">\n      <g>\n       <use xlink:href=\"#m2e65b9f0ef\" x=\"46.965625\" y=\"64.444068\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_9\">\n      <!-- 400 -->\n      <g transform=\"translate(20.878125 68.243287) 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       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(127.246094 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_4\">\n     <g id=\"line2d_9\">\n      <g>\n       <use xlink:href=\"#m2e65b9f0ef\" x=\"46.965625\" y=\"23.766102\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_10\">\n      <!-- 600 -->\n      <g transform=\"translate(20.878125 27.56532) 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       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(127.246094 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"text_11\">\n     <!-- count -->\n     <g transform=\"translate(14.798438 90.60625) rotate(-90) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-75\" d=\"M 544 1381 \nL 544 3500 \nL 1119 3500 \nL 1119 1403 \nQ 1119 906 1312 657 \nQ 1506 409 1894 409 \nQ 2359 409 2629 706 \nQ 2900 1003 2900 1516 \nL 2900 3500 \nL 3475 3500 \nL 3475 0 \nL 2900 0 \nL 2900 538 \nQ 2691 219 2414 64 \nQ 2138 -91 1772 -91 \nQ 1169 -91 856 284 \nQ 544 659 544 1381 \nz\nM 1991 3584 \nL 1991 3584 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-63\"/>\n      <use xlink:href=\"#DejaVuSans-6f\" transform=\"translate(54.980469 0)\"/>\n      <use xlink:href=\"#DejaVuSans-75\" transform=\"translate(116.162109 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6e\" transform=\"translate(179.541016 0)\"/>\n      <use xlink:href=\"#DejaVuSans-74\" transform=\"translate(242.919922 0)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"patch_23\">\n    <path d=\"M 46.965625 145.8 \nL 46.965625 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_24\">\n    <path d=\"M 242.265625 145.8 \nL 242.265625 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_25\">\n    <path d=\"M 46.965625 145.8 \nL 242.265625 145.8 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_26\">\n    <path d=\"M 46.965625 7.2 \nL 242.265625 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_27\">\n     <path d=\"M 170.0625 44.55625 \nL 235.265625 44.55625 \nQ 237.265625 44.55625 237.265625 42.55625 \nL 237.265625 14.2 \nQ 237.265625 12.2 235.265625 12.2 \nL 170.0625 12.2 \nQ 168.0625 12.2 168.0625 14.2 \nL 168.0625 42.55625 \nQ 168.0625 44.55625 170.0625 44.55625 \nz\n\" style=\"fill: #ffffff; opacity: 0.8; stroke: #cccccc; stroke-linejoin: miter\"/>\n    </g>\n    <g id=\"patch_28\">\n     <path d=\"M 172.0625 23.798438 \nL 192.0625 23.798438 \nL 192.0625 16.798438 \nL 172.0625 16.798438 \nz\n\" style=\"fill: #1f77b4\"/>\n    </g>\n    <g id=\"text_12\">\n     <!-- source -->\n     <g transform=\"translate(200.0625 23.798438) scale(0.1 -0.1)\">\n      <use xlink:href=\"#DejaVuSans-73\"/>\n      <use xlink:href=\"#DejaVuSans-6f\" transform=\"translate(52.099609 0)\"/>\n      <use xlink:href=\"#DejaVuSans-75\" transform=\"translate(113.28125 0)\"/>\n      <use xlink:href=\"#DejaVuSans-72\" transform=\"translate(176.660156 0)\"/>\n      <use xlink:href=\"#DejaVuSans-63\" transform=\"translate(215.523438 0)\"/>\n      <use xlink:href=\"#DejaVuSans-65\" transform=\"translate(270.503906 0)\"/>\n     </g>\n    </g>\n    <g id=\"patch_29\">\n     <path d=\"M 172.0625 38.476562 \nL 192.0625 38.476562 \nL 192.0625 31.476562 \nL 172.0625 31.476562 \nz\n\" style=\"fill: url(#hb6a2a9f17c)\"/>\n    </g>\n    <g id=\"text_13\">\n     <!-- target -->\n     <g transform=\"translate(200.0625 38.476562) scale(0.1 -0.1)\">\n      <defs>\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-67\" d=\"M 2906 1791 \nQ 2906 2416 2648 2759 \nQ 2391 3103 1925 3103 \nQ 1463 3103 1205 2759 \nQ 947 2416 947 1791 \nQ 947 1169 1205 825 \nQ 1463 481 1925 481 \nQ 2391 481 2648 825 \nQ 2906 1169 2906 1791 \nz\nM 3481 434 \nQ 3481 -459 3084 -895 \nQ 2688 -1331 1869 -1331 \nQ 1566 -1331 1297 -1286 \nQ 1028 -1241 775 -1147 \nL 775 -588 \nQ 1028 -725 1275 -790 \nQ 1522 -856 1778 -856 \nQ 2344 -856 2625 -561 \nQ 2906 -266 2906 331 \nL 2906 616 \nQ 2728 306 2450 153 \nQ 2172 0 1784 0 \nQ 1141 0 747 490 \nQ 353 981 353 1791 \nQ 353 2603 747 3093 \nQ 1141 3584 1784 3584 \nQ 2172 3584 2450 3431 \nQ 2728 3278 2906 2969 \nL 2906 3500 \nL 3481 3500 \nL 3481 434 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-74\"/>\n      <use xlink:href=\"#DejaVuSans-61\" transform=\"translate(39.208984 0)\"/>\n      <use xlink:href=\"#DejaVuSans-72\" transform=\"translate(100.488281 0)\"/>\n      <use xlink:href=\"#DejaVuSans-67\" transform=\"translate(139.851562 0)\"/>\n      <use xlink:href=\"#DejaVuSans-65\" transform=\"translate(203.328125 0)\"/>\n      <use xlink:href=\"#DejaVuSans-74\" transform=\"translate(264.851562 0)\"/>\n     </g>\n    </g>\n   </g>\n  </g>\n </g>\n <defs>\n  <clipPath id=\"p0b9ebb167d\">\n   <rect x=\"46.965625\" y=\"7.2\" width=\"195.3\" height=\"138.6\"/>\n  </clipPath>\n </defs>\n <defs>\n  <pattern id=\"hb6a2a9f17c\" patternUnits=\"userSpaceOnUse\" x=\"0\" y=\"0\" width=\"72\" height=\"72\">\n   <rect x=\"0\" y=\"0\" width=\"73\" height=\"73\" fill=\"#ff7f0e\"/>\n   <path d=\"M -36 36 \nL 36 -36 \nM -24 48 \nL 48 -24 \nM -12 60 \nL 60 -12 \nM 0 72 \nL 72 0 \nM 12 84 \nL 84 12 \nM 24 96 \nL 96 24 \nM 36 108 \nL 108 36 \n\" style=\"fill: #000000; stroke: #000000; stroke-width: 1.0; stroke-linecap: butt; stroke-linejoin: miter\"/>\n  </pattern>\n </defs>\n</svg>\n"
     },
     "metadata": {}
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=seq2seq-md-10 fw=all -->\n",
    "\n",
    "## The Seq2Seq Model\n",
    "\n",
    "We now fill the two interfaces with GRUs (that section). The GRU is a natural\n",
    "choice: its gated additive state carries information across the sentence without\n",
    "the vanishing gradients that would cripple a vanilla RNN encoder.\n",
    "\n",
    "### The Encoder\n",
    "\n",
    "The encoder embeds each source token and runs a multilayer GRU over the sequence.\n",
    "It returns the per-step hidden states of the top layer together with the final\n",
    "hidden state of every layer; the latter is the context $\\mathbf{c}$ of\n",
    "the figure, a fixed-shape summary of the whole source.\n",
    "\n",
    "One detail of initialization matters enough to name. The seq2seq paper\n",
    "[@Sutskever.Vinyals.Le.2014] initializes weights with Xavier-uniform, which\n",
    "frameworks do not all do by default. In PyTorch we apply a small helper via\n",
    "`module.apply`; MXNet passes `init.Xavier()` directly; TensorFlow's Keras layers\n",
    "already default to Xavier (glorot-uniform); and the Flax NNX layers default to\n",
    "LeCun-normal input weights with orthogonal recurrent weights, a combination\n",
    "already well suited to RNNs, so we leave it in place."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "seq2seq-encoder"
   },
   "source": [
    "class Seq2SeqEncoder(d2l.Encoder):\n",
    "    \"\"\"The RNN encoder for sequence-to-sequence learning.\"\"\"\n",
    "    def __init__(self, vocab_size, embed_size, num_hiddens, num_layers,\n",
    "                 dropout=0, rngs=None):\n",
    "        rngs = nnx.Rngs(params=0, dropout=1, carry=2) if rngs is None else rngs\n",
    "        self.embedding = nnx.Embed(vocab_size, embed_size, rngs=rngs)\n",
    "        self.rnn = d2l.GRU(embed_size, num_hiddens, num_layers, dropout,\n",
    "                           rngs=rngs)\n",
    "\n",
    "    def __call__(self, X, *args):\n",
    "        embs = self.embedding(d2l.astype(d2l.transpose(X), d2l.int64))\n",
    "        outputs, state = self.rnn(embs)\n",
    "        # outputs: (num_steps, batch_size, num_hiddens)\n",
    "        # state: (num_layers, batch_size, num_hiddens)\n",
    "        return outputs, state"
   ],
   "execution_count": 11,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=seq2seq-md-11 fw=all -->\n",
    "\n",
    "A quick shape check confirms the interface. On a batch of 4 sequences of length 9\n",
    "with a two-layer GRU of width 16, the per-step outputs have shape (`num_steps`,\n",
    "`batch_size`, `num_hiddens`) and the final state has one hidden vector per layer."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "seq2seq-encoder-check"
   },
   "source": [
    "vocab_size, embed_size, num_hiddens, num_layers = 10, 8, 16, 2\n",
    "batch_size, num_steps = 4, 9\n",
    "encoder = Seq2SeqEncoder(vocab_size, embed_size, num_hiddens, num_layers)\n",
    "X = d2l.zeros((batch_size, num_steps))\n",
    "enc_outputs, enc_state = encoder(X)\n",
    "d2l.check_shape(enc_outputs, (num_steps, batch_size, num_hiddens))"
   ],
   "execution_count": 12,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stderr",
     "text": [
      "/home/smola/d2l-neu/.venv-jax/lib/python3.12/site-packages/jax/_src/numpy/array_methods.py:125: UserWarning: Explicitly requested dtype int64 requested in astype is not available, and will be truncated to dtype int32. To enable more dtypes, set the jax_enable_x64 configuration option or the JAX_ENABLE_X64 shell environment variable. See https://github.com/jax-ml/jax#current-gotchas for more.\n  return lax_numpy.astype(self, dtype, copy=copy, device=device)\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=seq2seq-md-12 fw=all -->\n",
    "\n",
    "### The Decoder\n",
    "\n",
    "The decoder generates the target autoregressively. At target step $t'$ it takes\n",
    "the previous target token $y_{t'-1}$, its own previous hidden state\n",
    "$\\mathbf{s}_{t'-1}$, and the context $\\mathbf{c}$, and produces a distribution\n",
    "over the next token:\n",
    "\n",
    "$$\\mathbf{s}_{t'} = g(y_{t'-1}, \\mathbf{c}, \\mathbf{s}_{t'-1}).$$\n",
    "\n",
    "We realize $g$ with two design choices. First, the decoder's initial hidden state\n",
    "is the encoder's final hidden state, which requires the two GRUs to share width\n",
    "and depth. Second, to keep the source in view at every step and not only the\n",
    "first, we concatenate the context $\\mathbf{c}$ (the encoder's top-layer final\n",
    "state, broadcast across time) onto the decoder's input embedding at *every* step.\n",
    "A dense layer over the top-layer hidden states produces the vocabulary logits."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "seq2seq-decoder"
   },
   "source": [
    "class Seq2SeqDecoder(d2l.Decoder):\n",
    "    \"\"\"The RNN decoder for sequence-to-sequence learning.\"\"\"\n",
    "    def __init__(self, vocab_size, embed_size, num_hiddens, num_layers,\n",
    "                 dropout=0, rngs=None):\n",
    "        rngs = nnx.Rngs(params=2, dropout=3, carry=4) if rngs is None else rngs\n",
    "        self.embedding = nnx.Embed(vocab_size, embed_size, rngs=rngs)\n",
    "        self.rnn = d2l.GRU(embed_size + num_hiddens, num_hiddens,\n",
    "                           num_layers, dropout, rngs=rngs)\n",
    "        self.dense = nnx.Linear(num_hiddens, vocab_size, rngs=rngs)\n",
    "\n",
    "    def init_state(self, enc_all_outputs, *args):\n",
    "        return enc_all_outputs\n",
    "\n",
    "    def __call__(self, X, state):\n",
    "        embs = self.embedding(d2l.astype(d2l.transpose(X), d2l.int64))\n",
    "        enc_output, hidden_state = state\n",
    "        context = enc_output[-1]\n",
    "        context = jnp.tile(context, (embs.shape[0], 1, 1))\n",
    "        embs_and_context = d2l.concat((embs, context), -1)\n",
    "        outputs, hidden_state = self.rnn(embs_and_context, hidden_state)\n",
    "        outputs = d2l.swapaxes(self.dense(outputs), 0, 1)\n",
    "        return outputs, [enc_output, hidden_state]"
   ],
   "execution_count": 13,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=seq2seq-md-13 fw=all -->\n",
    "\n",
    "Running the decoder on the same toy batch, the output has shape (`batch_size`,\n",
    "`num_steps`, `vocab_size`): a distribution over the vocabulary at every step."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "seq2seq-decoder-check"
   },
   "source": [
    "decoder = Seq2SeqDecoder(vocab_size, embed_size, num_hiddens, num_layers)\n",
    "state = decoder.init_state(encoder(X))\n",
    "dec_outputs, state = decoder(X, state)\n",
    "d2l.check_shape(dec_outputs, (batch_size, num_steps, vocab_size))"
   ],
   "execution_count": 14,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=seq2seq-md-14 fw=all -->\n",
    "\n",
    "the figure unrolls the assembled model. The encoder consumes\n",
    "the source tokens; its final state becomes the context $\\mathbf{c}$ that seeds and\n",
    "feeds the decoder; the decoder is driven by the teacher-forced target and predicts\n",
    "the target shifted by one, terminating at `<eos>`.\n",
    "\n",
    "![The unrolled RNN encoder-decoder with teacher forcing. The encoder's final state is the context, fed to the decoder at every step; the decoder input is the target prefixed by `<bos>` and its label is the target ending in `<eos>`.](../img/mdl-modernrnn-seq2seq.svg)\n",
    "\n",
    "### The Loss with Masking\n",
    "\n",
    "Every batch is padded, but the padding is not real data, and letting the model be\n",
    "graded on predicting `<pad>` tokens would reward it for the wrong thing. We\n",
    "therefore compute the usual softmax cross-entropy at every position, then multiply\n",
    "by a mask that is zero at padded target positions and average over the real tokens\n",
    "only. This is worth doing carefully because it appears nowhere else in the book,\n",
    "and, as the exercises show, dropping it visibly degrades translation.\n",
    "\n",
    "The `Seq2Seq` class ties everything together and swaps SGD for Adam, which\n",
    "converges faster on this task."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "seq2seq-model"
   },
   "source": [
    "class Seq2Seq(d2l.EncoderDecoder):\n",
    "    \"\"\"The RNN encoder-decoder for sequence-to-sequence learning.\"\"\"\n",
    "    def __init__(self, encoder, decoder, tgt_pad, lr):\n",
    "        super().__init__(encoder, decoder)\n",
    "        self.tgt_pad, self.lr = tgt_pad, lr\n",
    "\n",
    "    def validation_step(self, batch):\n",
    "        return self.loss(self(*batch[:-1]), batch[-1])\n",
    "\n",
    "    def configure_optimizers(self):\n",
    "        return optax.adam(learning_rate=self.lr)"
   ],
   "execution_count": 15,
   "outputs": []
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "seq2seq-loss"
   },
   "source": [
    "@d2l.add_to_class(Seq2Seq)\n",
    "def loss(self, Y_hat, Y, averaged=False):\n",
    "    Y_hat = d2l.reshape(Y_hat, (-1, Y_hat.shape[-1]))\n",
    "    Y = d2l.reshape(Y, (-1,))\n",
    "    fn = optax.softmax_cross_entropy_with_integer_labels\n",
    "    l = fn(Y_hat, Y)\n",
    "    mask = d2l.astype(Y != self.tgt_pad, d2l.float32)\n",
    "    return d2l.reduce_sum(l * mask) / d2l.reduce_sum(mask)"
   ],
   "execution_count": 16,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=seq2seq-md-15 fw=all -->\n",
    "\n",
    "## Training, Decoding, and Evaluation\n",
    "\n",
    "We now train the translator on our 1,024-pair training split. Two layers, width 256, dropout\n",
    "0.2, Adam at learning rate 0.005, gradients clipped to norm 1, for 30 epochs."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "seq2seq-training"
   },
   "source": [
    "data = d2l.MTFraEng(batch_size=128)\n",
    "embed_size, num_hiddens, num_layers, dropout = 256, 256, 2, 0.2\n",
    "encoder = Seq2SeqEncoder(\n",
    "    len(data.tokenizer), embed_size, num_hiddens, num_layers, dropout)\n",
    "decoder = Seq2SeqDecoder(\n",
    "    len(data.tokenizer), embed_size, num_hiddens, num_layers, dropout)\n",
    "model = Seq2Seq(encoder, decoder, tgt_pad=data.tokenizer.pad, lr=0.005)\n",
    "trainer = d2l.Trainer(max_epochs=30, gradient_clip_val=1, num_gpus=1)\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=\"229.425pt\" height=\"183.35625pt\" viewBox=\"0 0 229.425 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-19T21:00:01.530807</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 229.425 183.35625 \nL 229.425 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 20.5625 145.8 \nL 215.8625 145.8 \nL 215.8625 7.2 \nL 20.5625 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=\"m09a4945cd3\" d=\"M 0 0 \nL 0 3.5 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#m09a4945cd3\" x=\"20.5625\" 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(17.38125 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=\"#m09a4945cd3\" x=\"53.1125\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_2\">\n      <!-- 5 -->\n      <g transform=\"translate(49.93125 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-35\" d=\"M 691 4666 \nL 3169 4666 \nL 3169 4134 \nL 1269 4134 \nL 1269 2991 \nQ 1406 3038 1543 3061 \nQ 1681 3084 1819 3084 \nQ 2600 3084 3056 2656 \nQ 3513 2228 3513 1497 \nQ 3513 744 3044 326 \nQ 2575 -91 1722 -91 \nQ 1428 -91 1123 -41 \nQ 819 9 494 109 \nL 494 744 \nQ 775 591 1075 516 \nQ 1375 441 1709 441 \nQ 2250 441 2565 725 \nQ 2881 1009 2881 1497 \nQ 2881 1984 2565 2268 \nQ 2250 2553 1709 2553 \nQ 1456 2553 1204 2497 \nQ 953 2441 691 2322 \nL 691 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-35\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_3\">\n     <g id=\"line2d_3\">\n      <g>\n       <use xlink:href=\"#m09a4945cd3\" x=\"85.6625\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_3\">\n      <!-- 10 -->\n      <g transform=\"translate(79.3 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=\"xtick_4\">\n     <g id=\"line2d_4\">\n      <g>\n       <use xlink:href=\"#m09a4945cd3\" x=\"118.2125\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_4\">\n      <!-- 15 -->\n      <g transform=\"translate(111.85 160.398438) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-31\"/>\n       <use xlink:href=\"#DejaVuSans-35\" transform=\"translate(63.623047 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_5\">\n     <g id=\"line2d_5\">\n      <g>\n       <use xlink:href=\"#m09a4945cd3\" x=\"150.7625\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_5\">\n      <!-- 20 -->\n      <g transform=\"translate(144.4 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       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_6\">\n     <g id=\"line2d_6\">\n      <g>\n       <use xlink:href=\"#m09a4945cd3\" x=\"183.3125\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_6\">\n      <!-- 25 -->\n      <g transform=\"translate(176.95 160.398438) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-32\"/>\n       <use xlink:href=\"#DejaVuSans-35\" transform=\"translate(63.623047 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_7\">\n     <g id=\"line2d_7\">\n      <g>\n       <use xlink:href=\"#m09a4945cd3\" x=\"215.8625\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_7\">\n      <!-- 30 -->\n      <g transform=\"translate(209.5 160.398438) 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\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"text_8\">\n     <!-- epoch -->\n     <g transform=\"translate(102.984375 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_8\">\n      <defs>\n       <path id=\"m729f5eddbc\" d=\"M 0 0 \nL -3.5 0 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#m729f5eddbc\" x=\"20.5625\" y=\"109.321405\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_9\">\n      <!-- 2 -->\n      <g transform=\"translate(7.2 113.120624) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-32\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_2\">\n     <g id=\"line2d_9\">\n      <g>\n       <use xlink:href=\"#m729f5eddbc\" x=\"20.5625\" y=\"69.309335\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_10\">\n      <!-- 4 -->\n      <g transform=\"translate(7.2 73.108553) 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=\"ytick_3\">\n     <g id=\"line2d_10\">\n      <g>\n       <use xlink:href=\"#m729f5eddbc\" x=\"20.5625\" y=\"29.297264\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_11\">\n      <!-- 6 -->\n      <g transform=\"translate(7.2 33.096483) 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>\n   <g id=\"line2d_11\">\n    <path d=\"M 21.783125 13.5 \nL 25.038125 51.687861 \nL 28.293125 62.672387 \nL 31.548125 63.214514 \nL 34.803125 69.704948 \nL 38.058125 70.645881 \nL 41.313125 76.728154 \nL 44.568125 75.783592 \nL 47.823125 82.805105 \nL 51.078125 81.528743 \nL 54.333125 87.128556 \nL 57.588125 85.791306 \nL 60.843125 91.673368 \nL 64.098125 91.478941 \nL 67.353125 96.093332 \nL 70.608125 95.924084 \nL 73.863125 99.107371 \nL 77.118125 99.832465 \nL 80.373125 104.585861 \nL 83.628125 102.452989 \nL 86.883125 106.60504 \nL 90.138125 105.626043 \nL 93.393125 110.537179 \nL 96.648125 109.547717 \nL 99.903125 113.610086 \nL 103.158125 112.428856 \nL 106.413125 116.6667 \nL 109.668125 114.778661 \nL 112.923125 119.812581 \nL 116.178125 116.440431 \nL 119.433125 121.52839 \nL 122.688125 119.207749 \nL 125.943125 123.29404 \nL 129.198125 121.554336 \nL 132.453125 124.693339 \nL 135.708125 123.814728 \nL 138.963125 127.096868 \nL 142.218125 125.37436 \nL 145.473125 128.757672 \nL 148.728125 127.139941 \nL 151.983125 129.710338 \nL 155.238125 129.282605 \nL 158.493125 131.531072 \nL 161.748125 130.132107 \nL 165.003125 133.239033 \nL 168.258125 131.337802 \nL 171.513125 134.338462 \nL 174.768125 132.83477 \nL 178.023125 135.442425 \nL 181.278125 133.922519 \nL 184.533125 136.327958 \nL 187.788125 134.964039 \nL 191.043125 137.749656 \nL 194.298125 135.802403 \nL 197.553125 137.939536 \nL 200.808125 136.965081 \nL 204.063125 138.943517 \nL 207.318125 137.470677 \nL 210.573125 139.5 \nL 213.828125 138.236851 \n\" clip-path=\"url(#pfeaf779140)\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_12\">\n    <path d=\"M 27.0725 39.097851 \nL 33.5825 40.879423 \nL 40.0925 35.649955 \nL 46.6025 38.54959 \nL 53.1125 40.617475 \nL 59.6225 43.419366 \nL 66.1325 45.490723 \nL 72.6425 48.112182 \nL 79.1525 50.009422 \nL 85.6625 51.233356 \nL 92.1725 53.228186 \nL 98.6825 53.035581 \nL 105.1925 52.355635 \nL 111.7025 53.046046 \nL 118.2125 52.328943 \nL 124.7225 52.369801 \nL 131.2325 51.227908 \nL 137.7425 49.479172 \nL 144.2525 48.531878 \nL 150.7625 49.023092 \nL 157.2725 48.235072 \nL 163.7825 48.480278 \nL 170.2925 46.92718 \nL 176.8025 46.861404 \nL 183.3125 45.242025 \nL 189.8225 44.744553 \nL 196.3325 44.584421 \nL 202.8425 43.181496 \nL 209.3525 41.19777 \nL 215.8625 41.930155 \n\" clip-path=\"url(#pfeaf779140)\" 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 20.5625 145.8 \nL 20.5625 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 215.8625 145.8 \nL 215.8625 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 20.5625 145.8 \nL 215.8625 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 20.5625 7.2 \nL 215.8625 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 129.271875 92.95625 \nL 208.8625 92.95625 \nQ 210.8625 92.95625 210.8625 90.95625 \nL 210.8625 62.04375 \nQ 210.8625 60.04375 208.8625 60.04375 \nL 129.271875 60.04375 \nQ 127.271875 60.04375 127.271875 62.04375 \nL 127.271875 90.95625 \nQ 127.271875 92.95625 129.271875 92.95625 \nz\n\" style=\"fill: #ffffff; opacity: 0.8; stroke: #cccccc; stroke-linejoin: miter\"/>\n    </g>\n    <g id=\"line2d_13\">\n     <path d=\"M 131.271875 68.142188 \nL 141.271875 68.142188 \nL 151.271875 68.142188 \n\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n    </g>\n    <g id=\"text_12\">\n     <!-- train_loss -->\n     <g transform=\"translate(159.271875 71.642188) 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       <path id=\"DejaVuSans-73\" d=\"M 2834 3397 \nL 2834 2853 \nQ 2591 2978 2328 3040 \nQ 2066 3103 1784 3103 \nQ 1356 3103 1142 2972 \nQ 928 2841 928 2578 \nQ 928 2378 1081 2264 \nQ 1234 2150 1697 2047 \nL 1894 2003 \nQ 2506 1872 2764 1633 \nQ 3022 1394 3022 966 \nQ 3022 478 2636 193 \nQ 2250 -91 1575 -91 \nQ 1294 -91 989 -36 \nQ 684 19 347 128 \nL 347 722 \nQ 666 556 975 473 \nQ 1284 391 1588 391 \nQ 1994 391 2212 530 \nQ 2431 669 2431 922 \nQ 2431 1156 2273 1281 \nQ 2116 1406 1581 1522 \nL 1381 1569 \nQ 847 1681 609 1914 \nQ 372 2147 372 2553 \nQ 372 3047 722 3315 \nQ 1072 3584 1716 3584 \nQ 2034 3584 2315 3537 \nQ 2597 3491 2834 3397 \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-6c\" transform=\"translate(282.763672 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6f\" transform=\"translate(310.546875 0)\"/>\n      <use xlink:href=\"#DejaVuSans-73\" transform=\"translate(371.728516 0)\"/>\n      <use xlink:href=\"#DejaVuSans-73\" transform=\"translate(423.828125 0)\"/>\n     </g>\n    </g>\n    <g id=\"line2d_14\">\n     <path d=\"M 131.271875 83.098438 \nL 141.271875 83.098438 \nL 151.271875 83.098438 \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_loss -->\n     <g transform=\"translate(159.271875 86.598438) 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-6c\" transform=\"translate(198.242188 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6f\" transform=\"translate(226.025391 0)\"/>\n      <use xlink:href=\"#DejaVuSans-73\" transform=\"translate(287.207031 0)\"/>\n      <use xlink:href=\"#DejaVuSans-73\" transform=\"translate(339.306641 0)\"/>\n     </g>\n    </g>\n   </g>\n  </g>\n </g>\n <defs>\n  <clipPath id=\"pfeaf779140\">\n   <rect x=\"20.5625\" 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=seq2seq-md-16 fw=all -->\n",
    "\n",
    "### Greedy Translation\n",
    "\n",
    "To translate we run the decoder autoregressively: feed `<bos>`, take an output\n",
    "token, feed it back, and stop at `<eos>`. Two forms are useful. The batched\n",
    "`predict_step`, attached to `EncoderDecoder`, decodes a whole minibatch in one\n",
    "call, and we use it for the length sweep below."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "seq2seq-predict-step"
   },
   "source": [
    "@d2l.add_to_class(d2l.EncoderDecoder)\n",
    "def predict_step(self, batch, num_steps, save_attention_weights=False):\n",
    "    model = nnx.view(self, deterministic=True, use_running_average=True,\n",
    "                     raise_if_not_found=False)\n",
    "    src, tgt, src_valid_len, _ = batch\n",
    "    enc_all_outputs = model.encoder(src, src_valid_len)\n",
    "    enc_attention_weights = (getattr(model.encoder, 'attention_weights', [])\n",
    "                             if save_attention_weights else [])\n",
    "    dec_state = model.decoder.init_state(enc_all_outputs, src_valid_len)\n",
    "    outputs, attention_weights = [d2l.expand_dims(tgt[:, 0], 1), ], []\n",
    "    for _ in range(num_steps):\n",
    "        Y, dec_state = model.decoder(outputs[-1], dec_state)\n",
    "        outputs.append(d2l.argmax(Y, 2))\n",
    "        if save_attention_weights:\n",
    "            attention_weights.append(model.decoder.attention_weights)\n",
    "    return d2l.concat(outputs[1:], 1), (attention_weights,\n",
    "                                        enc_attention_weights)"
   ],
   "execution_count": 18,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=seq2seq-md-17 fw=all -->\n",
    "\n",
    "A small helper turns one row of predicted ids into a string, cutting it at the\n",
    "first `<eos>`. We translate a few short English sentences whose French we know,\n",
    "decoding the whole batch at once with `predict_step`."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "seq2seq-detok"
   },
   "source": [
    "def to_text(pred, tokenizer):  # Decode one row of ids, stop at <eos>\n",
    "    ids = [int(i) for i in d2l.numpy(pred)]\n",
    "    if tokenizer.eos in ids:\n",
    "        ids = ids[:ids.index(tokenizer.eos)]\n",
    "    return tokenizer.decode(ids)"
   ],
   "execution_count": 19,
   "outputs": []
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "seq2seq-greedy"
   },
   "source": [
    "engs = ['i lost .', \"i'm calm .\", \"i'm home .\"]\n",
    "fras = [\"j'ai perdu .\", 'je suis calme .', 'je suis chez moi .']\n",
    "preds, _ = model.predict_step(data.build(engs, fras), data.num_steps)\n",
    "translations = [to_text(p, data.tokenizer) for p in preds]\n",
    "for eng, out in zip(engs, translations):\n",
    "    print(f'{eng} => {out!r}')"
   ],
   "execution_count": 20,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "i lost . => \"j'ai perdu .\"\ni'm calm . => 'je suis calme .'\ni'm home . => 'je suis timide .'\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=seq2seq-md-18 fw=all -->\n",
    "\n",
    "### Evaluation with chrF\n",
    "\n",
    "By what number do we call one translation better than another? For decades the\n",
    "default answer was BLEU (below), which counts matching word $n$-grams. The modern\n",
    "default for lexical scoring is chrF [@Popovic.2015], the metric the WMT\n",
    "evaluation campaigns recommend. chrF compares *character* $n$-grams rather than\n",
    "word $n$-grams, which sidesteps tokenization entirely and rewards getting a word\n",
    "*almost* right (a shared stem, a near-miss inflection) instead of scoring it a\n",
    "flat zero. For orders $n = 1, \\ldots, N$ it computes the precision and recall of\n",
    "character $n$-grams, and combines them with an F-score weighted toward recall\n",
    "($\\beta = 2$). It is about ten lines."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "seq2seq-chrf"
   },
   "source": [
    "def chrf(pred, label, n=6, beta=2):\n",
    "    \"\"\"chrF (Popovic, 2015): character n-gram F-score.\"\"\"\n",
    "    def ngrams(s, k):\n",
    "        s = s.replace(' ', '')\n",
    "        return collections.Counter(s[i:i + k] for i in range(len(s) - k + 1))\n",
    "    prec = rec = 0.0\n",
    "    for k in range(1, n + 1):\n",
    "        p, r = ngrams(pred, k), ngrams(label, k)\n",
    "        overlap = sum((p & r).values())\n",
    "        if sum(p.values()) and sum(r.values()):\n",
    "            prec += overlap / sum(p.values())\n",
    "            rec += overlap / sum(r.values())\n",
    "    prec, rec = prec / n, rec / n\n",
    "    if prec + rec == 0:\n",
    "        return 0.0\n",
    "    return (1 + beta**2) * prec * rec / (beta**2 * prec + rec)"
   ],
   "execution_count": 21,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=seq2seq-md-19 fw=all -->\n",
    "\n",
    "Scoring our greedy translations, a correct translation earns a chrF near 1 while a\n",
    "wrong word costs partial credit rather than everything. At this toy scale\n",
    "translation quality varies from framework to framework: on some tabs the model\n",
    "returns a fluent but wrong French sentence, and chrF's partial credit, not an\n",
    "all-or-nothing zero, is exactly what you then see in its score."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "seq2seq-chrf-eval"
   },
   "source": [
    "for eng, fra, out in zip(engs, fras, translations):\n",
    "    print(f'{eng} => {out!r}, chrF {chrf(out, data._preprocess(fra)):.3f}')"
   ],
   "execution_count": 22,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "i lost . => \"j'ai perdu .\", chrF 1.000\ni'm calm . => 'je suis calme .', chrF 1.000\ni'm home . => 'je suis timide .', chrF 0.342\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=seq2seq-md-20 fw=all -->\n",
    "\n",
    "**A note on BLEU.** You will still meet BLEU [@Papineni.Roukos.Ward.ea.2002]\n",
    "constantly, so it is worth naming. It is a geometric mean of word $n$-gram\n",
    "precisions $p_n$, damped by a *brevity penalty* that stops a model from gaming\n",
    "precision by emitting only a couple of sure words:\n",
    "\n",
    "$$\\text{BLEU} = \\exp\\!\\left(\\min\\!\\left(0, 1 - \\frac{\\text{len}_{\\text{label}}}{\\text{len}_{\\text{pred}}}\\right)\\right) \\prod_{n=1}^{k} p_n^{1/2^n}.$$\n",
    "\n",
    "We keep its implementation, `d2l.bleu`, in the library for the exercises, but\n",
    "chrF is the metric we teach."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "seq2seq-bleu"
   },
   "source": [
    "def bleu(pred_seq, label_seq, k):\n",
    "    \"\"\"Compute the BLEU.\"\"\"\n",
    "    pred_tokens, label_tokens = pred_seq.split(' '), label_seq.split(' ')\n",
    "    len_pred, len_label = len(pred_tokens), len(label_tokens)\n",
    "    score = math.exp(min(0, 1 - len_label / len_pred))\n",
    "    for n in range(1, min(k, len_pred) + 1):\n",
    "        num_matches, label_subs = 0, collections.defaultdict(int)\n",
    "        for i in range(len_label - n + 1):\n",
    "            label_subs[' '.join(label_tokens[i: i + n])] += 1\n",
    "        for i in range(len_pred - n + 1):\n",
    "            if label_subs[' '.join(pred_tokens[i: i + n])] > 0:\n",
    "                num_matches += 1\n",
    "                label_subs[' '.join(pred_tokens[i: i + n])] -= 1\n",
    "        score *= math.pow(num_matches / (len_pred - n + 1), math.pow(0.5, n))\n",
    "    return score"
   ],
   "execution_count": 23,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=seq2seq-md-21 fw=all -->\n",
    "\n",
    "Both chrF and BLEU are surface-overlap metrics; neither knows whether a\n",
    "translation *means* the same thing. That gap is filled by learned neural metrics\n",
    "such as COMET and, increasingly, by prompting a large language model to judge —\n",
    "a model of exactly the kind that section builds.\n",
    "\n",
    "### Beam Search\n",
    "\n",
    "Greedy decoding commits to the single best token at each step, which need not\n",
    "produce the best sequence overall. Beam search keeps the $k$ best prefixes alive.\n",
    "The decoding toolkit of that section plugs straight in: everything\n",
    "downstream of the model speaks one narrow interface, a `step_fn` mapping the token\n",
    "ids so far to NumPy logits for the next token. Here the source is fixed and the\n",
    "\"ids so far\" are the target prefix, so we encode the source (padded exactly as in\n",
    "training) and wrap the decoder. We show this on the PyTorch and JAX tabs."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "seq2seq-step-fn"
   },
   "source": [
    "def make_step_fn(model, src):  # src: padded source token ids\n",
    "    m = nnx.view(model, deterministic=True, use_running_average=True,\n",
    "                 raise_if_not_found=False)\n",
    "    enc = m.encoder(d2l.tensor([src]))\n",
    "    def step_fn(tgt_ids):\n",
    "        state = m.decoder.init_state(enc)\n",
    "        Y, _ = m.decoder(d2l.tensor([tgt_ids]), state)\n",
    "        return d2l.numpy(Y)[0, -1]\n",
    "    return step_fn"
   ],
   "execution_count": 24,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=seq2seq-md-22 fw=all -->\n",
    "\n",
    "Greedy is the special case $k = 1$; we score it against beams of size 2 and 5\n",
    "with chrF, reusing the padded encoding of the training batch."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "seq2seq-beam"
   },
   "source": [
    "tok = data.tokenizer\n",
    "for eng, fra in zip(engs, fras):\n",
    "    src = [int(i) for i in d2l.numpy(data.build([eng], [eng])[0][0])]\n",
    "    step_fn = make_step_fn(model, src)\n",
    "    row = []\n",
    "    for k in (1, 2, 5):\n",
    "        ids = d2l.beam_search(step_fn, [tok.bos], data.num_steps,\n",
    "                              beam_size=k, eos_id=tok.eos)[0][1]\n",
    "        out = tok.decode([i for i in ids[1:] if i != tok.eos])\n",
    "        row.append(f'k={k}: {chrf(out, data._preprocess(fra)):.2f}')\n",
    "    print(f'{eng:<12} ' + '  '.join(row))"
   ],
   "execution_count": 25,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "i lost .     k=1: 1.00  k=2: 1.00  k=5: 1.00\n"
     ]
    },
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "i'm calm .   k=1: 1.00  k=2: 1.00  k=5: 1.00\n"
     ]
    },
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "i'm home .   k=1: 0.34  k=2: 0.34  k=5: 0.34\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=seq2seq-md-23 fw=all -->\n",
    "\n",
    "On sentences this short the beams mostly agree with greedy, as expected: beam\n",
    "search buys the most where a locally suboptimal first token unlocks a better\n",
    "whole, which is rare here and common on the long sentences we turn to next.\n",
    "\n",
    "## The Fixed-Vector Bottleneck\n",
    "\n",
    "Everything the decoder knows about the source arrives through a single fixed-size\n",
    "vector $\\mathbf{c}$. For \"i'm home .\" that is plenty. For a long, clause-laden\n",
    "sentence it is a tight squeeze: the same 256 numbers must encode a three-word\n",
    "sentence and a thirty-word one, and the longer the source, the more must be\n",
    "forgotten to fit. We can watch this happen. Below we translate a held-out set of\n",
    "sentence pairs and plot mean chrF against source length."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "seq2seq-bottleneck"
   },
   "source": [
    "holdout = data._pairs(data._preprocess(data._download()))\n",
    "holdout = holdout[data.num_train + data.num_val::250][:600]\n",
    "h_src, h_tgt = [p[0] for p in holdout], [p[1] for p in holdout]\n",
    "h_preds, _ = model.predict_step(data.build(h_src, h_tgt), data.num_steps)"
   ],
   "execution_count": 26,
   "outputs": []
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "seq2seq-bottleneck-plot"
   },
   "source": [
    "import numpy as np\n",
    "buckets = collections.defaultdict(list)\n",
    "for s, t, p in zip(h_src, h_tgt, h_preds):\n",
    "    L = len(data.tokenizer.encode(s))\n",
    "    buckets[L].append(chrf(to_text(p, data.tokenizer), t))\n",
    "xs = sorted(L for L in buckets if len(buckets[L]) >= 8 and L <= data.num_steps)\n",
    "ys = [np.mean(buckets[L]) for L in xs]\n",
    "d2l.set_figsize()\n",
    "d2l.plt.plot(xs, ys, 'o-')\n",
    "d2l.plt.xlabel('source length (tokens)')\n",
    "d2l.plt.ylabel('mean chrF')\n",
    "d2l.plt.grid(True);"
   ],
   "execution_count": 27,
   "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=\"252.64375pt\" height=\"183.35625pt\" viewBox=\"0 0 252.64375 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-19T21:01:36.697370</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 252.64375 183.35625 \nL 252.64375 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 50.14375 145.8 \nL 245.44375 145.8 \nL 245.44375 7.2 \nL 50.14375 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      <path d=\"M 73.816477 145.8 \nL 73.816477 7.2 \n\" clip-path=\"url(#pf0fb65ac98)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_2\">\n      <defs>\n       <path id=\"mdc8c71f9b5\" d=\"M 0 0 \nL 0 3.5 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#mdc8c71f9b5\" x=\"73.816477\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_1\">\n      <!-- 5.0 -->\n      <g transform=\"translate(65.864915 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-35\" d=\"M 691 4666 \nL 3169 4666 \nL 3169 4134 \nL 1269 4134 \nL 1269 2991 \nQ 1406 3038 1543 3061 \nQ 1681 3084 1819 3084 \nQ 2600 3084 3056 2656 \nQ 3513 2228 3513 1497 \nQ 3513 744 3044 326 \nQ 2575 -91 1722 -91 \nQ 1428 -91 1123 -41 \nQ 819 9 494 109 \nL 494 744 \nQ 775 591 1075 516 \nQ 1375 441 1709 441 \nQ 2250 441 2565 725 \nQ 2881 1009 2881 1497 \nQ 2881 1984 2565 2268 \nQ 2250 2553 1709 2553 \nQ 1456 2553 1204 2497 \nQ 953 2441 691 2322 \nL 691 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n        <path id=\"DejaVuSans-2e\" d=\"M 684 794 \nL 1344 794 \nL 1344 0 \nL 684 0 \nL 684 794 \nz\n\" transform=\"scale(0.015625)\"/>\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-35\"/>\n       <use xlink:href=\"#DejaVuSans-2e\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(95.410156 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_2\">\n     <g id=\"line2d_3\">\n      <path d=\"M 110.805114 145.8 \nL 110.805114 7.2 \n\" clip-path=\"url(#pf0fb65ac98)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_4\">\n      <g>\n       <use xlink:href=\"#mdc8c71f9b5\" x=\"110.805114\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_2\">\n      <!-- 7.5 -->\n      <g transform=\"translate(102.853551 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-37\" d=\"M 525 4666 \nL 3525 4666 \nL 3525 4397 \nL 1831 0 \nL 1172 0 \nL 2766 4134 \nL 525 4134 \nL 525 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-37\"/>\n       <use xlink:href=\"#DejaVuSans-2e\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-35\" transform=\"translate(95.410156 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_3\">\n     <g id=\"line2d_5\">\n      <path d=\"M 147.79375 145.8 \nL 147.79375 7.2 \n\" clip-path=\"url(#pf0fb65ac98)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_6\">\n      <g>\n       <use xlink:href=\"#mdc8c71f9b5\" x=\"147.79375\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_3\">\n      <!-- 10.0 -->\n      <g transform=\"translate(136.660937 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       <use xlink:href=\"#DejaVuSans-2e\" transform=\"translate(127.246094 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(159.033203 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_4\">\n     <g id=\"line2d_7\">\n      <path d=\"M 184.782386 145.8 \nL 184.782386 7.2 \n\" clip-path=\"url(#pf0fb65ac98)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_8\">\n      <g>\n       <use xlink:href=\"#mdc8c71f9b5\" x=\"184.782386\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_4\">\n      <!-- 12.5 -->\n      <g transform=\"translate(173.649574 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-31\"/>\n       <use xlink:href=\"#DejaVuSans-32\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-2e\" transform=\"translate(127.246094 0)\"/>\n       <use xlink:href=\"#DejaVuSans-35\" transform=\"translate(159.033203 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_5\">\n     <g id=\"line2d_9\">\n      <path d=\"M 221.771023 145.8 \nL 221.771023 7.2 \n\" clip-path=\"url(#pf0fb65ac98)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_10\">\n      <g>\n       <use xlink:href=\"#mdc8c71f9b5\" x=\"221.771023\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_5\">\n      <!-- 15.0 -->\n      <g transform=\"translate(210.63821 160.398438) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-31\"/>\n       <use xlink:href=\"#DejaVuSans-35\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-2e\" transform=\"translate(127.246094 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(159.033203 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"text_6\">\n     <!-- source length (tokens) -->\n     <g transform=\"translate(91.591406 174.076563) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-73\" d=\"M 2834 3397 \nL 2834 2853 \nQ 2591 2978 2328 3040 \nQ 2066 3103 1784 3103 \nQ 1356 3103 1142 2972 \nQ 928 2841 928 2578 \nQ 928 2378 1081 2264 \nQ 1234 2150 1697 2047 \nL 1894 2003 \nQ 2506 1872 2764 1633 \nQ 3022 1394 3022 966 \nQ 3022 478 2636 193 \nQ 2250 -91 1575 -91 \nQ 1294 -91 989 -36 \nQ 684 19 347 128 \nL 347 722 \nQ 666 556 975 473 \nQ 1284 391 1588 391 \nQ 1994 391 2212 530 \nQ 2431 669 2431 922 \nQ 2431 1156 2273 1281 \nQ 2116 1406 1581 1522 \nL 1381 1569 \nQ 847 1681 609 1914 \nQ 372 2147 372 2553 \nQ 372 3047 722 3315 \nQ 1072 3584 1716 3584 \nQ 2034 3584 2315 3537 \nQ 2597 3491 2834 3397 \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-75\" d=\"M 544 1381 \nL 544 3500 \nL 1119 3500 \nL 1119 1403 \nQ 1119 906 1312 657 \nQ 1506 409 1894 409 \nQ 2359 409 2629 706 \nQ 2900 1003 2900 1516 \nL 2900 3500 \nL 3475 3500 \nL 3475 0 \nL 2900 0 \nL 2900 538 \nQ 2691 219 2414 64 \nQ 2138 -91 1772 -91 \nQ 1169 -91 856 284 \nQ 544 659 544 1381 \nz\nM 1991 3584 \nL 1991 3584 \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-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-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-20\" 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       <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-67\" d=\"M 2906 1791 \nQ 2906 2416 2648 2759 \nQ 2391 3103 1925 3103 \nQ 1463 3103 1205 2759 \nQ 947 2416 947 1791 \nQ 947 1169 1205 825 \nQ 1463 481 1925 481 \nQ 2391 481 2648 825 \nQ 2906 1169 2906 1791 \nz\nM 3481 434 \nQ 3481 -459 3084 -895 \nQ 2688 -1331 1869 -1331 \nQ 1566 -1331 1297 -1286 \nQ 1028 -1241 775 -1147 \nL 775 -588 \nQ 1028 -725 1275 -790 \nQ 1522 -856 1778 -856 \nQ 2344 -856 2625 -561 \nQ 2906 -266 2906 331 \nL 2906 616 \nQ 2728 306 2450 153 \nQ 2172 0 1784 0 \nQ 1141 0 747 490 \nQ 353 981 353 1791 \nQ 353 2603 747 3093 \nQ 1141 3584 1784 3584 \nQ 2172 3584 2450 3431 \nQ 2728 3278 2906 2969 \nL 2906 3500 \nL 3481 3500 \nL 3481 434 \nz\n\" transform=\"scale(0.015625)\"/>\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-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       <path id=\"DejaVuSans-28\" d=\"M 1984 4856 \nQ 1566 4138 1362 3434 \nQ 1159 2731 1159 2009 \nQ 1159 1288 1364 580 \nQ 1569 -128 1984 -844 \nL 1484 -844 \nQ 1016 -109 783 600 \nQ 550 1309 550 2009 \nQ 550 2706 781 3412 \nQ 1013 4119 1484 4856 \nL 1984 4856 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-6b\" d=\"M 581 4863 \nL 1159 4863 \nL 1159 1991 \nL 2875 3500 \nL 3609 3500 \nL 1753 1863 \nL 3688 0 \nL 2938 0 \nL 1159 1709 \nL 1159 0 \nL 581 0 \nL 581 4863 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-29\" d=\"M 513 4856 \nL 1013 4856 \nQ 1481 4119 1714 3412 \nQ 1947 2706 1947 2009 \nQ 1947 1309 1714 600 \nQ 1481 -109 1013 -844 \nL 513 -844 \nQ 928 -128 1133 580 \nQ 1338 1288 1338 2009 \nQ 1338 2731 1133 3434 \nQ 928 4138 513 4856 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-73\"/>\n      <use xlink:href=\"#DejaVuSans-6f\" transform=\"translate(52.099609 0)\"/>\n      <use xlink:href=\"#DejaVuSans-75\" transform=\"translate(113.28125 0)\"/>\n      <use xlink:href=\"#DejaVuSans-72\" transform=\"translate(176.660156 0)\"/>\n      <use xlink:href=\"#DejaVuSans-63\" transform=\"translate(215.523438 0)\"/>\n      <use xlink:href=\"#DejaVuSans-65\" transform=\"translate(270.503906 0)\"/>\n      <use xlink:href=\"#DejaVuSans-20\" transform=\"translate(332.027344 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6c\" transform=\"translate(363.814453 0)\"/>\n      <use xlink:href=\"#DejaVuSans-65\" transform=\"translate(391.597656 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6e\" transform=\"translate(453.121094 0)\"/>\n      <use xlink:href=\"#DejaVuSans-67\" transform=\"translate(516.5 0)\"/>\n      <use xlink:href=\"#DejaVuSans-74\" transform=\"translate(579.976562 0)\"/>\n      <use xlink:href=\"#DejaVuSans-68\" transform=\"translate(619.185547 0)\"/>\n      <use xlink:href=\"#DejaVuSans-20\" transform=\"translate(682.564453 0)\"/>\n      <use xlink:href=\"#DejaVuSans-28\" transform=\"translate(714.351562 0)\"/>\n      <use xlink:href=\"#DejaVuSans-74\" transform=\"translate(753.365234 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6f\" transform=\"translate(792.574219 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6b\" transform=\"translate(853.755859 0)\"/>\n      <use xlink:href=\"#DejaVuSans-65\" transform=\"translate(908.041016 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6e\" transform=\"translate(969.564453 0)\"/>\n      <use xlink:href=\"#DejaVuSans-73\" transform=\"translate(1032.943359 0)\"/>\n      <use xlink:href=\"#DejaVuSans-29\" transform=\"translate(1085.042969 0)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"matplotlib.axis_2\">\n    <g id=\"ytick_1\">\n     <g id=\"line2d_11\">\n      <path d=\"M 50.14375 118.595789 \nL 245.44375 118.595789 \n\" clip-path=\"url(#pf0fb65ac98)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_12\">\n      <defs>\n       <path id=\"md2ad3ee625\" d=\"M 0 0 \nL -3.5 0 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#md2ad3ee625\" x=\"50.14375\" y=\"118.595789\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_7\">\n      <!-- 0.08 -->\n      <g transform=\"translate(20.878125 122.395008) 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-30\"/>\n       <use xlink:href=\"#DejaVuSans-2e\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(95.410156 0)\"/>\n       <use xlink:href=\"#DejaVuSans-38\" transform=\"translate(159.033203 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_2\">\n     <g id=\"line2d_13\">\n      <path d=\"M 50.14375 87.44564 \nL 245.44375 87.44564 \n\" clip-path=\"url(#pf0fb65ac98)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_14\">\n      <g>\n       <use xlink:href=\"#md2ad3ee625\" x=\"50.14375\" y=\"87.44564\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_8\">\n      <!-- 0.10 -->\n      <g transform=\"translate(20.878125 91.244859) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-30\"/>\n       <use xlink:href=\"#DejaVuSans-2e\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-31\" transform=\"translate(95.410156 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(159.033203 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_3\">\n     <g id=\"line2d_15\">\n      <path d=\"M 50.14375 56.295492 \nL 245.44375 56.295492 \n\" clip-path=\"url(#pf0fb65ac98)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_16\">\n      <g>\n       <use xlink:href=\"#md2ad3ee625\" x=\"50.14375\" y=\"56.295492\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_9\">\n      <!-- 0.12 -->\n      <g transform=\"translate(20.878125 60.094711) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-30\"/>\n       <use xlink:href=\"#DejaVuSans-2e\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-31\" transform=\"translate(95.410156 0)\"/>\n       <use xlink:href=\"#DejaVuSans-32\" transform=\"translate(159.033203 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_4\">\n     <g id=\"line2d_17\">\n      <path d=\"M 50.14375 25.145343 \nL 245.44375 25.145343 \n\" clip-path=\"url(#pf0fb65ac98)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_18\">\n      <g>\n       <use xlink:href=\"#md2ad3ee625\" x=\"50.14375\" y=\"25.145343\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_10\">\n      <!-- 0.14 -->\n      <g transform=\"translate(20.878125 28.944562) 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-30\"/>\n       <use xlink:href=\"#DejaVuSans-2e\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-31\" transform=\"translate(95.410156 0)\"/>\n       <use xlink:href=\"#DejaVuSans-34\" transform=\"translate(159.033203 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"text_11\">\n     <!-- mean chrF -->\n     <g transform=\"translate(14.798437 103.117969) rotate(-90) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-6d\" d=\"M 3328 2828 \nQ 3544 3216 3844 3400 \nQ 4144 3584 4550 3584 \nQ 5097 3584 5394 3201 \nQ 5691 2819 5691 2113 \nL 5691 0 \nL 5113 0 \nL 5113 2094 \nQ 5113 2597 4934 2840 \nQ 4756 3084 4391 3084 \nQ 3944 3084 3684 2787 \nQ 3425 2491 3425 1978 \nL 3425 0 \nL 2847 0 \nL 2847 2094 \nQ 2847 2600 2669 2842 \nQ 2491 3084 2119 3084 \nQ 1678 3084 1418 2786 \nQ 1159 2488 1159 1978 \nL 1159 0 \nL 581 0 \nL 581 3500 \nL 1159 3500 \nL 1159 2956 \nQ 1356 3278 1631 3431 \nQ 1906 3584 2284 3584 \nQ 2666 3584 2933 3390 \nQ 3200 3197 3328 2828 \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-46\" d=\"M 628 4666 \nL 3309 4666 \nL 3309 4134 \nL 1259 4134 \nL 1259 2759 \nL 3109 2759 \nL 3109 2228 \nL 1259 2228 \nL 1259 0 \nL 628 0 \nL 628 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-6d\"/>\n      <use xlink:href=\"#DejaVuSans-65\" transform=\"translate(97.412109 0)\"/>\n      <use xlink:href=\"#DejaVuSans-61\" transform=\"translate(158.935547 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6e\" transform=\"translate(220.214844 0)\"/>\n      <use xlink:href=\"#DejaVuSans-20\" transform=\"translate(283.59375 0)\"/>\n      <use xlink:href=\"#DejaVuSans-63\" transform=\"translate(315.380859 0)\"/>\n      <use xlink:href=\"#DejaVuSans-68\" transform=\"translate(370.361328 0)\"/>\n      <use xlink:href=\"#DejaVuSans-72\" transform=\"translate(433.740234 0)\"/>\n      <use xlink:href=\"#DejaVuSans-46\" transform=\"translate(474.853516 0)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"line2d_19\">\n    <path d=\"M 59.021023 13.5 \nL 73.816477 46.80735 \nL 88.611932 41.824055 \nL 103.407386 64.556867 \nL 118.202841 62.55154 \nL 132.998295 84.748773 \nL 147.79375 113.904441 \nL 162.589205 104.2431 \nL 177.384659 127.591349 \nL 192.180114 114.245563 \nL 206.975568 139.5 \nL 221.771023 128.143092 \nL 236.566477 124.442171 \n\" clip-path=\"url(#pf0fb65ac98)\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n    <defs>\n     <path id=\"m755bc3f8b2\" d=\"M 0 3 \nC 0.795609 3 1.55874 2.683901 2.12132 2.12132 \nC 2.683901 1.55874 3 0.795609 3 0 \nC 3 -0.795609 2.683901 -1.55874 2.12132 -2.12132 \nC 1.55874 -2.683901 0.795609 -3 0 -3 \nC -0.795609 -3 -1.55874 -2.683901 -2.12132 -2.12132 \nC -2.683901 -1.55874 -3 -0.795609 -3 0 \nC -3 0.795609 -2.683901 1.55874 -2.12132 2.12132 \nC -1.55874 2.683901 -0.795609 3 0 3 \nz\n\" style=\"stroke: #1f77b4\"/>\n    </defs>\n    <g clip-path=\"url(#pf0fb65ac98)\">\n     <use xlink:href=\"#m755bc3f8b2\" x=\"59.021023\" y=\"13.5\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n     <use xlink:href=\"#m755bc3f8b2\" x=\"73.816477\" y=\"46.80735\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n     <use xlink:href=\"#m755bc3f8b2\" x=\"88.611932\" y=\"41.824055\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n     <use xlink:href=\"#m755bc3f8b2\" x=\"103.407386\" y=\"64.556867\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n     <use xlink:href=\"#m755bc3f8b2\" x=\"118.202841\" y=\"62.55154\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n     <use xlink:href=\"#m755bc3f8b2\" x=\"132.998295\" y=\"84.748773\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n     <use xlink:href=\"#m755bc3f8b2\" x=\"147.79375\" y=\"113.904441\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n     <use xlink:href=\"#m755bc3f8b2\" x=\"162.589205\" y=\"104.2431\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n     <use xlink:href=\"#m755bc3f8b2\" x=\"177.384659\" y=\"127.591349\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n     <use xlink:href=\"#m755bc3f8b2\" x=\"192.180114\" y=\"114.245563\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n     <use xlink:href=\"#m755bc3f8b2\" x=\"206.975568\" y=\"139.5\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n     <use xlink:href=\"#m755bc3f8b2\" x=\"221.771023\" y=\"128.143092\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n     <use xlink:href=\"#m755bc3f8b2\" x=\"236.566477\" y=\"124.442171\" style=\"fill: #1f77b4; stroke: #1f77b4\"/>\n    </g>\n   </g>\n   <g id=\"patch_3\">\n    <path d=\"M 50.14375 145.8 \nL 50.14375 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 245.44375 145.8 \nL 245.44375 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 50.14375 145.8 \nL 245.44375 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 50.14375 7.2 \nL 245.44375 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n  </g>\n </g>\n <defs>\n  <clipPath id=\"pf0fb65ac98\">\n   <rect x=\"50.14375\" 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=seq2seq-md-24 fw=all -->\n",
    "\n",
    "The curve slopes down: as the source grows, translation quality falls off, and it\n",
    "falls off precisely because the model must funnel more and more information\n",
    "through an unchanged $\\mathbf{c}$. This single bottleneck is where two large\n",
    "stories in this book began, and you have now read both. One escape lets the\n",
    "decoder look back at *all* the encoder's per-step states instead of only the\n",
    "final one, choosing what to attend to at each step — that is *attention*\n",
    "(that section), born in 2014 as exactly this repair\n",
    "[@Bahdanau.Cho.Bengio.2014], and its transformer descendants\n",
    "(that section) retired this model from production. The other\n",
    "keeps a single running state but makes it a far better memory, one that decides\n",
    "what to store and what to discard as it goes — the linear and selective state\n",
    "space models of that section. The same bottleneck, measured here\n",
    "on a fifty-line translator, motivated both.\n",
    "\n",
    "## Summary\n",
    "\n",
    "Sequence transduction maps one variable-length sequence to another. The\n",
    "encoder-decoder abstraction handles it by compressing the source into a\n",
    "fixed-shape state with an encoder and expanding that state into the target with a\n",
    "decoder, which is a conditional language model. We built a concrete instance from\n",
    "two GRUs, trained on English-French pairs tokenized by a shared byte-level BPE,\n",
    "using teacher forcing and a masked loss that ignores padding. At inference the\n",
    "decoding strategies of that section apply directly, from greedy to beam\n",
    "search, and we scored translations with chrF, the character-level metric that has\n",
    "displaced BLEU as the lexical default. The whole source passing through one fixed\n",
    "vector is a hard limit that degrades with sentence length, and escaping it,\n",
    "through attention or through better recurrent state, organizes everything that\n",
    "follows.\n",
    "\n",
    "## Exercises\n",
    "\n",
    "1. The dataset uses one shared vocabulary for both languages. Rebuild `MTFraEng`\n",
    "   with two separate tokenizers, one per language, and compare training loss and\n",
    "   translation quality. When would separate vocabularies help, and when hurt?\n",
    "1. Sweep the beam width $k \\in \\{1, 2, 4, 8, 16\\}$ on the three example\n",
    "   sentences and on a handful of longer held-out sentences. Where does quality\n",
    "   stop improving, and why (recall the large-beam discussion of\n",
    "   that section)?\n",
    "1. `d2l.beam_search` normalizes scores by length raised to $\\alpha$. Decode with\n",
    "   $\\alpha \\in \\{0, 0.75, 1.5\\}$ and relate the lengths of the winning\n",
    "   translations to the equation.\n",
    "1. Rerun training with the masked loss replaced by a plain cross-entropy over all\n",
    "   positions, padding included. What happens to the translations, and why?\n",
    "1. Replace the GRU with an LSTM (that section) in both encoder and decoder.\n",
    "   Does translation quality change at this scale?\n",
    "1. Feed the context $\\mathbf{c}$ only at the first decoder step (as in\n",
    "   @Sutskever.Vinyals.Le.2014) instead of at every step. How does that\n",
    "   change the results, and why might repeating the context help a small model?\n",
    "1. Score the example translations with both `chrf` and `d2l.bleu`. Construct a\n",
    "   translation on which the two metrics most disagree, and explain the mechanism."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=seq2seq-md-25 fw=jax -->\n",
    "\n",
    "[Discussions](https://d2l.discourse.group/t/18022)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "d2l-jax",
   "display_name": "d2l (JAX)",
   "language": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
