Multiply kernel and function

Given a valid kernel k_1(x, x'), and a (parametric) function f(x), it follows that:

k_2(x, x') = f(x) k_1(x, x') f(x'),

is also a valid kernel.

Could you please tell me how to implement this property in gpflow?

Thank you

Upvotes: 1

Views: 122

Answers (1)

Vincent Dutordoir
Vincent Dutordoir

Reputation: 276

The easiest way to achieve this in GPflow is to create a new class, that inherits from Kernel, which adds your desired functionality. The code you'll need for this will look similar to:

import gpflow
from typing import Callable

class MyKernel(gpflow.Kernel):

    def __init__(self, input_dim, base_kernel: gpflow.Kernel, function: Callable):
        super().__init__(input_dim)
        self.base_kernel = base_kernel
        self.function = function

    def K(self, X, X2=None):
        if X2 is None:
            X2 = X
        return self.function(X) * self.base_kernel.K(X, X2) * self.function(X2)

    def Kdiag(self, X):
        return self.function(X)**2 * self.base_kernel.Kdiag(X, X2)

If you also want to optimise the parameters of self.function you will want to make sure that self.function inherits from the gpflow's Parameterized class. This will make sure that all its parameters are collected when the objective gets optimised.

Hope this helps you out.

Upvotes: 3

Related Questions