JuliaDiff / JuliaDiff/ReverseDiff.jl
DimensionMismatch error
Nobody has claimed this yet.
- Dominant language
- Julia
- Stars
- 393
- Forks
- 60
- Avg merge
- 18h 24m
- Merged PRs (30d)
- 8
Description
I asked this question in the #autodiff channel on Slack but haven't found out if I'm doing something wrong, hitting a bug in ReverseDiff, or hitting an incompatibility with DifferentialEquations.
I’m trying to differentiate through a system of ODEs. The function F returns simulations F(x) and I’m trying to obtain the product J(x)*u and J(x)ᵀ*v where J is the Jacobian of F. It works with ForwardDiff, but ReverseDiff throws an error…
using DifferentialEquations, DiffEqSensitivity, ForwardDiff, ReverseDiff
function ODE(dx, x, p, t)
V, W = x
I, μ, a, b, c = p
dx[1] = (V - V^3 / 3 - W + I) / μ
dx[2] = μ * (a * V - b * W + c)
end
prob = ODEProblem(ODE, [2.0; 0.0], (0.0, 20.0), [0.5, 0.08, 1.0, 0.8, 0.7])
function F(x)
temp_prob = remake(prob, p = x)
sol = solve(temp_prob, Vern9(), saveat = 0.2)
return vec(sol)
end
jprod_fwd(f, x, u) = ForwardDiff.derivative(t -> f(x + t * u), 0)
jprod_fwd(F, rand(5), rand(5)) # works
jprod_rev(f, x, u) = ReverseDiff.jacobian(t -> f(x + t[1] * u), [zero(eltype(x))])
jprod_rev(F, rand(5), rand(5)) # throws DimensionMismatch ?!
jtprod_rev(f, x, u) = ReverseDiff.gradient(z -> dot(f(z), u), x)
jtprod_rev (generic function with 1 method)
jtprod_rev(F, rand(5), rand(202))
ERROR: DimensionMismatch("arrays could not be broadcast to a common size; got a dimension with lengths 5 and 6")
Here is the full error message: https://gist.github.com/7544d5c680995d4634cab19a2cdccfd6
pkg> status
Status `~/dev/julia/BLA/myenv/Project.toml`
[41bf760c] DiffEqSensitivity v6.57.0
[0c46a032] DifferentialEquations v6.18.0
[f6369f11] ForwardDiff v0.10.19
[37e2e3b7] ReverseDiff v1.9.0
Many thanks in advance!
ps: I am aware that using ReverseDiff for J(x)*u isn't the most efficient in many cases, but it should work. It works with Zygote.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the provided ODE example with the listed DifferentialEquations, DiffEqSensitivity, ForwardDiff, and ReverseDiff versions, focusing on the ReverseDiff.jacobian and gradient calls. Compare the full error from the linked gist with the working ForwardDiff and Zygote cases; done means identifying whether the mismatch is a ReverseDiff bug or a package incompatibility and capturing a minimal reproducer.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- julia
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100