File size: 1,949 Bytes
e96b27c
 
3a9fc0f
 
 
 
 
 
 
e96b27c
3a9fc0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
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:
<your 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.