Search code examples
pythonmachine-learninggaussiangpflow

Resetting kernel hyperparameter values in GPflow


My use case is this: I have a function that takes in a kernel of the user's choice then I will iterate through every date in the dataset and use a Gaussian Process Regression to estimate the model using the specified kernel. However, since I'm pointing to the kernel object, I need to reset it to the default values before I run the next iteration.

import gpflow

class WrapperClass(object):
    def __init__(self, kernel):
        super().__init__()
        self.kernel = kernel

    def fit(self, X, y):
        m = gpflow.models.GPR(X, y, self.kernel) # I need to reset the kernel here

# some code later
def some_function(Xs, ys, ts, f):
    for t in ts:
        X = Xs.loc[t]    # pandas dataframe
        y = ys.loc[t]    # pandas
        f.fit(X, y)

k1 = gpflow.kernels.RBF(1)
k2 = gpflow.kernels.White(0.1)
k = k1 + k2
f = WrapperClass(k)
sume_function(Xs, ys, ts, f)

I've found the method read_trainables() on the kernel so one strategy is to save the settings the user has provided, but there doesn't seem to be any way to set them?

In [7]: k1.read_trainables()
Out[7]: {'Sum/rbf/lengthscales': array(1.), 'Sum/rbf/variance': array(1.)}

Cheers, Steve


Solution

  • You can set the parameters of Parameterized objects (models, kernels, likelihoods etc) using assign(): k1.assign(k1.read_trainables()) (or some other dict of path-value pairs). You might as well create a new kernel object, though!

    Note that each time you create new parameterized objects - this applies both to kernels and models, as in your fit() method - you add operations to the tensorflow graph, which can slow down graph computation significantly if it grows a lot. You probably want to look into manually handling tf.Graph() and tf.Session() to keep them distinct for each model. (See the notebooks on session handling and further tips and tricks in the new GPflow documentation.)