#184Scaling laws — estimate the power-law exponentMedium

Scaling laws — estimate the power-law exponent

Background

Empirical scaling laws say test loss falls as a power law in model size:

L(n)AnαlogL=logAαlognL(n) \approx A\,n^{-\alpha} \quad\Longleftrightarrow\quad \log L = \log A - \alpha \log n

So on log–log axes the loss-vs-parameters curve is a straight line whose slope is α-\alpha. Estimating the exponent α\alpha is just fitting that line.

Problem statement

Implement scaling_exponent(ns, losses) that estimates α\alpha from measured (n, loss) pairs via a log–log linear fit.

Input

  • ns — array-like of parameter counts nn (all >0> 0).
  • losses — array-like of corresponding test losses (all >0> 0), same length.

Output

Returns a Python float: the estimated exponent α\alpha (the negative slope of logL\log L vs logn\log n).

Examples

Example 1 — a clean power law

Input:  ns = [1, 4, 16, 100], losses = [10, 5, 2.5, 1.0]
Output: 0.5

Explanation: L=10n0.5L = 10\,n^{-0.5}, so the log–log slope is 0.5-0.5 and α=0.5\alpha = 0.5.

Example 2

Input:  ns = [1, 2, 4, 8], losses = [2, 1, 0.5, 0.25]
Output: 1.0

Explanation: L=2n1L = 2\,n^{-1}, slope 1-1, exponent 11.

Constraints

  • Fit a line to log(losses) vs log(ns) (natural log is fine).
  • Return α=slope\alpha = -\text{slope} as a float.

Notes

  • A larger α\alpha means loss drops faster as you scale up — but the law only holds when data and compute scale alongside nn.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Clean power law example L = 10 n^-0.5
  • Reference L = 2 n^-1 gives alpha 1.0
  • Sample three-point decade fit