Loading codebase...
Complete Source Code
Every single line of code behind Build Atlas, organized across all 9 parts. From tensor math, autograd, and byte-level BPE to TinyGPT training, LoRA chat tuning, hybrid RAG, autonomous agents, and continuous-batch serving.
Runnable checkpoint: builds a 2-layer MLP with the autograd engine and checks gradients against finite differences.
"""Part 3 · Neural networks from scratch: autograd by hand, then the same classifier as a PyTorch MLP."""
import common # noqa: F401
import numpy as np
import torch
from atlas.autograd import MLP, Value, cross_entropy, numerical_grad
from atlas.baseline import TEST_LESSONS, TOPICS, TfidfVectorizer, dataset, macro_f1, run
from atlas.data import load_lessons
from atlas.train import TopicMLP, evaluate, fit, seed_everything
common.banner("Part 3 · Neural networks from scratch")
# 1. Our autograd engine agrees with finite differences.
xs = [0.3, -1.2, 0.7]
def f(v):
a, b, c = [Value(x) for x in v]
return ((a * b + c.tanh()) ** 2 / (a.exp() + 1)).data
a, b, c = [Value(x) for x in xs]
out = (a * b + c.tanh()) ** 2 / (a.exp() + 1)
out.backward()
print("gradient check autograd:", [round(v.grad, 6) for v in (a, b, c)], " finite differences:", [round(g, 6) for g in numerical_grad(f, xs)])
# 2. A scalar MLP learns XOR (not linearly separable).
data = [([0.0, 0.0], 0), ([0.0, 1.0], 1), ([1.0, 0.0], 1), ([1.0, 1.0], 0)]
m = MLP([2, 8, 2])
for step in range(300):
loss = sum((cross_entropy(m(x), y) for x, y in data), Value(0)) / len(data)
for p in m.parameters():
p.grad = 0.0
loss.backward()
for p in m.parameters():
p.data -= 0.5 * p.grad
print(f"XOR with our MLP: loss {loss.data:.4f}, predictions {[int(np.argmax([o.data for o in m(x)])) for x, _ in data]} (target [0, 1, 1, 0])")
# 3. The Part 2 task, as a PyTorch MLP with early stopping on held-out *training* lessons.
lessons = load_lessons()
texts, y, slugs = dataset(lessons)
test = np.array([s in TEST_LESSONS for s in slugs])
train_lessons = sorted({s for s, t in zip(slugs, test) if not t})
val_lessons = set(train_lessons[::4])
va = np.array([s in val_lessons for s in slugs]) & ~test
tr = ~test & ~va
vec = TfidfVectorizer().fit([t for t, k in zip(texts, tr) if k])
X, Y = torch.tensor(vec.transform(texts), dtype=torch.float32), torch.tensor(y)
seed_everything(0)
model = TopicMLP(X.shape[1], 64, len(TOPICS))
hist = fit(model, X[tr], Y[tr], X[va], Y[va], lambda t, p: macro_f1(t, p, len(TOPICS)))
_, pred = evaluate(model, X[test], Y[test])
base = run(lessons)
print(f"\nMLP stopped after {hist['epochs_run']} epochs (best validation macro-F1 {hist['best_val_score']:.3f})")
print(f"{'':22}{'accuracy':>10}{'macro-F1':>10}")
print(f"{'softmax regression':22}{base['accuracy']:>10.1%}{base['macro_f1']:>10.3f}")
print(f"{'MLP (PyTorch)':22}{float((pred == y[test]).mean()):>10.1%}{macro_f1(y[test], pred, len(TOPICS)):>10.3f}")
print("\nCheckpoint: you can derive gradients by hand and train a network with a careful loop.")