-
-
Notifications
You must be signed in to change notification settings - Fork 213
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
Second derivative of univariate functions: hessian() only takes arrays #891
Comments
This is a bug, the docstring says it should work. But for now, you could consider writing |
Thank you, that works, and without all the wrapping. For completeness, prms = Dict{Symbol,Float64}(:c => 1.0)
function myfun(x, c = prms[:c])
3*x^2 + 2*x + c
end
ForwardDiff.derivative(x -> ForwardDiff.derivative(myfun, x), 3.0) # OK
Zygote.hessian(x -> myfun(x[]), [3.0]) # OK
myfun''(3.0) # ERROR: Can't differentiate foreigncall expression |
Yes, unfortunately Zygote alone does not handle second derivatives very well. The rules it uses to work out gradients are often not written in such a way that they themselves are differentiable. Sometimes they work but it hasn't been something people focused on. For this For high-order derivatives of scalar functions, you may also look into TaylorSeries.jl. |
Great, thanks a lot for the explanations! I think |
Unlike gradient, hessian only has an (f, x::AbstractArray) method, so it won't take Floats. One has to pass an array of length one, and use a wrapper (anonymous) function to extract the value. Example:
Btw, the
f''()
notation fails for my actual function with a Can't differentiate foreigncall expression errorThe text was updated successfully, but these errors were encountered: