FiniteDiff.jl
FiniteDiff.jl copied to clipboard
Output depends on values in cache
I'm not sure if this is intended behavior, but the output of finite_difference_hessian! is dependent on the values stored in the cache, as illustrated below:
using FiniteDiff
using Random
Random.seed!(1)
# Simple function
f(x) = x[1]^2 + cos(x[2] + 2x[6]) + x[5]*x[4] + x[3]^2
H = zeros(6,6)
x = randn(6)
cache = FiniteDiff.HessianCache(copy(x))
FiniteDiff.finite_difference_hessian!(H,f,x,cache)
H[1,1] # about 2.0 (correct)
# Change the input value drastically
x = randn(6) * 1e6
FiniteDiff.finite_difference_hessian!(H,f,x,cache)
H[1,1] # usually 1.35e7 (far off)
# Set the cache to be equal to the current value
cache.xmm .= x
cache.xmp .= x
cache.xpm .= x
cache.xpp .= x
FiniteDiff.finite_difference_hessian!(H,f,x,cache)
H[1,1] # about 2.0 (correct)
If this is intended behavior, it's probably worth mentioning in the documentation somewhere, and let the user know they should reset the cache variables before calling finite_difference_hessian! for best performance.
That's not intentional. Yeah it should reset the x caches. It's only doing the element-wise tweaks but never resets it to the base value. Can you PR adding the setup?