Foundations to Frontier
Tests
J. Scientific Machine Learning › J2 Physics-Informed Neural Networks

PINN formulation: PDE residual loss

A physics-informed neural network (PINN) represents the solution of a differential equation as a neural network u(x) and trains it so that the equation holds. Instead of labelled data, the loss is the squared residual of the equation at a set of collocation points, plus penalty terms for the boundary and initial conditions. The derivatives in the residual come from automatic differentiation of the network, so no mesh is needed. This page builds a PINN from scratch for the one-dimensional Poisson problem u″ = f on [0, 1] with zero boundary values and exact solution sin πx + 0.3 sin 4πx. In the exercises you compute the network's derivatives exactly by the chain rule, train it with Adam and with Levenberg–Marquardt, and compare it with second-order finite differences. With 16 collocation points, Levenberg–Marquardt reached a relative error of 0.0001 and Adam 0.003, against 0.014 for finite differences on 16 nodes. With 8 points the residual could be driven to zero by wrong functions (worst error 0.07), and without the boundary term the error was 6, because the equation alone does not fix the solution.

Level 0: Foundation

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

Teaching a network to obey an equation

Usually a neural network learns from examples: inputs with known answers. A physics-informed neural network learns from a rule instead. We know the equation the answer must satisfy, for example how the bending of a string depends on the load on it. We pick many points, ask the network for its guess and its slopes at each point, and measure how badly the equation is broken there. Training pushes that error towards zero. We also tell it the values at the edges, because an equation alone has many solutions. The demo shows the guess, the true solution and the broken-equation error at each point.

PINN lab
Black: exact solution; green dashed: the physics-informed fit using only the equation at 16 points (yellow ticks) and u = 0 at the ends. Relative error 0.12%. With too few points the residual can be zero there while the solution is wrong in between.
Why engineers care

PINNs are used to reconstruct blood flow in arteries from a few MRI measurements and to fill in temperature and velocity fields around objects from sparse sensor data, where the equations of fluid flow do the rest.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A PINN's loss measures…
  2. 2. The points where the equation is checked are called…
  3. 3. Why add a boundary term?

Level 1: Beginner

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

By hand

  • Problem: find u with 𝒩u = f(x) in a domain Ω and ℬ[u] = g on its boundary, where 𝒩 is a differential operator (here 𝒩[u] = u″).
  • Ansatz: u_θ(x), a neural network with parameters θ. Smooth activations (tanh, sine, softplus) are used because the residual needs second and higher derivatives; ReLU has zero second derivative almost everywhere.
  • Loss: L(θ) = (1/N) Σ (𝒩u_θ − f(x_i))² + λ (1/M) Σ (ℬu_θ − g(y_j))², with N collocation points x_i inside Ω and M boundary points y_j. Data terms can be added if measurements exist.
  • Derivatives: computed by automatic differentiation of the network with respect to its input x (A8-5), not by finite differences; the gradient of the loss with respect to θ needs one more backward pass through those derivatives.
  • Optimisers: Adam first, then L-BFGS or Levenberg–Marquardt (a damped Gauss–Newton method) to reach high accuracy. Learning rate decay matters for Adam.
  • Origins: Lagaris, Likas and Fotiadis (1998) used small networks the same way; Raissi, Perdikaris and Karniadakis (2019) named PINNs and made them popular with modern autodiff frameworks.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Why are tanh networks preferred over ReLU for PINNs?
  2. 2. Derivatives of u_θ with respect to x are computed by…
  3. 3. A common optimiser schedule for PINNs:

Level 2: Intermediate

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

Building a PINN by hand

Exercise 1.

  • A one-hidden-layer tanh network u(x) = Σ w_j tanh(a_j x + b_j) + c.
  • Its first and second derivatives with respect to x by the chain rule: u″ = Σ w_j a_j² tanh″(a_j x + b_j).
  • The PINN loss (mean squared residual plus λ times the boundary term) and its gradient with respect to the output weights, checked against finite differences.

Practice.

  • Non-dimensionalise the problem so inputs and outputs are of order 1; tanh networks struggle with inputs of size 100.
  • Collocation points: more than the number of features you expect in the solution; resample them every few steps or use a Latin hypercube or Sobol’ set (J2-4 covers adaptive sampling).
  • Train in two stages: Adam with a decaying learning rate, then L-BFGS (or Levenberg–Marquardt for small networks) to drive the residual down.
  • Check against a classical solver on a case you can solve; a small residual does not prove a small error.
  • Weight the boundary term (λ) so both terms fall; J2-2 shows hard boundary conditions that remove the term.
  • Tools: DeepXDE, NVIDIA PhysicsNeMo (formerly Modulus), PyTorch or JAX with torch.func or jax.grad for input derivatives.
import torch

net = torch.nn.Sequential(torch.nn.Linear(1, 20), torch.nn.Tanh(), torch.nn.Linear(20, 1))
x = torch.linspace(0, 1, 64).reshape(-1, 1).requires_grad_(True)
f = lambda x: -torch.pi ** 2 * torch.sin(torch.pi * x)
opt = torch.optim.Adam(net.parameters(), lr=1e-2)
for step in range(2000):
    u = net(x)
    ux = torch.autograd.grad(u.sum(), x, create_graph=True)[0]
    uxx = torch.autograd.grad(ux.sum(), x, create_graph=True)[0]
    ub = net(torch.tensor([[0.0], [1.0]]))
    loss = ((uxx - f(x)) ** 2).mean() + 10 * (ub ** 2).mean()
    opt.zero_grad(); loss.backward(); opt.step()
🐍 Exercise 1: exact derivatives and the PINN loss
Write a one-layer tanh network, its exact first and second input derivatives, the PINN loss for u″ = f with boundary penalty, and the gradient of the residual loss with respect to the output weights; check all of them with finite differences.
📝 Quick check (Level 2)
0/3 answered
  1. 1. In PyTorch, input derivatives for the residual need…
  2. 2. A small PINN residual…
  3. 3. A library built for PINNs:

Level 3: Advanced

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

Theory

Why the second derivative is cheap here. For u(x) = Σ w_j tanh(a_j x + b_j) + c, u″(x) = Σ w_j a_j² tanh″(a_j x + b_j) with tanh″(z) = −2 tanh z (1 − tanh² z). The residual u″ − f is linear in the output weights w, so for fixed a and b the problem is linear least squares; the hidden-layer parameters make it non-linear. Autodiff frameworks compute the same expressions automatically, at a cost that grows with the order of the derivative.

Least squares, not just optimisation. The PINN loss is a sum of squares, so Gauss–Newton methods apply: with residual vector r(θ) and Jacobian J, the Levenberg–Marquardt step is d = −(JᵀJ + μI)⁻¹ Jᵀ r. For small networks this converges far faster and further than first-order methods; in the exercise it gave errors 30 times smaller than Adam with 16 points.

What the residual does not control. A zero residual at N points only says u″ = f at those points. Between them the network is free; with too few points it can satisfy the equation exactly while being wrong (with 8 points the worst error was 0.07 against 0.0001 with 16). Without boundary data the equation determines u only up to a linear function a + bx, so the network picks one arbitrarily: the error was 6.

Comparison with finite differences. Second-order finite differences have error O(h²); on 8, 16 and 64 interior nodes the errors were 0.053, 0.014 and 0.0009, an observed order of about 1.9. A PINN with a smooth network and a strong optimiser can beat that per point for smooth solutions, but it costs far more computation, and it gives no guarantee, which is why PINNs are mostly used for inverse problems (J2-5) and for problems with data.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The residual u″ − f of a one-layer tanh network is linear in…
  2. 2. Without a boundary term, u″ = f fixes u only up to…
  3. 3. The Levenberg–Marquardt step is…

Level 4: Expert

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

How accurate can a PINN be compared with a classical solver, and what makes it fail?

History.

  • I. Lagaris, A. Likas and D. Fotiadis (1998), “Artificial neural networks for solving ordinary and partial differential equations”, trained small networks on equation residuals with trial functions that satisfied the boundary conditions.
  • M. Raissi, P. Perdikaris and G. Karniadakis (2019), “Physics-informed neural networks”, combined residual losses with modern automatic differentiation and data, for forward and inverse problems.
  • L. Lu et al. (2021) released DeepXDE; S. Wang, Y. Teng and P. Perdikaris (2021) and A. Krishnapriyan et al. (2021) analysed why PINN training fails (J2-3).
  • Later work showed that second-order optimisers and careful sampling reach errors near machine precision on smooth problems (for example natural-gradient and Levenberg–Marquardt training, 2023–2024).

Exercise 2. Poisson problem u″ = f on [0, 1], u(0) = u(1) = 0, exact solution sin πx + 0.3 sin 4πx. Network: one hidden tanh layer of width 20 (61 parameters); loss mean squared residual plus 10 times the mean squared boundary value; collocation points evenly spaced including the ends. Adam: 2000 steps, learning rate 0.01 decaying 100 times. Levenberg–Marquardt: 150 steps. Errors are relative L2 errors on 501 test points, median of 3 initialisations, rounded to one significant figure; finite differences are measured at their nodes.

collocation points / nodes 8 16 64
PINN, Adam 0.02 0.003 0.01
PINN, Levenberg–Marquardt 0.04 0.0001 0.00004
Levenberg–Marquardt, worst of 3 0.07 0.0001 0.00006
finite differences (2nd order) 0.053 0.014 0.0009

Without the boundary term (64 points, Levenberg–Marquardt) the error was 6.

  • The optimiser matters more than the points. Adam stalled at errors around 0.003–0.01 even with 64 points; Levenberg–Marquardt kept improving. With a constant learning rate Adam’s results changed from machine to machine, which is why the decay is needed.
  • With enough points, the PINN beat finite differences. With 16 points the Levenberg–Marquardt PINN was about 140 times more accurate than finite differences on 16 nodes, because a smooth network approximates a smooth solution well; finite differences improve only as h².
  • Too few points let wrong solutions through. With 8 points the residual could be made small by functions that wiggle between the points.
  • The boundary term is essential. Without it the solution is fixed only up to a + bx, and the error was 6.
  • Seeds 1 and 2: Adam 0.02 / 0.002 / 0.005 and 0.02 / 0.006 / 0.02; Levenberg–Marquardt 0.07 / 0.00007 / 0.00001 and 0.09 / 0.0001 / 0.00009; worst of 3 with 8 points 0.3 and 0.1; no boundary loss 10 and 6.
🐍 Exercise 2 (research): how accurate can a PINN be compared with a classical solver, and what makes it fail?
Train a one-layer tanh PINN for u″ = f with Adam and with Levenberg–Marquardt at 8, 16 and 64 collocation points, compare it with second-order finite differences, and remove the boundary term to see what happens.

PINNs in science and engineering

  • Cardiovascular flow: PINNs reconstruct velocity and pressure in arteries from sparse 4D-flow MRI, using the Navier–Stokes equations as the residual.
  • Heat transfer and flow fields: temperature and velocity around objects inferred from a few sensors or from smoke visualisation (hidden fluid mechanics, Raissi et al., 2020).
  • Aerospace: PINNs as fast, mesh-free solvers inside design loops for heat shields and structures, and for inferring parameters from test data.
  • Geophysics: seismic travel times (the eikonal equation) and wave propagation, where a mesh-free method handles complex geometry.
  • Limits: for forward problems a classical solver is usually faster and more reliable; PINNs are most useful when equations and data must be combined (J2-5).

References

  1. I. E. Lagaris, A. Likas and D. I. Fotiadis (1998). “Artificial neural networks for solving ordinary and partial differential equations.” IEEE Transactions on Neural Networks.
  2. M. Raissi, P. Perdikaris and G. E. Karniadakis (2019). “Physics-informed neural networks: a deep learning framework for solving forward and inverse problems involving nonlinear partial differential equations.” Journal of Computational Physics.
  3. L. Lu, X. Meng, Z. Mao and G. E. Karniadakis (2021). “DeepXDE: a deep learning library for solving differential equations.” SIAM Review.
  4. G. E. Karniadakis et al. (2021). “Physics-informed machine learning.” Nature Reviews Physics.
  5. M. Raissi, A. Yazdani and G. E. Karniadakis (2020). “Hidden fluid mechanics: learning velocity and pressure fields from flow visualizations.” Science.