Skip to content
Streak:0Loading your progress…Sign in

Module 13 · Lesson 1 of 2

How models learn

A model is a function with parameters; training nudges them to reduce a loss. ~30 min

Lesson notes

Training loop: predict → measure error with a loss function → compute the gradient (direction that increases loss) → step parameters the opposite way, scaled by the learning rate → repeat over the data for several epochs. LLM pre-training is this loop at enormous scale with next-token prediction as the loss.

Data splits: train on the training set, tune choices (hyperparameters, prompts!) on the validation set, and report once on a held-out test set. Overfitting = great training score, worse validation score. Leakage = test information sneaking into training (duplicates across splits, future data) — it makes results look better than reality.

Mean squared error and its gradient for y ≈ w·x + b
pred = w * x + b
loss = np.mean((pred - y) ** 2)
dw = 2 * np.mean((pred - y) * x)
db = 2 * np.mean(pred - y)
w -= lr * dw
b -= lr * db

Practice

  1. 1. Leak-free data split

    medium25 XP

    Write split(records, key, ratios=(0.8, 0.1, 0.1), seed=0) returning (train, val, test). Records sharing the same key(record) (e.g. same customer) must land in the same split to prevent leakage. Shuffle the unique keys with random.Random(seed) (sorted first, for determinism), then assign keys to splits by the cumulative ratios over the number of keys.

    Ctrl/⌘ + Enter runs the tests · Esc then Tab leaves the editor

  2. 2. Gradient descent with NumPy

    hard50 XP

    Write fit_line(x, y, lr=0.05, epochs=500) -> (w, b, losses) fitting y ≈ w·x + b by full-batch gradient descent on MSE, starting from w=b=0. losses is the MSE recorded at the start of each epoch. Return floats.

    Ctrl/⌘ + Enter runs the tests · Esc then Tab leaves the editor