Power Laws in Deep Learning › Fitting Power Laws

Michael BrenndoerferOctober 21, 20259 min read
Language AI HandbookAll chapters
Part 23 of 67Scaling Laws
Chapter 162Power Laws in Deep Learning › Fitting Power Laws
Reading section 5 of 11 · Fitting Power Laws · 9 minSection 5 of 11 · 9 min

Book contents

Section 5 of 11 · 9 min read

Power Laws in Deep Learning › Fitting Power LawsOne section · Sections in this chapter5 / 11 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

To use power laws predictively, we need to estimate the parameters aa and bb from data. This is where the log-log transformation proves most useful in practice. Instead of wrestling with nonlinear curve fitting, which is sensitive to initialization, prone to local minima, and statistically tricky, we can use the simple, well-understood tools of linear regression on log-transformed data.

The strategy is straightforward: take logs of both the inputs and outputs, then apply ordinary linear regression. The slope of the resulting fit is the exponent bb, and the intercept is log⁡a\log a. This approach has several practical advantages over directly fitting y=axby = ax^b with nonlinear methods. Most importantly, it is numerically stable across the wide ranges typical of scaling experiments (compute budgets spanning 15 orders of magnitude, model sizes spanning 5 or more orders of magnitude). Nonlinear optimization applied directly to these ranges would require careful initialization and often fails to converge reliably.

Given data points (xi,yi)(x_i, y_i), we transform to (log⁡xi,log⁡yi)(\log x_i, \log y_i) and fit the linear model:

log⁡y=log⁡a+blog⁡x\log y = \log a + b \log x

where:

  • log⁡y\log y: the log-transformed dependent variable, computed from our measured loss values
  • log⁡x\log x: the log-transformed independent variable, computed from our resource measurements (parameters, data, compute)
  • log⁡a\log a: the y-intercept in log space, recovered from the linear regression
  • bb: the slope, which equals the power law exponent directly

Comparing this to the standard linear form Y=mX+cY = mX + c makes the connection explicit. The slope mm in our transformed regression equals bb. The intercept cc equals log⁡a\log a, which we convert back to the original coefficient via exponentiation: a=eca = e^c if using natural logarithms, or a=10ca = 10^c if using base-10 logarithms.

This approach works because ordinary least squares regression minimizes the sum of squared residuals. When we apply it to log-transformed data, we're effectively minimizing:

∑i(log⁡yi−log⁡a−blog⁡xi)2=∑i(log⁡yiaxib)2\sum_i \left( \log y_i - \log a - b \log x_i \right)^2 = \sum_i \left( \log \frac{y_i}{a x_i^b} \right)^2

This objective function penalizes relative errors, not absolute errors. A prediction that's off by 10% at loss 3.0 contributes the same squared residual as a prediction that's off by 10% at loss 1.0. This is exactly the right behavior when values span many orders of magnitude: relative errors are the real measure of fit quality.

Pitfalls in Power Law Fitting

Fitting power laws correctly requires care. Several subtle issues can trip up even experienced analysts, and understanding them will help you interpret published scaling results critically.

Use log-transformed data for regression, not raw data. This point deserves emphasis because it's counterintuitive. You might think, "I want to fit y=axby = ax^b, so I should use nonlinear regression to minimize the sum of squared errors in yy." The problem is that this approach overweights large values. If your losses range from 10 at small scales to 0.1 at large scales, the squared errors at the small end (potentially hundreds) will dwarf squared errors at the large end (fractions of 0.01). The optimizer focuses on matching the high-loss region while ignoring the low-loss region. By contrast, fitting in log space minimizes relative errors: a 10% miss at loss 10 contributes as much to the objective as a 10% miss at loss 0.1. This is usually what we want when values span many orders of magnitude.

Consider weighted regression for heteroskedastic data. If measurement uncertainty varies across your data range, weighted regression can improve estimates. For instance, if small-scale experiments are run once while large-scale experiments are run multiple times with averaged results, the large-scale points have lower variance and deserve more weight in the fit. In practice, this issue is common because large training runs are expensive and researchers typically replicate small-scale experiments more than large-scale ones.

Validate with residual analysis. After fitting, examine residuals in log space. If the residuals show systematic patterns, such as being consistently positive at small scales and negative at large scales, this indicates the power law model may not be appropriate. True power law data should produce residuals that scatter randomly around zero with no systematic trend. A curved residual pattern suggests you might be dealing with multiple regimes or a more complex functional form. This residual analysis step is often skipped in published work but is needed for building confidence in the fit.

Check for outliers before fitting. A single outlier in log space can have a disproportionate effect on the estimated slope. When fitting scaling laws, examine whether any data points deviate substantially from the trend before including them in the regression. Common sources of outliers include training runs that were stopped early, runs with suboptimal hyperparameters, or runs where something went wrong with the data pipeline.

Beware of the data selection effect. Researchers typically publish scaling experiments where the power law fit is clean. Experiments where the relationship is noisy or non-power-law are often not published. This means the published literature may systematically overestimate how cleanly power laws hold in practice. When you run your own scaling experiments, expect more noise than the published papers suggest.

Let's implement power law fitting and see these principles in action:

In[9]:
Code
def fit_power_law(x, y):
    """
    Fit a power law y = a * x^b using linear regression in log space.

    Returns: a, b, r_squared
    """
    log_x = np.log(x)
    log_y = np.log(y)

    # Linear regression in log space
    slope, intercept = np.polyfit(log_x, log_y, 1)

    # Convert back to power law parameters
    b = slope
    a = np.exp(intercept)

    # Calculate R-squared
    y_pred_log = intercept + slope * log_x
    ss_res = np.sum((log_y - y_pred_log) ** 2)
    ss_tot = np.sum((log_y - np.mean(log_y)) ** 2)
    r_squared = 1 - ss_res / ss_tot

    return a, b, r_squared

Let's test our fitting function on synthetic data where we know the true parameters. This makes possible us verify that the fitting procedure recovers the correct exponent and coefficient before applying it to real data:

In[10]:
Code
# Generate synthetic scaling data with noise
np.random.seed(42)
model_sizes = np.array([10e6, 50e6, 100e6, 500e6, 1e9, 5e9, 10e9])  # Parameters
true_a = 10.0
true_b = -0.076
noise_factor = 0.02  # 2% relative noise

losses = true_a * np.power(model_sizes, true_b)
losses_noisy = losses * (1 + np.random.randn(len(losses)) * noise_factor)
In[11]:
Code
# Fit power law to noisy data
a_fit, b_fit, r_squared = fit_power_law(model_sizes, losses_noisy)
Out[12]:
Console
True parameters: a = 10.000, b = -0.0760
Fitted parameters: a = 9.867, b = -0.0748
R-squared: 0.9934

The fitted parameters closely match the true values, with the coefficient recovered accurately and the exponent nearly identical. The R-squared value above 0.99 indicates an excellent fit. This shows that our log-space regression approach effectively recovers power law parameters even with noisy data. The coefficient may deviate slightly because noise interacts with the curve differently at different scales, but the exponent, which controls the slope of the log-log line, is recovered accurately.

Out[13]:
Visualization
Log-log plot with data points and fitted power law line.
Power law fitted to noisy scaling data on log-log axes. The fitted curve (red) closely matches the true relationship (green dashed) within the observed range and extrapolates into the blue-shaded region well beyond any observed data points, illustrating the predictive power of the method.

The fit recovers the true parameters with reasonable accuracy. More importantly, it lets extrapolation: we can predict performance for model sizes we haven't trained yet. The blue shaded region shows where we're extrapolating well beyond observed data, yet the fitted curve closely tracks the true relationship. This predictive power is what makes scaling laws so useful for planning large language model training runs.

To validate that our power law fit is appropriate, we examine the residuals, the differences between observed and predicted values in log space. A good fit produces residuals that scatter randomly around zero with no systematic pattern:

Out[14]:
Visualization
Scatter plot of residuals in log space versus model size, showing random scatter around a zero reference line.
Residual analysis for the power law fit shows log-space residuals versus log model size. The random scatter around zero without any systematic trend confirms the power law model is appropriate for this data range.

The residuals scatter randomly within a narrow band around zero, confirming that our power law model is appropriate for this data. If we saw a curved pattern in the residuals, for example, positive at both extremes and negative in the middle, it would suggest the true relationship isn't a simple power law. The residual plot is your first diagnostic when a scaling law fit looks suspicious.