# Lux.jl Neural ODE Initial Condition Optimization

How do we also optimize the initial condition of a neural ODE using Lux.jl? I have not been able to find any examples of this. Is there any way to just add the initial conditions as parameters to a neural ODE that is defined by Lux.Chain? Or we have to create our own custom layer if we want to optimize over initial conditions? Thanks you!

You don’t have to do anything special. It already differentiates with respect to the initial conditions of the ODE, so if you just make the initial conditions part of the state of the optimization problem then it’ll optimize them just like the parameters

Can you share what code you tried?

I see. I’ve just been following the main Neural ODE example from the DIffEqFlux website:

``````using ComponentArrays, Lux, DiffEqFlux, DifferentialEquations, Optimization, OptimizationOptimJL, Random, Plots

rng = Random.default_rng()
u0 = Float32[2.0; 0.0]
datasize = 30
tspan = (0.0f0, 1.5f0)
tsteps = range(tspan, tspan, length = datasize)

function trueODEfunc(du, u, p, t)
true_A = [-0.1 2.0; -2.0 -0.1]
du .= ((u.^3)'true_A)'
end

prob_trueode = ODEProblem(trueODEfunc, u0, tspan)
ode_data = Array(solve(prob_trueode, Tsit5(), saveat = tsteps))

dudt2 = Lux.Chain(x -> x.^3,
Lux.Dense(2, 50, tanh),
Lux.Dense(50, 2))
p, st = Lux.setup(rng, dudt2)
prob_neuralode = NeuralODE(dudt2, tspan, Tsit5(), saveat = tsteps)

function predict_neuralode(p)
Array(prob_neuralode(u0, p, st))
end

function loss_neuralode(p)
pred = predict_neuralode(p)
loss = sum(abs2, ode_data .- pred)
return loss, pred
end

# Do not plot by default for the documentation
# Users should change doplot=true to see the plots callbacks
callback = function (p, l, pred; doplot = false)
println(l)
# plot current prediction against data
if doplot
plt = scatter(tsteps, ode_data[1,:], label = "data")
scatter!(plt, tsteps, pred[1,:], label = "prediction")
display(plot(plt))
end
return false
end

pinit = ComponentArray(p)
callback(pinit, loss_neuralode(pinit)...; doplot=true)

# use Optimization.jl to solve the problem

optf = Optimization.OptimizationFunction((x, p) -> loss_neuralode(x), adtype)
optprob = Optimization.OptimizationProblem(optf, pinit)

result_neuralode = Optimization.solve(optprob,
callback = callback,
maxiters = 300)
``````

Here, the parameter vector p won’t naturally contain the initial condition of the ODE unless I change it somehow.

So in the predict function just make u0 a function of p by subsetting it or using a component array and making it a separate component of what’s being optimized?

I just don’t understand how to create an entirely new set of parameters to optimize in Lux. In PyTorch this can be easily done by defining:

``````u0 = torch.nn.Parameter(torch.zeros(n, d))
``````

Then I could optimize these parameters along with the weights and biases of my neural network.

Also, as a separate question. Are any of the optimization methods using continuous sensitivity analysis, or they discretize first and then differentiate?

We have some of all of it. If you want the details, read:

https://docs.sciml.ai/SciMLSensitivity/stable/manual/differential_equation_sensitivities/

Just extend the componentarray. If no one else gets around to it I’ll post it when at a computer

Thank you, yes this would be super useful to see an example!

Hi @ChrisRackauckas, I’m still having trouble understanding how to add the initial condition into the optimization. Any advice or a short example would be much appreciated!

I don’t have time to run it locally but this is what it should look like on your code:

``````using ComponentArrays, Lux, DiffEqFlux, DifferentialEquations, Optimization, OptimizationOptimJL, Random, Plots

rng = Random.default_rng()
u0 = Float32[2.0; 0.0]
datasize = 30
tspan = (0.0f0, 1.5f0)
tsteps = range(tspan, tspan, length = datasize)

function trueODEfunc(du, u, p, t)
true_A = [-0.1 2.0; -2.0 -0.1]
du .= ((u.^3)'true_A)'
end

prob_trueode = ODEProblem(trueODEfunc, u0, tspan)
ode_data = Array(solve(prob_trueode, Tsit5(), saveat = tsteps))

dudt2 = Lux.Chain(x -> x.^3,
Lux.Dense(2, 50, tanh),
Lux.Dense(50, 2))
p, st = Lux.setup(rng, dudt2)
prob_neuralode = NeuralODE(dudt2, tspan, Tsit5(), saveat = tsteps)

theta_init = ComponentArray(p=p,u0=u0)

function predict_neuralode(theta)
Array(prob_neuralode(theta.u0, theta.p, st))
end

function loss_neuralode(theta)
pred = predict_neuralode(theta)
loss = sum(abs2, ode_data .- pred)
return loss, pred
end

# Do not plot by default for the documentation
# Users should change doplot=true to see the plots callbacks
callback = function (theta, l, pred; doplot = false)
println(l)
# plot current prediction against data
if doplot
plt = scatter(tsteps, ode_data[1,:], label = "data")
scatter!(plt, tsteps, pred[1,:], label = "prediction")
display(plot(plt))
end
return false
end

callback(theta_init, loss_neuralode(theta_init)...; doplot=true)

# use Optimization.jl to solve the problem

optf = Optimization.OptimizationFunction((theta, x) -> loss_neuralode(theta), adtype)
optprob = Optimization.OptimizationProblem(optf, theta_init)

result_neuralode = Optimization.solve(optprob,
callback = callback,
maxiters = 300)
``````

let me know if that worked.

Chris’ example worked for me if you change the argument order of `x` and `theta` to this

``````optf = Optimization.OptimizationFunction((theta, x) -> loss_neuralode(theta), adtype)
``````

or at least it seems to update both `p` and `u0` and reduced the loss.

Cool, updated.

Thanks, yes it works great!