# Local sensitivities via ForwardSensitivity from DifferentialEquations in optimization with AD

I am trying to optimize local sensitivities of an ode problem using `ODEForwardSensitivityProblem` from the `DifferentialEquations` suite, but the last line throws a `TypeError`.

I am using forward sensitivites as the ode system is small.

The minimal example is taken from here.

``````using DiffEqSensitivity
using ForwardDiff

function f(du,u,p,t)
du = dx = p*u - p*u*u
du = dy = -p*u + u*u
end

p = [1.5,1.0,3.0]
prob = ODEForwardSensitivityProblem(f,[1.0;1.0],(0.0,10.0),p)

function sensitivity(x)
_u0 = convert.(eltype(x), prob.u0)
_u0 = x
_p = convert.(eltype(x), prob.p)
_p = x
_prob = remake(prob,u0=_u0,p=_p)
sol = solve(_prob,Tsit5())
dp = extract_local_sensitivities(sol)
dp[end]
end

sensitivity(ones(2))
``````

Any help is much appreciated as it is not possible to use `ForwardSensitivityProblem` with any optimizer where an exact jacobian is required or beneficial, at least with the AD packages I tried, i.e. `ForwardDiff`and `Zygote`.

If you’re doing ForwardDiff on the outside, then the problem is AD-differentiable, in which case you might as well use double ForwardDiff. The following works fine:

``````using DiffEqSensitivity, OrdinaryDiffEq
using ForwardDiff

function f(du,u,p,t)
du = dx = p*u - p*u*u
du = dy = -p*u + u*u
end

p = [1.5,1.0,3.0]
prob = ODEProblem(f,[1.0;1.0],(0.0,10.0),p)
function sensitivity(x)
_u0 = convert.(eltype(x), prob.u0)
_u0 = x
_p = convert.(eltype(x), prob.p)
_p = x
prob2 = remake(prob,u0=_u0,p=_p)
dp = ForwardDiff.jacobian(_p) do _p2
prob3 = remake(prob2,u0=convert.(eltype(_p2),prob2.u0),p=_p2)
solve(prob3,Tsit5())[end]
end
dp
end

sensitivity(ones(2))
``````

Now if you really want to use the sensitivity problem, you’re not differentiating the problem construction. You’d want to do something like:

``````using DiffEqSensitivity, OrdinaryDiffEq
using ForwardDiff

function f(du,u,p,t)
du = dx = p*u - p*u*u
du = dy = -p*u + u*u
end

p = [1.5,1.0,3.0]

function sensitivity(x)
_u0 = convert.(eltype(x), prob.u0)
_u0 = x
_p = convert.(eltype(x), prob.p)
_p = x
_prob = ODEForwardSensitivityProblem(f,_u0,(0.0,10.0),_p,ForwardSensitivity(autojacvec=false))
sol = solve(_prob,Tsit5())
dp = extract_local_sensitivities(sol)
dp[end]
end

sensitivity(ones(2))