KernelFunctions.jl icon indicating copy to clipboard operation
KernelFunctions.jl copied to clipboard

ForwardDiff fails for Matern kernels when the two arguments are equal

Open amaurais opened this issue 6 months ago • 0 comments

I have a project in which I take gradients of Matern kernels with respect to an argument via ForwardDiff and was able to do so without issue until recently. When using KernelFunctions v0.10.58 or greater or ForwardDiff v1.0.0 or greater, however, calling ForwardDiff.gradient with respect to an argument on a Matern kernel returns a vector of NaNs when the two arguments to the kernel are equal. If I use Zygote instead I get a vector of zeros. MWE:

using KernelFunctions 
using ForwardDiff 
using Zygote 

vec1 = randn(2) 
vec2 = randn(2) 

myfun(x) = MaternKernel(ν=2.1)(x, vec1) 

ForwardDiff.gradient(myfun, vec1) # vector of NaNs
ForwardDiff.gradient(myfun, vec2) # works as expected 

Zygote.gradient(myfun, vec1) # vector of 0s
Zygote.gradient(myfun, vec2) # works as expected 

For reference, ForwardDiff.gradient(myfun, vec1) returns a vector of zeros with KernelFunctions v0.10.57 and ForwardDiff v0.10.38.

amaurais avatar Apr 22 '25 18:04 amaurais