Skip to content

Instantly share code, notes, and snippets.

@mrksr
Last active June 12, 2020 14:45
Show Gist options
  • Select an option

  • Save mrksr/6f020d6ce75ece9e0e1df16df21ef47a to your computer and use it in GitHub Desktop.

Select an option

Save mrksr/6f020d6ce75ece9e0e1df16df21ef47a to your computer and use it in GitHub Desktop.
gpflow-predict-partial
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import tensorflow as tf
from GPflow.gpr import GPR
from GPflow.param import AutoFlow
from GPflow._settings import settings
float_type = settings.dtypes.float_type
class PartialGPR(GPR):
def build_predict(self, Xnew, full_cov=False, kx_kern=None):
"""
Xnew is a data matrix, point at which we want to predict
This method computes
p(F* | Y )
where F* are points on the GP at Xnew, Y are noisy observations at X.
"""
if kx_kern is None:
kx_kern = self.kern
Kx = kx_kern.K(self.X, Xnew)
K = self.kern.K(self.X) + tf.eye(tf.shape(self.X)[0], dtype=float_type) * self.likelihood.variance
L = tf.cholesky(K)
A = tf.matrix_triangular_solve(L, Kx, lower=True)
V = tf.matrix_triangular_solve(L, self.Y - self.mean_function(self.X))
fmean = tf.matmul(A, V, transpose_a=True) + self.mean_function(Xnew)
if full_cov:
fvar = self.kern.K(Xnew) - tf.matmul(A, A, transpose_a=True)
shape = tf.stack([1, 1, tf.shape(self.Y)[1]])
fvar = tf.tile(tf.expand_dims(fvar, 2), shape)
else:
fvar = self.kern.Kdiag(Xnew) - tf.reduce_sum(tf.square(A), 0)
fvar = tf.tile(tf.reshape(fvar, (-1, 1)), [1, tf.shape(self.Y)[1]])
return fmean, fvar
@AutoFlow(Xnew=(float_type, [None, None]))
def predict_f_partial(self, Xnew, full_cov=False, kern=None):
return self.build_predict(Xnew, full_cov, kern)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment