# Value of gradients with DiffEqFlux and solve

As recently announced, solve command is now compatible with Flux and should be used. I am confused about the value of the gradients which I obtain in these two very simple examples.

using Flux, DiffEqFlux, DifferentialEquations

#parameter
p=[5.0f0]
#ini state
u0=[2.0f0]

function f(u,p,t)
p*u
end

dt=0.005f0
tspan = (0.0,dt)

function dynamics1(p)
prob1 = ODEProblem(f,u0,tspan,p)
end

prob2 = ODEProblem(f,u0,tspan,p)
function dynamics2(p)
end

#some loss function
loss1(p) = sum(abs.(dynamics1(p)-u0))
loss2(p) = sum(abs.(dynamics2(p)-u0))

θ = Flux.params(p)
loss1(p)
end

loss2(p)
end
#gs1 exactly twice larger as the other version


The only difference is that prob1 is defined inside the gradient loop and prob2 outside. In the first case (gs1) I get exactly twice larger gradient than for gs2. Where does this factor 2 come from? When I use the old concrete_solve the results come out identical. Can it be linked to the fact that solve does not explicitly take the parameters p as an argument?

Yeah, something is odd here. If you explicitly connect p to the problem you get 2x the gradient. Looks like a Zygote issue:

using Flux, DiffEqFlux, DifferentialEquations

#parameter
p=[5.0f0]
#ini state
u0=[2.0f0]

function f(u,p,t)
p*u
end

dt=0.005f0
tspan = (0.0,dt)

function dynamics1(p)
prob1 = ODEProblem(f,u0,tspan,p)
end

prob2 = ODEProblem(f,u0,tspan,p)
function dynamics2(p)
end

function dynamics3(p)
end

function dynamics4()
end

#some loss function
loss1(p) = sum(abs.(dynamics1(p)-u0))
loss2(p) = sum(abs.(dynamics2(p)-u0))
loss3(p) = sum(abs.(dynamics3(p)-u0))
loss4() = sum(abs.(dynamics4()-u0))

θ = Flux.params(p)
loss1(p)
end

loss2(p)
end

loss3(p)
end