JuliaDiff / JuliaDiff/ForwardDiff.jl
Throw a `DimensionMismatch` error instead of `MethodError` when using wrong differentiation method for function
Nobody has claimed this yet.
- Dominant language
- Julia
- Stars
- 1k
- Forks
- 160
- PR merge metrics
- No merged PRs in 30d
Description
I find I frequently mix up derivative(), gradient() and jacobian(), especially when I'm debugging and testing several different functions and I forget to switch to the correct one. If I actually use gradient on a vector-valued function, for example, I get this:
MethodError: no method matching extract_gradient!(::Type{ForwardDiff.Tag{getfield(Main, Symbol("##58#59")),Float64}}, ::Array{Array{Dual{ForwardDiff.Tag{getfield(Main, Symbol("##58#59")),Float64},Float64,3},1},1}, ::Array{Dual{ForwardDiff.Tag{getfield(Main, Symbol("##58#59")),Float64},Float64,3},1})
Closest candidates are:
extract_gradient!(::Type{T}, ::AbstractArray, !Matched::Dual) where T at /home/jlumpe/.julia/packages/ForwardDiff/okZnq/src/gradient.jl:76
extract_gradient!(::Type{T}, ::AbstractArray, !Matched::Real) where T at /home/jlumpe/.julia/packages/ForwardDiff/okZnq/src/gradient.jl:75
extract_gradient!(::Type{T}, !Matched::DiffResults.DiffResult, !Matched::Dual) where T at /home/jlumpe/.julia/packages/ForwardDiff/okZnq/src/gradient.jl:70
...
Stacktrace:
[1] vector_mode_gradient(::getfield(Main, Symbol("##58#59")), ::Array{Float64,1}, ::ForwardDiff.GradientConfig{ForwardDiff.Tag{getfield(Main, Symbol("##58#59")),Float64},Float64,3,Array{Dual{ForwardDiff.Tag{getfield(Main, Symbol("##58#59")),Float64},Float64,3},1}}) at /home/jlumpe/.julia/packages/ForwardDiff/okZnq/src/gradient.jl:98
[2] gradient(::Function, ::Array{Float64,1}, ::ForwardDiff.GradientConfig{ForwardDiff.Tag{getfield(Main, Symbol("##58#59")),Float64},Float64,3,Array{Dual{ForwardDiff.Tag{getfield(Main, Symbol("##58#59")),Float64},Float64,3},1}}, ::Val{true}) at /home/jlumpe/.julia/packages/ForwardDiff/okZnq/src/gradient.jl:17
[3] gradient(::Function, ::Array{Float64,1}, ::ForwardDiff.GradientConfig{ForwardDiff.Tag{getfield(Main, Symbol("##58#59")),Float64},Float64,3,Array{Dual{ForwardDiff.Tag{getfield(Main, Symbol("##58#59")),Float64},Float64,3},1}}) at /home/jlumpe/.julia/packages/ForwardDiff/okZnq/src/gradient.jl:15 (repeats 2 times)
[4] top-level scope at In[60]:1
This error is rather cryptic and it's not immediately obvious what your mistake was when you run into it for the first time. It's not a big deal, but it might be nice to overload extract_gradient!() and others to throw a DimensionMismatch or something with an informative message instead.
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 in gradient.jl at the extract_gradient! overloads and vector_mode_gradient shown in the stack trace, then reproduce the example using gradient on a vector-valued function. Done means the misuse raises an informative DimensionMismatch instead of a cryptic MethodError.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- julia
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100