MWE:
# %%
using DifferentialEquations
using ComponentArrays
import DifferentiationInterface as DI
import SciMLSensitivity as SMS
import Enzyme
using Random
# Chaotic, so gradients are unreliable, but just for fun
function lorenz!(du, u, p, t)
du[1] = p[1] * (u[2] - u[1])
du[2] = u[1] * (p[2] - u[3]) - u[2]
du[3] = u[1] * u[2] - p[3] * u[3]
return nothing
end
# %%
const u0 = ComponentVector(x=1.0, y=0.0, z=0.0)
p_true = ComponentArray(sigma = 10.0, rho = 28.0, beta = 8.0/3.0)
p_init = ComponentArray(sigma = 5.0, rho = 15.0, beta = 1.0)
tspan = (0.0, 10.0)
prob_true = ODEProblem(lorenz!, u0, tspan, p_true)
sol_true = solve(prob_true, Tsit5(), saveat=0.1)
# %%
Random.seed!(123)
const data = Array(sol_true) + 2.0 * randn(size(Array(sol_true)))
# %%
function loss(p)
sol = solve(prob_true, Tsit5(); p, saveat=0.1)
return sum(abs2, Array(sol) - data)
end
backend = SMS.AutoEnzyme() # Works
dp = DI.gradient(loss, backend, p_init)
Results: Passing p_init instead of p_init[:] crashes the REPL.
MWE:
Results: Passing
p_initinstead ofp_init[:]crashes the REPL.