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

PeriodicKernel does not work with AD (see issue for work-around) #389

Closed
david-vicente opened this issue Oct 23, 2021 · 11 comments
Closed

Comments

@david-vicente
Copy link
Contributor

Hi.
I noticed that the PeriodicKernel is failling AD tests and I would like to implement the Mauna Loa GPR example. I tried looking for discussion regarding this issue in past Issues but there isn't much. Is the problem an instance of "it is doable but no one has had the time yet" or is it pending because of stuff that depends on AD packages?
Thanks!

@theogf
Copy link
Member

theogf commented Oct 25, 2021

I just reran the AD tests and it looks like using ForwardDiff or ReverseDiff is fine. However there are still some strong errors with Zygote. I think #386 should hopefully solve this problem.

Note that our PeriodicKernel is only equal to the ExpSineKernel from sci-kit learn for one dimension : vs

@david-vicente
Copy link
Contributor Author

Why isn't there a period length parameter as in gpytorch?

@theogf
Copy link
Member

theogf commented Oct 25, 2021

Because this would just be the lengthscale. You could achieve the same result with:

p = [1.0, 2.0, 1.5]
k = with_lengthscale(PeriodicKernel(;r=rand(3)), p)

where p is the same as in the gpytorch definition

The r here, corresponds to the sqrt(lambda) in gpytorch

@theogf theogf mentioned this issue Nov 5, 2021
15 tasks
@st--
Copy link
Member

st-- commented Nov 5, 2021

Oh, I was independently trying to build the MaunaLoa example (I put the work-in-progress here: https://github.com/JuliaGaussianProcesses/EcosystemIntegrationTesting/blob/main/api_testing/mauna_loa_example/script.jl as a Pluto.jl notebook) and ran into the same bug.

Here's code to reproduce the bug:

using AbstractGPs
using ParameterHandling
function build_gp(th)
    return GP(th.s^2 * with_lengthscale(PeriodicKernel(; r=[th.l/2]), th.p))
end
th = (; s = positive(exp(0.0)), l=positive(exp(1.0)), p=positive(exp(0.0)))
thflat, unflatten = ParameterHandling.value_flatten(th)
x = rand(5)
y = rand(5)
function loss(th)
    f=build_gp(th)
    return -logpdf(f(x, 0.01), y)
end
loss_packed = loss  unflatten
using Zygote
Zygote.gradient(loss_packed, thflat)

@st--
Copy link
Member

st-- commented Nov 5, 2021

@david-vicente the following two lines give equivalent kernels (in 1D):

k1 = with_lengthscale(PeriodicKernel(; r=[wiggle_scale / 2]), period)
k2 = with_lengthscale(SqExponentialKernel(), wiggle_scale)  PeriodicTransform(1/period)

and we might want to provide with_period(kernel, period) = kernel ∘ PeriodicTransform(1/period).
AutoDiff works fine for the PeriodicTransform. Using the second form, you can also swap out the base kernel, e.g. for a Matern32Kernel:)

(Note that wiggle_scale is relative to a period, not relative to the actual input scale!)

@david-vicente
Copy link
Contributor Author

david-vicente commented Nov 5, 2021

Thank you @st--. I did look to the PeriodicTransform at the time, but it wasn't obvious how to use it to achieve equivalent kernels from the documentation. Maybe we should add

k1 = with_lengthscale(PeriodicKernel(; r=[wiggle_scale / 2]), period)
k2 = with_lengthscale(SqExponentialKernel(), wiggle_scale)  PeriodicTransform(1/period)

to its documentation?

@st-- st-- changed the title Periodic Kernel PeriodicKernel does not work with AD (see issue for work-around) Nov 5, 2021
@st--
Copy link
Member

st-- commented Nov 5, 2021

@david-vicente I've just added the finished example to AbstractGPs: JuliaGaussianProcesses/AbstractGPs.jl#240

Yes it'd be great to improve the documentation. Would you be up for making the change and opening a PR? :)

@st--
Copy link
Member

st-- commented Nov 5, 2021

You could also add the with_periodic helper I suggested, if you wanted:)

@david-vicente
Copy link
Contributor Author

Can we do the same for multi-dimensional inputs?

@devmotion
Copy link
Member

Can we do the same for multi-dimensional inputs?

It would work in exactly the same way if we would make PeriodicTransform output complex numbers (using cispi and, on older Julia versions, cis).

@simsurace
Copy link
Member

Fixed by #531

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

No branches or pull requests

5 participants