fundamentals

Gradient descent

Gradient descent is the workhorse iterative optimizer used whenever a closed-form maximum-likelihood / maximum-a-posteriori solution is unavailable, undesirable, or just being demonstrated alongside the analytic answer. The package ships a tight, dataclass-returning helper that supports analytic gradients, a finite-difference fallback, and full iterate / loss history.

In this codebase

End-to-end snippet

import numpy as np
from active_inference import (
    gradient_descent, gd_linear_regression, mle_linear_regression,
)

# 1-D minimization with an analytic gradient.
result = gradient_descent(
    loss_fn=lambda x: (x - 3.0) ** 2,
    grad_fn=lambda x: 2.0 * (x - 3.0),
    x0=0.0, learning_rate=0.1, max_iter=200,
)
print(result.x, result.converged, result.n_iterations)

# Vectorized regression with L2 regularization.
rng = np.random.default_rng(0)
X = rng.normal(size=(200, 3))
y = X @ np.array([1.0, -0.5, 0.3]) + 0.5 + rng.normal(scale=0.1, size=200)

iterative = gd_linear_regression(X, y, learning_rate=1e-3, max_iter=2000)
analytic = mle_linear_regression(X, y)
print("‖θ_GD − θ_OLS‖:", np.linalg.norm(iterative.theta - analytic))

The stability bound

For a least-squares loss the gradient is linear in θ with Hessian H = XᵀX + λ I. The descent recursion contracts iff::

learning_rate · λ_max(H) < 2

In practice we usually pick learning_rate ∈ [1e-4, 1e-2] for the chapter scripts and check convergence against the closed-form solution.

Pitfalls

See also