Skip to content
QFT.org

Search QFT.org

Enter a word or phrase.

    You can also browse the main navigation and chapter contents.

    Conserved Quantities in Learned Dynamics

    A learned oscillator can predict one step accurately and still spiral inward over many periods. Constraining the model to conserve energy removes that failure, but it does not fix the clock: an orbit can stay on the correct energy circle while accumulating a large phase error.

    This article separates three questions: what the model learns, what its continuous equations conserve, and what the numerical integrator preserves. An actual fit to noisy oscillator derivatives makes each distinction measurable. The constrained model preserves the physical energy extremely well, yet its long-time state error is not uniformly smaller than that of the unconstrained model.

    Preparation. Hamilton's equations, least squares and time integration. The automatic-differentiation article explains how gradients of more general learned energies can be evaluated.

    Specify the invariant before choosing the architecture

    Use dimensionless canonical variables z=(q,p)Tz=(q,p)^T and

    H0(z)=12(q2+p2),J=(01−10),z˙=J∇H0=Jz.(1)H_0(z)=\frac12(q^2+p^2),\qquad J=\begin{pmatrix}0&1\\-1&0\end{pmatrix},\qquad \dot z=J\nabla H_0=Jz. \tag{1}

    The physical solution is z(t)=R(t)z(0)z(t)=R(t)z(0) with R(t)=etJR(t)=e^{tJ}. The exact angular frequency is one.

    Compare two bias-free linear networks:

    z˙=Az,A∈R2×2,(2)\dot z=A z,\qquad A\in\mathbb R^{2\times2}, \tag{2}

    and

    Hω(z)=ω2∣z∣2,z˙=J∇Hω=ωJz.(3)H_\omega(z)=\frac\omega2|z|^2,\qquad \dot z=J\nabla H_\omega=\omega Jz. \tag{3}

    The first has four fitted parameters; the second has one. Both are odd and fix the origin. Equation (3) builds in the radial energy geometry and learns only a time scale. It is a strong, correct prior for this target, not a demonstration that a network discovers an unknown energy function.

    For a differentiable autonomous learned Hamiltonian HθH_\theta, antisymmetry of JJ gives

    dHθdt=∇HθTJ∇Hθ=0.(4)\frac{dH_\theta}{dt} =\nabla H_\theta^T J\nabla H_\theta=0. \tag{4}

    This conserves the learned Hamiltonian. It need not conserve a separate physical H0H_0. The radial choice (3) does conserve both, because ∇Hω=ω∇H0\nabla H_\omega=\omega\nabla H_0. Learning an energy and differentiating it to construct the field is the principle in Greydanus, Dzamba and Yosinski, §2, Eqs. (2)–(3). Their nonlinear network experiments differ from the exactly solved linear fits here.

    For the unconstrained model,

    dH0dt=zTAz=zTA+AT2z.(5)\frac{dH_0}{dt}=z^TAz =z^T\frac{A+A^T}{2}z. \tag{5}

    A small symmetric part produces a small instantaneous drift, but its effect can accumulate. A small derivative residual alone does not set the duration over which the orbit remains accurate.

    Fit both models to the same noisy observations

    Each training replica contains 256 inputs uniform in area on the annulus 0.5≤∣z∣≤1.50.5\leq|z|\leq1.5. In polar coordinates this means r=0.25+2ur=\sqrt{0.25+2u}, φ=2πv\varphi=2\pi v, with independent uniform u,vu,v. Derivative observations are

    Fr=Jzr+ξr,ξr∼N(0,0.022I2).(6)F_r=Jz_r+\xi_r,\qquad \xi_r\sim\mathcal N(0,0.02^2I_2). \tag{6}

    The two models use exactly the same observations. Sixteen independent training replicas are retained; no fit is selected by its long-time outcome. An independent common test set contains 2048 noiseless inputs from the same annulus. There is no validation-selected regularization or time step.

    With X=(z1,…,zn)X=(z_1,\ldots,z_n) and F=(F1,…,Fn)F=(F_1,\ldots,F_n), full row rank gives

    A^=FXT(XXT)−1,ω^=∑r(Jzr)TFr∑r∣zr∣2.(7)\widehat A=FX^T(XX^T)^{-1},\qquad \widehat\omega= \frac{\sum_r(Jz_r)^TF_r}{\sum_r|z_r|^2}. \tag{7}

    The implementation solves the generic least squares by SVD rather than explicitly forming the inverse. It records the design rank, singular values and normal-equation residuals. The constrained estimate follows from differentiating its one-dimensional quadratic loss.

    For the predetermined illustrative replica 0,

    A^=(−0.00258713400.9984443505−0.9993957035−0.0015325633),ω^=0.9989379224.(8)\widehat A=\begin{pmatrix} -0.0025871340&0.9984443505\\ -0.9993957035&-0.0015325633 \end{pmatrix},\qquad \widehat\omega=0.9989379224. \tag{8}

    Its design condition number is 1.02661.0266, and both gradient residuals are below 5×10−165\times10^{-16}. There is no unresolved optimization error hiding the comparison.

    Replica 0 diagnosticGeneric fieldRadial Hamiltonian
    Noisy training MSE per component4.00731×10−44.00731\times10^{-4}4.03647×10−44.03647\times10^{-4}
    Clean test derivative RMSE per component1.92004×10−31.92004\times10^{-3}8.36166×10−48.36166\times10^{-4}
    Exact learned one-step RMSE, h=0.1h=0.11.92020×10−41.92020\times10^{-4}8.36166×10−58.36166\times10^{-5}

    The flexible model has the smaller training loss. The constrained model has the smaller clean derivative error in this replica, consistent with its correct stronger prior. These are actual sample outcomes, not a general ordering theorem.

    Five of the sixteen generic matrices have an eigenvalue with positive real part. All sixteen fitted ω^\widehat\omega values are positive. Both outcomes are reported rather than enforced after fitting. Replica 0 is stable but dissipative: its largest eigenvalue real part is −0.00205985-0.00205985.

    Separate learned evolution from time discretization

    First evolve each fitted model exactly, using etA^e^{t\widehat A} or R(ω^t)R(\widehat\omega t). These trajectories isolate model error. The companion checks an independent analytic 2×22\times2 exponential formula against a general matrix-exponential routine; the largest selected discrepancy is 1.28×10−131.28\times10^{-13}.

    Then apply implicit midpoint to the same learned field:

    zn+1−znh=Azn+1+zn2,zn+1=Mhzn,Mh=(I−hA/2)−1(I+hA/2).(9)\frac{z_{n+1}-z_n}{h} =A\frac{z_{n+1}+z_n}{2},\qquad z_{n+1}=M_hz_n,\qquad M_h=(I-hA/2)^{-1}(I+hA/2). \tag{9}

    For A=ωJA=\omega J, the two factors commute and AT=−AA^T=-A, giving MhTMh=IM_h^TM_h=I. Thus midpoint preserves this quadratic H0H_0 in exact arithmetic. It is also symplectic, MhTJMh=JM_h^TJM_h=J. For a generic fitted AA, neither identity follows simply from using the same named integrator.

    Quadratic-invariant preservation and symplecticity are distinct statements about the method and the underlying vector field. The Runge–Kutta coefficient condition that ensures quadratic preservation is given in Sanz-Serna, §2.1, Theorems 1–2 and Eq. (8). It does not say that every symplectic method preserves every nonlinear Hamiltonian exactly.

    For the radial oscillator, the midpoint step is a rotation through

    ϑh=2arctan⁡(hω/2),ωh=2harctan⁡(hω/2)=ω−h2ω312+O(h4).(10)\vartheta_h=2\arctan(h\omega/2),\qquad \omega_h=\frac2h\arctan(h\omega/2) =\omega-\frac{h^2\omega^3}{12}+O(h^4). \tag{10}

    The energy is exact, but the frequency is shifted. Relative to the true oscillator, the accumulated phase error is

    Δφ(T)=(ω^−1)T+(ω^h−ω^)T.(11)\Delta\varphi(T)=(\widehat\omega-1)T +(\widehat\omega_h-\widehat\omega)T. \tag{11}

    The first term is a learned-model error and the second an integration error. Reducing hh controls only the latter.

    For replica 0 at h=0.1h=0.1, the generic midpoint map has orthogonality defect 6.15×10−46.15\times10^{-4} and determinant 0.999589140.99958914. The constrained map's orthogonality and symplectic defects are about 1.2×10−161.2\times10^{-16}. Halving hh reduces the constrained integrator-only one-step RMSE from 6.53×10−56.53\times10^{-5} to 8.17×10−68.17\times10^{-6}, as expected for a local error of order h3h^3. Its exact learned-flow error remains nonzero.

    Energy and phase tell different long-time stories

    The frozen scan uses initial states (1,0)(1,0) and (0.6,0.8)(0.6,0.8), times T=1,10,100,1000T=1,10,100,1000, and steps h=0.1,0.05h=0.1,0.05. Energy error is (H0(z(T))−H0(z(0)))/H0(z(0))(H_0(z(T))-H_0(z(0)))/H_0(z(0)); state error is the Euclidean distance to the physical trajectory. Phase uses the continuously unwrapped atan2⁡(−p,q)\operatorname{atan2}(-p,q), subtracting its initial value and the true phase TT.

    For replica 0, initial state (1,0)(1,0) and T=1000T=1000:

    EvolutionRelative energy changePhase errorState error
    Generic, midpoint h=0.1h=0.1−0.983565-0.983565−1.90949-1.909491.049581.04958
    Radial model, exact learned ODErounding level−1.06208-1.062081.012861.01286
    Radial model, midpoint h=0.1h=0.1−4.59×10−13-4.59\times10^{-13}−1.89152-1.891521.621881.62188
    Radial model, midpoint h=0.05h=0.05rounding level−1.26967-1.269671.186091.18609

    The generic orbit has lost about 98.4%98.4\% of its energy. Nevertheless its endpoint state error is smaller than the radial model's h=0.1h=0.1 error: the latter is on the correct circle but substantially out of phase. Energy conservation is a useful physical constraint, not a complete accuracy certificate.

    Across all sixteen fits at T=1000T=1000, h=0.1h=0.1, the generic energy changes range from approximately −0.984-0.984 to 169.7169.7. The constrained energy changes stay below 2×10−122\times10^{-12} in magnitude, while their phase errors range from −2.12-2.12 to 0.4090.409 radians. The spread comes from independent noisy fits; it is not a confidence interval for a different physical system.

    Replica-zero learned oscillator energy and phase errors over time, with all sixteen final energy errors shown separately; conserved energy still permits a large phase error.

    Both learned models use the same derivative observations. Replica 0 was fixed before outcomes; the final-energy comparison retains all sixteen fits. The exact learned radial flow separates frequency error from midpoint's additional phase error. All plotted quantities come from the retained scan, with no selection by long-time accuracy.

    The pure kernel learned_oscillator.py and driver oscillator_checks.py retain fit diagnostics, one-step tests and both long-time trajectories in oscillator-learning-results.json, under examples/computational-qft/machine-learning/. Training, derivative testing and integration refinement use distinct checks.

    What other symmetries would guarantee

    For a canonical generator Q(z)Q(z), its derivative along a learned Hamiltonian flow is

    Q˙={Q,Hθ}=∇QTJ∇Hθ.(12)\dot Q=\{Q,H_\theta\}=\nabla Q^TJ\nabla H_\theta. \tag{12}

    An architecture invariant under the canonical transformation generated by QQ makes this bracket vanish in continuous time. Merely writing a Hamiltonian ensures its own autonomous energy conservation, not every charge of the target system. A discretization must then be assessed for the particular invariant: the quadratic midpoint argument used here does not automatically extend to nonquadratic charges or to arbitrary time-stepping rules.

    For constrained fields, the analogous question includes preservation of the constraint surface and the correct physical degrees of freedom. Gauge equivariance of a static map and Hamiltonian conservation of a time evolution are related design principles, but they are different mathematical requirements and need different tests.

    Summary

    Build the desired conservation statement into the continuous model, identify exactly which quantity it protects, and test the discrete evolution independently. The trained oscillator demonstrates a large improvement in energy behavior without eliminating model-frequency error or integration phase error. Derivative accuracy, energy, phase and long-time state accuracy are complementary diagnostics.

    Exercises

    1. Compare Euler with midpoint

    For z˙=ωJz\dot z=\omega Jz, derive the energy after nn forward-Euler steps. Show that midpoint instead preserves the norm but advances with the frequency (10). Explain why neither result establishes the correct physical frequency when ω\omega is fitted.

    Solution

    Euler gives Eh=I+hωJE_h=I+h\omega J. Since JT=−JJ^T=-J and J2=−IJ^2=-I,

    EhTEh=(1+h2ω2)I,H0(zn)=(1+h2ω2)nH0(z0).E_h^TE_h=(1+h^2\omega^2)I, \qquad H_0(z_n)=(1+h^2\omega^2)^nH_0(z_0).

    Thus a continuously conserved quadratic energy grows under Euler for hω≠0h\omega\ne0. At fixed final time T=nhT=nh, the growth factor is exp⁡[Thω2+O(Th3)]\exp[T h\omega^2+O(Th^3)] as h→0h\to0; long time and small step cannot be interchanged without controlling their product.

    For midpoint, set a=hω/2a=h\omega/2. Using (I−aJ)−1=(I+aJ)/(1+a2)(I-aJ)^{-1}=(I+aJ)/(1+a^2),

    Mh=(1−a2)I+2aJ1+a2=cos⁡ϑhI+sin⁡ϑhJ,ϑh=2arctan⁡a.M_h=\frac{(1-a^2)I+2aJ}{1+a^2} =\cos\vartheta_h I+\sin\vartheta_h J, \qquad \vartheta_h=2\arctan a.

    This is orthogonal and yields (10). Its exact norm does not determine its angle. Even h→0h\to0 only recovers frequency ω\omega; the physical frequency is one, so fitting error remains unless ω=1\omega=1.

    2. Energy conservation is not every Noether charge

    Take H=H0+λq4/24H=H_0+\lambda q^4/24. Compute H˙\dot H and Q˙\dot Q for Q=H0Q=H_0, and identify the symmetry that fails when λ≠0\lambda\ne0.

    Solution

    Hamilton's equations are

    q˙=p,p˙=−q−λ6q3.\dot q=p,\qquad \dot p=-q-\frac\lambda6q^3.

    Because HH is autonomous, H˙={H,H}=0\dot H=\{H,H\}=0. However,

    Q˙=qq˙+pp˙=−λ6pq3.\dot Q=q\dot q+p\dot p=-\frac\lambda6pq^3.

    The canonical transformation generated by QQ is rotation in the (q,p)(q,p) plane. The term q4q^4 is not invariant under those rotations, so the corresponding QQ is not conserved. Time-translation symmetry still conserves the full HH. This also shows why an arbitrary learned Hamiltonian should not be described as preserving a specified target energy unless the additional bracket condition is checked.

    References

    • S. Greydanus, M. Dzamba and J. Yosinski, “Hamiltonian Neural Networks,” arXiv:1906.01563v3. See §2, Eqs. (2)–(3), for Hamiltonian-gradient modeling and §§3.1–3.2 for learned dynamics. The linear training problem here is separately specified and solved.
    • J. M. Sanz-Serna, “Symplectic Runge–Kutta schemes for adjoint equations, automatic differentiation, optimal control and more,” arXiv:1503.04021v2. See §2.1, Theorems 1–2 and Eq. (8), for quadratic invariants and symplecticity. The Euler and Cayley identities above are derived directly.

    Further reading

    Scrollable formula. Use Left and Right arrow keys to reveal hidden content.

    Scrollable table. Use Left and Right arrow keys to reveal hidden columns.

    Scrollable figure. Use Left and Right arrow keys to reveal hidden content.