-
hi, i am new guy. in my project, I want to usea customer MultiOutput kernels, I tried, but failed. my kernel form refer to the attachment. can you help me code it by gpytorch when you are free? thank you very much! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
I show my code as follow, please help me to check it ,thanks @gpleiss @arthus701 class CurlFreeKernel(gpytorch.kernels.Kernel):
|
Beta Was this translation helpful? Give feedback.
-
I think there may have been a few typos in @arthus701 's suggestion. Here's my attempt. A debugging tip: make sure the sizes of the object actually match the sizes in the comments. from gpytorch.lazy import KroneckerLazyProduct
def forward(self, x1, x2, **params):
# THIS IS JUST A CRUDE SKETCH! DON'T TAKE ANYTHING HERE FOR GRANTED!
n = x1.size(-2)
m = x2.size(-2)
d = x1.size(-1)
x1 = x1 / self.lengthscale
x2 = x2 / self.lengthscale
# Diff tensor: ... x n x m x d
diff = x1.unsqueeze(-2) - x2.unsqueeze(-3)
# Outer product term
outer = diff.unsqueeze(-1) - diff.unsqueeze(-2) # ... x n x m x d x d
outer = outer.transpose(-2, -3).contiguous # ... x n x d x m x d
# Exp term
norm = diff.pow(2).sum(dim=-1) # ... x n x m
exp_part = (-0.5 * norm).exp()
exp_part = exp_part[..., :, None, :, None] # ... x n x 1 x m x 1
# Combine terms
eye = torch.eye(d, dtype=x1.dtype, device=x1.device) # ... x d x d
eye = eye[..., None, :, None, :] # ... x 1 x d x 1 x d
kernel = (eye - outer) * exp_part
kernel = kernel * self.outputscale / self.lengthscale.pow(2)
return kernel Since your kernel is multi-output, you will also need to add this method to the kernel: def num_outputs_per_input(self, x1, x2):
return x1.size(-1) |
Beta Was this translation helpful? Give feedback.
I think there may have been a few typos in @arthus701 's suggestion. Here's my attempt.
Again, this is un tested. I've commented what all the shapes should be.
A debugging tip: make sure the sizes of the object actually match the sizes in the comments.