Skip to content
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

Output depends on values in cache #127

Closed
bjack205 opened this issue Nov 19, 2021 · 1 comment · Fixed by #186
Closed

Output depends on values in cache #127

bjack205 opened this issue Nov 19, 2021 · 1 comment · Fixed by #186

Comments

@bjack205
Copy link

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.

@ChrisRackauckas
Copy link
Member

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants