Skip to content

Physics-informed networks: equations in the loss

A physics-informed neural network is fitted so that its output nearly satisfies a differential equation and its boundary conditions at chosen points. Compute a residual loss by hand and see why a small loss doesn't prove a solution.

Advanced ~14 min7 steps

What runs in CodeSplash

You will be able to

  • Write a PINN loss with data, residual and boundary terms
  • Compute an equation residual from a network's derivatives
  • Explain how loss weights and collocation points change what gets fitted
  • Check a PINN against a reference solver on a fine grid

Read first

01

The equation as a penalty

Read

A physics-informed neural networkPhysics-informed neural networkA network fitted so that it fits data and also makes a differential equation's residual and boundary errors small at chosen points. is a network uθ(x)u_\theta(x) from coordinates to a solution value. Automatic differentiation gives its derivatives, so you can plug it into the governing equation N[u]=0\mathcal{N}[u] = 0 and the boundary rule B[u]=g\mathcal{B}[u] = g, and penalize what's left:

L=λd∥uθ(xd)−yd∥2+λr∥N[uθ;η](xr)∥2+λb∥B[uθ]−g∥2.L = \lambda_d \lVert u_\theta(x_d) - y_d\rVert^2 + \lambda_r \lVert \mathcal{N}[u_\theta; \eta](x_r)\rVert^2 + \lambda_b \lVert \mathcal{B}[u_\theta] - g\rVert^2 .

The three terms fit observations, the equation at collocation pointsCollocation pointsThe points where a physics-informed network's equation residual is computed and penalized; elsewhere nothing is checked. xrx_r, and the boundaries. Unknown physical coefficients η\eta can be fitted too: that's an inverse problem.

Reference · SPE013 · Explained herePhysics-informed neural network (PINN)A PINN fits a neural function while penalizing differential-equation residuals and initial/boundary conditions, optionally alongside observations.
02

A residual by hand

Explore

The reference takes Poisson's equation −u′′=π2sin⁡(πx)-u'' = \pi^2 \sin(\pi x) on [0,1][0, 1] with u(0)=u(1)=0u(0) = u(1) = 0, whose solution is sin⁡(πx)\sin(\pi x). Its trial network is one tanh unit, u=atanh⁡(bx+c)+du = a \tanh(bx + c) + d, with fixed weights and an exact second derivative.

At 51 collocation points the loss (mean squared residual plus ten times the squared boundary values) is 38.99. The boundary values are −0.190 and 0.461, not zero. Training would move a,b,c,da, b, c, d to shrink both.

Python
import numpy as np
x = np.linspace(0, 1, 51); a, b, c, d = .5, 2., -.4, 0.
h = np.tanh(b * x + c); u = a * h + d
u_xx = -2 * a * b * b * h * (1 - h * h)                 # exact second derivative
residual = -u_xx - np.pi**2 * np.sin(np.pi * x)
print(np.mean(residual**2) + 10 * (u[0]**2 + u[-1]**2), u[[0, -1]])
R
x <- seq(0, 1, length.out = 51); a <- .5; b <- 2; cc <- -.4; d <- 0
h <- tanh(b * x + cc); u <- a * h + d
u_xx <- -2 * a * b^2 * h * (1 - h^2)                    # exact second derivative
residual <- -u_xx - pi^2 * sin(pi * x)
print(c(mean(residual^2) + 10 * (u[1]^2 + u[51]^2), u[c(1, 51)]))
03

Weights decide what wins

Predict

Predict first

You raise the boundary weight λb\lambda_b from 10 to 1,000 and retrain. What do you expect?

The boundary errors shrink and the interior residual may growNothing changes: the minimum is the same
Reveal

The boundary errors shrink and the interior residual may grow. The terms trade off, like a penalty weight in a regularized fit. Their scales also depend on units, so rescale the problem to dimensionless form before choosing weights. Badly balanced terms are a common reason a PINN fails to train.

04

Small loss, wrong solution

Read

The residual is checked only at collocation points. Between them, or where the solution changes sharply, the network can be far off. Networks also fit smooth, slowly varying shapes first, so stiff or multiscale problems can stall. And if the equation is wrong, the physics term pulls the fit toward the wrong answer.

Check a trained PINN the way the reference asks: evaluate residuals and conserved quantities on a fine grid that wasn't used in training, compare with a numerical solver where one exists, and for inverse problems check whether the fitted coefficients are identifiable.

05

What runs here

Read
06

Check: PINNs

Checkpoint

Check yourself

Which terms make up a typical PINN loss?

Show the answer

Answer: b

The physics enters as weighted penalties next to any observations.

Check yourself

A trained PINN has a near-zero residual at its collocation points. What does that show?

Show the answer

Answer: b

Residuals are only enforced where they are sampled. Check on a fine held-out grid.

Check yourself

In the reference's Poisson example, the fixed trial network gives nonzero values at x=0x = 0 and x=1x = 1. Which part of the loss do they raise?

Show the answer

Answer: c

The boundary rule is u(0)=u(1)=0u(0) = u(1) = 0, so any nonzero value there adds its weighted square to the loss.

Check yourself

The equation in your PINN leaves out friction that the real system has. What does the physics term do?

Show the answer

Answer: b

Physics constraints don't repair a wrong equation. They bias the fit toward it.

Check yourself

How should you validate a PINN for a problem that a standard solver can also handle?

Show the answer

Answer: a

A reference solution and held-out residuals test the claim that matters: that the network solves the problem.

07

Recap

Recap
  • A PINN penalizes the equation residual and boundary errors next to any data.
  • Loss weights and collocation points decide what gets fitted.
  • A small loss at sampled points doesn't prove a solution; a wrong equation biases the fit.
  • Check against a solver on a fine grid. PINNs are explained here only.

Concepts in this lesson