AI & Machine Learning

MyGPT: A GPT Language Model Built and Trained From Scratch

Complete short stories from a 17.3M-parameter GPT trained on a laptop

About This Project

Most “AI projects” today are a few lines of code calling someone else’s model. That is often the right choice for a business, but it hides how these systems work: how text becomes numbers, how a transformer learns which words matter, and why training data matters more than anything else.

MyGPT is a small GPT-style language model built entirely from first principles in PyTorch: the tokenizer, the transformer, the training loop and the text generator. It was trained on a single laptop GPU and is served through a Django web app, where a visitor types the first line of a story and the model writes the rest.

What makes it more than a tutorial exercise is the path from a model that didn’t work to one that does. Early versions trained on a few Wikipedia pages produced little more than “the the and <UNK>”. Version 5 changed the data, the tokenizer, the architecture and the hardware, and now writes complete short stories with dialogue, a problem and an ending.

Key Features

🧠 Transformer Written From Scratch
An 8-layer, 17.3-million-parameter decoder-only transformer with multi-head causal self-attention, pre-norm residual blocks, GELU feed-forward layers and tied input/output embeddings, written by hand rather than loaded from a library.

🔤 Own Byte-Level BPE Tokenizer
An 8,192-token byte-pair-encoding vocabulary trained on the dataset itself. Any text can be encoded, so the unknown-word problem of the earlier word-level versions is gone.

📚 Large-Scale Training Data
2.2 GB of short stories from the public TinyStories dataset, turned into 536 million tokens. The model saw 262 million of them during training, over 1,000 times more text than the earlier versions.

⚡ GPU Training on a Laptop
7.5 hours on a 4 GB NVIDIA Quadro T2000 with gradient accumulation, cosine learning-rate schedule, AdamW and gradient clipping. Profiling showed this GPU runs half-precision three times slower than full precision, so training was switched to full precision and sped up threefold.

💾 Checkpoints & Resume
Progress is saved every 500 steps, with the best model kept separately and a sample story written at each checkpoint, so a crash or restart never loses more than a few minutes.

🌐 Django Web App
A chat-style page with example prompts, creativity and length controls, and live model stats. Visitors only see their own stories, requests are protected against cross-site forgery, and each visitor is rate-limited.

Results Delivered
Validation loss fell from 2.62 to 1.35 over 16,000 training steps. Early samples mixed up characters’ names and lost the plot; the final model keeps one character throughout, writes natural dialogue and brings each story to an ending, generating a full story in about two seconds.

The project shows the full machine learning workflow, from data collection, tokenization and model design to GPU training, evaluation and deployment as a web app, the same skills used to build and integrate AI into business automation.