{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "---\n",
    "execute:\n",
    "  enabled: false\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=practice-md-1 fw=all -->\n",
    "\n",
    "# Practice\n",
    "\n",
    "This chapter introduced its methods one at a time, each with a clean\n",
    "experiment attached. A real training run gets no such courtesy: it must make\n",
    "all of the chapter's decisions at once, commit to them for weeks of compute,\n",
    "and survive whatever the data, the hardware, and the loss surface do in the\n",
    "meantime. This closing section is about that craft. We read the recipes that\n",
    "current large-scale runs disclose; implement the one standard ingredient the\n",
    "chapter has not yet built, gradient clipping; add the cheapest trick in the\n",
    "practitioner's kit, weight averaging; and end with the discipline that makes\n",
    "tuning and optimizer comparisons meaningful."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "practice"
   },
   "source": [
    "%matplotlib inline\n",
    "from d2l import jax as d2l\n",
    "from flax import nnx\n",
    "import jax\n",
    "from jax import numpy as jnp\n",
    "import math\n",
    "import optax"
   ],
   "execution_count": 1,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=practice-md-2 fw=all -->\n",
    "\n",
    "## The Recipe, as Disclosed\n",
    "\n",
    "A frontier language model is trained once, so its hyperparameters are not\n",
    "found by sweeping at full scale; they are assembled from smaller proxies\n",
    "(that section), from precedent, and from the recipes of earlier\n",
    "runs. Some of what the teams settled on is public. the table\n",
    "collects the optimizer configurations disclosed by four prominent\n",
    "pretraining reports from 2024--2026. Read it as evidence about practice, not\n",
    "as gospel: each entry is what a team reported shipping, usually with no\n",
    "ablation attached, and a dash means the report does not say.\n",
    "\n",
    ":What four pretraining reports disclose about their optimizer configuration.\n",
    "A dash means the report does not say. Batch sizes are quoted the way each\n",
    "report quotes them (tokens or sequences).\n",
    "\n",
    "| run | optimizer | $\\beta_1, \\beta_2$ | peak LR and schedule | warmup | clip | weight decay | batch size |\n",
    "|:--|:--|:--|:--|:--|:--|:--|:--|\n",
    "| Llama 3 405B [@Grattafiori.Dubey.Jauhri.ea.2024] | AdamW | — | $8{\\times}10^{-5}$, cosine to $8{\\times}10^{-7}$ | 8k steps | — | — | ramp 4M $\\to$ 8M $\\to$ 16M tokens |\n",
    "| DeepSeek-V3 [@Liu.Feng.Xue.ea.2024] | AdamW | 0.9, 0.95 | $2.2{\\times}10^{-4}$, constant to 10T tokens, cosine to $2.2{\\times}10^{-5}$ | 2k steps | 1.0 | 0.1 | ramp 3,072 $\\to$ 15,360 seqs over 469B tokens |\n",
    "| OLMo 2 7B [@OLMo.2025] | AdamW | 0.9, 0.95 | $3{\\times}10^{-4}$, cosine to 10% of peak | 2k steps | 1.0 | 0.1, embeddings exempt | 1,024 seqs, fixed |\n",
    "| Kimi K2 [@Kimi.Team.2025] | MuonClip | — | $2{\\times}10^{-4}$, constant to 10T tokens, cosine to $2{\\times}10^{-5}$ | 500 steps | — | 0.1 | 67M tokens, fixed |\n",
    "\n",
    "Read down the columns and a consensus core appears. The optimizer is AdamW\n",
    "with $(\\beta_1, \\beta_2) = (0.9, 0.95)$, the shortened second-moment window\n",
    "that that section discussed. Weight decay is 0.1, with the exemptions\n",
    "that section derived (OLMo 2 states the embedding exemption outright).\n",
    "Gradients are clipped at global norm 1. The schedule is a brief warmup, a\n",
    "few hundred to a few thousand steps out of a million, into either cosine\n",
    "decay or the warmup--stable--decay shape of that section, ending\n",
    "one to two orders of magnitude below the peak. Where batch handling is\n",
    "disclosed, the batch is ramped up early in training, while the gradient\n",
    "noise scale is still small (that section). The blanks carry\n",
    "information too: even a report of Llama 3's length leaves betas, weight\n",
    "decay, and clipping unstated, because much of the recipe travels as defaults in\n",
    "training code rather than as prose. And one row breaks the optimizer\n",
    "column: Kimi K2 runs the Muon split of that section, hidden matrices\n",
    "under orthogonalized momentum with AdamW for the rest, inside an otherwise\n",
    "consensus recipe. That is the current state of the art in one table: a\n",
    "stable core, one production-proven challenger, and the details you would\n",
    "need to reproduce any row only partly on the record.\n",
    "\n",
    "## Gradient Clipping\n",
    "\n",
    "One column of the table has no section behind it yet. Gradient clipping\n",
    "entered this book in that section as the fix for exploding RNN\n",
    "gradients; the table shows that it outlived the architecture that motivated\n",
    "it. Every run above that discloses a threshold clips, and every disclosed\n",
    "threshold is 1. Global-norm clipping treats all parameters as one long\n",
    "vector: with $\\mathbf{g}$ the concatenation of every parameter's gradient\n",
    "and $\\theta$ the threshold,\n",
    "\n",
    "$$\n",
    "\\mathbf{g} \\leftarrow \\min\\left(1,\\; \\frac{\\theta}{\\|\\mathbf{g}\\|_2}\\right) \\mathbf{g}.\n",
    "$$\n",
    "\n",
    "A gradient shorter than $\\theta$ passes untouched; a longer one keeps its\n",
    "direction and loses its length. The implementation is a few lines in either\n",
    "framework."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=practice-md-3 fw=jax -->\n",
    "\n",
    "In Optax, clipping is a gradient transformation like any other, so it\n",
    "composes with `optax.chain`: the clipped transformation drops into\n",
    "`nnx.Optimizer`, and the harness of that section needs no change at\n",
    "all."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "practice-gradient-clipping-1"
   },
   "source": [
    "def clipped(tx, max_norm=1.0):\n",
    "    \"\"\"Clip the global gradient norm before an optimizer's update.\"\"\"\n",
    "    return optax.chain(optax.clip_by_global_norm(max_norm), tx)"
   ],
   "execution_count": 2,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=practice-md-4 fw=all -->\n",
    "\n",
    "### A NaN, Averted\n",
    "\n",
    "that section swept SGD with momentum on `TinyLM` and found a knife's\n",
    "edge: the best learning rate sat one grid point below one that returned\n",
    "NaN. That divergent grid point is exactly what clipping is for, so we rerun\n",
    "it, with and without the guard, and nothing else changed."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "practice-a-nan-averted-1"
   },
   "source": [
    "data = d2l.TimeMachine(batch_size=64, num_steps=64, tokenization='char',\n",
    "                       num_train=100000)\n",
    "\n",
    "def final_loss(losses, k=100):\n",
    "    v = sum(losses[-k:]) / k\n",
    "    return v if math.isfinite(v) else float('inf')"
   ],
   "execution_count": 3,
   "outputs": []
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "practice-a-nan-averted-2"
   },
   "source": [
    "curves = {}\n",
    "for clip in (False, True):\n",
    "    model = d2l.TinyLM(len(data.vocab), rngs=nnx.Rngs(0))\n",
    "    tx = optax.sgd(0.3, momentum=0.9)\n",
    "    if clip:\n",
    "        tx = clipped(tx)\n",
    "    optimizer = nnx.Optimizer(model, tx, wrt=nnx.Param)\n",
    "    label = 'clipped at 1.0' if clip else 'unclipped'\n",
    "    curves[label] = d2l.train_lm(model, data, optimizer, num_steps=2000)\n",
    "    print(f'{label}: final loss {final_loss(curves[label]):.3f}')"
   ],
   "execution_count": 4,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "unclipped: final loss inf\n"
     ]
    },
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "clipped at 1.0: final loss 1.067\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=practice-md-5 fw=all -->\n",
    "\n",
    "The first steps tell the story, so we plot them raw, on a log scale."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "practice-a-nan-averted-3"
   },
   "source": [
    "d2l.plot(list(range(150)), [curves[k][:150] for k in curves], 'step',\n",
    "         'training loss', legend=list(curves), yscale='log')"
   ],
   "execution_count": 5,
   "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.836183pt\" height=\"183.35625pt\" viewBox=\"0 0 249.836183 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-19T15:07:17.710224</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.836183 183.35625 \nL 249.836183 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 45.478125 145.8 \nL 240.778125 145.8 \nL 240.778125 7.2 \nL 45.478125 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 54.355398 145.8 \nL 54.355398 7.2 \n\" clip-path=\"url(#p206faf74ec)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_2\">\n      <defs>\n       <path id=\"m289c7d06e7\" d=\"M 0 0 \nL 0 3.5 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#m289c7d06e7\" x=\"54.355398\" 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(51.174148 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_3\">\n      <path d=\"M 113.934409 145.8 \nL 113.934409 7.2 \n\" clip-path=\"url(#p206faf74ec)\" 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=\"#m289c7d06e7\" x=\"113.934409\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_2\">\n      <!-- 50 -->\n      <g transform=\"translate(107.571909 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       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_3\">\n     <g id=\"line2d_5\">\n      <path d=\"M 173.513421 145.8 \nL 173.513421 7.2 \n\" clip-path=\"url(#p206faf74ec)\" 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=\"#m289c7d06e7\" x=\"173.513421\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_3\">\n      <!-- 100 -->\n      <g transform=\"translate(163.969671 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-30\" transform=\"translate(127.246094 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_4\">\n     <g id=\"line2d_7\">\n      <path d=\"M 233.092433 145.8 \nL 233.092433 7.2 \n\" clip-path=\"url(#p206faf74ec)\" 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=\"#m289c7d06e7\" x=\"233.092433\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_4\">\n      <!-- 150 -->\n      <g transform=\"translate(223.548683 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-30\" transform=\"translate(127.246094 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"text_5\">\n     <!-- step -->\n     <g transform=\"translate(132.3125 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-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-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      </defs>\n      <use xlink:href=\"#DejaVuSans-73\"/>\n      <use xlink:href=\"#DejaVuSans-74\" transform=\"translate(52.099609 0)\"/>\n      <use xlink:href=\"#DejaVuSans-65\" transform=\"translate(91.308594 0)\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(152.832031 0)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"matplotlib.axis_2\">\n    <g id=\"ytick_1\">\n     <g id=\"line2d_9\">\n      <path d=\"M 45.478125 129.567924 \nL 240.778125 129.567924 \n\" clip-path=\"url(#p206faf74ec)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_10\">\n      <defs>\n       <path id=\"mbd64b65d61\" d=\"M 0 0 \nL -3.5 0 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#mbd64b65d61\" x=\"45.478125\" y=\"129.567924\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_6\">\n      <!-- $\\mathdefault{10^{1}}$ -->\n      <g transform=\"translate(20.878125 133.367142) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-31\" transform=\"translate(0 0.684375)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0.684375)\"/>\n       <use xlink:href=\"#DejaVuSans-31\" transform=\"translate(128.203125 38.965625) scale(0.7)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_2\">\n     <g id=\"line2d_11\">\n      <path d=\"M 45.478125 98.908694 \nL 240.778125 98.908694 \n\" clip-path=\"url(#p206faf74ec)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_12\">\n      <g>\n       <use xlink:href=\"#mbd64b65d61\" x=\"45.478125\" y=\"98.908694\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_7\">\n      <!-- $\\mathdefault{10^{3}}$ -->\n      <g transform=\"translate(20.878125 102.707913) 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-31\" transform=\"translate(0 0.765625)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0.765625)\"/>\n       <use xlink:href=\"#DejaVuSans-33\" transform=\"translate(128.203125 39.046875) scale(0.7)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_3\">\n     <g id=\"line2d_13\">\n      <path d=\"M 45.478125 68.249465 \nL 240.778125 68.249465 \n\" clip-path=\"url(#p206faf74ec)\" 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=\"#mbd64b65d61\" x=\"45.478125\" y=\"68.249465\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_8\">\n      <!-- $\\mathdefault{10^{5}}$ -->\n      <g transform=\"translate(20.878125 72.048684) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-31\" transform=\"translate(0 0.684375)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0.684375)\"/>\n       <use xlink:href=\"#DejaVuSans-35\" transform=\"translate(128.203125 38.965625) scale(0.7)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_4\">\n     <g id=\"line2d_15\">\n      <path d=\"M 45.478125 37.590236 \nL 240.778125 37.590236 \n\" clip-path=\"url(#p206faf74ec)\" 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=\"#mbd64b65d61\" x=\"45.478125\" y=\"37.590236\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_9\">\n      <!-- $\\mathdefault{10^{7}}$ -->\n      <g transform=\"translate(20.878125 41.389454) 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-31\" transform=\"translate(0 0.684375)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0.684375)\"/>\n       <use xlink:href=\"#DejaVuSans-37\" transform=\"translate(128.203125 38.965625) scale(0.7)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"text_10\">\n     <!-- training loss -->\n     <g transform=\"translate(14.798438 107.116406) rotate(-90) scale(0.1 -0.1)\">\n      <defs>\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-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-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-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      </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-69\" transform=\"translate(232.763672 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6e\" transform=\"translate(260.546875 0)\"/>\n      <use xlink:href=\"#DejaVuSans-67\" transform=\"translate(323.925781 0)\"/>\n      <use xlink:href=\"#DejaVuSans-20\" transform=\"translate(387.402344 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6c\" transform=\"translate(419.189453 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6f\" transform=\"translate(446.972656 0)\"/>\n      <use xlink:href=\"#DejaVuSans-73\" transform=\"translate(508.154297 0)\"/>\n      <use xlink:href=\"#DejaVuSans-73\" transform=\"translate(560.253906 0)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"line2d_17\">\n    <path d=\"M 54.355398 136.104848 \nL 55.546978 135.363702 \nL 56.738558 132.192506 \nL 57.930138 134.521112 \nL 59.121719 122.546658 \nL 60.313299 128.809775 \nL 61.504879 131.315206 \nL 62.696459 123.368553 \nL 63.88804 131.303065 \nL 65.07962 131.791623 \nL 66.2712 130.550103 \nL 67.46278 128.652421 \nL 68.654361 128.977561 \nL 69.845941 128.646224 \nL 71.037521 129.613039 \nL 72.229101 124.180079 \nL 73.420681 125.440575 \nL 74.612262 125.021138 \nL 75.803842 124.992074 \nL 76.995422 127.185879 \nL 78.187002 123.954246 \nL 79.378583 119.957933 \nL 80.570163 121.851356 \nL 81.761743 124.328589 \nL 82.953323 121.89063 \nL 84.144904 120.186103 \nL 85.336484 115.102705 \nL 86.528064 117.193503 \nL 87.719644 114.340148 \nL 88.911224 113.963398 \nL 90.102805 114.725245 \nL 91.294385 114.227144 \nL 92.485965 114.02113 \nL 93.677545 107.658738 \nL 94.869126 105.954619 \nL 96.060706 106.367103 \nL 97.252286 102.986684 \nL 98.443866 101.515195 \nL 99.635447 103.722544 \nL 100.827027 102.263695 \nL 102.018607 101.016206 \nL 103.210187 93.415526 \nL 104.401767 94.129014 \nL 105.593348 91.377792 \nL 106.784928 93.489357 \nL 107.976508 91.92962 \nL 109.168088 85.459517 \nL 111.551249 83.514746 \nL 113.934409 72.334043 \nL 115.12599 78.964031 \nL 116.31757 70.777062 \nL 117.50915 55.01467 \nL 118.70073 48.141533 \nL 119.89231 49.960262 \nL 121.083891 47.822851 \nL 122.275471 48.249209 \nL 123.467051 24.41448 \nL 124.658631 18.771692 \nL 125.850212 19.23338 \nL 127.041792 18.219905 \nL 128.233372 13.5 \nL 129.424952 19.808708 \nL 130.616533 17.002014 \nL 131.808113 19.34937 \nL 132.999693 17.139098 \nL 134.191273 17.570827 \nL 135.382853 19.941663 \nL 136.574434 20.11047 \nL 137.766014 23.941565 \nL 138.957594 20.458595 \nL 140.149174 21.942798 \nL 141.340755 25.23194 \nL 142.532335 14.228327 \nL 143.723915 16.101985 \nL 144.915495 23.146288 \nL 146.107076 22.832598 \nL 147.298656 22.39038 \nL 148.490236 22.756797 \nL 149.681816 18.288956 \nL 150.873397 14.400787 \nL 152.064977 16.317167 \nL 153.256557 14.504977 \nL 154.448137 15.643639 \nL 156.831298 15.266097 \nL 158.022878 17.988718 \nL 159.214458 22.762723 \nL 161.597619 19.648583 \nL 162.789199 22.390537 \nL 163.980779 21.573972 \nL 165.172359 23.739767 \nL 166.36394 20.280607 \nL 167.55552 18.54525 \nL 168.7471 18.241836 \nL 171.13026 20.677021 \nL 171.13026 20.677021 \n\" clip-path=\"url(#p206faf74ec)\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_18\">\n    <path d=\"M 54.355398 136.103256 \nL 55.546978 137.170104 \nL 56.738558 136.498842 \nL 57.930138 137.318328 \nL 59.121719 136.649055 \nL 60.313299 137.392899 \nL 61.504879 136.896678 \nL 62.696459 137.00241 \nL 63.88804 137.648843 \nL 65.07962 135.929701 \nL 66.2712 136.475172 \nL 67.46278 136.711961 \nL 68.654361 136.681787 \nL 69.845941 137.235275 \nL 72.229101 137.407218 \nL 73.420681 137.039428 \nL 74.612262 137.405447 \nL 75.803842 137.906785 \nL 76.995422 136.673601 \nL 78.187002 136.575992 \nL 79.378583 137.873786 \nL 81.761743 137.612939 \nL 82.953323 137.907861 \nL 84.144904 137.491592 \nL 85.336484 138.066683 \nL 86.528064 137.771127 \nL 87.719644 137.681789 \nL 88.911224 138.191488 \nL 91.294385 137.912865 \nL 93.677545 138.332743 \nL 99.635447 138.59873 \nL 100.827027 138.501684 \nL 102.018607 138.765384 \nL 103.210187 138.711864 \nL 104.401767 138.868495 \nL 106.784928 138.732654 \nL 109.168088 138.903057 \nL 116.31757 139.019004 \nL 117.50915 138.948834 \nL 119.89231 138.992838 \nL 121.083891 138.937226 \nL 122.275471 139.074953 \nL 124.658631 138.948608 \nL 131.808113 139.080332 \nL 134.191273 139.086585 \nL 136.574434 139.139896 \nL 150.873397 139.205638 \nL 153.256557 139.274554 \nL 156.831298 139.071787 \nL 158.022878 139.212464 \nL 159.214458 139.164229 \nL 160.406038 139.228665 \nL 163.980779 139.101778 \nL 166.36394 139.274427 \nL 168.7471 139.184343 \nL 171.13026 139.318645 \nL 172.321841 139.218021 \nL 174.705001 139.29467 \nL 178.279742 139.197766 \nL 179.471322 139.358496 \nL 181.854483 139.246148 \nL 183.046063 139.339352 \nL 185.429223 139.239623 \nL 200.919766 139.413621 \nL 202.111346 139.29534 \nL 204.494507 139.475814 \nL 205.686087 139.252338 \nL 208.069248 139.424591 \nL 210.452408 139.366322 \nL 215.218729 139.482122 \nL 218.79347 139.331568 \nL 219.98505 139.430434 \nL 223.559791 139.277665 \nL 224.751371 139.444107 \nL 225.942951 139.380957 \nL 227.134531 139.452753 \nL 228.326112 139.357158 \nL 231.900852 139.5 \nL 231.900852 139.5 \n\" clip-path=\"url(#p206faf74ec)\" style=\"fill: none; stroke-dasharray: 5.55,2.4; stroke-dashoffset: 0; stroke: #bf00bf; stroke-width: 1.5\"/>\n   </g>\n   <g id=\"patch_3\">\n    <path d=\"M 45.478125 145.8 \nL 45.478125 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 240.778125 145.8 \nL 240.778125 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 45.478125 145.8 \nL 240.778125 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 45.478125 7.2 \nL 240.778125 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 133.217187 92.678125 \nL 233.778125 92.678125 \nQ 235.778125 92.678125 235.778125 90.678125 \nL 235.778125 62.321875 \nQ 235.778125 60.321875 233.778125 60.321875 \nL 133.217187 60.321875 \nQ 131.217187 60.321875 131.217187 62.321875 \nL 131.217187 90.678125 \nQ 131.217187 92.678125 133.217187 92.678125 \nz\n\" style=\"fill: #ffffff; opacity: 0.8; stroke: #cccccc; stroke-linejoin: miter\"/>\n    </g>\n    <g id=\"line2d_19\">\n     <path d=\"M 135.217187 68.420313 \nL 145.217187 68.420313 \nL 155.217187 68.420313 \n\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n    </g>\n    <g id=\"text_11\">\n     <!-- unclipped -->\n     <g transform=\"translate(163.217187 71.920313) 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       <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-64\" d=\"M 2906 2969 \nL 2906 4863 \nL 3481 4863 \nL 3481 0 \nL 2906 0 \nL 2906 525 \nQ 2725 213 2448 61 \nQ 2172 -91 1784 -91 \nQ 1150 -91 751 415 \nQ 353 922 353 1747 \nQ 353 2572 751 3078 \nQ 1150 3584 1784 3584 \nQ 2172 3584 2448 3432 \nQ 2725 3281 2906 2969 \nz\nM 947 1747 \nQ 947 1113 1208 752 \nQ 1469 391 1925 391 \nQ 2381 391 2643 752 \nQ 2906 1113 2906 1747 \nQ 2906 2381 2643 2742 \nQ 2381 3103 1925 3103 \nQ 1469 3103 1208 2742 \nQ 947 2381 947 1747 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-75\"/>\n      <use xlink:href=\"#DejaVuSans-6e\" transform=\"translate(63.378906 0)\"/>\n      <use xlink:href=\"#DejaVuSans-63\" transform=\"translate(126.757812 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6c\" transform=\"translate(181.738281 0)\"/>\n      <use xlink:href=\"#DejaVuSans-69\" transform=\"translate(209.521484 0)\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(237.304688 0)\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(300.78125 0)\"/>\n      <use xlink:href=\"#DejaVuSans-65\" transform=\"translate(364.257812 0)\"/>\n      <use xlink:href=\"#DejaVuSans-64\" transform=\"translate(425.78125 0)\"/>\n     </g>\n    </g>\n    <g id=\"line2d_20\">\n     <path d=\"M 135.217187 83.098438 \nL 145.217187 83.098438 \nL 155.217187 83.098438 \n\" style=\"fill: none; stroke-dasharray: 5.55,2.4; stroke-dashoffset: 0; stroke: #bf00bf; stroke-width: 1.5\"/>\n    </g>\n    <g id=\"text_12\">\n     <!-- clipped at 1.0 -->\n     <g transform=\"translate(163.217187 86.598438) scale(0.1 -0.1)\">\n      <defs>\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      </defs>\n      <use xlink:href=\"#DejaVuSans-63\"/>\n      <use xlink:href=\"#DejaVuSans-6c\" transform=\"translate(54.980469 0)\"/>\n      <use xlink:href=\"#DejaVuSans-69\" transform=\"translate(82.763672 0)\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(110.546875 0)\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(174.023438 0)\"/>\n      <use xlink:href=\"#DejaVuSans-65\" transform=\"translate(237.5 0)\"/>\n      <use xlink:href=\"#DejaVuSans-64\" transform=\"translate(299.023438 0)\"/>\n      <use xlink:href=\"#DejaVuSans-20\" transform=\"translate(362.5 0)\"/>\n      <use xlink:href=\"#DejaVuSans-61\" transform=\"translate(394.287109 0)\"/>\n      <use xlink:href=\"#DejaVuSans-74\" transform=\"translate(455.566406 0)\"/>\n      <use xlink:href=\"#DejaVuSans-20\" transform=\"translate(494.775391 0)\"/>\n      <use xlink:href=\"#DejaVuSans-31\" transform=\"translate(526.5625 0)\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" transform=\"translate(590.185547 0)\"/>\n      <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(621.972656 0)\"/>\n     </g>\n    </g>\n   </g>\n  </g>\n </g>\n <defs>\n  <clipPath id=\"p206faf74ec\">\n   <rect x=\"45.478125\" 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=practice-md-6 fw=jax -->\n",
    "\n",
    "The unclipped run makes progress for a moment, then feeds back on itself:\n",
    "an oversized step lands the iterate somewhere steeper, the gradient there\n",
    "is larger, momentum compounds it, and the loss climbs through four orders\n",
    "of magnitude before overflowing within the first couple hundred steps. The\n",
    "clipped run, at the identical learning rate, trains to a final loss in the\n",
    "range of the tuned runs of that section. The Optax combinator does\n",
    "not report how often it fired; the instrumented PyTorch tab counts six\n",
    "interventions in two thousand steps, and the picture here is the same."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=practice-md-7 fw=all -->\n",
    "\n",
    "That count is the right mental model for clipping. On a healthy step the\n",
    "guard does nothing; on the rare step when a spike arrives out of the\n",
    "gradient distribution's tail, it is the difference between an incident and\n",
    "a log entry. The tail is not an accident of our small model: gradient noise\n",
    "in language models is measurably heavy-tailed, and under such noise clipped\n",
    "SGD provably converges where plain SGD can fail\n",
    "[@Zhang.Karimireddy.Veit.ea.2020]. The threshold should be set\n",
    "accordingly, above the typical norms of a healthy run, which you know\n",
    "because you logged them. A guard that fires on most steps is not guarding\n",
    "anything; it is a learning-rate cut in disguise, and the fix is\n",
    "to lower the learning rate or raise the threshold\n",
    "[@Godbole.Dahl.Gilmer.ea.2023]. For the Adam family the arithmetic\n",
    "differs but the conclusion holds. In steady state Adam's normalization\n",
    "keeps every coordinate's step near $\\eta$ (that section), transiently\n",
    "up to a few times that — $|\\hat{m}/\\sqrt{\\hat{v}}|$ can briefly reach\n",
    "$(1 - \\beta_1)/\\sqrt{1 - \\beta_2} \\approx 3$ at the defaults — so clipping\n",
    "is no substitute for lowering a too-large Adam learning rate, and in our\n",
    "runs it was not. What it\n",
    "still buys is protection for the estimates: one enormous gradient otherwise\n",
    "enters $\\mathbf{m}$ and $\\mathbf{v}$ and distorts the steps for the\n",
    "$\\sim 1/(1-\\beta_2)$ steps the averages take to forget it.\n",
    "\n",
    "### The Stability Kit at Scale\n",
    "\n",
    "At trillion-token scale, clipping is one item in a larger kit, most of it\n",
    "aimed at the places where transformer blow-ups concentrate: the attention\n",
    "logits and the output softmax. PaLM's training added a *z-loss*, a small\n",
    "penalty on $\\log^2 Z$ of the softmax normalizer, to keep the output logits\n",
    "from drifting large [@chowdhery2022palm]. *QK-norm* normalizes queries\n",
    "and keys immediately before their dot product, so attention logits cannot\n",
    "grow with the norms of what feeds them\n",
    "[@Henry.Dachapally.Pawar.ea.2020]; OLMo 2 adopted it as part of the\n",
    "stability overhaul that its report documents [@OLMo.2025]. MuonClip's\n",
    "*QK-clip* rescales the query and key projections whenever the largest\n",
    "attention logit crosses a cap, the addition that carried Kimi K2 through\n",
    "15.5T tokens without a loss spike (that section,\n",
    "[@Kimi.Team.2025]). And when prevention fails, the practice is\n",
    "unglamorous. The PaLM team, facing about twenty spikes in a run, rewound to\n",
    "a checkpoint a few hundred steps earlier and skipped the offending batches,\n",
    "after establishing that the same batches caused no spike when replayed from\n",
    "a different state: spikes came from state and data together, not from bad\n",
    "data alone [@chowdhery2022palm]. The OPT team published its training\n",
    "logbook along with the model; it records two months of restarts, mid-flight\n",
    "learning-rate cuts, and hardware failures in a way no polished paper does,\n",
    "and it remains the best public record of what babysitting a large run is\n",
    "actually like [@zhang2022opt].\n",
    "\n",
    "## Weight Averaging\n",
    "\n",
    "The chapter's third recurring decision, living with noise, has one more\n",
    "tool, and it is nearly free. A constant-rate iterate rattles around its\n",
    "noise ball (that section), and a schedule quenches the rattling by\n",
    "decaying the rate (that section). Averaging quenches it without\n",
    "touching the rate: the bounces roughly cancel in the mean, so an average of\n",
    "iterates sits near the center of the region the run is circling.\n",
    "Stochastic weight averaging made this a standard trick, averaging\n",
    "checkpoints from the tail of training and evaluating the average\n",
    "[@Izmailov.Podoprikhin.Garipov.ea.2018]. You have met the running form\n",
    "twice: that section put an exponential moving average of\n",
    "the weights into the standard recipe, and that section showed\n",
    "the machinery for maintaining one. It is\n",
    "$\\bar{\\mathbf{x}}_t = \\alpha\\, \\bar{\\mathbf{x}}_{t-1} + (1 - \\alpha)\\, \\mathbf{x}_t$:\n",
    "the same leaky average this chapter has applied to gradients (momentum) and\n",
    "to squared gradients (Adam), now applied to the weights themselves, purely\n",
    "for evaluation. The training run never sees it. What is new here is the\n",
    "noise-ball reading of *why* it helps.\n",
    "\n",
    "We test it on the schedule testbed of that section, reproduced\n",
    "verbatim, in the situation where that section's story began: the constant\n",
    "learning rate baseline, whose test accuracy stalled and jittered. Alongside\n",
    "the live weights we maintain an EMA with $\\alpha = 0.999$, an averaging\n",
    "window of about a thousand steps, roughly four epochs here."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "practice-weight-averaging-1"
   },
   "source": [
    "xavier = nnx.initializers.xavier_uniform()\n",
    "\n",
    "class Net(nnx.Module):\n",
    "    def __init__(self, rngs=None):\n",
    "        rngs = nnx.Rngs(0) if rngs is None else rngs\n",
    "        self.conv1 = nnx.Conv(1, 6, kernel_size=(5, 5), padding='same',\n",
    "                              kernel_init=xavier, rngs=rngs)\n",
    "        self.bn1 = nnx.BatchNorm(6, rngs=rngs)\n",
    "        self.conv2 = nnx.Conv(6, 16, kernel_size=(5, 5), padding='valid',\n",
    "                              kernel_init=xavier, rngs=rngs)\n",
    "        self.bn2 = nnx.BatchNorm(16, rngs=rngs)\n",
    "        self.fc1 = nnx.Linear(16 * 5 * 5, 120, kernel_init=xavier, rngs=rngs)\n",
    "        self.bn3 = nnx.BatchNorm(120, rngs=rngs)\n",
    "        self.fc2 = nnx.Linear(120, 84, kernel_init=xavier, rngs=rngs)\n",
    "        self.bn4 = nnx.BatchNorm(84, rngs=rngs)\n",
    "        self.fc3 = nnx.Linear(84, 10, kernel_init=xavier, rngs=rngs)\n",
    "\n",
    "    def __call__(self, x):\n",
    "        x = nnx.max_pool(nnx.relu(self.bn1(self.conv1(x))),\n",
    "                         window_shape=(2, 2), strides=(2, 2))\n",
    "        x = nnx.max_pool(nnx.relu(self.bn2(self.conv2(x))),\n",
    "                         window_shape=(2, 2), strides=(2, 2))\n",
    "        x = x.reshape((x.shape[0], -1))\n",
    "        x = nnx.relu(self.bn3(self.fc1(x)))\n",
    "        x = nnx.relu(self.bn4(self.fc2(x)))\n",
    "        return self.fc3(x)\n",
    "\n",
    "fashion = d2l.FashionMNIST(batch_size=256)\n",
    "train_iter = fashion.get_dataloader(train=True)\n",
    "test_iter = fashion.get_dataloader(train=False)"
   ],
   "execution_count": 6,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=practice-md-8 fw=jax -->\n",
    "\n",
    "The EMA update is one `tree.map` over the parameters and BatchNorm\n",
    "statistics of the two models. Averaging the running statistics keeps the\n",
    "averaged model approximately consistent — an EMA of running statistics is\n",
    "not the exact statistic of the EMA weights; the clean alternative is to\n",
    "recompute them for the averaged weights, as the checkpoint-averaging note\n",
    "below prescribes."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "practice-weight-averaging-2"
   },
   "source": [
    "lr, num_epochs = 0.3, 15\n",
    "model = Net(rngs=nnx.Rngs(0))\n",
    "ema = nnx.clone(model)\n",
    "optimizer = nnx.Optimizer(model, optax.sgd(lr), wrt=nnx.Param)\n",
    "\n",
    "@nnx.jit\n",
    "def train_step(model, optimizer, X, y):\n",
    "    def loss_fn(model):\n",
    "        return optax.softmax_cross_entropy_with_integer_labels(\n",
    "            model(X), y).mean()\n",
    "    l, grads = nnx.value_and_grad(loss_fn)(model)\n",
    "    optimizer.update(model, grads)\n",
    "    return l\n",
    "\n",
    "@nnx.jit\n",
    "def ema_step(ema, model, decay=0.999):\n",
    "    kinds = (nnx.Param, nnx.BatchStat)  # skip any non-float state\n",
    "    new = jax.tree.map(lambda e, p: decay * e + (1 - decay) * p,\n",
    "                       nnx.state(ema, kinds), nnx.state(model, kinds))\n",
    "    nnx.update(ema, new)\n",
    "\n",
    "@nnx.jit\n",
    "def count_correct(model, X, y):\n",
    "    return (model(X).argmax(axis=1) == y).sum()\n",
    "\n",
    "def accuracy(model, data_iter):\n",
    "    model.eval()  # use running averages in the BatchNorm layers\n",
    "    correct = total = 0\n",
    "    for X, y in data_iter:\n",
    "        correct += int(count_correct(model, jnp.asarray(X), jnp.asarray(y)))\n",
    "        total += y.shape[0]\n",
    "    return correct / total\n",
    "\n",
    "live_acc, avg_acc = [], []\n",
    "for epoch in range(num_epochs):\n",
    "    model.train()\n",
    "    for X, y in train_iter:\n",
    "        train_step(model, optimizer, jnp.asarray(X), jnp.asarray(y))\n",
    "        ema_step(ema, model)\n",
    "    live_acc.append(accuracy(model, test_iter))\n",
    "    avg_acc.append(accuracy(ema, test_iter))\n",
    "print(f'final test accuracy: live {live_acc[-1]:.3f}, '\n",
    "      f'EMA {avg_acc[-1]:.3f}')"
   ],
   "execution_count": 7,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "final test accuracy: live 0.889, EMA 0.911\n"
     ]
    }
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "practice-weight-averaging-3"
   },
   "source": [
    "d2l.plot(list(range(1, num_epochs + 1)), [live_acc, avg_acc], 'epoch',\n",
    "         'test accuracy', legend=['live weights', 'EMA of weights'])"
   ],
   "execution_count": 8,
   "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=\"246.28125pt\" height=\"183.35625pt\" viewBox=\"0 0 246.28125 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-19T15:07:57.296579</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 246.28125 183.35625 \nL 246.28125 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 43.78125 145.8 \nL 239.08125 145.8 \nL 239.08125 7.2 \nL 43.78125 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 103.385795 145.8 \nL 103.385795 7.2 \n\" clip-path=\"url(#p6e3fd2f36d)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_2\">\n      <defs>\n       <path id=\"mc74c624927\" d=\"M 0 0 \nL 0 3.5 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#mc74c624927\" x=\"103.385795\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_1\">\n      <!-- 5 -->\n      <g transform=\"translate(100.204545 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_2\">\n     <g id=\"line2d_3\">\n      <path d=\"M 166.794886 145.8 \nL 166.794886 7.2 \n\" clip-path=\"url(#p6e3fd2f36d)\" 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=\"#mc74c624927\" x=\"166.794886\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_2\">\n      <!-- 10 -->\n      <g transform=\"translate(160.432386 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        <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-31\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_3\">\n     <g id=\"line2d_5\">\n      <path d=\"M 230.203977 145.8 \nL 230.203977 7.2 \n\" clip-path=\"url(#p6e3fd2f36d)\" 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=\"#mc74c624927\" x=\"230.203977\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_3\">\n      <!-- 15 -->\n      <g transform=\"translate(223.841477 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=\"text_4\">\n     <!-- epoch -->\n     <g transform=\"translate(126.203125 174.076563) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-65\" d=\"M 3597 1894 \nL 3597 1613 \nL 953 1613 \nQ 991 1019 1311 708 \nQ 1631 397 2203 397 \nQ 2534 397 2845 478 \nQ 3156 559 3463 722 \nL 3463 178 \nQ 3153 47 2828 -22 \nQ 2503 -91 2169 -91 \nQ 1331 -91 842 396 \nQ 353 884 353 1716 \nQ 353 2575 817 3079 \nQ 1281 3584 2069 3584 \nQ 2775 3584 3186 3129 \nQ 3597 2675 3597 1894 \nz\nM 3022 2063 \nQ 3016 2534 2758 2815 \nQ 2500 3097 2075 3097 \nQ 1594 3097 1305 2825 \nQ 1016 2553 972 2059 \nL 3022 2063 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-70\" d=\"M 1159 525 \nL 1159 -1331 \nL 581 -1331 \nL 581 3500 \nL 1159 3500 \nL 1159 2969 \nQ 1341 3281 1617 3432 \nQ 1894 3584 2278 3584 \nQ 2916 3584 3314 3078 \nQ 3713 2572 3713 1747 \nQ 3713 922 3314 415 \nQ 2916 -91 2278 -91 \nQ 1894 -91 1617 61 \nQ 1341 213 1159 525 \nz\nM 3116 1747 \nQ 3116 2381 2855 2742 \nQ 2594 3103 2138 3103 \nQ 1681 3103 1420 2742 \nQ 1159 2381 1159 1747 \nQ 1159 1113 1420 752 \nQ 1681 391 2138 391 \nQ 2594 391 2855 752 \nQ 3116 1113 3116 1747 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-6f\" d=\"M 1959 3097 \nQ 1497 3097 1228 2736 \nQ 959 2375 959 1747 \nQ 959 1119 1226 758 \nQ 1494 397 1959 397 \nQ 2419 397 2687 759 \nQ 2956 1122 2956 1747 \nQ 2956 2369 2687 2733 \nQ 2419 3097 1959 3097 \nz\nM 1959 3584 \nQ 2709 3584 3137 3096 \nQ 3566 2609 3566 1747 \nQ 3566 888 3137 398 \nQ 2709 -91 1959 -91 \nQ 1206 -91 779 398 \nQ 353 888 353 1747 \nQ 353 2609 779 3096 \nQ 1206 3584 1959 3584 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-63\" d=\"M 3122 3366 \nL 3122 2828 \nQ 2878 2963 2633 3030 \nQ 2388 3097 2138 3097 \nQ 1578 3097 1268 2742 \nQ 959 2388 959 1747 \nQ 959 1106 1268 751 \nQ 1578 397 2138 397 \nQ 2388 397 2633 464 \nQ 2878 531 3122 666 \nL 3122 134 \nQ 2881 22 2623 -34 \nQ 2366 -91 2075 -91 \nQ 1284 -91 818 406 \nQ 353 903 353 1747 \nQ 353 2603 823 3093 \nQ 1294 3584 2113 3584 \nQ 2378 3584 2631 3529 \nQ 2884 3475 3122 3366 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-68\" d=\"M 3513 2113 \nL 3513 0 \nL 2938 0 \nL 2938 2094 \nQ 2938 2591 2744 2837 \nQ 2550 3084 2163 3084 \nQ 1697 3084 1428 2787 \nQ 1159 2491 1159 1978 \nL 1159 0 \nL 581 0 \nL 581 4863 \nL 1159 4863 \nL 1159 2956 \nQ 1366 3272 1645 3428 \nQ 1925 3584 2291 3584 \nQ 2894 3584 3203 3211 \nQ 3513 2838 3513 2113 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-65\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(61.523438 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6f\" transform=\"translate(125 0)\"/>\n      <use xlink:href=\"#DejaVuSans-63\" transform=\"translate(186.181641 0)\"/>\n      <use xlink:href=\"#DejaVuSans-68\" transform=\"translate(241.162109 0)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"matplotlib.axis_2\">\n    <g id=\"ytick_1\">\n     <g id=\"line2d_7\">\n      <path d=\"M 43.78125 140.752485 \nL 239.08125 140.752485 \n\" clip-path=\"url(#p6e3fd2f36d)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_8\">\n      <defs>\n       <path id=\"ma5612ca8c6\" d=\"M 0 0 \nL -3.5 0 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#ma5612ca8c6\" x=\"43.78125\" y=\"140.752485\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_5\">\n      <!-- 0.2 -->\n      <g transform=\"translate(20.878125 144.551704) scale(0.1 -0.1)\">\n       <defs>\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-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-30\"/>\n       <use xlink:href=\"#DejaVuSans-2e\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-32\" transform=\"translate(95.410156 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_2\">\n     <g id=\"line2d_9\">\n      <path d=\"M 43.78125 104.967197 \nL 239.08125 104.967197 \n\" clip-path=\"url(#p6e3fd2f36d)\" 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=\"#ma5612ca8c6\" x=\"43.78125\" y=\"104.967197\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_6\">\n      <!-- 0.4 -->\n      <g transform=\"translate(20.878125 108.766416) 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-34\" transform=\"translate(95.410156 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_3\">\n     <g id=\"line2d_11\">\n      <path d=\"M 43.78125 69.181909 \nL 239.08125 69.181909 \n\" clip-path=\"url(#p6e3fd2f36d)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_12\">\n      <g>\n       <use xlink:href=\"#ma5612ca8c6\" x=\"43.78125\" y=\"69.181909\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_7\">\n      <!-- 0.6 -->\n      <g transform=\"translate(20.878125 72.981127) 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-30\"/>\n       <use xlink:href=\"#DejaVuSans-2e\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-36\" transform=\"translate(95.410156 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_4\">\n     <g id=\"line2d_13\">\n      <path d=\"M 43.78125 33.39662 \nL 239.08125 33.39662 \n\" clip-path=\"url(#p6e3fd2f36d)\" 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=\"#ma5612ca8c6\" x=\"43.78125\" y=\"33.39662\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_8\">\n      <!-- 0.8 -->\n      <g transform=\"translate(20.878125 37.195839) 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-38\" transform=\"translate(95.410156 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"text_9\">\n     <!-- test accuracy -->\n     <g transform=\"translate(14.798438 110.25) rotate(-90) 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-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-20\" 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-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-79\" d=\"M 2059 -325 \nQ 1816 -950 1584 -1140 \nQ 1353 -1331 966 -1331 \nL 506 -1331 \nL 506 -850 \nL 844 -850 \nQ 1081 -850 1212 -737 \nQ 1344 -625 1503 -206 \nL 1606 56 \nL 191 3500 \nL 800 3500 \nL 1894 763 \nL 2988 3500 \nL 3597 3500 \nL 2059 -325 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-74\"/>\n      <use xlink:href=\"#DejaVuSans-65\" transform=\"translate(39.208984 0)\"/>\n      <use xlink:href=\"#DejaVuSans-73\" transform=\"translate(100.732422 0)\"/>\n      <use xlink:href=\"#DejaVuSans-74\" transform=\"translate(152.832031 0)\"/>\n      <use xlink:href=\"#DejaVuSans-20\" transform=\"translate(192.041016 0)\"/>\n      <use xlink:href=\"#DejaVuSans-61\" transform=\"translate(223.828125 0)\"/>\n      <use xlink:href=\"#DejaVuSans-63\" transform=\"translate(285.107422 0)\"/>\n      <use xlink:href=\"#DejaVuSans-63\" transform=\"translate(340.087891 0)\"/>\n      <use xlink:href=\"#DejaVuSans-75\" transform=\"translate(395.068359 0)\"/>\n      <use xlink:href=\"#DejaVuSans-72\" transform=\"translate(458.447266 0)\"/>\n      <use xlink:href=\"#DejaVuSans-61\" transform=\"translate(499.560547 0)\"/>\n      <use xlink:href=\"#DejaVuSans-63\" transform=\"translate(560.839844 0)\"/>\n      <use xlink:href=\"#DejaVuSans-79\" transform=\"translate(615.820312 0)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"line2d_15\">\n    <path d=\"M 52.658523 29.549702 \nL 65.340341 19.11829 \nL 78.022159 20.281312 \nL 90.703977 20.352883 \nL 103.385795 17.507952 \nL 116.067614 15.002982 \nL 128.749432 25.845924 \nL 141.43125 18.939364 \nL 154.113068 17.937376 \nL 166.794886 18.474155 \nL 179.476705 15.235586 \nL 192.158523 16.95328 \nL 204.840341 15.647117 \nL 217.522159 23.197813 \nL 230.203977 17.454274 \n\" clip-path=\"url(#p6e3fd2f36d)\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_16\">\n    <path d=\"M 52.658523 139.5 \nL 65.340341 104.770378 \nL 78.022159 87.146123 \nL 90.703977 75.784294 \nL 103.385795 61.828032 \nL 116.067614 46.941352 \nL 128.749432 33.39662 \nL 141.43125 24.790258 \nL 154.113068 20.012922 \nL 166.794886 17.060636 \nL 179.476705 15.557654 \nL 192.158523 14.609344 \nL 204.840341 14.054672 \nL 217.522159 13.893638 \nL 230.203977 13.5 \n\" clip-path=\"url(#p6e3fd2f36d)\" style=\"fill: none; stroke-dasharray: 5.55,2.4; stroke-dashoffset: 0; stroke: #bf00bf; stroke-width: 1.5\"/>\n   </g>\n   <g id=\"patch_3\">\n    <path d=\"M 43.78125 145.8 \nL 43.78125 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 239.08125 145.8 \nL 239.08125 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 43.78125 145.8 \nL 239.08125 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 43.78125 7.2 \nL 239.08125 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 123.373438 140.8 \nL 232.08125 140.8 \nQ 234.08125 140.8 234.08125 138.8 \nL 234.08125 110.44375 \nQ 234.08125 108.44375 232.08125 108.44375 \nL 123.373438 108.44375 \nQ 121.373438 108.44375 121.373438 110.44375 \nL 121.373438 138.8 \nQ 121.373438 140.8 123.373438 140.8 \nz\n\" style=\"fill: #ffffff; opacity: 0.8; stroke: #cccccc; stroke-linejoin: miter\"/>\n    </g>\n    <g id=\"line2d_17\">\n     <path d=\"M 125.373438 116.542188 \nL 135.373438 116.542188 \nL 145.373438 116.542188 \n\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n    </g>\n    <g id=\"text_10\">\n     <!-- live weights -->\n     <g transform=\"translate(153.373438 120.042188) scale(0.1 -0.1)\">\n      <defs>\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-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-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       <path id=\"DejaVuSans-77\" d=\"M 269 3500 \nL 844 3500 \nL 1563 769 \nL 2278 3500 \nL 2956 3500 \nL 3675 769 \nL 4391 3500 \nL 4966 3500 \nL 4050 0 \nL 3372 0 \nL 2619 2869 \nL 1863 0 \nL 1184 0 \nL 269 3500 \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-6c\"/>\n      <use xlink:href=\"#DejaVuSans-69\" transform=\"translate(27.783203 0)\"/>\n      <use xlink:href=\"#DejaVuSans-76\" transform=\"translate(55.566406 0)\"/>\n      <use xlink:href=\"#DejaVuSans-65\" transform=\"translate(114.746094 0)\"/>\n      <use xlink:href=\"#DejaVuSans-20\" transform=\"translate(176.269531 0)\"/>\n      <use xlink:href=\"#DejaVuSans-77\" transform=\"translate(208.056641 0)\"/>\n      <use xlink:href=\"#DejaVuSans-65\" transform=\"translate(289.84375 0)\"/>\n      <use xlink:href=\"#DejaVuSans-69\" transform=\"translate(351.367188 0)\"/>\n      <use xlink:href=\"#DejaVuSans-67\" transform=\"translate(379.150391 0)\"/>\n      <use xlink:href=\"#DejaVuSans-68\" transform=\"translate(442.626953 0)\"/>\n      <use xlink:href=\"#DejaVuSans-74\" transform=\"translate(506.005859 0)\"/>\n      <use xlink:href=\"#DejaVuSans-73\" transform=\"translate(545.214844 0)\"/>\n     </g>\n    </g>\n    <g id=\"line2d_18\">\n     <path d=\"M 125.373438 131.220313 \nL 135.373438 131.220313 \nL 145.373438 131.220313 \n\" style=\"fill: none; stroke-dasharray: 5.55,2.4; stroke-dashoffset: 0; stroke: #bf00bf; stroke-width: 1.5\"/>\n    </g>\n    <g id=\"text_11\">\n     <!-- EMA of weights -->\n     <g transform=\"translate(153.373438 134.720313) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-45\" d=\"M 628 4666 \nL 3578 4666 \nL 3578 4134 \nL 1259 4134 \nL 1259 2753 \nL 3481 2753 \nL 3481 2222 \nL 1259 2222 \nL 1259 531 \nL 3634 531 \nL 3634 0 \nL 628 0 \nL 628 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-4d\" d=\"M 628 4666 \nL 1569 4666 \nL 2759 1491 \nL 3956 4666 \nL 4897 4666 \nL 4897 0 \nL 4281 0 \nL 4281 4097 \nL 3078 897 \nL 2444 897 \nL 1241 4097 \nL 1241 0 \nL 628 0 \nL 628 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-41\" d=\"M 2188 4044 \nL 1331 1722 \nL 3047 1722 \nL 2188 4044 \nz\nM 1831 4666 \nL 2547 4666 \nL 4325 0 \nL 3669 0 \nL 3244 1197 \nL 1141 1197 \nL 716 0 \nL 50 0 \nL 1831 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-66\" d=\"M 2375 4863 \nL 2375 4384 \nL 1825 4384 \nQ 1516 4384 1395 4259 \nQ 1275 4134 1275 3809 \nL 1275 3500 \nL 2222 3500 \nL 2222 3053 \nL 1275 3053 \nL 1275 0 \nL 697 0 \nL 697 3053 \nL 147 3053 \nL 147 3500 \nL 697 3500 \nL 697 3744 \nQ 697 4328 969 4595 \nQ 1241 4863 1831 4863 \nL 2375 4863 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-45\"/>\n      <use xlink:href=\"#DejaVuSans-4d\" transform=\"translate(63.183594 0)\"/>\n      <use xlink:href=\"#DejaVuSans-41\" transform=\"translate(149.462891 0)\"/>\n      <use xlink:href=\"#DejaVuSans-20\" transform=\"translate(217.871094 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6f\" transform=\"translate(249.658203 0)\"/>\n      <use xlink:href=\"#DejaVuSans-66\" transform=\"translate(310.839844 0)\"/>\n      <use xlink:href=\"#DejaVuSans-20\" transform=\"translate(346.044922 0)\"/>\n      <use xlink:href=\"#DejaVuSans-77\" transform=\"translate(377.832031 0)\"/>\n      <use xlink:href=\"#DejaVuSans-65\" transform=\"translate(459.619141 0)\"/>\n      <use xlink:href=\"#DejaVuSans-69\" transform=\"translate(521.142578 0)\"/>\n      <use xlink:href=\"#DejaVuSans-67\" transform=\"translate(548.925781 0)\"/>\n      <use xlink:href=\"#DejaVuSans-68\" transform=\"translate(612.402344 0)\"/>\n      <use xlink:href=\"#DejaVuSans-74\" transform=\"translate(675.78125 0)\"/>\n      <use xlink:href=\"#DejaVuSans-73\" transform=\"translate(714.990234 0)\"/>\n     </g>\n    </g>\n   </g>\n  </g>\n </g>\n <defs>\n  <clipPath id=\"p6e3fd2f36d\">\n   <rect x=\"43.78125\" 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=practice-md-9 fw=all -->\n",
    "\n",
    "Two curves, two lessons. First, the EMA needs its window to fill before it\n",
    "is worth anything: for the first several epochs it trails badly, because\n",
    "the average still carries heavy weight on stale, near-initialization\n",
    "iterates. This is lag, not the zero-initialization deficit that Adam's bias\n",
    "correction removes (that section): the average starts at the initial\n",
    "weights, so its coefficients already sum to one and there is nothing to\n",
    "correct — the early estimates are simply dominated by old iterates. That\n",
    "lag is why\n",
    "practical EMAs either warm up the decay or start averaging late. Second,\n",
    "once the window has filled, the averaged model is better and much steadier.\n",
    "In our runs the EMA ends half a point to a point above the live weights,\n",
    "and the steadiness matters more than the size: the live curve gambles a few\n",
    "points of accuracy on when you happen to stop, and the average does not.\n",
    "The averaged model\n",
    "at a constant rate lands in the range the decayed schedules of\n",
    "that section reached, which is the schedule-free observation of\n",
    "that section from the other side: decay and averaging are two ways to\n",
    "quench the same noise. That also predicts a limit. Add the same\n",
    "EMA to a run whose schedule already decayed well, and on a model this size\n",
    "the remaining nudge is too small to call from a single run. The technique\n",
    "earns its keep where the decayed endpoint is expensive to reach, or where\n",
    "no decay is coming. The cost is one extra copy of the parameters, cheap by\n",
    "the accounting of that section. One footnote for checkpoint\n",
    "averaging: if you average a few saved checkpoints instead of keeping a\n",
    "running EMA, the BatchNorm statistics belong to none of the averaged\n",
    "weights, so recompute them with a pass over the training data before\n",
    "trusting the result.\n",
    "\n",
    "### Averaging at Scale\n",
    "\n",
    "At scale the trick wears several uniforms. Averaging the latest $k$\n",
    "checkpoints of an LLM run, uniformly rather than exponentially, buys a\n",
    "consistent mid-training speedup [@Kaddour.2022], and the top row of\n",
    "the table does it in production: the Llama 3 model\n",
    "that shipped is the average of checkpoints from its final annealing phase\n",
    "[@Grattafiori.Dubey.Jauhri.ea.2024]. Model soups push the idea past a single run,\n",
    "averaging separately fine-tuned models into one, with accuracy gains at no\n",
    "inference cost [@Wortsman.Ilharco.Gadre.ea.2022]. And in one model\n",
    "family averaging is not an optimization but a requirement: diffusion models\n",
    "(that section) are evaluated on EMA weights essentially always,\n",
    "and sample quality depends on the averaging window strongly enough that\n",
    "@Karras.Aittala.Lehtinen.ea.2024 built machinery to reconstruct the\n",
    "EMA at any window *after* training, just to be able to tune it. A plausible\n",
    "reading of the asymmetry: a classifier's accuracy is one forward pass and\n",
    "plateaus, while a diffusion sampler applies the network hundreds of times\n",
    "in sequence, so weight noise that a single pass shrugs off compounds across\n",
    "the trajectory.\n",
    "\n",
    "## How to Tune\n",
    "\n",
    "Everything in this chapter was tuned somehow, and the somehow deserves to\n",
    "be stated as method rather than left as folklore. The clearest statement of\n",
    "the method practitioners actually use is Google's Tuning Playbook\n",
    "[@Godbole.Dahl.Gilmer.ea.2023], and its central move is a vocabulary.\n",
    "In any experiment, split the hyperparameters into three classes:\n",
    "*scientific* hyperparameters, the ones your question is about; *nuisance*\n",
    "hyperparameters, which must be re-optimized for every setting of the\n",
    "scientific ones before the comparison means anything; and *fixed*\n",
    "hyperparameters, held constant and acknowledged as limits on the claim. The\n",
    "playbook's rule is that a comparison is a statement about scientific\n",
    "hyperparameters only after the nuisances have been re-tuned per arm, and\n",
    "its most common instance is also the most common failure in published\n",
    "comparisons: the learning rate is almost always a nuisance, and a\n",
    "comparison at one shared learning rate is a comparison of nothing.\n",
    "\n",
    "This vocabulary names what the chapter has been doing since\n",
    "that section. In each race, the optimizer was the scientific\n",
    "hyperparameter; the learning rate was the nuisance, re-tuned per contestant\n",
    "on a four-point grid spaced by factors of about three; steps, batch size,\n",
    "initialization, and the absence of a schedule were fixed and stated, which\n",
    "is why every conclusion was phrased as conditional on that protocol. The\n",
    "design scales up without changing shape: more nuisance dimensions (peak\n",
    "rate, decay horizon, warmup, weight decay), quasi-random search once a grid\n",
    "is too coarse, exploration before a final exploitation sweep. What does not\n",
    "survive is skipping the re-tune. @Schmidt.Schneider.Hennig.2021\n",
    "benchmarked fifteen optimizers across many problems and found that trying\n",
    "several optimizers at default settings works about as well as extensively\n",
    "tuning a single one. Read it as consolation or as warning: defaults encode\n",
    "a lot of accumulated tuning, and an untuned comparison measures effort, not\n",
    "algorithms. that section's deflation of headline speedups is the same\n",
    "lesson with larger budgets.\n",
    "\n",
    "Budgets shape the rest. With a handful of runs, take the consensus column\n",
    "of the table as given and spend every run on the peak\n",
    "learning rate. With tens of runs, add weight decay and the schedule, and\n",
    "sweep jointly only what is genuinely coupled: $\\eta$ and $\\lambda$ act\n",
    "through their product (that section), so their joint grid is really\n",
    "a ridge plus one cross-direction. Past that, you are running a study, and\n",
    "the playbook is the reference. On the models of this chapter a run cost\n",
    "seconds to minutes, which is why we could afford the middle tier\n",
    "everywhere; the entire point of that section was to keep that\n",
    "affordability relevant as models grow.\n",
    "\n",
    "Finally, the log. A result you cannot reproduce is a rumor, so record, for\n",
    "every run, the full configuration including the hyperparameters you\n",
    "consider fixed, the code version, the seed, and the one thing you changed,\n",
    "and change one thing at a time. Keep the diverged runs: the NaN edge of a\n",
    "sweep marks the stability boundary, and that section read its NaN\n",
    "column as data, not as failure. Write the conclusion next to the curves\n",
    "while you still believe it, because a directory of loss curves with no\n",
    "sentences attached goes stale within weeks. Assignments in the CS336 mold\n",
    "now grade the experiment log alongside the final loss, and that is the\n",
    "right emphasis: the log is the experiment.\n",
    "\n",
    "## What We Did Not Teach\n",
    "\n",
    "Three method families were left out on purpose. Sharpness-aware\n",
    "minimization takes an inner ascent step before each descent step to seek\n",
    "the flat minima whose connection to generalization\n",
    "that section discussed; it doubles the gradient cost,\n",
    "and its dependable wins are in vision and fine-tuning rather than\n",
    "large-scale pretraining [@Foret.Kleiner.Mobahi.ea.2021]. Variance-reduction methods of the\n",
    "SVRG family own an elegant theory for finite sums that has never paid its\n",
    "way on deep networks; the theory, and the honest post-mortem, live in\n",
    "that section. LARS and LAMB, the layerwise-adaptive\n",
    "methods once synonymous with large-batch training, are superseded: with\n",
    "nuisance hyperparameters re-tuned, exactly the rule of the previous\n",
    "section, standard momentum and AdamW match them at the batch sizes they\n",
    "were designed for [@Nado.Gilmer.Shallue.ea.2021].\n",
    "\n",
    "What remains is not optimization but placement. This chapter priced the\n",
    "optimizer's state (that section) and always kept it on one device.\n",
    "Spreading gradients and state across a data-parallel group, ZeRO-style\n",
    "sharding, and the overlap of communication with computation belong to\n",
    "that section and the training-systems material of\n",
    "that section. The updates computed there are the ones\n",
    "derived here; the systems side decides where the bytes live and when they\n",
    "move.\n",
    "\n",
    "## Summary\n",
    "\n",
    "Disclosed practice at the frontier is a small, stable recipe: AdamW with\n",
    "$(0.9, 0.95)$, weight decay 0.1 with embeddings and norms exempt, global\n",
    "gradient clipping at 1, brief warmup into cosine or warmup--stable--decay,\n",
    "and an early batch ramp, with the Muon split as the one production\n",
    "challenger inside the same frame. Clipping is cheap insurance against a\n",
    "heavy gradient tail: healthy runs barely trigger it, and when it triggers\n",
    "on most steps it has become a learning-rate cut. Beyond it, large runs\n",
    "carry a stability kit (z-loss, QK-norm, QK-clip) and a rewind-and-skip\n",
    "playbook for the spikes that get through. Weight averaging quenches\n",
    "noise without touching the learning rate; it is a point of accuracy and a\n",
    "steadier endpoint on our testbed, checkpoint averaging in production LLMs,\n",
    "and mandatory equipment for diffusion. Tuning is a protocol, not a talent:\n",
    "scientific, nuisance, and fixed hyperparameters, nuisances re-tuned per\n",
    "arm, budgets spent on the learning rate first, and a log that makes every\n",
    "run reproducible.\n",
    "\n",
    "This section closes the chapter, so step back once. Every method in it was\n",
    "a way of making three decisions: which direction to move, which is a choice\n",
    "of norm; how far to move as training proceeds, which is a schedule; and how\n",
    "to live with sampled noise, which is batching, momentum, and averaging. The\n",
    "recipe table is one coordinated setting of all three, tested at an expense\n",
    "no benchmark will ever match. When a run of your own\n",
    "misbehaves, the useful first question is which of the three decisions is\n",
    "failing. Optimizers will keep changing; the decomposition has been stable\n",
    "for decades, and it is what this chapter was actually about.\n",
    "\n",
    "## Exercises\n",
    "\n",
    "1. Reproduce the tuning protocol with a budget. Using AdamW on `TinyLM`,\n",
    "   reach a training loss of 1.1 in as few steps as you can, tuning only the\n",
    "   learning rate, with a budget of ten runs. Keep a log: for every run,\n",
    "   record the learning rate, the steps taken, the final loss, and the\n",
    "   one-sentence conclusion you drew before launching the next run. Report\n",
    "   the log, not just the winning configuration.\n",
    "1. Change the batch size of the clipping demo's model from 64 to 256 and\n",
    "   repeat the four-point learning-rate sweep of that section for\n",
    "   AdamW. Where does the optimum move, and how does the shift compare with\n",
    "   the square-root and linear scaling rules of that section?\n",
    "   What would you have concluded had you reused the batch-64 learning rate\n",
    "   without re-tuning?\n",
    "1. Sweep the clipping threshold $\\theta \\in \\{0.01, 0.1, 0.5, 1, 4,\n",
    "   \\infty\\}$ in the demonstration above, recording the final loss and the\n",
    "   fraction of steps on which clipping fired. Identify the three regimes:\n",
    "   guard (fires rarely), brake (fires constantly), and absent. Show that\n",
    "   when clipping fires on every step, the update of\n",
    "   the equation is normalized gradient descent with step size\n",
    "   $\\eta\\theta$, and explain why that is not the same as training at a\n",
    "   lower learning rate.\n",
    "1. The decay timescale of that section is $\\tau = B/(\\eta \\lambda D)$\n",
    "   epochs for batch size $B$, dataset size $D$ (both in tokens), learning\n",
    "   rate $\\eta$, and weight decay $\\lambda$. Compute $\\tau$ for the\n",
    "   DeepSeek-V3 and OLMo 2 rows of the table at their\n",
    "   peak learning rates. What fraction of each dataset does the averaging\n",
    "   horizon span? Following @Bergsma.Dey.Gosal.ea.2025b, what should\n",
    "   happen to $\\lambda$ if the batch size were doubled and $\\tau$ is to be\n",
    "   preserved?\n",
    "1. Sweep the EMA decay $\\alpha \\in \\{0.9, 0.99, 0.999, 0.9999\\}$ in the\n",
    "   weight-averaging demonstration. Relate the averaging window\n",
    "   $1/(1-\\alpha)$ to what you observe at both ends: when does the EMA track\n",
    "   the live weights too closely to help, and when does it average in\n",
    "   iterates too old to be useful within the 15-epoch budget?\n",
    "1. Test the finding of @Schmidt.Schneider.Hennig.2021 on this\n",
    "   chapter's testbed: run SGD with momentum, Adam, and AdamW at their\n",
    "   framework default settings on `TinyLM` for 2,000 steps, and compare the\n",
    "   best of the three against the grid-tuned Adam of that section.\n",
    "   Given a fixed budget of four runs, which strategy would you choose on\n",
    "   this problem, and what does the answer depend on?"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "d2l-jax",
   "display_name": "d2l (JAX)",
   "language": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
