Skip to content

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

fix for the property and vocab size

raiyanyahyae55ae3a014f7
diff --git a/notebooks/03_embeddings.ipynb b/notebooks/03_embeddings.ipynb
index da96fb5..5e5b343 100644
--- a/notebooks/03_embeddings.ipynb
+++ b/notebooks/03_embeddings.ipynb
@@ -96,7 +96,7 @@
"\n",
"Output shape: torch.Size([1, 5, 768])\n",
"Each token is now a 768-dimensional vector\n",
- "First token vector preview: [18.91689682006836, -2.2418112754821777, 41.377845764160156, -2.377814531326294, 29.869359970092773] ...\n"
+ "First token vector preview: [-16.388792037963867, -13.410012245178223, 8.839740753173828, 23.09480094909668, -17.038358688354492] ...\n"
]
}
],
@@ -142,8 +142,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "Without scaling: mean=0.0087, std=0.9968\n",
- "With scaling: mean=0.2402, std=27.6252\n"
+ "Without scaling: mean=-0.0146, std=1.0058\n",
+ "With scaling: mean=-0.4043, std=27.8724\n"
]
}
],
@@ -175,20 +175,12 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "Text: 'The cat sat on the mat'\n",
+ "Text: The cat sat on the mat\n",
"Token IDs: [464, 3797, 3332, 319, 262, 2603]\n",
- "\n"
- ]
- },
- {
- "ename": "AttributeError",
- "evalue": "'SimpleTokenizer' object has no attribute 'vocab_size'",
- "output_type": "error",
- "traceback": [
- "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
- "\u001b[31mAttributeError\u001b[39m Traceback (most recent call last)",
- "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[5]\u001b[39m\u001b[32m, line 29\u001b[39m\n\u001b[32m 25\u001b[39m print(f\"Text: '{text}'\")\n\u001b[32m 26\u001b[39m print(f\"Token IDs: {token_ids}\")\n\u001b[32m 27\u001b[39m print()\n\u001b[32m 28\u001b[39m \n\u001b[32m---> \u001b[39m\u001b[32m29\u001b[39m real_embedding = Embedding(tokenizer.vocab_size, d_model=\u001b[32m768\u001b[39m)\n\u001b[32m 30\u001b[39m input_tensor = torch.tensor([token_ids])\n\u001b[32m 31\u001b[39m vectors = real_embedding(input_tensor)\n\u001b[32m 32\u001b[39m print(f\"Output shape: {vectors.shape}\")\n",
- "\u001b[31mAttributeError\u001b[39m: 'SimpleTokenizer' object has no attribute 'vocab_size'"
+ "\n",
+ "Output shape: torch.Size([1, 6, 768])\n",
+ "Each of the 6 tokens is now a 768-dim vector\n",
+ "The word cat (token 3797) has vector: [-43.199928283691406, -31.866304397583008, 17.82335090637207] ...\n"
]
}
],
@@ -209,15 +201,21 @@
" 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\n",
+ "\n",
"tokenizer = SimpleTokenizer()\n",
"text = \"The cat sat on the mat\"\n",
"token_ids = tokenizer.encode(text)\n",
- "print(f\"Text: '{text}'\")\n",
+ "print(f\"Text: {text}\")\n",
"print(f\"Token IDs: {token_ids}\")\n",
"print()\n",
"\n",
@@ -226,7 +224,7 @@
"vectors = real_embedding(input_tensor)\n",
"print(f\"Output shape: {vectors.shape}\")\n",
"print(f\"Each of the {len(token_ids)} tokens is now a 768-dim vector\")\n",
- "print(f\"The word 'cat' (token {token_ids[1]}) has vector: {vectors[0, 1, :3].tolist()} ...\")"
+ "print(f\"The word cat (token {token_ids[1]}) has vector: {vectors[0, 1, :3].tolist()} ...\")"
]
}
],