{ "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 }