{
 "cells": [
  {
   "id": "191eee6f",
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# Notebook pipeline demo\n",
    "\n",
    "A character-level bigram language model, small enough to run in seconds. It exists\n",
    "to prove the publishing path: prose, code, printed output, a pandas table,\n",
    "theme-aware charts, math, an aside and tagged cells should all render as a native\n",
    "Mementropy page.\n",
    "\n",
    "## Prerequisites and support\n",
    "\n",
    "The saved environment reports Python 3.12.12. For the public download, use an isolated Python environment with JupyterLab and NumPy, pandas, Matplotlib, IPython. These are package prerequisites, not a locked or newly tested environment. The Python examples generate their own teaching data and do not require cloud credentials or a pretrained model.\n",
    "\n",
    "The public download omits the site-only `mementropy.mplstyle` setup cell; use Matplotlib's default style. Before running chart cells, import `matplotlib.pyplot as plt` in your Jupyter kernel: that import was part of the omitted site-style cell. No style file or environment lock is bundled. If a rerun differs, record package versions and compare to the saved outputs rather than treating them as a guaranteed result.\n",
    "\n",
    "**Evidence:** these are saved outputs from an earlier execution, not rerun for the 2026-10-29 editorial update. Rendering displays saved cells; it does not execute them. The public download is not a locked environment or a production validation.\n"
   ]
  },
  {
   "id": "d318b3ad",
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## Corpus\n",
    "\n",
    "The training text is three sentences. A real lab would load a dataset; the point\n",
    "here is the shape of the output.\n",
    "\n",
    ":::note[Source of truth]\n",
    "This page is generated from a Jupyter notebook written in `hungersync-enterprise/notebooks/`\n",
    "and copied to `mementropy/notebooks/` when finished. Edit the notebook, never the page.\n",
    ":::"
   ]
  },
  {
   "id": "62dddcf0",
   "metadata": {},
   "cell_type": "code",
   "execution_count": 2,
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "corpus = (\n",
    "    \"information survives transmission when the receiver can rebuild it. \"\n",
    "    \"knowledge decays when nobody rehearses it. \"\n",
    "    \"a model learns the statistics of the text it is shown.\"\n",
    ")\n",
    "chars = sorted(set(corpus))\n",
    "stoi = {c: i for i, c in enumerate(chars)}\n",
    "itos = {i: c for c, i in stoi.items()}\n",
    "data = np.array([stoi[c] for c in corpus])\n",
    "V = len(chars)\n",
    "\n",
    "print(f\"characters: {len(corpus)}\")\n",
    "print(f\"vocabulary: {V} symbols -> {''.join(chars)!r}\")"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "characters: 165\n",
      "vocabulary: 24 symbols -> ' .abcdefghiklmnorstuvwxy'\n"
     ]
    }
   ]
  },
  {
   "id": "afff5d5a",
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## Bigram counts\n",
    "\n",
    "Count how often each character follows another. This is a pandas DataFrame; the\n",
    "page applies the site's table design instead of pandas' own styling."
   ]
  },
  {
   "id": "6e22c803",
   "metadata": {},
   "cell_type": "code",
   "execution_count": 3,
   "source": [
    "counts = np.zeros((V, V), dtype=int)\n",
    "for a, b in zip(data[:-1], data[1:]):\n",
    "    counts[a, b] += 1\n",
    "\n",
    "show = lambda c: \"space\" if c == \" \" else c\n",
    "pairs = (\n",
    "    pd.DataFrame(\n",
    "        [(show(itos[a]), show(itos[b]), counts[a, b])\n",
    "         for a in range(V) for b in range(V) if counts[a, b]],\n",
    "        columns=[\"current\", \"next\", \"count\"],\n",
    "    )\n",
    "    .assign(share=lambda d: (d[\"count\"] / d[\"count\"].sum()).round(3))\n",
    "    .sort_values(\"count\", ascending=False, kind=\"stable\")\n",
    "    .head(8)\n",
    "    .reset_index(drop=True)\n",
    ")\n",
    "pairs"
   ],
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>current</th>\n",
       "      <th>next</th>\n",
       "      <th>count</th>\n",
       "      <th>share</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>h</td>\n",
       "      <td>e</td>\n",
       "      <td>6</td>\n",
       "      <td>0.037</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>s</td>\n",
       "      <td>space</td>\n",
       "      <td>6</td>\n",
       "      <td>0.037</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>space</td>\n",
       "      <td>t</td>\n",
       "      <td>5</td>\n",
       "      <td>0.030</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>n</td>\n",
       "      <td>space</td>\n",
       "      <td>5</td>\n",
       "      <td>0.030</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>space</td>\n",
       "      <td>i</td>\n",
       "      <td>4</td>\n",
       "      <td>0.024</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>e</td>\n",
       "      <td>space</td>\n",
       "      <td>4</td>\n",
       "      <td>0.024</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>space</td>\n",
       "      <td>r</td>\n",
       "      <td>3</td>\n",
       "      <td>0.018</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>space</td>\n",
       "      <td>s</td>\n",
       "      <td>3</td>\n",
       "      <td>0.018</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  current   next  count  share\n",
       "0       h      e      6  0.037\n",
       "1       s  space      6  0.037\n",
       "2   space      t      5  0.030\n",
       "3       n  space      5  0.030\n",
       "4   space      i      4  0.024\n",
       "5       e  space      4  0.024\n",
       "6   space      r      3  0.018\n",
       "7   space      s      3  0.018"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ]
  },
  {
   "id": "0e7e6224",
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## Model\n",
    "\n",
    "A neural bigram model is one weight matrix $W \\in \\mathbb{R}^{|V| \\times |V|}$. Given\n",
    "the previous character $x_{t-1}$ it predicts\n",
    "\n",
    "$$\n",
    "P(x_t \\mid x_{t-1}) = \\operatorname{softmax}\\left(W_{x_{t-1}}\\right)_{x_t}\n",
    "$$\n",
    "\n",
    "and training minimises the mean negative log-likelihood\n",
    "$\\mathcal{L} = -\\frac{1}{N}\\sum_{t} \\log P(x_t \\mid x_{t-1})$."
   ]
  },
  {
   "id": "a671c356",
   "metadata": {},
   "cell_type": "code",
   "execution_count": 4,
   "source": [
    "rng = np.random.default_rng(7)\n",
    "split = int(0.85 * len(data))\n",
    "train, val = data[:split], data[split:]\n",
    "\n",
    "def nll(W, seq):\n",
    "    logits = W[seq[:-1]]\n",
    "    logits = logits - logits.max(axis=1, keepdims=True)\n",
    "    probs = np.exp(logits) / np.exp(logits).sum(axis=1, keepdims=True)\n",
    "    return -np.log(probs[np.arange(len(seq) - 1), seq[1:]]).mean(), probs\n",
    "\n",
    "W = rng.normal(0, 0.1, size=(V, V))\n",
    "history = []\n",
    "for step in range(150):\n",
    "    loss, probs = nll(W, train)\n",
    "    grad = probs.copy()\n",
    "    grad[np.arange(len(train) - 1), train[1:]] -= 1\n",
    "    dW = np.zeros_like(W)\n",
    "    np.add.at(dW, train[:-1], grad / (len(train) - 1))\n",
    "    W -= 5.0 * dW\n",
    "    history.append((step, loss, nll(W, val)[0]))\n",
    "\n",
    "hist = pd.DataFrame(history, columns=[\"step\", \"train\", \"validation\"])\n",
    "print(f\"final train loss {hist.train.iloc[-1]:.3f} | validation loss {hist.validation.iloc[-1]:.3f}\")"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "final train loss 1.687 | validation loss 3.404\n"
     ]
    }
   ]
  },
  {
   "id": "0a557a3b",
   "metadata": {},
   "cell_type": "code",
   "execution_count": 5,
   "source": [
    "#| fig-cap: Mean negative log-likelihood per step. Validation loss flattens early; a bigram model runs out of signal fast.\n",
    "fig, ax = plt.subplots()\n",
    "ax.plot(hist.step, hist.train, label=\"train\")\n",
    "ax.plot(hist.step, hist.validation, label=\"validation\")\n",
    "ax.set_title(\"Loss falls, then the gap opens\")\n",
    "ax.set_xlabel(\"step\")\n",
    "ax.set_ylabel(\"negative log-likelihood\")\n",
    "ax.legend(loc=\"center right\")\n",
    "plt.show()"
   ],
   "outputs": [
    {
     "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=\"455.184141pt\" height=\"258.0085pt\" viewBox=\"0 0 455.184141 258.0085\" 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-10-06T14:04:19.288796</dc:date>\n",
       "    <dc:format>image/svg+xml</dc:format>\n",
       "    <dc:creator>\n",
       "     <cc:Agent>\n",
       "      <dc:title>Matplotlib v3.11.2, 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 258.0085 \n",
       "L 455.184141 258.0085 \n",
       "L 455.184141 0 \n",
       "L 0 0 \n",
       "L 0 258.0085 \n",
       "z\n",
       "\" style=\"fill: none\"/>\n",
       "  </g>\n",
       "  <g id=\"axes_1\">\n",
       "   <g id=\"patch_2\">\n",
       "    <path d=\"M 46.224141 218.421 \n",
       "L 447.984141 218.421 \n",
       "L 447.984141 29.925 \n",
       "L 46.224141 29.925 \n",
       "L 46.224141 218.421 \n",
       "z\n",
       "\" style=\"fill: none\"/>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_1\">\n",
       "    <g id=\"xtick_1\">\n",
       "     <g id=\"line2d_1\"/>\n",
       "     <g id=\"text_1\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: middle; fill: #6f706a\" x=\"64.485959\" y=\"232.513\" transform=\"rotate(-0 64.485959 232.513)\">0</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_2\">\n",
       "     <g id=\"line2d_2\"/>\n",
       "     <g id=\"text_2\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: middle; fill: #6f706a\" x=\"113.510974\" y=\"232.513\" transform=\"rotate(-0 113.510974 232.513)\">20</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_3\">\n",
       "     <g id=\"line2d_3\"/>\n",
       "     <g id=\"text_3\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: middle; fill: #6f706a\" x=\"162.535989\" y=\"232.513\" transform=\"rotate(-0 162.535989 232.513)\">40</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_4\">\n",
       "     <g id=\"line2d_4\"/>\n",
       "     <g id=\"text_4\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: middle; fill: #6f706a\" x=\"211.561005\" y=\"232.513\" transform=\"rotate(-0 211.561005 232.513)\">60</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_5\">\n",
       "     <g id=\"line2d_5\"/>\n",
       "     <g id=\"text_5\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: middle; fill: #6f706a\" x=\"260.58602\" y=\"232.513\" transform=\"rotate(-0 260.58602 232.513)\">80</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_6\">\n",
       "     <g id=\"line2d_6\"/>\n",
       "     <g id=\"text_6\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: middle; fill: #6f706a\" x=\"309.611035\" y=\"232.513\" transform=\"rotate(-0 309.611035 232.513)\">100</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_7\">\n",
       "     <g id=\"line2d_7\"/>\n",
       "     <g id=\"text_7\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: middle; fill: #6f706a\" x=\"358.63605\" y=\"232.513\" transform=\"rotate(-0 358.63605 232.513)\">120</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_8\">\n",
       "     <g id=\"line2d_8\"/>\n",
       "     <g id=\"text_8\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: middle; fill: #6f706a\" x=\"407.661066\" y=\"232.513\" transform=\"rotate(-0 407.661066 232.513)\">140</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_9\">\n",
       "     <text style=\"font-size: 9px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: middle; fill: #484945\" x=\"247.104141\" y=\"248.8915\" transform=\"rotate(-0 247.104141 248.8915)\">step</text>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_2\">\n",
       "    <g id=\"ytick_1\">\n",
       "     <g id=\"line2d_9\">\n",
       "      <path d=\"M 46.224141 203.614115 \n",
       "L 447.984141 203.614115 \n",
       "\" clip-path=\"url(#p545fa06806)\" style=\"fill: none; stroke: #e3e4de; stroke-width: 0.8; stroke-linecap: round\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_10\"/>\n",
       "     <g id=\"text_10\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: end; fill: #6f706a\" x=\"40.224141\" y=\"207.660115\" transform=\"rotate(-0 40.224141 207.660115)\">1.75</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_2\">\n",
       "     <g id=\"line2d_11\">\n",
       "      <path d=\"M 46.224141 178.660674 \n",
       "L 447.984141 178.660674 \n",
       "\" clip-path=\"url(#p545fa06806)\" style=\"fill: none; stroke: #e3e4de; stroke-width: 0.8; stroke-linecap: round\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_12\"/>\n",
       "     <g id=\"text_11\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: end; fill: #6f706a\" x=\"40.224141\" y=\"182.706674\" transform=\"rotate(-0 40.224141 182.706674)\">2.00</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_3\">\n",
       "     <g id=\"line2d_13\">\n",
       "      <path d=\"M 46.224141 153.707233 \n",
       "L 447.984141 153.707233 \n",
       "\" clip-path=\"url(#p545fa06806)\" style=\"fill: none; stroke: #e3e4de; stroke-width: 0.8; stroke-linecap: round\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_14\"/>\n",
       "     <g id=\"text_12\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: end; fill: #6f706a\" x=\"40.224141\" y=\"157.753233\" transform=\"rotate(-0 40.224141 157.753233)\">2.25</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_4\">\n",
       "     <g id=\"line2d_15\">\n",
       "      <path d=\"M 46.224141 128.753791 \n",
       "L 447.984141 128.753791 \n",
       "\" clip-path=\"url(#p545fa06806)\" style=\"fill: none; stroke: #e3e4de; stroke-width: 0.8; stroke-linecap: round\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_16\"/>\n",
       "     <g id=\"text_13\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: end; fill: #6f706a\" x=\"40.224141\" y=\"132.799791\" transform=\"rotate(-0 40.224141 132.799791)\">2.50</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_5\">\n",
       "     <g id=\"line2d_17\">\n",
       "      <path d=\"M 46.224141 103.80035 \n",
       "L 447.984141 103.80035 \n",
       "\" clip-path=\"url(#p545fa06806)\" style=\"fill: none; stroke: #e3e4de; stroke-width: 0.8; stroke-linecap: round\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_18\"/>\n",
       "     <g id=\"text_14\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: end; fill: #6f706a\" x=\"40.224141\" y=\"107.84635\" transform=\"rotate(-0 40.224141 107.84635)\">2.75</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_6\">\n",
       "     <g id=\"line2d_19\">\n",
       "      <path d=\"M 46.224141 78.846909 \n",
       "L 447.984141 78.846909 \n",
       "\" clip-path=\"url(#p545fa06806)\" style=\"fill: none; stroke: #e3e4de; stroke-width: 0.8; stroke-linecap: round\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_20\"/>\n",
       "     <g id=\"text_15\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: end; fill: #6f706a\" x=\"40.224141\" y=\"82.892909\" transform=\"rotate(-0 40.224141 82.892909)\">3.00</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_7\">\n",
       "     <g id=\"line2d_21\">\n",
       "      <path d=\"M 46.224141 53.893468 \n",
       "L 447.984141 53.893468 \n",
       "\" clip-path=\"url(#p545fa06806)\" style=\"fill: none; stroke: #e3e4de; stroke-width: 0.8; stroke-linecap: round\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_22\"/>\n",
       "     <g id=\"text_16\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: end; fill: #6f706a\" x=\"40.224141\" y=\"57.939468\" transform=\"rotate(-0 40.224141 57.939468)\">3.25</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_17\">\n",
       "     <text style=\"font-size: 9px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: middle; fill: #484945\" x=\"15.768\" y=\"124.173\" transform=\"rotate(-90 15.768 124.173)\">negative log-likelihood</text>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"line2d_23\">\n",
       "    <path d=\"M 64.485959 60.690963 \n",
       "L 69.38846 71.647014 \n",
       "L 74.290962 81.92872 \n",
       "L 79.193463 91.506063 \n",
       "L 84.095965 100.358939 \n",
       "L 88.998466 108.482113 \n",
       "L 93.900968 115.888602 \n",
       "L 98.803469 122.610036 \n",
       "L 103.705971 128.69367 \n",
       "L 108.608473 134.197003 \n",
       "L 113.510974 139.181676 \n",
       "L 118.413476 143.708223 \n",
       "L 123.315977 147.83254 \n",
       "L 128.218479 151.604119 \n",
       "L 133.12098 155.065666 \n",
       "L 138.023482 158.253525 \n",
       "L 142.925983 161.198496 \n",
       "L 147.828485 163.926729 \n",
       "L 152.730986 166.460567 \n",
       "L 160.084739 169.938216 \n",
       "L 167.438491 173.075934 \n",
       "L 174.792243 175.918818 \n",
       "L 182.145995 178.504532 \n",
       "L 189.499748 180.864834 \n",
       "L 196.8535 183.026677 \n",
       "L 206.658503 185.639621 \n",
       "L 216.463506 187.985652 \n",
       "L 226.268509 190.101829 \n",
       "L 236.073512 192.01855 \n",
       "L 248.329766 194.171816 \n",
       "L 260.58602 196.09247 \n",
       "L 275.293524 198.135407 \n",
       "L 290.001029 199.934933 \n",
       "L 304.708534 201.52739 \n",
       "L 321.867289 203.162913 \n",
       "L 341.477295 204.785481 \n",
       "L 361.087301 206.189121 \n",
       "L 383.148558 207.552454 \n",
       "L 407.661066 208.848107 \n",
       "L 429.722322 209.853 \n",
       "L 429.722322 209.853 \n",
       "\" clip-path=\"url(#p545fa06806)\" style=\"fill: none; stroke: #0969da; stroke-width: 1.8; stroke-linecap: round\"/>\n",
       "   </g>\n",
       "   <g id=\"line2d_24\">\n",
       "    <path d=\"M 64.485959 59.608508 \n",
       "L 69.38846 65.168888 \n",
       "L 74.290962 70.099273 \n",
       "L 76.742213 72.322315 \n",
       "L 79.193463 74.382046 \n",
       "L 81.644714 76.278088 \n",
       "L 84.095965 78.011041 \n",
       "L 86.547216 79.582596 \n",
       "L 88.998466 80.995601 \n",
       "L 91.449717 82.25409 \n",
       "L 93.900968 83.363256 \n",
       "L 96.352219 84.329364 \n",
       "L 98.803469 85.15961 \n",
       "L 101.25472 85.861937 \n",
       "L 106.157222 86.91705 \n",
       "L 111.059723 87.564898 \n",
       "L 115.962225 87.874172 \n",
       "L 120.864726 87.907418 \n",
       "L 125.767228 87.718663 \n",
       "L 130.669729 87.352905 \n",
       "L 138.023482 86.550456 \n",
       "L 145.377234 85.524855 \n",
       "L 155.182237 83.923678 \n",
       "L 167.438491 81.691813 \n",
       "L 189.499748 77.388109 \n",
       "L 231.171011 69.231459 \n",
       "L 255.683518 64.676464 \n",
       "L 280.196026 60.35113 \n",
       "L 304.708534 56.254623 \n",
       "L 329.221041 52.376716 \n",
       "L 356.1848 48.348368 \n",
       "L 383.148558 44.552048 \n",
       "L 410.112316 40.971132 \n",
       "L 429.722322 38.493 \n",
       "L 429.722322 38.493 \n",
       "\" clip-path=\"url(#p545fa06806)\" style=\"fill: none; stroke: #1a7f64; stroke-width: 1.8; stroke-linecap: round\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_3\">\n",
       "    <path d=\"M 46.224141 218.421 \n",
       "L 46.224141 29.925 \n",
       "\" style=\"fill: none; stroke: #d1d2cb; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_4\">\n",
       "    <path d=\"M 46.224141 218.421 \n",
       "L 447.984141 218.421 \n",
       "\" style=\"fill: none; stroke: #d1d2cb; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n",
       "   </g>\n",
       "   <g id=\"text_18\">\n",
       "    <text style=\"font-weight: 600; font-size: 11px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: start; fill: #171817\" x=\"46.224141\" y=\"17.925\" transform=\"rotate(-0 46.224141 17.925)\">Loss falls, then the gap opens</text>\n",
       "   </g>\n",
       "   <g id=\"legend_1\">\n",
       "    <g id=\"line2d_25\">\n",
       "     <path d=\"M 380.170078 117.2625 \n",
       "L 388.670078 117.2625 \n",
       "L 397.170078 117.2625 \n",
       "\" style=\"fill: none; stroke: #0969da; stroke-width: 1.8; stroke-linecap: round\"/>\n",
       "    </g>\n",
       "    <g id=\"text_19\">\n",
       "     <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: start; fill: #484945\" x=\"403.970078\" y=\"120.2375\" transform=\"rotate(-0 403.970078 120.2375)\">train</text>\n",
       "    </g>\n",
       "    <g id=\"line2d_26\">\n",
       "     <path d=\"M 380.170078 131.415 \n",
       "L 388.670078 131.415 \n",
       "L 397.170078 131.415 \n",
       "\" style=\"fill: none; stroke: #1a7f64; stroke-width: 1.8; stroke-linecap: round\"/>\n",
       "    </g>\n",
       "    <g id=\"text_20\">\n",
       "     <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: start; fill: #484945\" x=\"403.970078\" y=\"134.39\" transform=\"rotate(-0 403.970078 134.39)\">validation</text>\n",
       "    </g>\n",
       "   </g>\n",
       "  </g>\n",
       " </g>\n",
       " <defs>\n",
       "  <clipPath id=\"p545fa06806\">\n",
       "   <rect x=\"46.224141\" y=\"29.925\" width=\"401.76\" height=\"188.496\"/>\n",
       "  </clipPath>\n",
       " </defs>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<Figure size 720x340 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ]
  },
  {
   "id": "443f12ca",
   "metadata": {},
   "cell_type": "code",
   "execution_count": 6,
   "source": [
    "#| code-fold: true\n",
    "#| fig-cap: What the trained model expects after the letter \"e\".\n",
    "p = np.exp(W[stoi[\"e\"]] - W[stoi[\"e\"]].max())\n",
    "p /= p.sum()\n",
    "top = np.argsort(p)[::-1][:10]\n",
    "fig, ax = plt.subplots(figsize=(7.2, 2.8))\n",
    "ax.bar([\"space\" if itos[i] == \" \" else itos[i] for i in top], p[top], color=[\"#0969da\"] + [\"#6f706a\"] * 9, width=0.62)\n",
    "ax.set_title('Next character after \"e\"')\n",
    "ax.set_ylabel(\"probability\")\n",
    "ax.yaxis.set_major_formatter(lambda v, _: f\"{v:.0%}\")\n",
    "plt.show()"
   ],
   "outputs": [
    {
     "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=\"456.595937pt\" height=\"208.2595pt\" viewBox=\"0 0 456.595937 208.2595\" 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-10-06T14:04:19.321254</dc:date>\n",
       "    <dc:format>image/svg+xml</dc:format>\n",
       "    <dc:creator>\n",
       "     <cc:Agent>\n",
       "      <dc:title>Matplotlib v3.11.2, 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 208.2595 \n",
       "L 456.595937 208.2595 \n",
       "L 456.595937 0 \n",
       "L 0 0 \n",
       "L 0 208.2595 \n",
       "z\n",
       "\" style=\"fill: none\"/>\n",
       "  </g>\n",
       "  <g id=\"axes_1\">\n",
       "   <g id=\"patch_2\">\n",
       "    <path d=\"M 47.635938 185.157 \n",
       "L 449.395938 185.157 \n",
       "L 449.395938 29.925 \n",
       "L 47.635938 29.925 \n",
       "L 47.635938 185.157 \n",
       "z\n",
       "\" style=\"fill: none\"/>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_1\">\n",
       "    <g id=\"xtick_1\">\n",
       "     <g id=\"line2d_1\"/>\n",
       "     <g id=\"text_1\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: middle; fill: #6f706a\" x=\"77.667327\" y=\"199.249\" transform=\"rotate(-0 77.667327 199.249)\">space</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_2\">\n",
       "     <g id=\"line2d_2\"/>\n",
       "     <g id=\"text_2\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: middle; fill: #6f706a\" x=\"115.633685\" y=\"199.249\" transform=\"rotate(-0 115.633685 199.249)\">n</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_3\">\n",
       "     <g id=\"line2d_3\"/>\n",
       "     <g id=\"text_3\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: middle; fill: #6f706a\" x=\"153.600043\" y=\"199.249\" transform=\"rotate(-0 153.600043 199.249)\">a</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_4\">\n",
       "     <g id=\"line2d_4\"/>\n",
       "     <g id=\"text_4\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: middle; fill: #6f706a\" x=\"191.566401\" y=\"199.249\" transform=\"rotate(-0 191.566401 199.249)\">c</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_5\">\n",
       "     <g id=\"line2d_5\"/>\n",
       "     <g id=\"text_5\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: middle; fill: #6f706a\" x=\"229.532759\" y=\"199.249\" transform=\"rotate(-0 229.532759 199.249)\">s</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_6\">\n",
       "     <g id=\"line2d_6\"/>\n",
       "     <g id=\"text_6\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: middle; fill: #6f706a\" x=\"267.499116\" y=\"199.249\" transform=\"rotate(-0 267.499116 199.249)\">d</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_7\">\n",
       "     <g id=\"line2d_7\"/>\n",
       "     <g id=\"text_7\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: middle; fill: #6f706a\" x=\"305.465474\" y=\"199.249\" transform=\"rotate(-0 305.465474 199.249)\">h</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_8\">\n",
       "     <g id=\"line2d_8\"/>\n",
       "     <g id=\"text_8\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: middle; fill: #6f706a\" x=\"343.431832\" y=\"199.249\" transform=\"rotate(-0 343.431832 199.249)\">b</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_9\">\n",
       "     <g id=\"line2d_9\"/>\n",
       "     <g id=\"text_9\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: middle; fill: #6f706a\" x=\"381.39819\" y=\"199.249\" transform=\"rotate(-0 381.39819 199.249)\">i</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_10\">\n",
       "     <g id=\"line2d_10\"/>\n",
       "     <g id=\"text_10\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: middle; fill: #6f706a\" x=\"419.364548\" y=\"199.249\" transform=\"rotate(-0 419.364548 199.249)\">r</text>\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 47.635938 185.157 \n",
       "L 449.395938 185.157 \n",
       "\" clip-path=\"url(#p3278052d89)\" style=\"fill: none; stroke: #e3e4de; stroke-width: 0.8; stroke-linecap: round\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_12\"/>\n",
       "     <g id=\"text_11\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: end; fill: #6f706a\" x=\"41.635938\" y=\"189.203\" transform=\"rotate(-0 41.635938 189.203)\">0%</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_2\">\n",
       "     <g id=\"line2d_13\">\n",
       "      <path d=\"M 47.635938 167.868189 \n",
       "L 449.395938 167.868189 \n",
       "\" clip-path=\"url(#p3278052d89)\" style=\"fill: none; stroke: #e3e4de; stroke-width: 0.8; stroke-linecap: round\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_14\"/>\n",
       "     <g id=\"text_12\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: end; fill: #6f706a\" x=\"41.635938\" y=\"171.914189\" transform=\"rotate(-0 41.635938 171.914189)\">2%</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_3\">\n",
       "     <g id=\"line2d_15\">\n",
       "      <path d=\"M 47.635938 150.579379 \n",
       "L 449.395938 150.579379 \n",
       "\" clip-path=\"url(#p3278052d89)\" style=\"fill: none; stroke: #e3e4de; stroke-width: 0.8; stroke-linecap: round\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_16\"/>\n",
       "     <g id=\"text_13\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: end; fill: #6f706a\" x=\"41.635938\" y=\"154.625379\" transform=\"rotate(-0 41.635938 154.625379)\">4%</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_4\">\n",
       "     <g id=\"line2d_17\">\n",
       "      <path d=\"M 47.635938 133.290568 \n",
       "L 449.395938 133.290568 \n",
       "\" clip-path=\"url(#p3278052d89)\" style=\"fill: none; stroke: #e3e4de; stroke-width: 0.8; stroke-linecap: round\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_18\"/>\n",
       "     <g id=\"text_14\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: end; fill: #6f706a\" x=\"41.635938\" y=\"137.336568\" transform=\"rotate(-0 41.635938 137.336568)\">6%</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_5\">\n",
       "     <g id=\"line2d_19\">\n",
       "      <path d=\"M 47.635938 116.001757 \n",
       "L 449.395938 116.001757 \n",
       "\" clip-path=\"url(#p3278052d89)\" style=\"fill: none; stroke: #e3e4de; stroke-width: 0.8; stroke-linecap: round\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_20\"/>\n",
       "     <g id=\"text_15\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: end; fill: #6f706a\" x=\"41.635938\" y=\"120.047757\" transform=\"rotate(-0 41.635938 120.047757)\">8%</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_6\">\n",
       "     <g id=\"line2d_21\">\n",
       "      <path d=\"M 47.635938 98.712946 \n",
       "L 449.395938 98.712946 \n",
       "\" clip-path=\"url(#p3278052d89)\" style=\"fill: none; stroke: #e3e4de; stroke-width: 0.8; stroke-linecap: round\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_22\"/>\n",
       "     <g id=\"text_16\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: end; fill: #6f706a\" x=\"41.635938\" y=\"102.758946\" transform=\"rotate(-0 41.635938 102.758946)\">10%</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_7\">\n",
       "     <g id=\"line2d_23\">\n",
       "      <path d=\"M 47.635938 81.424136 \n",
       "L 449.395938 81.424136 \n",
       "\" clip-path=\"url(#p3278052d89)\" style=\"fill: none; stroke: #e3e4de; stroke-width: 0.8; stroke-linecap: round\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_24\"/>\n",
       "     <g id=\"text_17\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: end; fill: #6f706a\" x=\"41.635938\" y=\"85.470136\" transform=\"rotate(-0 41.635938 85.470136)\">12%</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_8\">\n",
       "     <g id=\"line2d_25\">\n",
       "      <path d=\"M 47.635938 64.135325 \n",
       "L 449.395938 64.135325 \n",
       "\" clip-path=\"url(#p3278052d89)\" style=\"fill: none; stroke: #e3e4de; stroke-width: 0.8; stroke-linecap: round\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_26\"/>\n",
       "     <g id=\"text_18\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: end; fill: #6f706a\" x=\"41.635938\" y=\"68.181325\" transform=\"rotate(-0 41.635938 68.181325)\">14%</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_9\">\n",
       "     <g id=\"line2d_27\">\n",
       "      <path d=\"M 47.635938 46.846514 \n",
       "L 449.395938 46.846514 \n",
       "\" clip-path=\"url(#p3278052d89)\" style=\"fill: none; stroke: #e3e4de; stroke-width: 0.8; stroke-linecap: round\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_28\"/>\n",
       "     <g id=\"text_19\">\n",
       "      <text style=\"font-size: 8.5px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: end; fill: #6f706a\" x=\"41.635938\" y=\"50.892514\" transform=\"rotate(-0 41.635938 50.892514)\">16%</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_20\">\n",
       "     <text style=\"font-size: 9px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: middle; fill: #484945\" x=\"15.768\" y=\"107.541\" transform=\"rotate(-90 15.768 107.541)\">probability</text>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"patch_3\">\n",
       "    <path d=\"M 65.897756 185.157 \n",
       "L 89.436898 185.157 \n",
       "L 89.436898 37.317 \n",
       "L 65.897756 37.317 \n",
       "z\n",
       "\" clip-path=\"url(#p3278052d89)\" style=\"fill: #0969da\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_4\">\n",
       "    <path d=\"M 103.864114 185.157 \n",
       "L 127.403256 185.157 \n",
       "L 127.403256 88.327976 \n",
       "L 103.864114 88.327976 \n",
       "z\n",
       "\" clip-path=\"url(#p3278052d89)\" style=\"fill: #6f706a\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_5\">\n",
       "    <path d=\"M 141.830472 185.157 \n",
       "L 165.369614 185.157 \n",
       "L 165.369614 88.328287 \n",
       "L 141.830472 88.328287 \n",
       "z\n",
       "\" clip-path=\"url(#p3278052d89)\" style=\"fill: #6f706a\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_6\">\n",
       "    <path d=\"M 179.79683 185.157 \n",
       "L 203.335972 185.157 \n",
       "L 203.335972 88.328524 \n",
       "L 179.79683 88.328524 \n",
       "z\n",
       "\" clip-path=\"url(#p3278052d89)\" style=\"fill: #6f706a\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_7\">\n",
       "    <path d=\"M 217.763188 185.157 \n",
       "L 241.302329 185.157 \n",
       "L 241.302329 88.329061 \n",
       "L 217.763188 88.329061 \n",
       "z\n",
       "\" clip-path=\"url(#p3278052d89)\" style=\"fill: #6f706a\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_8\">\n",
       "    <path d=\"M 255.729546 185.157 \n",
       "L 279.268687 185.157 \n",
       "L 279.268687 139.949666 \n",
       "L 255.729546 139.949666 \n",
       "z\n",
       "\" clip-path=\"url(#p3278052d89)\" style=\"fill: #6f706a\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_9\">\n",
       "    <path d=\"M 293.695903 185.157 \n",
       "L 317.235045 185.157 \n",
       "L 317.235045 139.949961 \n",
       "L 293.695903 139.949961 \n",
       "z\n",
       "\" clip-path=\"url(#p3278052d89)\" style=\"fill: #6f706a\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_10\">\n",
       "    <path d=\"M 331.662261 185.157 \n",
       "L 355.201403 185.157 \n",
       "L 355.201403 139.958945 \n",
       "L 331.662261 139.958945 \n",
       "z\n",
       "\" clip-path=\"url(#p3278052d89)\" style=\"fill: #6f706a\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_11\">\n",
       "    <path d=\"M 369.628619 185.157 \n",
       "L 393.167761 185.157 \n",
       "L 393.167761 139.971447 \n",
       "L 369.628619 139.971447 \n",
       "z\n",
       "\" clip-path=\"url(#p3278052d89)\" style=\"fill: #6f706a\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_12\">\n",
       "    <path d=\"M 407.594977 185.157 \n",
       "L 431.134119 185.157 \n",
       "L 431.134119 140.011591 \n",
       "L 407.594977 140.011591 \n",
       "z\n",
       "\" clip-path=\"url(#p3278052d89)\" style=\"fill: #6f706a\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_13\">\n",
       "    <path d=\"M 47.635938 185.157 \n",
       "L 47.635938 29.925 \n",
       "\" style=\"fill: none; stroke: #d1d2cb; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_14\">\n",
       "    <path d=\"M 47.635937 185.157 \n",
       "L 449.395937 185.157 \n",
       "\" style=\"fill: none; stroke: #d1d2cb; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n",
       "   </g>\n",
       "   <g id=\"text_21\">\n",
       "    <text style=\"font-weight: 600; font-size: 11px; font-family: 'Inter', 'Helvetica Neue', 'Helvetica', 'Arial', 'DejaVu Sans', sans-serif; text-anchor: start; fill: #171817\" x=\"47.635938\" y=\"17.925\" transform=\"rotate(-0 47.635938 17.925)\">Next character after \"e\"</text>\n",
       "   </g>\n",
       "  </g>\n",
       " </g>\n",
       " <defs>\n",
       "  <clipPath id=\"p3278052d89\">\n",
       "   <rect x=\"47.635938\" y=\"29.925\" width=\"401.76\" height=\"155.232\"/>\n",
       "  </clipPath>\n",
       " </defs>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<Figure size 720x280 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ]
  },
  {
   "id": "91ddb37d",
   "metadata": {},
   "cell_type": "code",
   "execution_count": 7,
   "source": [
    "def sample(W, start=\"t\", n=72, seed=3):\n",
    "    g = np.random.default_rng(seed)\n",
    "    out, i = [start], stoi[start]\n",
    "    for _ in range(n):\n",
    "        p = np.exp(W[i] - W[i].max())\n",
    "        p /= p.sum()\n",
    "        i = g.choice(V, p=p)\n",
    "        out.append(itos[i])\n",
    "    return \"\".join(out)\n",
    "\n",
    "print(sample(W))"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "t. ticarat. reitreleb whesmit. matrceburermorens the s tareil tisuivesmiv\n"
     ]
    }
   ]
  },
  {
   "id": "159d7b64",
   "metadata": {},
   "cell_type": "code",
   "execution_count": 8,
   "source": [
    "import sys\n",
    "missing = sorted(set(corpus) - set(corpus[split:]))\n",
    "print(f\"note: {len(missing)} of {V} symbols never appear in the validation split\", file=sys.stderr)"
   ],
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "note: 12 of 24 symbols never appear in the validation split\n"
     ]
    }
   ]
  },
  {
   "id": "960c0002",
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## What to notice\n",
    "\n",
    "- Code blocks use the site's Expressive Code theme, copy button included.\n",
    "- Printed output, tables and charts sit in quiet panels built from the same tokens\n",
    "  as the rest of the site.\n",
    "- Charts are inline SVG. Switch the theme and they switch with it.\n",
    "\n",
    ":::tip\n",
    "Tag a cell `remove-cell` to keep setup code off the page, `remove-input` to show only\n",
    "its output, or start it with `#| code-fold: true` to fold the code.\n",
    ":::"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.12.12"
  },
  "mementropy": {
   "contentKind": "notebook",
   "capabilities": [
    "bigram-language-modeling",
    "notebook-rendering"
   ],
   "implementations": [
    "NumPy",
    "pandas",
    "Matplotlib",
    "IPython"
   ],
   "evidenceStatus": "executed-example",
   "updatedDate": "2026-10-29"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
