Neural Second Order Ordinary Differential Equation
The neural ODE focuses and finding a neural network such that:
\[u^\prime = NN(u)\]
However, often in physics-based modeling, the key object is not the velocity but the acceleration: knowing the acceleration tells you the force field and thus the generating process for the dynamical system. Thus what we want to do is find the force, i.e.:
\[u^{\prime\prime} = NN(u)\]
(Note that in order to be the acceleration, we should divide the output of the neural network by the mass!)
An example of training a neural network on a second order ODE is as follows:
import SciMLSensitivity as SMS
import OrdinaryDiffEq as ODE
import Lux
import Optimization as OPT
import OptimizationOptimisers as OPO
import RecursiveArrayTools
import Random
import ComponentArrays as CA
u0 = Float32[0.0; 2.0]
du0 = Float32[0.0; 0.0]
tspan = (0.0f0, 1.0f0)
t = range(tspan[1], tspan[2], length = 20)
model = Lux.Chain(Lux.Dense(2, 50, tanh), Lux.Dense(50, 2))
ps, st = Lux.setup(Random.default_rng(), model)
ps = CA.ComponentArray(ps)
model = Lux.StatefulLuxLayer{true}(model, ps, st)
ff(du, u, p, t) = model(u, p)
prob = ODE.SecondOrderODEProblem{false}(ff, du0, u0, tspan, ps)
function predict(p)
Array(ODE.solve(prob, ODE.Tsit5(); p, saveat = t))
end
correct_pos = Float32.(transpose(hcat(collect(0:0.05:1)[2:end], collect(2:-0.05:1)[2:end])))
function loss_n_ode(p)
pred = predict(p)
sum(abs2, correct_pos .- pred[1:2, :])
end
l1 = loss_n_ode(ps)
callback = function (state, l)
println(l)
l < 0.01
end
adtype = OPT.AutoZygote()
optf = OPT.OptimizationFunction((x, p) -> loss_n_ode(x), adtype)
optprob = OPT.OptimizationProblem(optf, ps)
res = OPT.solve(optprob, OPO.Adam(0.01); callback, maxiters = 1000)retcode: Default
u: ComponentVector{Float32}(layer_1 = (weight = Float32[-0.25315562 1.8242632; 0.8786554 -1.1197206; … ; 3.0630221 0.44900325; -0.70025885 0.8338958], bias = Float32[0.5994995, -0.7794348, -0.97163993, -0.4457326, -0.1703152, -0.1941054, -0.2730461, -0.06992979, 8.119837, -0.8759146 … 0.17655027, -5.1521606, -0.856352, -0.65041333, -2.9528058, -0.5701355, -0.032192953, 0.65779316, -0.25796655, 0.5165257]), layer_2 = (weight = Float32[0.020530721 -0.24056602 … -0.015299135 0.045750283; 0.5269971 -0.33295453 … -0.495186 0.23025742], bias = Float32[0.017385058, 0.4503357]))