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[-1.9206271 1.9113175; -0.073875956 -0.7335598; … ; 2.0522287 -0.09730334; -3.4615722 -0.64458156], bias = Float32[-0.23694964, -0.8167005, 0.5975093, 0.4918493, -0.4115598, -0.953506, -1.4034407, 7.614639, 1.2893108, -0.39812756 … 0.4013341, -0.990038, -0.9293806, 0.14866865, -2.4439874, -2.1010656, 0.17779596, 4.1699, 0.43708217, 1.0499145]), layer_2 = (weight = Float32[0.063518316 -0.02974085 … -0.04043958 -0.59169775; 0.52702016 -0.40985125 … 0.3065347 0.74266267], bias = Float32[0.1200867, 0.5054528]))