Recover unknown coefficients
PINN inverse_parameter.py
A measured curve is known to be a mix of three shapes, \(\sin\pi x\), \(\cos\pi x\) and \(x(1-x)\). How much of each? The three coefficients are trainable objects of the graph, and the misfit to the data is the only loss. The shapes are nearly dependent on \([0,1]\), so the optimiser has to find its way along a long, shallow valley.

Left: each coefficient divided by its true value. Right: the fitted curve on the data (dashed).
The problem
with synthetic data at \(a=3.14\), \(b=-2.71\), \(c=42\).
The jNO code
Each coefficient is a scalar parameter with its own optimiser, and the residual is the expression at the parameters minus the same expression at the truth:
field = lambda a, b, c: a * jno.np.sin(π * x) + b * jno.np.cos(π * x) + c * x * (1 - x)
k1, k2, k3 = jax.random.split(jax.random.PRNGKey(0), 3)
a = jno.np.parameter((1,), key=k1, name="a").optimizer(optax.adam(1e-2))
b = jno.np.parameter((1,), key=k2, name="b").optimizer(optax.adam(1e-2))
c = jno.np.parameter((1,), key=k3, name="c").optimizer(optax.adam(1e-2))
crux.solve(n) continues where the previous call stopped, with the optimiser state kept, so the run can be read in
chunks:
crux = jno.core([(field(a, b, c) - field(*truth)).mse])
for n in steps:
crux.solve(int(n), log=False)
path.append(np.ravel(crux.eval([a, b, c])))
Result
After 30 000 iterations all three coefficients are recovered to a relative error of at most \(4\times10^{-5}\): \(a=3.140\), \(b=-2.710\), \(c=42.000\). On the way, \(a\) overshoots to about three times its value while \(c\) stays low; only after about \(10^4\) iterations does the valley lead \(c\) to its value.
Full script
The whole script, as it runs in the tests
"""Three coefficients recovered from a synthetic field: a sin(pi x) + b cos(pi x) + c x (1 - x)."""
import os
from pathlib import Path
import jax
import numpy as np
import optax
import jno
run = jno.setup(__file__)
π = jno.np.pi
truth = np.array([3.14, -2.71, 42.0])
d = jno.Path(0.0, 0.0).line_to(1.0, 0.0).curve(size=0.01).domain()
x, _ = d.variable("interior")
field = lambda a, b, c: a * jno.np.sin(π * x) + b * jno.np.cos(π * x) + c * x * (1 - x)
k1, k2, k3 = jax.random.split(jax.random.PRNGKey(0), 3)
a = jno.np.parameter((1,), key=k1, name="a").optimizer(optax.adam(1e-2))
b = jno.np.parameter((1,), key=k2, name="b").optimizer(optax.adam(1e-2))
c = jno.np.parameter((1,), key=k3, name="c").optimizer(optax.adam(1e-2))
crux = jno.core([(field(a, b, c) - field(*truth)).mse])
steps = np.diff(np.unique(np.geomspace(1, 30_000, 100).astype(int)), prepend=0) # 30 000 iterations in chunks
path = [np.ravel(crux.eval([a, b, c]))]
for n in steps:
crux.solve(int(n), log=False)
path.append(np.ravel(crux.eval([a, b, c])))
rec = path[-1]
print(f"Recovered: a={rec[0]:.3f} b={rec[1]:.3f} c={rec[2]:.3f} (truth: {truth[0]}, {truth[1]}, {truth[2]})")
assert np.all(np.abs(rec - truth) / np.abs(truth) < 1e-1)