TinyGPT from Scratch (12.3M)
A small (12,297,984 parameters) decoder-only GPT language model trained from random weights in PyTorch on a deduplicated subset of roneneldan/TinyStories, using a custom from-scratch byte-level BPE tokenizer (vocab_size = 4,096).
- Source Code: 12somyasahu/tiny-gpt-from-scratch
Dataset Credit & License
- Dataset:
roneneldan/TinyStories(Eldan & Li, 2023) - Dataset License:
cdla-sharing-1.0(Community Data License Agreement β Sharing β Version 1.0) - Splits Used:
- Train:
226,132unique stories (51,523,415tokens) - Validation:
2,000unique stories (407,388tokens) - Test:
2,000unique stories (406,775tokens) - Verified
0duplicate stories across train, validation, and test splits.
- Train:
How to Load and Generate
import torch
from modeling_tiny_gpt import load_from_folder
device = "cuda" if torch.cuda.is_available() else "cpu"
model, tokenizer = load_from_folder(".", device=device)
prompt = "Once upon a time, a little robot"
ids = tokenizer.encode(prompt)
x = torch.tensor([ids], dtype=torch.long, device=device)
out = model.generate(x, max_new_tokens=100, temperature=0.8, top_k=40, eos_id=tokenizer.eos_id)
print(tokenizer.decode(out[0].tolist(), skip_special_tokens=True))
Evaluation Results
Trained on an NVIDIA GeForce RTX 3050 6GB Laptop GPU (fp16 AMP, 4,500 steps, best checkpoint at step 4,250):
| Model | Val Loss | Val Perplexity | Test Loss | Test Perplexity |
|---|---|---|---|---|
| Bigram Baseline (Laplace-smoothed) | 3.6836 |
39.79 |
3.6744 |
39.43 |
| TinyGPT (12.3M params) | 1.9156 |
6.79 |
1.8853 |
6.59 |
Sample Outputs (Good & Bad)
- Good (
temp=0.8, top_k=40):Once upon a time, a little girl named Lily found a toy car that she loved to play with. She showed it to her mommy and said, "Mommy, this car is so pretty!" Her mommy replied, "Yes, it is very pretty. It is an ordinary toy."
- Bad β High Temperature (
temp=1.8, top_k=None):Once upon a time, there was a big gippedouldAnna. Sp sniff very wellicescapvered like stars growingse bent ones down branches...
- Bad β Greedy Decoding (
temp=0.0, top_k=None):The cat sat on the mat and watched the cat. The cat was happy and said, "Thank you, cat. You are a good friend." The cat and the cat became good friends.
Limitations
- Narrow domain: Trained only on a TinyStories subset; it cannot answer general-knowledge questions, follow instructions, or write code.
- Short context (
256tokens) and small capacity (12.3Mparams): Long-range consistency is limited. - Decoding sensitivity: Greedy decoding (
temperature=0) can repeat phrases, while high temperatures (>1.2) withouttop_kproduce subword gibberish.
Acknowledgments
Built with AI assistance using Google Antigravity CLI powered by Gemini 4 Hydrogen (Medium).
- Downloads last month
- 45