Skip to content

Instantly share code, notes, and snippets.

View JGalego's full-sized avatar
🕹️
ai-to-zx.com

João Galego JGalego

🕹️
ai-to-zx.com
View GitHub Profile
@JGalego
JGalego / microgpt.py
Last active February 23, 2026 22:44 — forked from karpathy/microgpt.py
microgpt in 100 lines of less-than-ideal Python code
"""Atomic GPT training & inference in pure Python. @karpathy, modified by @JGalego"""
import os, math, random; random.seed(42)
if not os.path.exists('input.txt'):
import urllib.request
urllib.request.urlretrieve('https://raw.githubusercontent.com/karpathy/makemore/988aa59/names.txt', 'input.txt')
with open('input.txt', encoding='utf-8') as f: docs = [l.strip() for l in f if l.strip()]
random.shuffle(docs)
uchars = sorted(set(''.join(docs)))
BOS, vocab_size = len(uchars), len(uchars) + 1