FluxML / Zygote.jl

21st century AD
https://fluxml.ai/Zygote.jl/
Other
1.48k stars 212 forks source link

Zygote incorrectly treats an interpolation as an AbstractArray #868

Open leventov opened 3 years ago

leventov commented 3 years ago
using Interpolations, Zygote
itp = interpolate(([0.0,1.0],), [1.0, 2.0], Gridded(Linear()))
f(c) = itp[c]
f'(0.5)

ERROR: ArgumentError: invalid index: 0.5 of type Float64
Stacktrace:
 [1] to_index(::Float64) at ./indices.jl:297
 [2] to_index(::Array{Float64,1}, ::Float64) at ./indices.jl:274
 [3] to_indices at ./indices.jl:325 [inlined]
 [4] to_indices at ./indices.jl:322 [inlined]
 [5] view at ./subarray.jl:157 [inlined]
 [6] (::Zygote.var"#356#358"{Interpolations.GriddedInterpolation{Float64,1,Float64,Gridded{Linear},Tuple{Array{Float64,1}}},Tuple{Float64}})(::Float64) at /Users/leventov/.julia/packages/Zygote/ggM8Z/src/lib/array.jl:41
 [7] (::Zygote.var"#2209#back#352"{Zygote.var"#356#358"{Interpolations.GriddedInterpolation{Float64,1,Float64,Gridded{Linear},Tuple{Array{Float64,1}}},Tuple{Float64}}})(::Float64) at /Users/leventov/.julia/packages/ZygoteRules/OjfTt/src/adjoint.jl:59
 [8] f at ./REPL[36]:1 [inlined]
 [9] (::typeof(∂(f)))(::Float64) at /Users/leventov/.julia/packages/Zygote/ggM8Z/src/compiler/interface2.jl:0
 [10] (::Zygote.var"#41#42"{typeof(∂(f))})(::Float64) at /Users/leventov/.julia/packages/Zygote/ggM8Z/src/compiler/interface.jl:40
 [11] gradient(::Function, ::Float64) at /Users/leventov/.julia/packages/Zygote/ggM8Z/src/compiler/interface.jl:49
 [12] (::Zygote.var"#43#44"{typeof(f)})(::Float64) at /Users/leventov/.julia/packages/Zygote/ggM8Z/src/compiler/interface.jl:52
 [13] top-level scope at REPL[41]:1

Possibly related to https://github.com/FluxML/Zygote.jl/issues/643.

mcabbott commented 3 years ago

Maybe the short-term work-around is to define f(c) = Zygote.forwarddiff(c -> itp[c], c).

leventov commented 3 years ago

@mcabbott thanks. As an anecdote, I arrived at this workaround myself, but it took me significant time lurking around the documentation and trying to connect the dots. I think it would be useful to add a section to documentation "what if Zygote fails to differentiate your code".

mcabbott commented 3 years ago

I haven't looked in ages but yes, sure. Maybe the "Debugging" section should start with ways to get things running at all, not with profiling?