Skip to content

Choosing an optimizer: Adam, SOAP, SS-ESOAP and SSBroyden on one hard PINN

PINN optimizer_comparison_2d.py

The same network, the same initialisation and the same budget of 4000 steps, trained with four optimizers. The solution has a thin layer and a fast ripple on top of a smooth bump: the ill-conditioned kind of loss on which first-order methods stall.

Two rows of four panels during training, one column per optimizer (Adam, SOAP, SS-ESOAP, Adam then SSBroyden), all from one initialisation. Top: the network's u(x, y) on one green scale, with the dashed circle r = 0.3 where the exact solution has its layer. Bottom: the pointwise error |u - u*| on one orange-red scale set by the worst final error. All four fields soon look alike; the error row shows the difference: Adam ends with a ripple pattern of error spots, SOAP with one error patch, SS-ESOAP and SSBroyden almost clean.

Top: the trained fields, which soon look alike. Bottom: the error \(|u-u^\*|\) on one scale, where the optimizers differ.

The problem

Poisson's equation \(-\Delta u=f\) on the unit square, \(u=0\) on the boundary, with the manufactured solution

\[u^\* = \text{bump}(x,y) + \text{step across } r=0.3\ (\text{width}\approx0.017) + 0.05\,\sin8\pi x\,\sin8\pi y .\]

The jNO code

The forcing is derived from the exact solution by jNO:

d = jno.shape.rect(0, 0, 1, 1).structured(n=80).domain()
x, y, _ = d.variable("interior", split=True)
bubble = x * (1 - x) * y * (1 - y)

ring = tanh((0.09 - ((x - 0.5) ** 2 + (y - 0.5) ** 2)) / 0.01)  # a step across the circle r = 0.3, about 0.017 wide
u_exact = sin(π * x) * sin(π * y) * (0.6 + 0.4 * ring) + 0.05 * sin(8 * π * x) * sin(8 * π * y)
ue = u_exact.scalar.bind(x=x, y=y)
f = -(ue.xx + ue.yy)  # the forcing that makes u_exact the solution

Every run builds a fresh network from the same key and trains in chunks; optimizer state persists across solve calls, and net.optimizer(...) starts a new one, which is how the last run switches from Adam to the quasi-Newton SSBroyden after 3200 steps:

rate = lambda n: optax.exponential_decay(3e-3, n // 3, 0.5, end_value=1e-5)
runs = {  # name -> [(optimizer, steps), ...]
    "Adam": [(optax.adam(rate(STEPS)), STEPS)],
    "SOAP": [(jno.optimizers.soap(rate(STEPS)), STEPS)],
    "SS-ESOAP": [(jno.optimizers.ss_esoap(rate(STEPS)), STEPS)],
    "Adam, then SSBroyden": [(optax.adam(rate(WARM)), WARM), (jno.optimizers.ssbroyden(), STEPS - WARM)],
}

fine = jno.shape.rect(0, 0, 1, 1).structured(n=128).domain()  # where the snapshots are drawn
snapshots, errors = {}, {}
for name, stages in runs.items():
    net = jno.nn(foundax.mlp(2, hidden_dims=40, num_layers=3, activation=jax.nn.tanh, key=jax.random.PRNGKey(0)))
    u = (net(x, y) * bubble).scalar.bind(x=x, y=y)  # hard u = 0 on the boundary
    crux = jno.core([(-(u.xx + u.yy) - f).mse], domain=d)
    snapshots[name] = []
    for optimizer, steps in stages:
        net.optimizer(optimizer)
        for _ in range(steps // CHUNK):
            crux.solve(CHUNK, log=False)
            snapshots[name].append(np.asarray(crux.eval([u], domain=fine)[0]).ravel())
    u_h, u_ref = (np.asarray(v).ravel() for v in crux.eval([u, u_exact]))
    errors[name] = float(np.linalg.norm(u_h - u_ref) / np.linalg.norm(u_ref))  # reference: the u_exact above
    print(f"{name:22s} rel-L2 = {errors[name]:.2e}")

Result

optimizer rel. \(L^2\) error
Adam \(1.0\times10^{-1}\)
SOAP \(8.2\times10^{-2}\)
SS-ESOAP \(3.9\times10^{-2}\)
Adam, then SSBroyden \(3.6\times10^{-2}\)

The two optimizers that use curvature information reach less than half of Adam's error, and the script asserts that. The digits vary between runs and devices (another run: Adam \(9.8\times10^{-2}\), SOAP \(1.4\times10^{-1}\), SS-ESOAP \(3.1\times10^{-2}\), SSBroyden \(2.9\times10^{-3}\)); that ordering is what holds. Adam fits the large scale first and has not reached the ripple at this budget. Other entries of jno.optimizers (nncg, lm, engd, score) plug into the same loop; see the optimizer guide.

Full script

The whole script, as it runs in the tests
"""Four optimisers, one PINN: Poisson -Δu = f on the unit square with a thin ring layer and a fast ripple in u."""

import foundax
import jax
import numpy as np
import optax

import jno

jax.config.update("jax_enable_x64", True)  # the quasi-Newton phases need float64
run = jno.setup(__file__)
π, sin, tanh = jno.np.pi, jno.np.sin, jno.np.tanh
STEPS, WARM, CHUNK = 4000, 3200, 80  # the same budget for every optimiser; a snapshot every CHUNK steps

d = jno.shape.rect(0, 0, 1, 1).structured(n=80).domain()
x, y, _ = d.variable("interior", split=True)
bubble = x * (1 - x) * y * (1 - y)

ring = tanh((0.09 - ((x - 0.5) ** 2 + (y - 0.5) ** 2)) / 0.01)  # a step across the circle r = 0.3, about 0.017 wide
u_exact = sin(π * x) * sin(π * y) * (0.6 + 0.4 * ring) + 0.05 * sin(8 * π * x) * sin(8 * π * y)
ue = u_exact.scalar.bind(x=x, y=y)
f = -(ue.xx + ue.yy)  # the forcing that makes u_exact the solution

rate = lambda n: optax.exponential_decay(3e-3, n // 3, 0.5, end_value=1e-5)
runs = {  # name -> [(optimizer, steps), ...]
    "Adam": [(optax.adam(rate(STEPS)), STEPS)],
    "SOAP": [(jno.optimizers.soap(rate(STEPS)), STEPS)],
    "SS-ESOAP": [(jno.optimizers.ss_esoap(rate(STEPS)), STEPS)],
    "Adam, then SSBroyden": [(optax.adam(rate(WARM)), WARM), (jno.optimizers.ssbroyden(), STEPS - WARM)],
}

fine = jno.shape.rect(0, 0, 1, 1).structured(n=128).domain()  # where the snapshots are drawn
snapshots, errors = {}, {}
for name, stages in runs.items():
    net = jno.nn(foundax.mlp(2, hidden_dims=40, num_layers=3, activation=jax.nn.tanh, key=jax.random.PRNGKey(0)))
    u = (net(x, y) * bubble).scalar.bind(x=x, y=y)  # hard u = 0 on the boundary
    crux = jno.core([(-(u.xx + u.yy) - f).mse], domain=d)
    snapshots[name] = []
    for optimizer, steps in stages:
        net.optimizer(optimizer)
        for _ in range(steps // CHUNK):
            crux.solve(CHUNK, log=False)
            snapshots[name].append(np.asarray(crux.eval([u], domain=fine)[0]).ravel())
    u_h, u_ref = (np.asarray(v).ravel() for v in crux.eval([u, u_exact]))
    errors[name] = float(np.linalg.norm(u_h - u_ref) / np.linalg.norm(u_ref))  # reference: the u_exact above
    print(f"{name:22s} rel-L2 = {errors[name]:.2e}")

assert max(errors["SS-ESOAP"], errors["Adam, then SSBroyden"]) < 0.5 * errors["Adam"]  # the curvature-aware ones win