File size: 5,128 Bytes
52db7c8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Import Dependencies"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import torch\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from src.model import GPTModel\n",
"from src.training import train\n",
"from src.inference import generate\n",
"from src.utils import vocab_size\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Decalre Hyperparams"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"batch_size = 64\n",
"block_size = 256\n",
"max_iters = 5000\n",
"eval_interval = 500\n",
"learning_rate = 3e-4\n",
"device = \"cuda:1\" if torch.cuda.is_available() else \"cpu\"\n",
"eval_iters = 200\n",
"n_embeds = 384\n",
"n_heads = 6\n",
"n_layers = 6\n",
"dropout = 0.2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Initialize Model and Optimizer"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"model = GPTModel(vocab_size, n_embeds, block_size, n_heads, n_layers, dropout, device)\n",
"model = model.to(device)\n",
"optimizer = torch.optim.AdamW(model.parameters(), lr=learning_rate)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Model Training"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Step 0: train loss 4.3249, val loss 4.3219\n",
"Step 500: train loss 2.0213, val loss 2.0953\n",
"Step 1000: train loss 1.6067, val loss 1.7813\n",
"Step 1500: train loss 1.4462, val loss 1.6380\n",
"Step 2000: train loss 1.3516, val loss 1.5810\n",
"Step 2500: train loss 1.2836, val loss 1.5376\n",
"Step 3000: train loss 1.2309, val loss 1.5148\n",
"Step 3500: train loss 1.1910, val loss 1.4904\n",
"Step 4000: train loss 1.1522, val loss 1.4822\n",
"Step 4500: train loss 1.1186, val loss 1.4838\n"
]
}
],
"source": [
"train(\n",
" model,\n",
" optimizer,\n",
" max_iters,\n",
" eval_interval,\n",
" eval_iters,\n",
" block_size,\n",
" batch_size,\n",
" device,\n",
")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load the model and Generate text"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hellows thence grown from thee.\n",
"Since thou hast raim, thou thast well were quarterned; and\n",
"ever man tree can saw for words word from her at hour\n",
"Whiles contrations or devoided from ere years;\n",
"Yea, foul vice, indelice on the bird of the\n",
"noble of Hermione.\n",
"\n",
"PARIS:\n",
"Sir, adies, sir, hate no choping but to your good.\n",
"\n",
"HENRY BOLINGBROKE:\n",
"Yes, to ask you might, foreweed.\n",
"\n",
"WARCK:\n",
"'Tis he made moust true.\n",
"\n",
"RORSET:\n",
"It is an hour fastal that cracknaf at the chase\n",
"Upon; you are your hearing news a daughter.\n",
"\n",
"KING EDWARD IV:\n",
"Tut, Lord Warwick, thou shouldst aft Rutlansps?\n",
"Thou tust but back hild, he countemn'd my lady's seal,\n",
"For access dead the treature moon! and the Englisting!\n",
"Thy vage for yonder see thou be donen?\n",
"O, count thou dost not Romeo, thou pratheeo sir,\n",
"That sweet thou feigh with no past blood on\n",
"Be see, here through on that find bears, if an\n",
"pretterinctors three and aspect die meeds thou,\n",
"Behing mine of thy denigning state lain business?\n",
"\n",
"SAMPSA:\n",
"Sir, ha! but thou refused? thyself food, gr\n"
]
}
],
"source": [
"model = torch.load(\"checkpoints/model.pth\", map_location={\"cpu\": device})\n",
"results = generate(\"hello\", model, block_size, 1000, device)\n",
"print(results)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|