Foundations to Frontier
Tests
E. Deep Learning › E3 Optimizers (one page each)

Sophia

Sophia (second-order clipped stochastic optimisation) divides the momentum by a cheap estimate of the Hessian's diagonal, refreshed every few steps, and clips each coordinate's step so a bad curvature estimate cannot cause a huge move. Its authors reported reaching GPT-2's loss in half the steps of Adam. This page covers the update, Hutchinson's diagonal estimator, the clipping safeguard and why the step is Newton-like when unclipped. In the exercises you show Hutchinson's estimator is unbiased with noise set by the off-diagonal Hessian entries, then compare Sophia and Adam on a small network and find, honestly, that Adam reached a loss 20 times lower because Sophia clipped 75–97% of its coordinates.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Curvature-aware steps with a safety limit

Newton’s method divides the gradient by the curvature: it takes big steps where the loss is flat and small steps where it is steep. Computing the full curvature of a large network is impossible, but its diagonal can be estimated cheaply now and then. Sophia divides its momentum by that estimate. Because estimates can be wrong (even negative), it caps every coordinate’s step at a fixed size.

Optimizer lab: Sophia versus plain gradient descent
Sophia: final loss 0.067 (target reached at step 47) · gradient descent: final loss 2.2e-9 (step 22)
Orange: the chosen optimizer; grey dashed: plain gradient descent with the same learning rate. Darker shading means higher loss. On the ill-conditioned bowl, raise κ and watch gradient descent zig-zag across the narrow direction; on the saddle it stalls near the flat start; on the plateau its gradients are tiny. Adaptive methods rescale each direction; momentum methods build speed along consistent directions. The target is 1% of the starting loss (for the saddle, a loss below −0.2; its minima are at −0.25).
Why engineers care

Pre-training large language models costs millions of dollars in compute, so an optimiser that halves the number of steps is valuable. Sophia claimed that on GPT-2-scale models. Later independent benchmarks found smaller or no gains once Adam was carefully tuned, which is a reminder to test such claims on your own workload.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Sophia divides the momentum by…
  2. 2. Clipping in Sophia guards against…
  3. 3. Sophia's curvature estimate is refreshed…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Momentum: m ← β₁m + (1 − β₁)g (β₁ = 0.96).
  • Curvature: every k steps (k = 10), h ← β₂h + (1 − β₂)ĥ (β₂ = 0.99), with ĥ an estimate of diag(H).
  • Update: w ← w − η·clip(m/max(γh, ε), −1, 1), with γ around 0.01–0.1.
  • Hutchinson estimate: ĥ = u ⊙ (Hu) for a random ±1 vector u; it needs one Hessian-vector product. Example: H = [[2, 1], [1, 3]], u = (1, −1): Hu = (1, −2), so ĥ = (1, 2), while the true diagonal is (2, 3). Averaging many samples converges to it.
  • Newton-like: on a 1-D quadratic, m ≈ hx, so the unclipped step is η·x/γ. With η = γ it is exactly Newton’s step.
  • Clipped steps: if h is tiny or negative, the ratio is huge, so the step is ±η, as in Lion.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Hutchinson's estimate uses…
  2. 2. With η = γ and no clipping, Sophia on a quadratic takes…
  3. 3. For negative curvature estimates, Sophia's step is…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Sophia in code

Exercise 1.

  • Averaging 20 000 Hutchinson samples recovered the diagonal of a random 6 × 6 symmetric Hessian to within sampling error.
  • One sample’s standard deviation for entry i matched √(Σⱼ≠ᵢ Hᵢⱼ²), for example 1.129 measured against 1.122 predicted.
  • For a diagonal Hessian a single sample is exact.

Practice.

  • Hessian-vector products cost about one extra backward pass, using automatic differentiation or finite differences of gradients. Refreshing every 10 steps keeps the overhead small (the paper reports about 5% for Sophia-G, which also uses part of the batch for the estimate).
  • The paper’s second variant, Sophia-G, uses a Gauss–Newton–Bartlett estimate built from sampled labels; it is always non-negative.
  • Tune η and γ together. Watch the fraction of clipped coordinates: if nearly all are clipped, Sophia is effectively a sign-of-momentum method.
# github.com/Liuhong99/Sophia : SophiaG(params, lr=2e-4, betas=(0.965, 0.99), rho=0.01, weight_decay=0.1)
🐍 Exercise 1: Hutchinson's estimator and Sophia's step
Estimate the Hessian's diagonal with Rademacher probes, check its bias and noise, and implement Sophia's clipped step.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Hutchinson noise for entry i comes from…
  2. 2. Refreshing the estimate every 10 steps makes its cost…
  3. 3. Sophia-G's estimate is always…

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

Theory

Unbiasedness. For Rademacher u, E[uᵢuⱼ] = δᵢⱼ, so E[uᵢ(Hu)ᵢ] = Σⱼ HᵢⱼE[uᵢuⱼ] = Hᵢᵢ. The variance of one sample is Σⱼ≠ᵢ Hᵢⱼ², so the estimate is noisy when the Hessian is far from diagonal.

Why divide by curvature. In a direction with curvature h, a step of size g/h reaches the minimum of the local quadratic. Adam divides by √(E[g²]), which scales with the gradient, not the curvature. Liu et al. argue this makes Adam’s steps poorly sized when curvature varies strongly across directions, as in language models.

Why clip. Non-convex losses have negative and near-zero curvature, where g/h is huge or points uphill. Clipping caps each coordinate’s move at η, which bounds the step even when the estimate is bad. Liu et al. prove a convergence rate that does not depend on the condition number for a class of convex functions.

In exercise 2 75–97% of coordinates were clipped. Sophia then behaves like Lion with a different momentum, and its curvature information is mostly unused.

📝 Quick check (Level 3)
0/3 answered
  1. 1. E[uᵢuⱼ] for Rademacher u is…
  2. 2. Adam's denominator scales with…
  3. 3. When almost all coordinates are clipped, Sophia behaves like…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Sophia on a small network

History.

  • M. Hutchinson (1989) introduced the randomised trace estimator.
  • Z. Yao et al. (2021) used Hutchinson’s diagonal in AdaHessian.
  • H. Liu, Z. Li, D. Hall, P. Liang and T. Ma (2023) introduced Sophia and reported 2× fewer steps than Adam on GPT-2-scale pre-training.
  • Later benchmarks (for example Kaddour et al., 2023) found much smaller gains for Sophia and similar methods once baselines were tuned.

Exercise 2. The 2–32–32–3 tanh network on spirals: mini-batches of 32, 400 steps, mean full-data loss over the last 50 steps, two seeds. Sophia uses a finite-difference Hessian-vector product every 10 steps.

η Adam Sophia γ = 0.01 Sophia γ = 0.1
0.001 0.457 0.126 0.127
0.003 0.076 0.020 0.011
0.01 0.013 0.051 0.023
0.03 0.0021 0.312 0.219
0.1 0.0006 3.42 2.91
  • Tuned Adam reached a loss about 20× lower than tuned Sophia (0.0006 against 0.011).
  • Sophia clipped most coordinates: 97% at γ = 0.01 and 75% at γ = 0.1. The curvature estimates on this small, noisy problem were often tiny or negative, so the clipping safeguard took over.
  • Lesson: this does not test Sophia’s claim, which concerns language-model pre-training at scale. But it shows how to diagnose it: if the clip fraction is near 1, Sophia is not using curvature.
🐍 Exercise 2 (research): Sophia versus Adam
Train the spirals network with Adam and with Sophia, sweep the learning rate and γ, and measure the fraction of clipped coordinates.

Sophia in AI, robotics and aerospace

  • Language-model pre-training is where Sophia was proposed and evaluated.
  • Hessian-vector products (the same tool) are used for sensitivity analysis and uncertainty estimates in safety-critical models.
  • When adopting a new optimiser, measure on your own workload with tuned baselines, as exercise 2 does.

References

  1. H. Liu, Z. Li, D. Hall, P. Liang, T. Ma (2023). “Sophia: a scalable stochastic second-order optimizer for language model pre-training.” arXiv:2305.14342 (ICLR 2024).
  2. M. F. Hutchinson (1989). “A stochastic estimator of the trace of the influence matrix for Laplacian smoothing splines.” Communications in Statistics – Simulation and Computation 18(3).
  3. Z. Yao, A. Gholami, S. Shen, M. Mustafa, K. Keutzer, M. Mahoney (2021). “AdaHessian: an adaptive second order optimizer for machine learning.” Proc. AAAI.
  4. J. Kaddour, O. Key, P. Nawrot, P. Minervini, M. Kusner (2023). “No train no gain: revisiting efficient training algorithms for transformer-based language models.” Advances in NeurIPS.