2013-06-05 14:11:49 +01:00
|
|
|
# Copyright (c) 2012, GPy authors (see AUTHORS.txt).
|
|
|
|
|
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
2014-02-14 11:38:34 +00:00
|
|
|
from ..util.linalg import mdot, tdot, symmetrify, backsub_both_sides, dtrtrs, dpotrs, dpotri
|
2014-01-22 15:24:05 +00:00
|
|
|
from gp import GP
|
|
|
|
|
from parameterization.param import Param
|
2014-01-27 15:02:25 +00:00
|
|
|
from ..inference.latent_function_inference import varDTC
|
2014-01-29 17:02:44 +00:00
|
|
|
from .. import likelihoods
|
2014-02-11 15:23:49 +00:00
|
|
|
from GPy.util.misc import param_to_array
|
2013-06-05 14:11:49 +01:00
|
|
|
|
2014-01-22 15:24:05 +00:00
|
|
|
class SparseGP(GP):
|
2013-06-05 14:11:49 +01:00
|
|
|
"""
|
2014-01-22 15:24:05 +00:00
|
|
|
A general purpose Sparse GP model
|
2013-06-05 14:11:49 +01:00
|
|
|
|
2014-01-28 14:40:07 +00:00
|
|
|
This model allows (approximate) inference using variational DTC or FITC
|
|
|
|
|
(Gaussian likelihoods) as well as non-conjugate sparse methods based on
|
|
|
|
|
these.
|
|
|
|
|
|
2013-06-05 14:11:49 +01:00
|
|
|
:param X: inputs
|
2013-06-05 16:14:43 +01:00
|
|
|
:type X: np.ndarray (num_data x input_dim)
|
2013-06-05 14:11:49 +01:00
|
|
|
:param likelihood: a likelihood instance, containing the observed data
|
|
|
|
|
:type likelihood: GPy.likelihood.(Gaussian | EP | Laplace)
|
2013-09-20 13:38:20 +01:00
|
|
|
:param kernel: the kernel (covariance function). See link kernels
|
2013-06-05 14:11:49 +01:00
|
|
|
:type kernel: a GPy.kern.kern instance
|
|
|
|
|
:param X_variance: The uncertainty in the measurements of X (Gaussian variance)
|
2013-06-05 16:14:43 +01:00
|
|
|
:type X_variance: np.ndarray (num_data x input_dim) | None
|
2014-01-22 15:24:05 +00:00
|
|
|
:param Z: inducing inputs
|
|
|
|
|
:type Z: np.ndarray (num_inducing x input_dim)
|
2013-09-20 13:38:20 +01:00
|
|
|
:param num_inducing: Number of inducing points (optional, default 10. Ignored if Z is not None)
|
2013-06-05 14:11:49 +01:00
|
|
|
:type num_inducing: int
|
2013-09-20 13:38:20 +01:00
|
|
|
|
2013-06-05 14:11:49 +01:00
|
|
|
"""
|
|
|
|
|
|
2014-01-22 15:24:05 +00:00
|
|
|
def __init__(self, X, Y, Z, kernel, likelihood, inference_method=None, X_variance=None, name='sparse gp'):
|
|
|
|
|
|
|
|
|
|
#pick a sensible inference method
|
|
|
|
|
if inference_method is None:
|
|
|
|
|
if isinstance(likelihood, likelihoods.Gaussian):
|
2014-01-29 17:02:44 +00:00
|
|
|
inference_method = varDTC.VarDTC()
|
2014-02-11 20:05:36 +00:00
|
|
|
else:
|
2014-02-13 08:53:14 +00:00
|
|
|
#inference_method = ??
|
2014-02-11 20:05:36 +00:00
|
|
|
raise NotImplementedError, "what to do what to do?"
|
2014-01-22 15:24:05 +00:00
|
|
|
print "defaulting to ", inference_method, "for latent function inference"
|
|
|
|
|
|
2014-02-05 16:23:35 +00:00
|
|
|
self.Z = Param('inducing inputs', Z)
|
2013-06-05 14:11:49 +01:00
|
|
|
self.num_inducing = Z.shape[0]
|
|
|
|
|
|
2014-01-29 17:02:44 +00:00
|
|
|
if not (X_variance is None):
|
2013-06-05 14:11:49 +01:00
|
|
|
assert X_variance.shape == X.shape
|
2014-01-29 17:02:44 +00:00
|
|
|
self.X_variance = X_variance
|
|
|
|
|
|
|
|
|
|
GP.__init__(self, X, Y, kernel, likelihood, inference_method=inference_method, name=name)
|
2014-01-30 12:03:02 +00:00
|
|
|
self.add_parameter(self.Z, index=0)
|
2014-02-13 08:53:14 +00:00
|
|
|
self.parameters_changed()
|
|
|
|
|
|
2014-02-05 16:23:35 +00:00
|
|
|
|
2014-01-22 15:24:05 +00:00
|
|
|
def parameters_changed(self):
|
2014-02-11 16:54:33 +00:00
|
|
|
self.posterior, self._log_marginal_likelihood, self.grad_dict = self.inference_method.inference(self.kern, self.X, self.X_variance, self.Z, self.likelihood, self.Y)
|
2013-06-05 14:11:49 +01:00
|
|
|
|
2014-02-13 08:53:14 +00:00
|
|
|
#The derivative of the bound wrt the inducing inputs Z ( unless they're all fixed)
|
|
|
|
|
if not self.Z.is_fixed:
|
|
|
|
|
self.Z.gradient = self.kern.gradients_X(self.grad_dict['dL_dKmm'], self.Z)
|
|
|
|
|
if self.X_variance is None:
|
|
|
|
|
self.Z.gradient += self.kern.gradients_X(self.grad_dict['dL_dKnm'].T, self.Z, self.X)
|
|
|
|
|
else:
|
|
|
|
|
self.Z.gradient += self.kern.dpsi1_dZ(self.grad_dict['dL_dpsi1'], self.Z, self.X, self.X_variance)
|
|
|
|
|
self.Z.gradient += self.kern.dpsi2_dZ(self.grad_dict['dL_dpsi2'], self.Z, self.X, self.X_variance)
|
2013-06-05 14:11:49 +01:00
|
|
|
|
|
|
|
|
def _raw_predict(self, Xnew, X_variance_new=None, which_parts='all', full_cov=False):
|
2013-08-21 09:53:49 +01:00
|
|
|
"""
|
2014-01-22 15:24:05 +00:00
|
|
|
Make a prediction for the latent function values
|
2013-06-05 14:11:49 +01:00
|
|
|
"""
|
2014-01-30 12:03:02 +00:00
|
|
|
if X_variance_new is None:
|
|
|
|
|
Kx = self.kern.K(self.Z, Xnew, which_parts=which_parts)
|
|
|
|
|
mu = np.dot(Kx.T, self.posterior.woodbury_vector)
|
|
|
|
|
if full_cov:
|
|
|
|
|
Kxx = self.kern.K(Xnew, which_parts=which_parts)
|
|
|
|
|
var = Kxx - mdot(Kx.T, self.posterior.woodbury_inv, Kx) # NOTE this won't work for plotting
|
|
|
|
|
else:
|
|
|
|
|
Kxx = self.kern.Kdiag(Xnew, which_parts=which_parts)
|
|
|
|
|
var = Kxx - np.sum(Kx * np.dot(self.posterior.woodbury_inv, Kx), 0)
|
|
|
|
|
else:
|
|
|
|
|
# assert which_parts=='all', "swithching out parts of variational kernels is not implemented"
|
|
|
|
|
Kx = self.kern.psi1(self.Z, Xnew, X_variance_new) # , which_parts=which_parts) TODO: which_parts
|
|
|
|
|
mu = np.dot(Kx, self.Cpsi1V)
|
|
|
|
|
if full_cov:
|
|
|
|
|
raise NotImplementedError, "TODO"
|
|
|
|
|
else:
|
|
|
|
|
Kxx = self.kern.psi0(self.Z, Xnew, X_variance_new)
|
|
|
|
|
psi2 = self.kern.psi2(self.Z, Xnew, X_variance_new)
|
|
|
|
|
var = Kxx - np.sum(np.sum(psi2 * Kmmi_LmiBLmi[None, :, :], 1), 1)
|
|
|
|
|
return mu, var[:,None]
|
2013-06-05 14:11:49 +01:00
|
|
|
|
2013-10-07 12:41:20 +01:00
|
|
|
|
2014-01-24 15:48:23 +00:00
|
|
|
def _getstate(self):
|
2013-08-02 20:10:02 +01:00
|
|
|
"""
|
2014-01-22 15:24:05 +00:00
|
|
|
Get the current state of the class,
|
|
|
|
|
here just all the indices, rest can get recomputed
|
2013-08-02 20:10:02 +01:00
|
|
|
"""
|
2014-01-24 15:48:23 +00:00
|
|
|
return GP._getstate(self) + [self.Z,
|
2014-01-22 15:24:05 +00:00
|
|
|
self.num_inducing,
|
|
|
|
|
self.has_uncertain_inputs,
|
|
|
|
|
self.X_variance]
|
2013-10-07 12:41:20 +01:00
|
|
|
|
2014-01-24 15:48:23 +00:00
|
|
|
def _setstate(self, state):
|
2014-01-22 15:24:05 +00:00
|
|
|
self.X_variance = state.pop()
|
|
|
|
|
self.has_uncertain_inputs = state.pop()
|
|
|
|
|
self.num_inducing = state.pop()
|
|
|
|
|
self.Z = state.pop()
|
2014-01-24 15:48:23 +00:00
|
|
|
GP._setstate(self, state)
|