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 = 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 = callback, maxiters = 1000)retcode: Default
u: ComponentVector{Float32}(layer_1 = (weight = Float32[-0.73031205 -2.6946378; 0.022100346 -0.31637338; … ; 9.5946045 3.1692915; 6.5385766 1.6269816], bias = Float32[-0.016489875, -0.782069, 0.22492121, -1.2540073, -0.27993032, -0.60395455, -0.24480951, -0.27349123, -0.8439519, 0.084966525 … 0.078537956, -1.2222751, -0.46922836, 0.5023793, -5.0747, 0.35336572, 0.92084473, 0.34227437, -6.735433, -3.280322]), layer_2 = (weight = Float32[-0.0901203 -0.37209958 … -0.12961103 -0.5987668; -0.69230527 -0.58657426 … -1.6194822 -2.1994972], bias = Float32[0.14047655, 0.32171643]))