Set a prior over a linear mean function in gpflow 2.9.0

64 views Asked by At

I set a prior over a linear mean function in my model in gpflow 2.9.0 but, when I inspect it through the monitor, the prior does not appear.

This is my model:

import gpflow
import numpy as np
import tensorflow_probability as tfp
import tensorflow as tf


A = gpflow.Parameter(np.zeros(1))
A.prior=tfp.distributions.Normal(loc=1.0, scale=10.0)
b = gpflow.Parameter(np.zeros(1))
b.prior=tfp.distributions.Normal(loc=1.0, scale=10.0)
mf = gpflow.functions.Linear(A,b)

#I just write a simplify form of the mean. I don't write the rest of the model k, lik...

I get this in the monitor:

So, I want to know if there is another arg of the class Parameter that I'm using wrong

1

There are 1 answers

0
LePe77it On

I fixed the bug creating a new class for the mean function like this:

from gpflow.mean_functions import MeanFunction 
#can aggregate this 'from' in the first part
class Linear(MeanFunction):
    def __init__(self, A=None, b=None):
        MeanFunction.__init__(self)
        self.A = gpflow.Parameter(A, dtype=gpflow.default_float()) if A is not None else None
        self.b = gpflow.Parameter(b, dtype=gpflow.default_float()) if b is not None else None

    def __call__(self, X):
        if self.A is None:
            return self.b * tf.ones((X.shape[0], 1), dtype=gpflow.default_float())
        else:
            return tf.matmul(X, self.A) + self.b

So, to set the priors over A and b, you can do as shown in the first piece of code, then design the model, e.g., as:

gpflow.models.GPMC((X, y), kernel=k, mean_function=Linear(A,b), likelihood=lik)