第27回:PINNsを作る — strong residualを点で最小化する

ChatGPTで作る有限要素プログラム入門 Season 3 第3回(通算第27回)

はじめに――「PDEを点で満たす」とは何を意味するのか

第25・26回ではDeep Ritz法を energy minimisationとして見ました. 今回は同じPoisson problemを Physics-Informed Neural Networks(PINNs)で解きます.

PINNsの基本形は非常に単純です. PDE residual

\[ r_\theta(x) = \mathcal N u_\theta(x)-f(x) \tag{1} \]

をcollocation points \[ X_N=\{x_1,\ldots,x_N\} \] で評価し,

\[ \boxed{ \mathcal L_N(\theta) = \sum_{i=1}^N w_i |r_\theta(x_i)|^2 } \tag{2} \]

を小さくします.

第27回の問い
\[ \boxed{ \mathcal L_N(\theta)\ll1 \quad\Longrightarrow\quad u_\theta\approx u \text{ と言ってよいのか?} } \tag{3} \]

1.同じ1次元Poisson problemを使う

今回も

\[ -u''(x)=f(x), \qquad 0<x<1, \qquad u(0)=u(1)=0 \tag{4} \]

を考え,

\[ \boxed{ u^\ast(x)=\sin(\pi x), \qquad f(x)=\pi^2\sin(\pi x) } \tag{5} \]

とします. Deep RitzとPINNsを同じproblemで比較できるようにします.

2.PINNsはstrong formを直接使う

Poisson operator \[ \mathcal N u=-u'' \] に対してPINN residualは

\[ \boxed{ r_\theta(x) = -u_\theta''(x) - f(x). } \tag{6} \]

Deep Ritzがfirst derivativeを使ったenergyを最小化したのに対し, PINNsではsecond derivativeをpointwiseに評価します.

3.boundary conditionは今回もhard constraintにする

boundary penaltyのweightを別の変数として持ち込まないため, 前回と同じ

\[ \boxed{ u_\theta(x) = x(1-x)N_\theta(x) } \tag{7} \]

を使います.

これにより \[ u_\theta(0)=u_\theta(1)=0 \] はparameterに関係なくexactly成立します.

4.strong residualにはtrial functionの二階微分が必要

今回のtanh networkはsmoothなので \[ u_\theta''(x) \] をautomatic differentiationで計算できます.

しかし一般のPDEで重要なのは,

\[ \boxed{ \text{networkがsmooth} \quad\not\Rightarrow\quad \text{exact solutionがstrong-form regularityを持つ} } \tag{8} \]

という点です.

weak solutionが自然には \(H_0^1\) にしか属さないproblemで, pointwise second derivative residualを主役にすることは, variational formulationより強いregularityを暗に要求する場合があります.

5.continuous strong-residual lossを定義する

まず点を置く前のcontinuous quantityとして

\[ \boxed{ \mathcal L(\theta) = \int_0^1 |r_\theta(x)|^2\,dx = \|r_\theta\|_{L^2(0,1)}^2 } \tag{9} \]

を考えます.

PINNsのsampled lossは

\[ \boxed{ \mathcal L_N(\theta) = \sum_{i=1}^N w_i|r_\theta(x_i)|^2 } \tag{10} \]

であり,

\[ \boxed{ \mathcal L_N \neq \mathcal L } \tag{11} \]

です. この違いがSeason 3のpoint-placement problemです.

6.continuous residualならenergy errorを上から制御できる

error \[ e=u_\theta-u^\ast \] とすると

\[ -e''=r_\theta, \qquad e(0)=e(1)=0. \tag{12} \]

\(e\) をtest functionにすると

\[ \int_0^1|e'|^2dx = \int_0^1r_\theta e\,dx. \tag{13} \]

Poincaré inequality \[ \|e\|_{L^2} \le \frac1\pi |e|_{H^1} \] から

\[ \boxed{ |u_\theta-u^\ast|_{H^1} \le \frac1\pi \|r_\theta\|_{L^2}. } \tag{14} \]

7.\(L^2\) errorもcontinuous residualで制御できる

さらに

\[ \|e\|_{L^2} \le \frac1\pi|e|_{H^1} \] と式 (14) を組み合わせると

\[ \boxed{ \|u_\theta-u^\ast\|_{L^2} \le \frac1{\pi^2} \|r_\theta\|_{L^2}. } \tag{15} \]

従ってcontinuous \(L^2\) residualが本当に小さいなら, このproblemではsolution errorも小さいと言えます.

8.問題はsampled residualしかtrainingでは見ていないこと

trainingで直接得るのは

\[ \sqrt{\mathcal L_N(\theta)} \]

です. 式 (14) に必要なのは

\[ \|r_\theta\|_{L^2} = \sqrt{\mathcal L(\theta)}. \]

従って

\[ \boxed{ \sqrt{\mathcal L_N} \text{ が小さい} \quad\not\Rightarrow\quad \|r_\theta\|_{L^2} \text{ が小さい} } \tag{16} \]

を最初に区別しなければなりません.

9.Deep RitzとPINNsは違うnormを見ている

Poisson error \(e\) に対し, Deep Ritzのenergy gapは

\[ \boxed{ 2(E(u_\theta)-E(u^\ast)) = |e|_{H^1}^2. } \tag{17} \]

一方,continuous PINN lossは

\[ \boxed{ \mathcal L(\theta) = \|e''\|_{L^2}^2. } \tag{18} \]

つまり同じsolution errorを 異なるdifferential scaleで見ています.

10.Fourier modeを見ると違いがはっきりする

error mode

\[ e_k(x)=\sin(k\pi x) \tag{19} \]

に対して

\[ |e_k|_{H^1}^2 = \frac{(k\pi)^2}{2}, \tag{20} \]

一方

\[ \|e_k''\|_{L^2}^2 = \frac{(k\pi)^4}{2}. \tag{21} \]

従って

\[ \boxed{ \text{strong }L^2\text{ residual} \text{ は高周波errorをenergy normより強く重み付けする.} } \tag{22} \]

PINNsとDeep Ritzのlossを数値だけで直接比較してはいけません.

11.residual functionalの自然なnormは \(H^{-1}\) にも見える

weak residualを

\[ \langle R_\theta,v\rangle = \int_0^1 u_\theta'v'\,dx - \int_0^1fv\,dx \tag{23} \]

とすると

\[ \boxed{ \|R_\theta\|_{H^{-1}} = |u_\theta-u^\ast|_{H^1} } \tag{24} \]

です.

つまりstrong \(L^2\) residualは, このweak residual normより強いquantityを最小化しています.

12.finite collocationにはexact zero-lossの見落としが存在する

固定されたcollocation points

\[ X_N=\{x_1,\ldots,x_N\} \]

を考えます.

次のsmooth perturbationを作ります.

\[ \boxed{ \psi_N(x) = x(1-x) \prod_{i=1}^N (x-x_i)^3. } \tag{25} \]

これは

\[ \psi_N(0)=\psi_N(1)=0 \tag{26} \]

を満たし,さらに各collocation pointで

\[ \boxed{ \psi_N''(x_i)=0. } \tag{27} \]

13.sampled lossがexactly zeroでもsolutionは違い得る

任意の \(c\ne0\) に対し

\[ \widetilde u(x) = u^\ast(x) + c\psi_N(x) \tag{28} \]

と置きます. boundary conditionはexact solutionと同じです. さらに

\[ -\widetilde u''(x_i)-f(x_i) = -c\psi_N''(x_i) = 0. \tag{29} \]

従って

\[ \boxed{ \mathcal L_N(\widetilde u)=0 } \tag{30} \]

ですが

\[ \boxed{ \widetilde u\ne u^\ast } \tag{31} \]

です.

finite samplingの本質
collocation pointsでPDEをexactly満たしても, 点の間でPDEを破る自由度は残り得る.

14.これはnetworkに限った問題ではない

式 (25)–(31) はneural networkもoptimizerも使っていません. つまり

\[ \boxed{ \text{finite collocationのinformation loss} } \tag{32} \]

そのものです.

実際のPINNではnetwork classとimplicit regularisationが この極端なperturbationを選びにくくする場合がありますが, sampled lossだけから排除できるわけではありません.

15.PINN networkを実装する

前回と同じtanh networkを使います.

class Net(torch.nn.Module):
    def __init__(self, width=32, depth=3):
        super().__init__()

        layers = [
            torch.nn.Linear(1, width),
            torch.nn.Tanh()
        ]

        for _ in range(depth - 1):
            layers += [
                torch.nn.Linear(width, width),
                torch.nn.Tanh()
            ]

        layers += [torch.nn.Linear(width, 1)]
        self.net = torch.nn.Sequential(*layers)

    def forward(self, x):
        raw = self.net(x)
        return x * (1.0 - x) * raw

16.automatic differentiationで二階微分を作る

def pde_residual(model, x):
    x = (
        x.clone()
         .detach()
         .requires_grad_(True)
    )

    u = model(x)

    ux = torch.autograd.grad(
        u,
        x,
        grad_outputs=torch.ones_like(u),
        create_graph=True
    )[0]

    uxx = torch.autograd.grad(
        ux,
        x,
        grad_outputs=torch.ones_like(ux),
        create_graph=True
    )[0]

    f = (
        math.pi ** 2
        * torch.sin(math.pi * x)
    )

    return -uxx - f

17.baselineでは64点Gauss–Legendreを使う

第27回ではまだpoint placementを比較せず, Deep Ritzのbaselineと同じく

\[ \boxed{ N=64 \text{ Gauss--Legendre points} } \tag{33} \]

を使います.

目的はstrong-residual formulationそのものを確認することです. 点配置比較は第28回で行います.

18.sampled PINN loss

def pinn_loss(model, xq, wq):
    r = pde_residual(model, xq)
    return torch.sum(
        wq * r ** 2
    )

Gauss weightsを使っているので, このbaselineでは

\[ \mathcal L_N \approx \int_0^1 |r_\theta(x)|^2dx \]

を高精度に評価することを狙っています.

19.training scheduleもDeep Ritzと揃える

method comparisonでoptimizer差を混ぜないため,

\[ \boxed{ 4000\text{ Adam steps} + \text{L-BFGS refinement} } \tag{34} \]

を使います.

model = Net()

optimizer = torch.optim.Adam(
    model.parameters(),
    lr=1.0e-3
)

for epoch in range(4000):
    optimizer.zero_grad()

    loss = pinn_loss(
        model,
        xq,
        wq
    )

    loss.backward()
    optimizer.step()

20.baseline numerical result

double precision,seed 7, 64-point Gauss–Legendre, Adam 4000 + L-BFGS で得た代表的な結果は

quantityvalue
sampled loss \(\mathcal L_N\)\(6.08\times10^{-7}\)
reference \(\|r_\theta\|_{L^2}\)\(7.80\times10^{-4}\)
\(L^2\) solution error\(1.19\times10^{-6}\)
\(H^1\) seminorm error\(2.83\times10^{-5}\)
max pointwise error\(2.28\times10^{-6}\)

reference quantitiesはtraining pointsとは独立な 512-point Gauss–Legendre ruleで評価しています.

21.continuous residual boundを実際に監査する

式 (14) の右辺は

\[ \frac1\pi \|r_\theta\|_{L^2} \approx 2.48\times10^{-4}. \tag{35} \]

実際のenergy errorは

\[ |u_\theta-u^\ast|_{H^1} \approx 2.83\times10^{-5}. \tag{36} \]

従って

\[ \boxed{ 2.83\times10^{-5} \le 2.48\times10^{-4} } \tag{37} \]

となり,continuous residual reliability boundと整合します.

22.このboundはsampled lossそのものの保証ではない

今回 \[ \sqrt{\mathcal L_N} \] とreference \[ \|r_\theta\|_{L^2} \] がほぼ一致したのは, smooth testに対して64-point Gauss ruleを使ったためです.

一般には

\[ \boxed{ \sqrt{\mathcal L_N} \approx \|r_\theta\|_{L^2} } \tag{38} \]

を別途検証しなければ, 式 (14) をtraining lossへ直接適用できません.

23.Deep Ritz baselineとの違い

項目 Deep Ritz PINNs
continuous targetenergy \(E(u_\theta)\)strong residual \(\|r_\theta\|_{L^2}^2\)
Poissonで使う微分first derivativesecond derivative
自然なsolution scaleenergy / \(H^1\)strong residual / \(L^2\)
pointの役割quadrature pointscollocation / residual sampling points
finite-sample failurewrong sampled energyresidualを点の間で見落とす

24.PINNsでもpoint countだけでは足りない

sampled residualはempirical measure

\[ \mu_N = \sum_iw_i\delta_{x_i} \tag{39} \]

に対して

\[ \mathcal L_N(\theta) = \int |r_\theta|^2\,d\mu_N \tag{40} \]

と書けます.

従ってPINNsでも

\[ \boxed{ \text{point count} + \text{point geometry} + \text{weights} } \tag{41} \]

を一体として記録する必要があります.

25.training pointsとは独立なresidual auditを置く

Season 3ではPINN training後に必ず

\[ \boxed{ \text{independent reference grid / quadrature} } \tag{42} \]

を置きます.

そこで少なくとも

\[ \boxed{ \|r_\theta\|_{L^2}, \quad \|u_\theta-u^\ast\|_{L^2}, \quad |u_\theta-u^\ast|_{H^1} } \tag{43} \]

を評価します.

training setだけでtestしてはいけません.

26.exact solutionがない場合はどうするか

実問題では \[ u^\ast \] を知りません. そこで

  • independent residual quadrature,
  • weak residual / dual norm,
  • equilibrated-flux certificate,
  • mesh / point refinement consistency,

を組み合わせます.

第23回の考え方を使えば,

\[ \boxed{ \text{training loss} \quad\text{と}\quad \text{guaranteed residual bound} } \tag{44} \]

を別物として扱えます.

27.activation functionもstrong residual設計の一部である

Poisson strong residualには \(u_\theta''\) が必要です. 従ってactivationを変えると残差の意味も変わります.

tanhはsmoothですが, piecewise linear ReLU networkではclassical second derivativeは elementwise almost everywhere zeroで, kinksにdistributional contributionが集まります.

従って

\[ \boxed{ \text{strong-form PINN} + \text{activation regularity} } \tag{45} \]

は切り離せません.

28.AIがPINN実装で間違えやすい点

mistake何が壊れるか
sampled lossをcontinuous residual normと呼ぶsampling errorを消してしまう
training pointsでだけaccuracyを評価between-point failureを検出できない
Deep Ritz lossとPINN lossの大小を直接比較異なるnorm / differential orderを混同する
smooth networkだからexact solutionもsmoothと仮定PDE regularityを過大評価する
ReLUでclassical second derivative residualを無批判に使うdistributional derivativeを見落とす
点数だけでgeneralisationを判断sampling geometryを無視する

29.PINN implementation audit prompt

Audit prompt
このstrong-form PINN implementationを数学的に監査してください.
(1) continuous PDEとstrong residualのsign conventionが一致しているか,
(2) boundary conditionをhard constraintかpenaltyか明示しているか,
(3) activationとnetworkが必要な微分をclassically持つか,
(4) sampled loss \(\mathcal L_N\) とcontinuous residual norm \(\mathcal L\) を区別しているか,
(5) collocation pointsとweightsを全て記録しているか,
(6) training setとは独立なreference pointsでresidualを評価しているか,
(7) exact solutionがある場合は \(L^2\)・\(H^1\) errorを別に評価しているか,
(8) continuous residual reliability boundをsampled lossへ無条件に適用していないか,
(9) Deep Ritzとの比較でlossのnormと微分次数を揃えて説明しているか,
(10) finite collocationではzero sampled residualでもbetween-point errorが残り得ることを確認しているか,
(11) random samplingではseed variationを調べているか,
(12) point countとsampling coverageを同一視していないか
を順に確認してください.

30.第27回の結論

PINNsの基本式は

\[ \boxed{ \min_{\theta} \sum_{i=1}^N w_i | \mathcal Nu_\theta(x_i)-f(x_i) |^2 } \tag{46} \]

です. しかしこの式には二つの問題が重なっています.

\[ \boxed{ \text{strong residual norm} \quad+\quad \text{finite sampling} } \tag{47} \]

です.

continuous \(L^2\) residualが小さければ, 今回のPoisson problemでは

\[ \boxed{ |u_\theta-u|_{H^1} \le \frac1\pi \|r_\theta\|_{L^2} } \tag{48} \]

とsolution errorを制御できます.

しかしfinite collocationでは

\[ \boxed{ \mathcal L_N=0 \quad\text{でも}\quad u_\theta\ne u } \tag{49} \]

が可能です.

Season 3 第3回の原則
PINNsで小さくしているのはPDE residualそのものではなく, 選んだ点から見えたPDE residualである.

次回はこの差を正面から調べ, 第28回:PINNsでは点配置で答えが変わるか — uniform・random・adaptive sampling へ進みます.

参考文献・関連ページ

  1. M. Raissi, P. Perdikaris and G. E. Karniadakis, “Physics-informed neural networks: A deep learning framework for solving forward and inverse problems involving nonlinear partial differential equations”, Journal of Computational Physics 378 (2019), 686–707.
  2. W. E and B. Yu, “The Deep Ritz Method: A Deep Learning-Based Numerical Algorithm for Solving Variational Problems”, Communications in Mathematics and Statistics 6 (2018), 1–12.
  3. S. C. Brenner and L. R. Scott, The Mathematical Theory of Finite Element Methods, 3rd ed., Springer, 2008.
  4. 第25回:Deep Ritz法を作る — FEMのenergy minimisationからneural trial spaceへ
  5. 第26回:Deep Ritzでは点をどこに置くか — quadratureとsampling geometry
  6. 第23回:有限要素解の誤差を保証する — equilibrated fluxとhypercircle
  7. 関連する先行ノート:1次元Poisson問題でPINNsを動かす
  8. 関連する先行ノート:残差点の配置を変えると何が起きるか

ページの先頭へ