Test code in pytorch 2.4 (#285)

* test code in pytorch 2.4

* update
This commit is contained in:
Sebastian Raschka
2024-07-24 21:53:41 -05:00
committed by GitHub
parent 6cbe6520a2
commit 08040f024c
17 changed files with 509 additions and 377 deletions

View File

@@ -28,6 +28,35 @@
"# Chapter 5 Exercise solutions"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "37aa4692-2357-4d88-b072-6d2d988d7f4f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"numpy version: 1.26.4\n",
"tiktoken version: 0.7.0\n",
"torch version: 2.4.0\n",
"tensorflow version: 2.16.1\n"
]
}
],
"source": [
"from importlib.metadata import version\n",
"\n",
"pkgs = [\"numpy\", \n",
" \"tiktoken\", \n",
" \"torch\",\n",
" \"tensorflow\" # For OpenAI's pretrained weights\n",
" ]\n",
"for p in pkgs:\n",
" print(f\"{p} version: {version(p)}\")"
]
},
{
"cell_type": "markdown",
"id": "5fea8be3-30a1-4623-a6d7-b095c6c1092e",
@@ -58,7 +87,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"id": "42dda298-3014-4c36-8d63-97c210bcf4e8",
"metadata": {},
"outputs": [],
@@ -109,7 +138,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"id": "b5605236-e300-4844-aea7-509d868efbdd",
"metadata": {},
"outputs": [
@@ -172,7 +201,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"id": "1d4163c0-22ad-4f5b-8e20-b7420e9dbfc6",
"metadata": {},
"outputs": [
@@ -182,7 +211,7 @@
"tensor(0.0430)"
]
},
"execution_count": 3,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
@@ -250,7 +279,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"id": "a61a4034-797a-4635-bf42-ddfff1b07125",
"metadata": {},
"outputs": [],
@@ -275,13 +304,13 @@
"\n",
"tokenizer = tiktoken.get_encoding(\"gpt2\")\n",
"model = GPTModel(GPT_CONFIG_124M)\n",
"model.load_state_dict(torch.load(\"model.pth\"))\n",
"model.load_state_dict(torch.load(\"model.pth\", weights_only=True))\n",
"model.eval();"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"id": "ee95a272-b852-43b4-9827-ea7e1dbd5724",
"metadata": {},
"outputs": [],
@@ -292,7 +321,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"id": "4ab43658-3240-484a-9072-a40a0ed85be6",
"metadata": {},
"outputs": [
@@ -322,7 +351,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"id": "ebb22d06-393a-42d3-ab64-66646d33b39b",
"metadata": {},
"outputs": [
@@ -352,7 +381,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"id": "75469f24-47cc-458d-a200-fe64c648131d",
"metadata": {},
"outputs": [
@@ -400,7 +429,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 10,
"id": "94eae6ba-d9fd-417a-8e31-fc39e9299870",
"metadata": {},
"outputs": [],
@@ -424,7 +453,7 @@
"\n",
"tokenizer = tiktoken.get_encoding(\"gpt2\")\n",
"\n",
"checkpoint = torch.load(\"model_and_optimizer.pth\")\n",
"checkpoint = torch.load(\"model_and_optimizer.pth\", weights_only=True)\n",
"model = GPTModel(GPT_CONFIG_124M)\n",
"model.load_state_dict(checkpoint[\"model_state_dict\"])\n",
"model.to(device)\n",
@@ -444,7 +473,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 11,
"id": "b5a78470-0652-4abd-875a-664e23c07c36",
"metadata": {},
"outputs": [],
@@ -507,7 +536,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 12,
"id": "ab4693dc-1359-47a7-8110-1e90f514a49e",
"metadata": {},
"outputs": [
@@ -576,7 +605,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 13,
"id": "68d162d6-bbb9-4d6d-82ee-1c410694f872",
"metadata": {},
"outputs": [],
@@ -604,7 +633,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 14,
"id": "d8373461-7dad-47da-a489-3e23f0799b23",
"metadata": {},
"outputs": [
@@ -630,7 +659,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 15,
"id": "cdd44873-d6c2-4471-a20f-f639b09fdcd3",
"metadata": {},
"outputs": [],
@@ -655,7 +684,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 16,
"id": "c7d562e4-33f6-4611-9b75-6ad1cb441d3b",
"metadata": {},
"outputs": [],
@@ -670,7 +699,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 17,
"id": "46eda9ea-ccb0-46ee-931b-3c07502b2544",
"metadata": {},
"outputs": [],
@@ -725,7 +754,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 18,
"id": "4e3574a2-687d-47a2-a2f6-457fe9d595f1",
"metadata": {},
"outputs": [
@@ -733,8 +762,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Training loss: 3.7547483444213867\n",
"Validation loss: 3.5596189498901367\n"
"Training loss: 3.7547486888037787\n",
"Validation loss: 3.5596182346343994\n"
]
}
],
@@ -759,23 +788,29 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 19,
"id": "1a79a4b6-fe8f-40c2-a018-e731dcf391b3",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"checkpoint: 100%|███████████████████████████| 77.0/77.0 [00:00<00:00, 43.5kiB/s]\n",
"encoder.json: 100%|███████████████████████| 1.04M/1.04M [00:00<00:00, 2.75MiB/s]\n",
"hparams.json: 100%|█████████████████████████| 91.0/91.0 [00:00<00:00, 60.2kiB/s]\n",
"model.ckpt.data-00000-of-00001: 100%|█████| 6.23G/6.23G [06:02<00:00, 17.2MiB/s]\n",
"model.ckpt.index: 100%|████████████████████| 20.7k/20.7k [00:00<00:00, 171kiB/s]\n",
"model.ckpt.meta: 100%|████████████████████| 1.84M/1.84M [00:00<00:00, 4.27MiB/s]\n",
"vocab.bpe: 100%|████████████████████████████| 456k/456k [00:00<00:00, 1.73MiB/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"File already exists and is up-to-date: gpt2/1558M/checkpoint\n",
"File already exists and is up-to-date: gpt2/1558M/encoder.json\n",
"File already exists and is up-to-date: gpt2/1558M/hparams.json\n",
"File already exists and is up-to-date: gpt2/1558M/model.ckpt.data-00000-of-00001\n",
"File already exists and is up-to-date: gpt2/1558M/model.ckpt.index\n",
"File already exists and is up-to-date: gpt2/1558M/model.ckpt.meta\n",
"File already exists and is up-to-date: gpt2/1558M/vocab.bpe\n",
"Training loss: 3.3046313656700983\n",
"Validation loss: 3.1195149421691895\n"
"Training loss: 3.3046312861972384\n",
"Validation loss: 3.1195147037506104\n"
]
}
],
@@ -832,7 +867,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 20,
"id": "31e0972b-e85e-4904-a0f5-24c3eacd5fa2",
"metadata": {},
"outputs": [],
@@ -858,7 +893,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 21,
"id": "b641ee88-f9d4-43ec-a787-e34199eed356",
"metadata": {},
"outputs": [
@@ -902,7 +937,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 22,
"id": "c98f56f4-98fc-43b4-9ee5-726e9d17c73f",
"metadata": {},
"outputs": [],
@@ -912,7 +947,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 23,
"id": "b1f7853c-6e81-4f1f-a1d0-61e2c7d33a20",
"metadata": {},
"outputs": [
@@ -957,7 +992,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
"version": "3.11.4"
}
},
"nbformat": 4,