-
-
Notifications
You must be signed in to change notification settings - Fork 46
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
ForwardDiff.gradient fails on interpolated function when passing a vector input #229
Comments
We have a derivative overload, but it must only capture and handle the chunk size 1 case. @sathvikbhagavan could you take a look? |
@Leebre, I am not sure why you would do Doing: julia> grad = ForwardDiff.derivative.((interp,), myvec)
20-element Vector{Float64}:
0.34909318900754527
0.5810773018949766
0.5810773018949766
0.34909318900754527
0.34909318900754527
-0.44018781608409474
0.5810773018949766
-0.28332889046138166
⋮
0.34909318900754527
0.34909318900754527
-0.44018781608409474
0.34909318900754527
0.34909318900754527
0.5810773018949766
0.34909318900754527 works. You can also use julia> DataInterpolations.derivative.((interp,), myvec)
20-element Vector{Float64}:
0.34909318900754527
0.5810773018949766
0.5810773018949766
0.34909318900754527
0.34909318900754527
-0.44018781608409474
0.5810773018949766
-0.28332889046138166
⋮
0.34909318900754527
0.34909318900754527
-0.44018781608409474
0.34909318900754527
0.34909318900754527
0.5810773018949766
0.34909318900754527 |
@sathvikbhagavan well ... it can be vector-valued though, in the sense that the function can accept a vector argument and output a vector. To provide more context for my use case: I am not calling I suppose I could add a for loop in my f-function, to pass each entry of the vector individually to the interpolation function. However, I would be concerned that adding for loops to my inner f-function may have a performance impact on my code. The interpolation functions in Interpolations.jl do allow for a vector of dual numbers to be passed through. I noticed this issue after switching from Interpolations.jl to DataInterpolations. Perhaps @ChrisRackauckas could provide some additional comments on whether this functionality should be present? |
It needs to be a Jacobian otherwise you get a ForwardDiff error: using DataInterpolations
using ForwardDiff
# create an interpolated function from some discrete data:
u = rand(5)
t = 0:4
interp = LinearInterpolation(u, t,extrapolate=true)
# ForwardDiff derivative works with a single scalar argument:
grad1 = ForwardDiff.derivative(interp,2.4)
# However, the AD seems to fail, if I try to compute a gradient from a vector input (which my more complex f-function is doing):
myvec = rand(20).*4.0
interp(myvec) # (works)
grad = ForwardDiff.jacobian(interp,myvec) # works! but this needs #230 |
@ChrisRackauckas thanks for addressing this. I will give it a try when I get a chance over the next few days! |
Describe the bug 🐞
ForwardDiff.gradient seems to fail (AD error) when trying to differentiate an interpolated function using a vector input.
Expected behavior
ForwardDiff should work and output a valid gradient vector.
Minimal Reproducible Example 👇
MWE to duplicate the AD error I'm seeing with DataInterpolations.jl
Error & Stacktrace⚠️
Environment (please complete the following information):
using Pkg; Pkg.status()
using Pkg; Pkg.status(; mode = PKGMODE_MANIFEST)
versioninfo()
Additional context
The issue was discovered when trying to use an interpolated function inside an f-function being used with OrdinaryDiffEQ (KenCarp4 solver).
The text was updated successfully, but these errors were encountered: