Skip to content

Build a modern LLM from scratch. Every line commented. Explained like we are five.

add notebooks section to readme and tokenization notebook

raiyanyahya090ef850018e
diff --git a/README.md b/README.md
index ef81fe0..15dbca5 100644
--- a/README.md
+++ b/README.md
@@ -148,6 +148,24 @@ Step 50,000/50,000 | Loss: 2.8901 | LR: 1.00e-05 | Toks/sec: 44,800
---
+## πŸ““ Jupyter Notebooks
+
+Alongside the textbook, each chapter has a companion notebook you can run live. These strip away the explanations and give you pure, clean code that executes from top to bottom. If the textbook teaches you why, the notebooks let you see it happen.
+
+We're going to run this whole project on a very small dataset so you can watch training happen in minutes rather than weeks. Every notebook is self-contained β€” open it, run all cells, and you'll see the model learn in real time.
+
+```bash
+# Install everything you need
+pip install jupyter tiktoken torch numpy datasets matplotlib
+
+# Start with chapter 2 (tokenization)
+jupyter notebook notebooks/02_tokenization.ipynb
+```
+
+Notebooks live in the `notebooks/` directory, one per chapter. Open any of them and hit **Cell β†’ Run All**.
+
+---
+
## πŸ“– How to Read
Each chapter follows the same **4-step structure**:
diff --git a/notebooks/.ipynb_checkpoints/02_tokenization-checkpoint.ipynb b/notebooks/.ipynb_checkpoints/02_tokenization-checkpoint.ipynb
new file mode 100644
index 0000000..f8663fb
--- /dev/null
+++ b/notebooks/.ipynb_checkpoints/02_tokenization-checkpoint.ipynb
@@ -0,0 +1,146 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Chapter 2 β€” Tokenization\n",
+ "\n",
+ "Turning words into numbers using Byte Pair Encoding (BPE)."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Imports"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from dataclasses import dataclass\n",
+ "import tiktoken"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Configuration"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "@dataclass\n",
+ "class TokenizerConfig:\n",
+ " name: str = \"gpt2\"\n",
+ " vocab_size: int = 50257"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Tokenizer"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "class SimpleTokenizer:\n",
+ " def __init__(self, config=None):\n",
+ " self.config = config or TokenizerConfig()\n",
+ " self.enc = tiktoken.get_encoding(self.config.name)\n",
+ " self.eos_token = \"<|endoftext|>\"\n",
+ " self.eos_token_id = self.enc.encode(\n",
+ " self.eos_token, allowed_special={self.eos_token}\n",
+ " )[0]\n",
+ "\n",
+ " def encode(self, text):\n",
+ " return self.enc.encode(text, allowed_special={self.eos_token})\n",
+ "\n",
+ " def decode(self, ids):\n",
+ " return self.enc.decode(ids)\n",
+ "\n",
+ " @property\n",
+ " def vocab_size(self):\n",
+ " return self.config.vocab_size"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Try it out"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "tokenizer = SimpleTokenizer()\n",
+ "print(f\"Vocabulary size: {tokenizer.vocab_size:,}\")\n",
+ "print(f\"EOS token ID: {tokenizer.eos_token_id}\")\n",
+ "print()\n",
+ "\n",
+ "text = \"The cat sat on the mat.\"\n",
+ "encoded = tokenizer.encode(text)\n",
+ "decoded = tokenizer.decode(encoded)\n",
+ "\n",
+ "print(f\"Original: '{text}'\")\n",
+ "print(f\"Encoded: {encoded}\")\n",
+ "print(f\"Decoded: '{decoded}'\")\n",
+ "print(f\"Roundtrip: {text == decoded}\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Rare words and emojis"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "rare = tokenizer.encode(\"antidisestablishmentarianism\")\n",
+ "pieces = [tokenizer.decode([t]) for t in rare]\n",
+ "print(f\"Pieces: {pieces}\")\n",
+ "print(f\"Decoded: '{tokenizer.decode(rare)}'\")\n",
+ "print()\n",
+ "\n",
+ "emoji = tokenizer.encode(\"Hello world!\")\n",
+ "print(f\"Emoji test: {tokenizer.decode(emoji)}\")"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 3",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "name": "python",
+ "version": "3.10.0"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
diff --git a/notebooks/02_tokenization.ipynb b/notebooks/02_tokenization.ipynb
new file mode 100644
index 0000000..09b5a9d
--- /dev/null
+++ b/notebooks/02_tokenization.ipynb
@@ -0,0 +1,198 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "id": "0463642b",
+ "metadata": {},
+ "source": [
+ "# Chapter 2 β€” Tokenization\n",
+ "\n",
+ "Turning words into numbers using Byte Pair Encoding (BPE)."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "cd98145d",
+ "metadata": {},
+ "source": [
+ "## Imports"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "id": "1efdf6ed",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from dataclasses import dataclass\n",
+ "import tiktoken"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "3cc990dc",
+ "metadata": {},
+ "source": [
+ "## Configuration"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "id": "da490ead",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "@dataclass\n",
+ "class TokenizerConfig:\n",
+ " name: str = \"gpt2\"\n",
+ " vocab_size: int = 50257"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "7576dd93",
+ "metadata": {},
+ "source": [
+ "## Tokenizer"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "id": "95364f63",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "class SimpleTokenizer:\n",
+ " def __init__(self, config=None):\n",
+ " self.config = config or TokenizerConfig()\n",
+ " self.enc = tiktoken.get_encoding(self.config.name)\n",
+ " self.eos_token = \"<|endoftext|>\"\n",
+ " self.eos_token_id = self.enc.encode(\n",
+ " self.eos_token, allowed_special={self.eos_token}\n",
+ " )[0]\n",
+ "\n",
+ " def encode(self, text):\n",
+ " return self.enc.encode(text, allowed_special={self.eos_token})\n",
+ "\n",
+ " def decode(self, ids):\n",
+ " return self.enc.decode(ids)\n",
+ "\n",
+ " @property\n",
+ " def vocab_size(self):\n",
+ " return self.config.vocab_size"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "7867ea73",
+ "metadata": {},
+ "source": [
+ "## Try it out"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "id": "13322dca",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Vocabulary size: 50,257\n",
+ "EOS token ID: 50256\n",
+ "\n",
+ "Original: 'The cat sat on the mat.'\n",
+ "Encoded: [464, 3797, 3332, 319, 262, 2603, 13]\n",
+ "Decoded: 'The cat sat on the mat.'\n",
+ "Roundtrip: True\n"
+ ]
+ }
+ ],
+ "source": [
+ "tokenizer = SimpleTokenizer()\n",
+ "print(f\"Vocabulary size: {tokenizer.vocab_size:,}\")\n",
+ "print(f\"EOS token ID: {tokenizer.eos_token_id}\")\n",
+ "print()\n",
+ "\n",
+ "text = \"The cat sat on the mat.\"\n",
+ "encoded = tokenizer.encode(text)\n",
+ "decoded = tokenizer.decode(encoded)\n",
+ "\n",
+ "print(f\"Original: '{text}'\")\n",
+ "print(f\"Encoded: {encoded}\")\n",
+ "print(f\"Decoded: '{decoded}'\")\n",
+ "print(f\"Roundtrip: {text == decoded}\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "3d05a01a",
+ "metadata": {},
+ "source": [
+ "## Rare words and emojis"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "id": "e7f0ffda",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Pieces: ['ant', 'idis', 'establishment', 'arian', 'ism']\n",
+ "Decoded: 'antidisestablishmentarianism'\n",
+ "\n",
+ "Emoji test: Hello world!\n"
+ ]
+ }
+ ],
+ "source": [
+ "rare = tokenizer.encode(\"antidisestablishmentarianism\")\n",
+ "pieces = [tokenizer.decode([t]) for t in rare]\n",
+ "print(f\"Pieces: {pieces}\")\n",
+ "print(f\"Decoded: '{tokenizer.decode(rare)}'\")\n",
+ "print()\n",
+ "\n",
+ "emoji = tokenizer.encode(\"Hello world!\")\n",
+ "print(f\"Emoji test: {tokenizer.decode(emoji)}\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "d4cf53c3-1492-4fb1-8a2d-a5d6b0ef928e",
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 3 (ipykernel)",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.14.4"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}