--- license: mit library_name: pytorch tags: - text-generation - from-scratch - character-level - tiny-model - educational --- # TinyGPT: a 1M parameter model trained from scratch A tiny character-level transformer (about 1.04 million parameters), written in PyTorch and trained **from scratch** (not fine-tuned from any other model) on a small "friendly Python teacher" dataset. It is a learning project and is not meant to be useful for real questions. ## Model details | | | |---|---| | Parameters | ~1.04M | | Layers / heads / embedding size | 5 / 4 / 128 | | Context length | 128 characters | | Tokenizer | character-level (108 characters, see `vocab.json`) | | Training data | 3,000 question/answer examples (~1.5M characters) | | Final loss | train ~0.16, validation ~0.89 | ## What it can and can't do - It picks up the **style** of the training data: friendly tone, emojis, code blocks. - It mostly **memorizes** its training answers. The training set has only about 150 distinct questions, so it does not understand new questions, and for unseen wording it tends to produce confident-looking but wrong or nonsensical text. - Questions worded like the training data work best, for example "How does a while loop work?" or "What is recursion?". ## Usage Put `tinygpt.pt` (the weights) in the same folder as the other files, then: ```bash pip install torch python generate.py "How does a while loop work?" ``` The model expects this prompt format (`generate.py` adds it for you): ``` ### Question: ### Answer: ``` ## Files - `tinygpt.pt`: trained weights (PyTorch state dict) - `model.py`: the model code (needed to load the weights) - `config.json`: model settings - `vocab.json`: the character vocabulary, in order - `generate.py`: example script ## Training Trained for 3,000 steps with AdamW (lr 2e-3), batch size 64, on 1,529,604 characters, with 15 held-out questions used for validation.