Skip to content

ForwardDiff fails for Matern kernels when the two arguments are equal #571

@amaurais

Description

@amaurais

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions