GPy/GPy/models/sparse_GP.py

266 lines
13 KiB
Python
Raw Normal View History

2013-01-28 00:16:23 +00:00
# Copyright (c) 2012, GPy authors (see AUTHORS.txt).
# Licensed under the BSD 3-clause license (see LICENSE.txt)
import numpy as np
import pylab as pb
2013-03-11 18:56:37 +00:00
from ..util.linalg import mdot, jitchol, chol_inv, pdinv, trace_dot
2013-01-28 00:16:23 +00:00
from ..util.plot import gpplot
from .. import kern
from GP import GP
2013-04-10 11:04:49 +01:00
from scipy import linalg
2013-01-28 00:16:23 +00:00
#Still TODO:
# make use of slices properly (kernel can now do this)
# enable heteroscedatic noise (kernel will need to compute psi2 as a (NxMxM) array)
class sparse_GP(GP):
"""
2013-02-01 09:47:30 +00:00
Variational sparse GP model
2013-01-28 00:16:23 +00:00
:param X: inputs
:type X: np.ndarray (N x Q)
2013-02-01 09:47:30 +00:00
:param likelihood: a likelihood instance, containing the observed data
:type likelihood: GPy.likelihood.(Gaussian | EP)
2013-01-28 00:16:23 +00:00
:param kernel : the kernel/covariance function. See link kernels
:type kernel: a GPy kernel
:param X_variance: The uncertainty in the measurements of X (Gaussian variance)
:type X_variance: np.ndarray (N x Q) | None
2013-02-01 09:47:30 +00:00
:param Z: inducing inputs (optional, see note)
:type Z: np.ndarray (M x Q) | None
2013-01-28 00:16:23 +00:00
:param Zslices: slices for the inducing inputs (see slicing TODO: link)
:param M : Number of inducing points (optional, default 10. Ignored if Z is not None)
:type M: int
:param normalize_(X|Y) : whether to normalize the data before computing (predictions will be in original scales)
:type normalize_(X|Y): bool
"""
def __init__(self, X, likelihood, kernel, Z, X_variance=None, Xslices=None,Zslices=None, normalize_X=False):
self.scale_factor = 100.0# a scaling factor to help keep the algorithm stable
self.auto_scale_factor = False
self.Z = Z
self.Zslices = Zslices
self.Xslices = Xslices
self.M = Z.shape[0]
self.likelihood = likelihood
2013-01-28 00:16:23 +00:00
if X_variance is None:
2013-01-28 00:16:23 +00:00
self.has_uncertain_inputs=False
else:
assert X_variance.shape==X.shape
2013-01-28 00:16:23 +00:00
self.has_uncertain_inputs=True
self.X_variance = X_variance
2013-01-28 00:16:23 +00:00
2013-02-15 13:54:01 +00:00
if not self.likelihood.is_heteroscedastic:
self.likelihood.trYYT = np.trace(np.dot(self.likelihood.Y, self.likelihood.Y.T)) # TODO: something more elegant here?
GP.__init__(self, X, likelihood, kernel=kernel, normalize_X=normalize_X, Xslices=Xslices)
2013-01-28 00:16:23 +00:00
2013-03-11 13:26:39 +00:00
#normalize X uncertainty also
2013-01-28 00:16:23 +00:00
if self.has_uncertain_inputs:
self.X_variance /= np.square(self._Xstd)
2013-01-28 00:16:23 +00:00
2013-03-04 12:43:05 +00:00
def _compute_kernel_matrices(self):
2013-01-28 17:47:08 +00:00
# kernel computations, using BGPLVM notation
self.Kmm = self.kern.K(self.Z)
if self.has_uncertain_inputs:
self.psi0 = self.kern.psi0(self.Z,self.X, self.X_variance)
self.psi1 = self.kern.psi1(self.Z,self.X, self.X_variance).T
self.psi2 = self.kern.psi2(self.Z,self.X, self.X_variance)
2013-01-29 18:01:47 +00:00
else:
self.psi0 = self.kern.Kdiag(self.X,slices=self.Xslices)
2013-01-29 18:01:47 +00:00
self.psi1 = self.kern.K(self.Z,self.X)
2013-03-04 12:43:05 +00:00
self.psi2 = None
def _computations(self):
2013-03-08 11:46:17 +00:00
#TODO: find routine to multiply triangular matrices
2013-03-04 12:43:05 +00:00
#TODO: slices for psi statistics (easy enough)
sf = self.scale_factor
sf2 = sf**2
#The rather complex computations of psi2_beta_scaled
if self.likelihood.is_heteroscedastic:
2013-03-12 10:04:02 +00:00
assert self.likelihood.D == 1 #TODO: what if the likelihood is heterscedatic and there are multiple independent outputs?
2013-03-04 12:43:05 +00:00
if self.has_uncertain_inputs:
2013-03-06 15:43:58 +00:00
self.psi2_beta_scaled = (self.psi2*(self.likelihood.precision.flatten().reshape(self.N,1,1)/sf2)).sum(0)
2013-03-04 12:43:05 +00:00
else:
2013-03-06 15:43:58 +00:00
tmp = self.psi1*(np.sqrt(self.likelihood.precision.flatten().reshape(1,self.N))/sf)
2013-03-04 12:43:05 +00:00
self.psi2_beta_scaled = np.dot(tmp,tmp.T)
else:
if self.has_uncertain_inputs:
self.psi2_beta_scaled = (self.psi2*(self.likelihood.precision/sf2)).sum(0)
else:
tmp = self.psi1*(np.sqrt(self.likelihood.precision)/sf)
2013-03-04 12:43:05 +00:00
self.psi2_beta_scaled = np.dot(tmp,tmp.T)
2013-01-28 17:47:08 +00:00
self.Kmmi, self.Lm, self.Lmi, self.Kmm_logdet = pdinv(self.Kmm)
2013-02-01 09:47:30 +00:00
self.V = (self.likelihood.precision/self.scale_factor)*self.likelihood.Y
2013-04-10 11:04:49 +01:00
#Compute A = L^-1 psi2 beta L^-T
#self. A = mdot(self.Lmi,self.psi2_beta_scaled,self.Lmi.T)
2013-04-10 11:04:49 +01:00
tmp = linalg.lapack.flapack.dtrtrs(self.Lm,self.psi2_beta_scaled.T,lower=1)[0]
self.A = linalg.lapack.flapack.dtrtrs(self.Lm,np.asarray(tmp.T,order='F'),lower=1)[0]
2013-02-01 09:47:30 +00:00
self.B = np.eye(self.M)/sf2 + self.A
2013-01-28 17:47:08 +00:00
self.Bi, self.LB, self.LBi, self.B_logdet = pdinv(self.B)
2013-02-01 09:47:30 +00:00
self.psi1V = np.dot(self.psi1, self.V)
2013-04-10 11:04:49 +01:00
#tmp = np.dot(self.Lmi.T, self.LBi.T)
tmp = linalg.lapack.clapack.dtrtrs(self.Lm.T,np.asarray(self.LBi.T,order='C'),lower=0)[0]
self.C = np.dot(tmp,tmp.T) #TODO: tmp is triangular. replace with dtrmm (blas) when available
self.Cpsi1V = np.dot(self.C,self.psi1V)
self.Cpsi1VVpsi1 = np.dot(self.Cpsi1V,self.psi1V.T)
2013-04-10 11:04:49 +01:00
#self.E = np.dot(self.Cpsi1VVpsi1,self.C)/sf2
self.E = np.dot(self.Cpsi1V/sf,self.Cpsi1V.T/sf)
2013-02-01 09:47:30 +00:00
2013-03-08 11:46:17 +00:00
# Compute dL_dpsi # FIXME: this is untested for the heterscedastic + uncertin inputs case
2013-03-06 15:43:58 +00:00
self.dL_dpsi0 = - 0.5 * self.D * (self.likelihood.precision * np.ones([self.N,1])).flatten()
self.dL_dpsi1 = np.dot(self.Cpsi1V,self.V.T)
if self.likelihood.is_heteroscedastic:
2013-03-06 15:43:58 +00:00
if self.has_uncertain_inputs:
self.dL_dpsi2 = 0.5 * self.likelihood.precision[:,None,None] * self.D * self.Kmmi[None,:,:] # dB
self.dL_dpsi2 += - 0.5 * self.likelihood.precision[:,None,None]/sf2 * self.D * self.C[None,:,:] # dC
self.dL_dpsi2 += - 0.5 * self.likelihood.precision[:,None,None]* self.E[None,:,:] # dD
else:
self.dL_dpsi1 += mdot(self.Kmmi,self.psi1*self.likelihood.precision.flatten().reshape(1,self.N)) #dB
self.dL_dpsi1 += -mdot(self.C,self.psi1*self.likelihood.precision.flatten().reshape(1,self.N)/sf2) #dC
self.dL_dpsi1 += -mdot(self.E,self.psi1*self.likelihood.precision.flatten().reshape(1,self.N)) #dD
self.dL_dpsi2 = None
else:
2013-02-15 16:30:00 +00:00
self.dL_dpsi2 = 0.5 * self.likelihood.precision * self.D * self.Kmmi # dB
self.dL_dpsi2 += - 0.5 * self.likelihood.precision/sf2 * self.D * self.C # dC
self.dL_dpsi2 += - 0.5 * self.likelihood.precision * self.E # dD
if self.has_uncertain_inputs:
#repeat for each of the N psi_2 matrices
self.dL_dpsi2 = np.repeat(self.dL_dpsi2[None,:,:],self.N,axis=0)
else:
self.dL_dpsi1 += 2.*np.dot(self.dL_dpsi2,self.psi1)
self.dL_dpsi2 = None
2013-01-28 17:47:08 +00:00
# Compute dL_dKmm
#self.dL_dKmm_old = -0.5 * self.D * mdot(self.Lmi.T, self.A, self.Lmi)*sf2 # dB
#self.dL_dKmm += -0.5 * self.D * (- self.C/sf2 - 2.*mdot(self.C, self.psi2_beta_scaled, self.Kmmi) + self.Kmmi) # dC
#self.dL_dKmm += np.dot(np.dot(self.E*sf2, self.psi2_beta_scaled) - self.Cpsi1VVpsi1, self.Kmmi) + 0.5*self.E # dD
2013-04-16 11:09:33 +01:00
tmp = linalg.lapack.flapack.dtrtrs(self.Lm,np.asfortranarray(self.A),lower=1,trans=1)[0]
self.dL_dKmm = -0.5*self.D*sf2*linalg.lapack.flapack.dtrtrs(self.Lm,np.asfortranarray(tmp.T),lower=1,trans=1)[0] #dA
tmp = np.dot(self.D*self.C + self.E*sf2,self.psi2_beta_scaled) - self.Cpsi1VVpsi1
#tmp = np.dot(tmp,self.Kmmi)
tmp = linalg.lapack.flapack.dpotrs(self.Lm,np.asfortranarray(tmp.T),lower=1)[0].T
self.dL_dKmm += 0.5*(self.D*(self.C/sf2 - self.Kmmi) + self.E) + tmp # d(C+D)
2013-02-01 09:47:30 +00:00
#the partial derivative vector for the likelihood
if self.likelihood.Nparams ==0:
#save computation here.
self.partial_for_likelihood = None
elif self.likelihood.is_heteroscedastic:
raise NotImplementedError, "heteroscedatic derivates not implemented"
#self.partial_for_likelihood = - 0.5 * self.D*self.likelihood.precision + 0.5 * (self.likelihood.Y**2).sum(1)*self.likelihood.precision**2 #dA
#self.partial_for_likelihood += 0.5 * self.D * (self.psi0*self.likelihood.precision**2 - (self.psi2*self.Kmmi[None,:,:]*self.likelihood.precision[:,None,None]**2).sum(1).sum(1)/sf2) #dB
#self.partial_for_likelihood += 0.5 * self.D * np.sum(self.Bi*self.A)*self.likelihood.precision #dC
#self.partial_for_likelihood += -np.diag(np.dot((self.C - 0.5 * mdot(self.C,self.psi2_beta_scaled,self.C) ) , self.psi1VVpsi1 ))*self.likelihood.precision #dD
else:
#likelihood is not heterscedatic
self.partial_for_likelihood = - 0.5 * self.N*self.D*self.likelihood.precision + 0.5 * np.sum(np.square(self.likelihood.Y))*self.likelihood.precision**2
self.partial_for_likelihood += 0.5 * self.D * (self.psi0.sum()*self.likelihood.precision**2 - np.trace(self.A)*self.likelihood.precision*sf2)
self.partial_for_likelihood += 0.5 * self.D * trace_dot(self.Bi,self.A)*self.likelihood.precision
self.partial_for_likelihood += self.likelihood.precision*(0.5*trace_dot(self.psi2_beta_scaled,self.E*sf2) - np.trace(self.Cpsi1VVpsi1))
2013-02-01 09:47:30 +00:00
def log_likelihood(self):
""" Compute the (lower bound on the) log marginal likelihood """
sf2 = self.scale_factor**2
if self.likelihood.is_heteroscedastic:
A = -0.5*self.N*self.D*np.log(2.*np.pi) +0.5*np.sum(np.log(self.likelihood.precision)) -0.5*np.sum(self.V*self.likelihood.Y)
B = -0.5*self.D*(np.sum(self.likelihood.precision.flatten()*self.psi0) - np.trace(self.A)*sf2)
else:
A = -0.5*self.N*self.D*(np.log(2.*np.pi) + np.log(self.likelihood._variance)) -0.5*self.likelihood.precision*self.likelihood.trYYT
B = -0.5*self.D*(np.sum(self.likelihood.precision*self.psi0) - np.trace(self.A)*sf2)
C = -0.5*self.D * (self.B_logdet + self.M*np.log(sf2))
D = 0.5*np.trace(self.Cpsi1VVpsi1)
return A+B+C+D
2013-02-01 09:47:30 +00:00
def _set_params(self, p):
self.Z = p[:self.M*self.Q].reshape(self.M, self.Q)
self.kern._set_params(p[self.Z.size:self.Z.size+self.kern.Nparam])
self.likelihood._set_params(p[self.Z.size+self.kern.Nparam:])
2013-03-04 12:43:05 +00:00
self._compute_kernel_matrices()
if self.auto_scale_factor:
2013-04-15 08:14:01 +01:00
self.scale_factor = np.sqrt(self.psi2.sum(0).mean()*self.likelihood.precision)
#if self.auto_scale_factor:
# if self.likelihood.is_heteroscedastic:
# self.scale_factor = max(1,np.sqrt(self.psi2_beta_scaled.sum(0).mean()))
# else:
# self.scale_factor = np.sqrt(self.psi2.sum(0).mean()*self.likelihood.precision)
2013-01-28 00:16:23 +00:00
self._computations()
2013-02-01 09:47:30 +00:00
def _get_params(self):
return np.hstack([self.Z.flatten(),GP._get_params(self)])
2013-02-01 09:47:30 +00:00
def _get_param_names(self):
return sum([['iip_%i_%i'%(i,j) for j in range(self.Z.shape[1])] for i in range(self.Z.shape[0])],[]) + GP._get_param_names(self)
2013-02-01 09:47:30 +00:00
2013-03-06 15:43:58 +00:00
def update_likelihood_approximation(self):
"""
Approximates a non-gaussian likelihood using Expectation Propagation
For a Gaussian (or direct: TODO) likelihood, no iteration is required:
this function does nothing
"""
if self.has_uncertain_inputs:
raise NotImplementedError, "EP approximation not implemented for uncertain inputs"
else:
2013-03-11 11:39:48 +00:00
self.likelihood.fit_DTC(self.Kmm,self.psi1)
#self.likelihood.fit_FITC(self.Kmm,self.psi1,self.psi0)
2013-03-06 15:43:58 +00:00
self._set_params(self._get_params()) # update the GP
2013-02-01 09:47:30 +00:00
def _log_likelihood_gradients(self):
return np.hstack((self.dL_dZ().flatten(), self.dL_dtheta(), self.likelihood._gradients(partial=self.partial_for_likelihood)))
2013-01-28 00:16:23 +00:00
def dL_dtheta(self):
"""
Compute and return the derivative of the log marginal likelihood wrt the parameters of the kernel
"""
dL_dtheta = self.kern.dK_dtheta(self.dL_dKmm,self.Z)
if self.has_uncertain_inputs:
dL_dtheta += self.kern.dpsi0_dtheta(self.dL_dpsi0, self.Z,self.X,self.X_variance)
dL_dtheta += self.kern.dpsi1_dtheta(self.dL_dpsi1.T,self.Z,self.X, self.X_variance)
dL_dtheta += self.kern.dpsi2_dtheta(self.dL_dpsi2, self.Z,self.X, self.X_variance)
2013-01-28 00:16:23 +00:00
else:
dL_dtheta += self.kern.dK_dtheta(self.dL_dpsi1,self.Z,self.X)
2013-01-28 00:16:23 +00:00
dL_dtheta += self.kern.dKdiag_dtheta(self.dL_dpsi0, self.X)
return dL_dtheta
def dL_dZ(self):
"""
The derivative of the bound wrt the inducing inputs Z
"""
2013-02-01 09:47:30 +00:00
dL_dZ = 2.*self.kern.dK_dX(self.dL_dKmm,self.Z)#factor of two becase of vertical and horizontal 'stripes' in dKmm_dZ
2013-01-28 00:16:23 +00:00
if self.has_uncertain_inputs:
dL_dZ += self.kern.dpsi1_dZ(self.dL_dpsi1,self.Z,self.X, self.X_variance)
dL_dZ += 2.*self.kern.dpsi2_dZ(self.dL_dpsi2,self.Z,self.X, self.X_variance) # 'stripes'
2013-01-28 00:16:23 +00:00
else:
dL_dZ += self.kern.dK_dX(self.dL_dpsi1,self.Z,self.X)
2013-01-28 00:16:23 +00:00
return dL_dZ
def _raw_predict(self, Xnew, slices, full_cov=False):
2013-03-11 13:26:39 +00:00
"""Internal helper function for making predictions, does not account for normalization"""
2013-02-01 09:47:30 +00:00
2013-01-28 00:16:23 +00:00
Kx = self.kern.K(self.Z, Xnew)
2013-02-01 09:47:30 +00:00
mu = mdot(Kx.T, self.C/self.scale_factor, self.psi1V)
2013-01-28 00:16:23 +00:00
if full_cov:
Kxx = self.kern.K(Xnew)
var = Kxx - mdot(Kx.T, (self.Kmmi - self.C/self.scale_factor**2), Kx) #NOTE this won't work for plotting
2013-01-28 00:16:23 +00:00
else:
Kxx = self.kern.Kdiag(Xnew)
2013-02-01 09:47:30 +00:00
var = Kxx - np.sum(Kx*np.dot(self.Kmmi - self.C/self.scale_factor**2, Kx),0)
return mu,var[:,None]