-
-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Which types work as container of parameters? #178
Comments
It's because the generated ODE only works with mutation and tuples are not mutable. So SciML/SciMLSensitivity.jl#113 |
Thanks for the quick answer! Even though that means we will probably have to change our API... |
No problem. This will take a bit to solve. |
Does that mean your are working on a solution to this? So it might be possible at some point in the future? |
We would also need to solve SciML/DifferentialEquations.jl#566 |
I've been trying other types of multi-dimensional parameters: N-dimensional arrays work fine Arrays of Arrays don't work What's the matter with these constructions? using Flux, Optim, DiffEqFlux, OrdinaryDiffEq, Plots
### Works fine
function f!(dx, x, p, t)
dx[1] = p[1, 1]
dx[2] = p[2, 1]
end
p = [1. 5.; 5. 1.]
### MethodError: no method matching zero(::Type{Array{Float64,1}})
function f!(dx, x, p, t)
dx[1] = p[1][1]
dx[2] = p[2][1]
end
p = [[1.], [5.]]
### MethodError: no method matching zero(::Type{Any})
function f!(dx, x, p, t)
dx[1] = p.one
dx[2] = p.two
end
mutable struct two_p
one::Float64
two::Float64
end
p = two_p(1.,5.)
### ODE stuff
x0 = [1., 2.]
tspan = (0., 2.)
prob = ODEProblem(f!, x0, tspan, p)
sol = solve(prob, Tsit5())
plot(sol)
### DiffEqFlux Stuff starts here ###
function predict_adjoint(p) # Our 1-layer neural network
Array(concrete_solve(prob, Tsit5(), x0, p))
end
function loss_adjoint(p)
prediction = predict_adjoint(p)
loss = sum(abs2, prediction[:,end] .-1)
loss, prediction
end
cb = function (p, l, pred) # callback function to observe training
display(l)
# using `remake` to re-create our `prob` with current parameters `p`
display(plot(solve(remake(prob,p=p), Tsit5())))
return false
end
# Display the ODE with the initial parameter values.
cb(p,loss_adjoint(p)...)
res = DiffEqFlux.sciml_train(loss_adjoint, p, BFGS(), cb = cb)
|
You need something that can solve a differential equation, or move to a sensealg that is doing pure AD instead of using an adjoint method. |
I recently learned about p = ArrayPartition([1.],[5.]) I get |
ReverseDiff.jl doesn't like ArrayPartitions, and there's a few things left to get them fully supported in Zygote. |
Constructing the p = [1., 5.]
function wrapper!(dx, x, p, t)
f!(dx, x, ArrayPartition([p[1]],[p[2]]),t)
end |
Yeah, it just doesn't allow custom array types to pass along its adjoints, so that's then what causes the issue. |
Documented upstream in the DiffEqSensitivity docstrings. |
Our package provides an API that handles parameters differently depending on them being either tuples or arrays. If only arrays are passed everything works fine, but the tuple syntax seems to be incompatible with DiffEqFlux right now. Why is that?
MWE:
It would be nice if one of the two constructions worked.
The text was updated successfully, but these errors were encountered: