Skip to content
Chapter 7 of 8

Scaling laws

Here is the empirical discovery that drove the entire modern era of AI. As you scale a model up, more parameters, more data, more compute, the loss does not fall randomly or unpredictably. It falls along a POWER LAW. Write loss as a constant times size to a negative power and you have it, and the signature of a power law is that it becomes a straight line when you plot the logarithm of the loss against the logarithm of the size.

That predictability is what changed the economics of the field. If loss follows a known curve, you can FORECAST the loss of a model before you train it, which is how labs justified spending enormous sums on a single larger run: they could see roughly where it would land. You will reproduce the shape from first principles. You build a target whose structure has power-law decaying components, approximate it with models of increasing capacity, and watch the leftover error trace a clean power law. Then you fit the exponent with least squares and confirm it matches the value the math predicts.
logloss log (size N) slope = the scaling exponent a power law: double the size, the loss drops by a fixed fraction, predictably
A scaling law is a straight line on a log-log plot
the scaling law
loss = A * N^(-alpha)

loss equals a constant times model size to a negative power, so bigger models have lower loss in a predictable way

on a log-log plot
log(loss) = log(A) - alpha * log(N)

take logs of both sides and the power law becomes a straight line whose slope is minus the scaling exponent

The lab: read it, then run it

labs/transformers/tf7-scaling-laws.py
#!/usr/bin/env python3
"""
LAB TF7: Scaling laws, why loss falls in a straight line on a log-log plot.

The empirical discovery that drove the whole modern era: as you scale a model's
size (or its data, or its compute), the loss does not drop randomly. It follows a
POWER LAW. Loss = A * N^(-alpha), which is a straight line when you plot log(loss)
against log(size). That predictability is what let labs justify spending millions
on a bigger run: you can forecast the loss before you train.

You reproduce the shape from first principles here. Build a target signal whose
frequency components have power-law-decaying strength, then approximate it with
models of increasing capacity N (keeping the first N components). The leftover
error is a clean power law in N. You fit the exponent in log-log space with least
squares and PROVE it matches the known truth and that the fit is near-perfect.

Run: python3 modules/academy-content/labs/transformers/tf7-scaling-laws.py
"""
import sys
import numpy as np

rng = np.random.default_rng(29)  # reproducible

# STEP 1: a target with power-law spectrum. Component k has amplitude k^(-P).
# Truncating to the first N components leaves residual energy sum_{k>N} k^(-2P),
# whose square root (the RMSE) is a power law in N. That is our "loss vs scale".
P = 1.2
KMAX = 4000
k = np.arange(1, KMAX + 1)
amp = k ** (-P)                    # true component strengths
total_energy = np.sum(amp ** 2)
print("STEP 1: target signal with power-law spectrum, exponent P=%.2f, %d components" % (P, KMAX))

# STEP 2: measure loss (residual RMSE) at increasing model capacity N.
capacities = [2, 4, 8, 16, 32, 64, 128, 256, 512]
losses = []
for N in capacities:
    residual_energy = np.sum(amp[N:] ** 2)     # energy in components we dropped
    losses.append(np.sqrt(residual_energy))
losses = np.array(losses)
print("STEP 2: loss vs capacity N (bigger N, lower loss):")
for N, L in zip(capacities, losses):
    print("   N=%4d  loss=%.6f" % (N, L))

# STEP 3: fit a line to log(loss) vs log(N). The slope IS the scaling exponent.
logN = np.log(np.array(capacities, dtype=float))
logL = np.log(losses)
slope, intercept = np.polyfit(logN, logL, 1)
# R^2 of the log-log fit: how straight is the line (how clean is the power law).
pred = slope * logN + intercept
ss_res = np.sum((logL - pred) ** 2)
ss_tot = np.sum((logL - logL.mean()) ** 2)
r2 = 1 - ss_res / ss_tot
print("STEP 3: log-log least-squares fit: slope=%.3f  R^2=%.5f" % (slope, r2))

# STEP 4: the invariants.
#  (a) loss strictly decreases with scale (diminishing but real returns).
#  (b) the relationship is a power law: the log-log fit is essentially a line.
#  (c) the fitted exponent matches theory. Truncating a k^(-P) spectrum gives an
#      RMSE tail ~ N^(-(P-0.5)), so the expected slope is -(P-0.5).
expected_slope = -(P - 0.5)
monotonic = bool(np.all(np.diff(losses) < 0))
straight = r2 > 0.99
matches = abs(slope - expected_slope) < 0.06
print("STEP 4: loss monotically falls: %s | log-log is a line (R^2>0.99): %s" % (
    "YES" if monotonic else "NO", "YES" if straight else "NO"))
print("        fitted exponent %.3f vs theory %.3f (match): %s" % (
    slope, expected_slope, "YES" if matches else "NO"))

ok = monotonic and straight and matches
print("")
print("LOSS FOLLOWS A POWER LAW IN SCALE (FITTED EXPONENT MATCHES): %s" % ("YES" if ok else "NO"))
if not ok:
    sys.exit(1)
print("")
print("A straight line on a log-log plot. That is a scaling law, and its slope lets")
print("you predict the loss of a model you have not trained yet.")
Lab (read-only)
tf7-scaling-laws.py

Measure loss versus model capacity on a power-law target, fit the exponent in log-log space, and prove the curve is a straight line whose slope matches theory.

Proves: LOSS FOLLOWS A POWER LAW IN SCALE (FITTED EXPONENT MATCHES): YES

The measured loss fell smoothly as capacity grew, and when you took logs it lined up almost perfectly straight: an R-squared of 0.999 on the log-log fit. The fitted slope, negative sixty-eight hundredths, landed right on the value the theory predicts for that target, so this was not a curve you eyeballed into being a line, it was a genuine power law with a slope you can derive. That is the whole significance of scaling laws in one lab. The relationship between scale and loss is lawful and predictable, so you can extrapolate: measure a few small models, fit the line, and read off roughly where a much larger one will land. Real scaling laws add data and compute as their own axes and even predict the compute-optimal balance between them, but the shape is the one you just fit. One chapter left, the payoff: why this architecture beat everything that came before it.
Check your understanding
  1. 1. What is a scaling law?

  2. 2. Why does a power law look like a straight line on a log-log plot?

  3. 3. In the lab, what did the log-log fit show?

  4. 4. Why do scaling laws matter economically?