From b6ffb57263a4175868449ad72c13e57226446f2e Mon Sep 17 00:00:00 2001 From: Ricardo Date: Fri, 25 Jan 2013 09:30:31 +0000 Subject: [PATCH 01/10] Fixing GP_EP --- GPy/examples/classification.py | 3 +- GPy/examples/ep_fix.py | 39 +++ GPy/inference/EP.py | 314 +++++++++++++++++++++++ GPy/inference/Expectation_Propagation.py | 2 +- GPy/inference/likelihoods.py | 2 +- GPy/models/GP_EP.py | 2 +- GPy/models/GP_EP2.py | 280 ++++++++++++++++++++ GPy/models/__init__.py | 1 + 8 files changed, 638 insertions(+), 5 deletions(-) create mode 100644 GPy/examples/ep_fix.py create mode 100644 GPy/inference/EP.py create mode 100644 GPy/models/GP_EP2.py diff --git a/GPy/examples/classification.py b/GPy/examples/classification.py index 989ed08a..fb14139d 100644 --- a/GPy/examples/classification.py +++ b/GPy/examples/classification.py @@ -76,11 +76,10 @@ def toy_linear_1d_classification(model_type='Full', inducing=4, seed=default_see # create simple GP model if model_type=='Full': - m = GPy.models.simple_GP_EP(data['X'],likelihood) + m = GPy.models.GP_EP(data['X'],likelihood) else: # create sparse GP EP model m = GPy.models.sparse_GP_EP(data['X'],likelihood=likelihood,inducing=inducing,ep_proxy=model_type) - m.constrain_positive('var') m.constrain_positive('len') diff --git a/GPy/examples/ep_fix.py b/GPy/examples/ep_fix.py new file mode 100644 index 00000000..e4999f30 --- /dev/null +++ b/GPy/examples/ep_fix.py @@ -0,0 +1,39 @@ +# Copyright (c) 2012, GPy authors (see AUTHORS.txt). +# Licensed under the BSD 3-clause license (see LICENSE.txt) + + +""" +Simple Gaussian Processes classification +""" +import pylab as pb +import numpy as np +import GPy +pb.ion() + +default_seed=10000 + +model_type='Full' +inducing=4 +seed=default_seed +"""Simple 1D classification example. +:param model_type: type of model to fit ['Full', 'FITC', 'DTC']. +:param seed : seed value for data generation (default is 4). +:type seed: int +:param inducing : number of inducing variables (only used for 'FITC' or 'DTC'). +:type inducing: int +""" +data = GPy.util.datasets.toy_linear_1d_classification(seed=seed) +likelihood = GPy.inference.likelihoods.probit(data['Y'][:, 0:1]) + +m = GPy.models.GP_EP2(data['X'],likelihood) + +#m.constrain_positive('var') +#m.constrain_positive('len') +#m.tie_param('lengthscale') +m.approximate_likelihood() +# Optimize and plot +#m.optimize() +#m.em(plot_all=False) # EM algorithm +m.plot() + +print(m) diff --git a/GPy/inference/EP.py b/GPy/inference/EP.py new file mode 100644 index 00000000..fa691961 --- /dev/null +++ b/GPy/inference/EP.py @@ -0,0 +1,314 @@ +import numpy as np +import random +import pylab as pb #TODO erase me +from scipy import stats, linalg +from .likelihoods import likelihood +from ..core import model +from ..util.linalg import pdinv,mdot,jitchol +from ..util.plot import gpplot +from .. import kern + +class EP: + def __init__(self,covariance,likelihood,Kmn=None,Knn_diag=None,epsilon=1e-3,powerep=[1.,1.]): + """ + Expectation Propagation + + Arguments + --------- + X : input observations + likelihood : Output's likelihood (likelihood class) + kernel : a GPy kernel (kern class) + inducing : Either an array specifying the inducing points location or a sacalar defining their number. None value for using a non-sparse model is used. + powerep : Power-EP parameters (eta,delta) - 2x1 numpy array (floats) + epsilon : Convergence criterion, maximum squared difference allowed between mean updates to stop iterations (float) + """ + self.likelihood = likelihood + assert covariance.shape[0] == covariance.shape[1] + if Kmn is not None: + self.Kmm = covariance + self.Kmn = Kmn + self.M = self.Kmn.shape[0] + self.N = self.Kmn.shape[1] + assert self.M < self.N, 'The number of inducing inputs must be smaller than the number of observations' + else: + self.K = covariance + self.N = self.K.shape[0] + if Knn_diag is not None: + self.Knn_diag = Knn_diag + assert len(Knn_diag) == self.N, 'Knn_diagonal has size different from N' + + self.epsilon = epsilon + self.eta, self.delta = powerep + self.jitter = 1e-12 + + """ + Initial values - Likelihood approximation parameters: + p(y|f) = t(f|tau_tilde,v_tilde) + """ + self.tau_tilde = np.zeros(self.N) + self.v_tilde = np.zeros(self.N) + + def restart_EP(self): + """ + Set the EP approximation to initial state + """ + self.tau_tilde = np.zeros(self.N) + self.v_tilde = np.zeros(self.N) + self.mu = np.zeros(self.N) + +class Full(EP): + def fit_EP(self): + """ + The expectation-propagation algorithm. + For nomenclature see Rasmussen & Williams 2006 (pag. 52-60) + """ + #Prior distribution parameters: p(f|X) = N(f|0,K) + #self.K = self.kernel.K(self.X,self.X) + + #Initial values - Posterior distribution parameters: q(f|X,Y) = N(f|mu,Sigma) + self.mu=np.zeros(self.N) + self.Sigma=self.K.copy() + + """ + Initial values - Cavity distribution parameters: + q_(f|mu_,sigma2_) = Product{q_i(f|mu_i,sigma2_i)} + sigma_ = 1./tau_ + mu_ = v_/tau_ + """ + self.tau_ = np.empty(self.N,dtype=float) + self.v_ = np.empty(self.N,dtype=float) + + #Initial values - Marginal moments + z = np.empty(self.N,dtype=float) + self.Z_hat = np.empty(self.N,dtype=float) + phi = np.empty(self.N,dtype=float) + mu_hat = np.empty(self.N,dtype=float) + sigma2_hat = np.empty(self.N,dtype=float) + self.mu_hat = mu_hat #TODO erase me + self.sigma2_hat = sigma2_hat #TODO erase me + + #Approximation + epsilon_np1 = self.epsilon + 1. + epsilon_np2 = self.epsilon + 1. + self.iterations = 0 + self.np1 = [self.tau_tilde.copy()] + self.np2 = [self.v_tilde.copy()] + while epsilon_np1 > self.epsilon or epsilon_np2 > self.epsilon: + update_order = np.arange(self.N) + #random.shuffle(update_order) #TODO uncomment + for i in update_order: + #Cavity distribution parameters + self.tau_[i] = 1./self.Sigma[i,i] - self.eta*self.tau_tilde[i] + self.v_[i] = self.mu[i]/self.Sigma[i,i] - self.eta*self.v_tilde[i] + #Marginal moments + self.Z_hat[i], mu_hat[i], sigma2_hat[i] = self.likelihood.moments_match(i,self.tau_[i],self.v_[i]) + self.mu_hat[i] = mu_hat[i] #TODO erase me + self.sigma2_hat[i] = sigma2_hat[i] #TODO erase me + #if i == 3: + # a = b + #Site parameters update + Delta_tau = self.delta/self.eta*(1./sigma2_hat[i] - 1./self.Sigma[i,i]) + Delta_v = self.delta/self.eta*(mu_hat[i]/sigma2_hat[i] - self.mu[i]/self.Sigma[i,i]) + print Delta_tau + self.tau_tilde[i] = self.tau_tilde[i] + Delta_tau + self.v_tilde[i] = self.v_tilde[i] + Delta_v + #Posterior distribution parameters update + si=self.Sigma[:,i].reshape(self.N,1) + self.Sigma = self.Sigma - Delta_tau/(1.+ Delta_tau*self.Sigma[i,i])*np.dot(si,si.T) + self.mu = np.dot(self.Sigma,self.v_tilde) + self.iterations += 1 + #Sigma recomptutation with Cholesky decompositon + Sroot_tilde_K = np.sqrt(self.tau_tilde)[:,None]*(self.K) + B = np.eye(self.N) + np.sqrt(self.tau_tilde)[None,:]*Sroot_tilde_K + L = jitchol(B) + V,info = linalg.flapack.dtrtrs(L,Sroot_tilde_K,lower=1) + self.Sigma = self.K - np.dot(V.T,V) + self.mu = np.dot(self.Sigma,self.v_tilde) + epsilon_np1 = sum((self.tau_tilde-self.np1[-1])**2)/self.N + epsilon_np2 = sum((self.v_tilde-self.np2[-1])**2)/self.N + self.np1.append(self.tau_tilde.copy()) + self.np2.append(self.v_tilde.copy()) + +class DTC(EP): + def fit_EP(self): + """ + The expectation-propagation algorithm with sparse pseudo-input. + For nomenclature see ... 2013. + """ + + """ + Prior approximation parameters: + q(f|X) = int_{df}{N(f|KfuKuu_invu,diag(Kff-Qff)*N(u|0,Kuu)} = N(f|0,Sigma0) + Sigma0 = Qnn = Knm*Kmmi*Kmn + """ + self.Kmmi, self.Kmm_hld = pdinv(self.Kmm) + self.KmnKnm = np.dot(self.Kmn, self.Kmn.T) + self.KmmiKmn = np.dot(self.Kmmi,self.Kmn) + self.Qnn_diag = np.sum(self.Kmn*self.KmmiKmn,-2) + self.LLT0 = self.Kmm.copy() + + """ + Posterior approximation: q(f|y) = N(f| mu, Sigma) + Sigma = Diag + P*R.T*R*P.T + K + mu = w + P*gamma + """ + self.mu = np.zeros(self.N) + self.LLT = self.Kmm.copy() + self.Sigma_diag = self.Qnn_diag.copy() + + """ + Initial values - Cavity distribution parameters: + q_(g|mu_,sigma2_) = Product{q_i(g|mu_i,sigma2_i)} + sigma_ = 1./tau_ + mu_ = v_/tau_ + """ + self.tau_ = np.empty(self.N,dtype=float) + self.v_ = np.empty(self.N,dtype=float) + + #Initial values - Marginal moments + z = np.empty(self.N,dtype=float) + self.Z_hat = np.empty(self.N,dtype=float) + phi = np.empty(self.N,dtype=float) + mu_hat = np.empty(self.N,dtype=float) + sigma2_hat = np.empty(self.N,dtype=float) + + #Approximation + epsilon_np1 = 1 + epsilon_np2 = 1 + self.iterations = 0 + self.np1 = [self.tau_tilde.copy()] + self.np2 = [self.v_tilde.copy()] + while epsilon_np1 > self.epsilon or epsilon_np2 > self.epsilon: + update_order = np.arange(self.N) + random.shuffle(update_order) + for i in update_order: + #Cavity distribution parameters + self.tau_[i] = 1./self.Sigma_diag[i] - self.eta*self.tau_tilde[i] + self.v_[i] = self.mu[i]/self.Sigma_diag[i] - self.eta*self.v_tilde[i] + #Marginal moments + self.Z_hat[i], mu_hat[i], sigma2_hat[i] = self.likelihood.moments_match(i,self.tau_[i],self.v_[i]) + #Site parameters update + Delta_tau = self.delta/self.eta*(1./sigma2_hat[i] - 1./self.Sigma_diag[i]) + Delta_v = self.delta/self.eta*(mu_hat[i]/sigma2_hat[i] - self.mu[i]/self.Sigma_diag[i]) + self.tau_tilde[i] = self.tau_tilde[i] + Delta_tau + self.v_tilde[i] = self.v_tilde[i] + Delta_v + #Posterior distribution parameters update + self.LLT = self.LLT + np.outer(self.Kmn[:,i],self.Kmn[:,i])*Delta_tau + L = jitchol(self.LLT) + V,info = linalg.flapack.dtrtrs(L,self.Kmn,lower=1) + self.Sigma_diag = np.sum(V*V,-2) + si = np.sum(V.T*V[:,i],-1) + self.mu = self.mu + (Delta_v-Delta_tau*self.mu[i])*si + self.iterations += 1 + #Sigma recomputation with Cholesky decompositon + self.LLT0 = self.LLT0 + np.dot(self.Kmn*self.tau_tilde[None,:],self.Kmn.T) + self.L = jitchol(self.LLT) + V,info = linalg.flapack.dtrtrs(L,self.Kmn,lower=1) + V2,info = linalg.flapack.dtrtrs(L.T,V,lower=0) + self.Sigma_diag = np.sum(V*V,-2) + Knmv_tilde = np.dot(self.Kmn,self.v_tilde) + self.mu = np.dot(V2.T,Knmv_tilde) + epsilon_np1 = sum((self.tau_tilde-self.np1[-1])**2)/self.N + epsilon_np2 = sum((self.v_tilde-self.np2[-1])**2)/self.N + self.np1.append(self.tau_tilde.copy()) + self.np2.append(self.v_tilde.copy()) + +class FITC(EP): + def fit_EP(self): + """ + The expectation-propagation algorithm with sparse pseudo-input. + For nomenclature see Naish-Guzman and Holden, 2008. + """ + + """ + Prior approximation parameters: + q(f|X) = int_{df}{N(f|KfuKuu_invu,diag(Kff-Qff)*N(u|0,Kuu)} = N(f|0,Sigma0) + Sigma0 = diag(Knn-Qnn) + Qnn, Qnn = Knm*Kmmi*Kmn + """ + self.Kmmi, self.Kmm_hld = pdinv(self.Kmm) + self.P0 = self.Kmn.T + self.KmnKnm = np.dot(self.P0.T, self.P0) + self.KmmiKmn = np.dot(self.Kmmi,self.P0.T) + self.Qnn_diag = np.sum(self.P0.T*self.KmmiKmn,-2) + self.Diag0 = self.Knn_diag - self.Qnn_diag + self.R0 = jitchol(self.Kmmi).T + + """ + Posterior approximation: q(f|y) = N(f| mu, Sigma) + Sigma = Diag + P*R.T*R*P.T + K + mu = w + P*gamma + """ + self.w = np.zeros(self.N) + self.gamma = np.zeros(self.M) + self.mu = np.zeros(self.N) + self.P = self.P0.copy() + self.R = self.R0.copy() + self.Diag = self.Diag0.copy() + self.Sigma_diag = self.Knn_diag + + """ + Initial values - Cavity distribution parameters: + q_(g|mu_,sigma2_) = Product{q_i(g|mu_i,sigma2_i)} + sigma_ = 1./tau_ + mu_ = v_/tau_ + """ + self.tau_ = np.empty(self.N,dtype=float) + self.v_ = np.empty(self.N,dtype=float) + + #Initial values - Marginal moments + z = np.empty(self.N,dtype=float) + self.Z_hat = np.empty(self.N,dtype=float) + phi = np.empty(self.N,dtype=float) + mu_hat = np.empty(self.N,dtype=float) + sigma2_hat = np.empty(self.N,dtype=float) + + #Approximation + epsilon_np1 = 1 + epsilon_np2 = 1 + self.iterations = 0 + self.np1 = [self.tau_tilde.copy()] + self.np2 = [self.v_tilde.copy()] + while epsilon_np1 > self.epsilon or epsilon_np2 > self.epsilon: + update_order = np.arange(self.N) + random.shuffle(update_order) + for i in update_order: + #Cavity distribution parameters + self.tau_[i] = 1./self.Sigma_diag[i] - self.eta*self.tau_tilde[i] + self.v_[i] = self.mu[i]/self.Sigma_diag[i] - self.eta*self.v_tilde[i] + #Marginal moments + self.Z_hat[i], mu_hat[i], sigma2_hat[i] = self.likelihood.moments_match(i,self.tau_[i],self.v_[i]) + #Site parameters update + Delta_tau = self.delta/self.eta*(1./sigma2_hat[i] - 1./self.Sigma_diag[i]) + Delta_v = self.delta/self.eta*(mu_hat[i]/sigma2_hat[i] - self.mu[i]/self.Sigma_diag[i]) + self.tau_tilde[i] = self.tau_tilde[i] + Delta_tau + self.v_tilde[i] = self.v_tilde[i] + Delta_v + #Posterior distribution parameters update + dtd1 = Delta_tau*self.Diag[i] + 1. + dii = self.Diag[i] + self.Diag[i] = dii - (Delta_tau * dii**2.)/dtd1 + pi_ = self.P[i,:].reshape(1,self.M) + self.P[i,:] = pi_ - (Delta_tau*dii)/dtd1 * pi_ + Rp_i = np.dot(self.R,pi_.T) + RTR = np.dot(self.R.T,np.dot(np.eye(self.M) - Delta_tau/(1.+Delta_tau*self.Sigma_diag[i]) * np.dot(Rp_i,Rp_i.T),self.R)) + self.R = jitchol(RTR).T + self.w[i] = self.w[i] + (Delta_v - Delta_tau*self.w[i])*dii/dtd1 + self.gamma = self.gamma + (Delta_v - Delta_tau*self.mu[i])*np.dot(RTR,self.P[i,:].T) + self.RPT = np.dot(self.R,self.P.T) + self.Sigma_diag = self.Diag + np.sum(self.RPT.T*self.RPT.T,-1) + self.mu = self.w + np.dot(self.P,self.gamma) + self.iterations += 1 + #Sigma recomptutation with Cholesky decompositon + self.Diag = self.Diag0/(1.+ self.Diag0 * self.tau_tilde) + self.P = (self.Diag / self.Diag0)[:,None] * self.P0 + self.RPT0 = np.dot(self.R0,self.P0.T) + L = jitchol(np.eye(self.M) + np.dot(self.RPT0,(1./self.Diag0 - self.Diag/(self.Diag0**2))[:,None]*self.RPT0.T)) + self.R,info = linalg.flapack.dtrtrs(L,self.R0,lower=1) + self.RPT = np.dot(self.R,self.P.T) + self.Sigma_diag = self.Diag + np.sum(self.RPT.T*self.RPT.T,-1) + self.w = self.Diag * self.v_tilde + self.gamma = np.dot(self.R.T, np.dot(self.RPT,self.v_tilde)) + self.mu = self.w + np.dot(self.P,self.gamma) + epsilon_np1 = sum((self.tau_tilde-self.np1[-1])**2)/self.N + epsilon_np2 = sum((self.v_tilde-self.np2[-1])**2)/self.N + self.np1.append(self.tau_tilde.copy()) + self.np2.append(self.v_tilde.copy()) diff --git a/GPy/inference/Expectation_Propagation.py b/GPy/inference/Expectation_Propagation.py index 05453f1d..520fc607 100644 --- a/GPy/inference/Expectation_Propagation.py +++ b/GPy/inference/Expectation_Propagation.py @@ -116,7 +116,7 @@ class Full(EP_base): self.np1.append(self.tau_tilde.copy()) self.np2.append(self.v_tilde.copy()) if messages: - print "EP iteration %i, epsiolon %d"%(self.iterations,epsilon_np1) + print "EP iteration %i, epsilon %d"%(self.iterations,epsilon_np1) class FITC(EP_base): """ diff --git a/GPy/inference/likelihoods.py b/GPy/inference/likelihoods.py index 5f0eb7ff..ff4770f6 100644 --- a/GPy/inference/likelihoods.py +++ b/GPy/inference/likelihoods.py @@ -32,7 +32,7 @@ class likelihood: """ assert X_new.shape[1] == 1, 'Number of dimensions must be 1' gpplot(X_new,Mean_new,Var_new) - pb.errorbar(X_u,Mean_u,2*np.sqrt(Var_u),fmt='r+') + pb.errorbar(X_u.flatten(),Mean_u.flatten(),2*np.sqrt(Var_u.flatten()),fmt='r+') pb.plot(X_u,Mean_u,'ro') def plot2D(self,X,X_new,F_new,U=None): diff --git a/GPy/models/GP_EP.py b/GPy/models/GP_EP.py index 51d69d0a..302ff366 100644 --- a/GPy/models/GP_EP.py +++ b/GPy/models/GP_EP.py @@ -57,7 +57,7 @@ class GP_EP(model): def posterior_param(self): self.K = self.kernel.K(self.X) self.Sroot_tilde_K = np.sqrt(self.ep_approx.tau_tilde)[:,None]*self.K - B = np.eye(self.N) + np.sqrt(self.ep_approx.tau_tilde)[None,:]*self.Sroot_tilde_K + B = np.eye(self.N) + np.sqrt(self.ep_approx.tau_tilde)*self.Sroot_tilde_K #self.L = np.linalg.cholesky(B) self.L = jitchol(B) V,info = linalg.flapack.dtrtrs(self.L,self.Sroot_tilde_K,lower=1) diff --git a/GPy/models/GP_EP2.py b/GPy/models/GP_EP2.py new file mode 100644 index 00000000..c68e7b70 --- /dev/null +++ b/GPy/models/GP_EP2.py @@ -0,0 +1,280 @@ +# 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 +from scipy import stats, linalg +from .. import kern +from ..inference.EP import Full +from ..inference.likelihoods import likelihood,probit,poisson,gaussian +from ..core import model +from ..util.linalg import pdinv,mdot #,jitchol +from ..util.plot import gpplot, Tango + +class GP_EP2(model): + def __init__(self,X,likelihood,kernel=None,normalize_X=False,Xslices=None,epsilon_ep=1e-3,epsion_em=.1,powerep=[1.,1.]): + """ + Simple Gaussian Process with Non-Gaussian likelihood + + Arguments + --------- + :param X: input observations (NxD numpy.darray) + :param likelihood: a GPy likelihood (likelihood class) + :param kernel: a GPy kernel, defaults to rbf+white + :param normalize_X: whether to normalize the input data before computing (predictions will be in original scales) + :type normalize_X: False|True + :param epsilon_ep: convergence criterion for the Expectation Propagation algorithm, defaults to 1e-3 + :param powerep: power-EP parameters [$\eta$,$\delta$], defaults to [1.,1.] (list) + :param Xslices: how the X,Y data co-vary in the kernel (i.e. which "outputs" they correspond to). See (link:slicing) + :rtype: model object. + """ + #.. Note:: Multiple independent outputs are allowed using columns of Y #TODO add this note? + if kernel is None: + kernel = kern.rbf(X.shape[1]) + kern.bias(X.shape[1]) + kern.white(X.shape[1]) + + # parse arguments + self.Xslices = Xslices + assert isinstance(kernel, kern.kern) + self.likelihood = likelihood + #self.Y = self.likelihood.Y #we might not need this + self.kern = kernel + self.X = X + assert len(self.X.shape)==2 + #assert len(self.Y.shape)==2 + #assert self.X.shape[0] == self.Y.shape[0] + #self.N, self.D = self.Y.shape + self.D = 1 + self.N, self.Q = self.X.shape + + #here's some simple normalisation + if normalize_X: + self._Xmean = X.mean(0)[None,:] + self._Xstd = X.std(0)[None,:] + self.X = (X.copy() - self._Xmean) / self._Xstd + if hasattr(self,'Z'): + self.Z = (self.Z - self._Xmean) / self._Xstd + else: + self._Xmean = np.zeros((1,self.X.shape[1])) + self._Xstd = np.ones((1,self.X.shape[1])) + + #THIS PART IS NOT NEEDED + """ + if normalize_Y: + self._Ymean = Y.mean(0)[None,:] + self._Ystd = Y.std(0)[None,:] + self.Y = (Y.copy()- self._Ymean) / self._Ystd + else: + self._Ymean = np.zeros((1,self.Y.shape[1])) + self._Ystd = np.ones((1,self.Y.shape[1])) + + if self.D > self.N: + # then it's more efficient to store YYT + self.YYT = np.dot(self.Y, self.Y.T) + else: + self.YYT = None + """ + self.eta,self.delta = powerep + self.epsilon_ep = epsilon_ep + self.tau_tilde = np.zeros([self.N,self.D]) + self.v_tilde = np.zeros([self.N,self.D]) + model.__init__(self) + + def _set_params(self,p): + self.kern._set_params_transformed(p) + self.K = self.kern.K(self.X,slices1=self.Xslices) + self.posterior_params() + + def _get_params(self): + return self.kern._get_params_transformed() + + def _get_param_names(self): + return self.kern._get_param_names_transformed() + + def approximate_likelihood(self): + self.ep_approx = Full(self.K,self.likelihood,epsilon=self.epsilon_ep,powerep=[self.eta,self.delta]) + self.ep_approx.fit_EP() + self.tau_tilde = self.ep_approx.tau_tilde[:,None] + self.v_tilde = self.ep_approx.tau_tilde[:,None] + self.posterior_params() + self.Y = self.v_tilde/self.tau_tilde + self._Ymean = np.zeros((1,self.Y.shape[1])) + self._Ystd = np.ones((1,self.Y.shape[1])) + #self.YYT = np.dot(self.Y, self.Y.T) + + def posterior_params(self): + self.Sroot_tilde_K = np.sqrt(self.tau_tilde.flatten())[:,None]*self.K + B = np.eye(self.N) + np.sqrt(self.tau_tilde.flatten())[None,:]*self.Sroot_tilde_K + self.Bi,self.L,self.Li,B_logdet = pdinv(B) + V = np.dot(self.Li,self.Sroot_tilde_K) + #V,info = linalg.flapack.dtrtrs(self.L,self.Sroot_tilde_K,lower=1) + self.Sigma = self.K - np.dot(V.T,V) + self.mu = np.dot(self.Sigma,self.v_tilde.flatten()) + + + #def _model_fit_term(self): + # """ + # Computes the model fit using YYT if it's available + # """ + # if self.YYT is None: + # return -0.5*np.sum(np.square(np.dot(self.Li,self.Y))) + # else: + # return -0.5*np.sum(np.multiply(self.Ki, self.YYT)) + + def log_likelihood(self): + mu_ = self.ep_approx.v_/self.ep_approx.tau_ + L1 =.5*sum(np.log(1+self.ep_approx.tau_tilde*1./self.ep_approx.tau_))-sum(np.log(np.diag(self.L))) + L2A =.5*np.sum((self.Sigma-np.diag(1./(self.ep_approx.tau_+self.ep_approx.tau_tilde))) * np.dot(self.ep_approx.v_tilde[:,None],self.ep_approx.v_tilde[None,:])) + L2B = .5*np.dot(mu_*(self.ep_approx.tau_/(self.ep_approx.tau_tilde+self.ep_approx.tau_)),self.ep_approx.tau_tilde*mu_ - 2*self.ep_approx.v_tilde) + L3 = sum(np.log(self.ep_approx.Z_hat)) + return L1 + L2A + L2B + L3 + + def dL_dK(self): #FIXME + if self.YYT is None: + alpha = np.dot(self.Ki,self.Y) + dL_dK = 0.5*(np.dot(alpha,alpha.T)-self.D*self.Ki) + else: + dL_dK = 0.5*(mdot(self.Ki, self.YYT, self.Ki) - self.D*self.Ki) + + return dL_dK + + def _log_likelihood_gradients(self): #FIXME + return self.kern.dK_dtheta(partial=self.dL_dK(),X=self.X) + + def predict(self,Xnew, slices=None, full_cov=False): + """ + + Predict the function(s) at the new point(s) Xnew. + + Arguments + --------- + :param Xnew: The points at which to make a prediction + :type Xnew: np.ndarray, Nnew x self.Q + :param slices: specifies which outputs kernel(s) the Xnew correspond to (see below) + :type slices: (None, list of slice objects, list of ints) + :param full_cov: whether to return the folll covariance matrix, or just the diagonal + :type full_cov: bool + :rtype: posterior mean, a Numpy array, Nnew x self.D + :rtype: posterior variance, a Numpy array, Nnew x Nnew x (self.D) + + .. Note:: "slices" specifies how the the points X_new co-vary wich the training points. + + - If None, the new points covary throigh every kernel part (default) + - If a list of slices, the i^th slice specifies which data are affected by the i^th kernel part + - If a list of booleans, specifying which kernel parts are active + + If full_cov and self.D > 1, the return shape of var is Nnew x Nnew x self.D. If self.D == 1, the return shape is Nnew x Nnew. + This is to allow for different normalisations of the output dimensions. + + + """ + + #normalise X values + Xnew = (Xnew.copy() - self._Xmean) / self._Xstd + mu, var, phi = self._raw_predict(Xnew, slices, full_cov) + + #un-normalise + mu = mu*self._Ystd + self._Ymean + if full_cov: + if self.D==1: + var *= np.square(self._Ystd) + else: + var = var[:,:,None] * np.square(self._Ystd) + else: + if self.D==1: + var *= np.square(np.squeeze(self._Ystd)) + else: + var = var[:,None] * np.square(self._Ystd) + + return mu,var,phi + + def _raw_predict(self,_Xnew,slices, full_cov=False): + """Internal helper function for making predictions, does not account for normalisation""" + """ + Kx = self.kern.K(self.X,_Xnew, slices1=self.Xslices,slices2=slices) + mu = np.dot(np.dot(Kx.T,self.Ki),self.Y) + KiKx = np.dot(self.Ki,Kx) + if full_cov: + Kxx = self.kern.K(_Xnew, slices1=slices,slices2=slices) + var = Kxx - np.dot(KiKx.T,Kx) + else: + Kxx = self.kern.Kdiag(_Xnew, slices=slices) + var = Kxx - np.sum(np.multiply(KiKx,Kx),0) + return mu, var + """ + K_x = self.kern.K(self.X,_Xnew) + Kxx = self.kern.K(_Xnew) + #aux1,info = linalg.flapack.dtrtrs(self.L,np.dot(self.Sroot_tilde_K,self.ep_approx.v_tilde),lower=1) + #aux2,info = linalg.flapack.dtrtrs(self.L.T, aux1,lower=0) + #aux2 = mdot(self.Li.T,self.Li,self.Sroot_tilde_K,self.ep_approx.v_tilde) + aux2 = mdot(self.Bi,self.Sroot_tilde_K,self.ep_approx.v_tilde) + zeta = np.sqrt(self.ep_approx.tau_tilde)*aux2 + f = np.dot(K_x.T,self.ep_approx.v_tilde-zeta) + #v,info = linalg.flapack.dtrtrs(self.L,np.sqrt(self.ep_approx.tau_tilde)[:,None]*K_x,lower=1) + v = mdot(self.Li,np.sqrt(self.ep_approx.tau_tilde)[:,None]*K_x) + variance = Kxx - np.dot(v.T,v) + vdiag = np.diag(variance) + y=self.likelihood.predictive_mean(f,vdiag) + return f,vdiag,y + + def plot(self,samples=0,plot_limits=None,which_data='all',which_functions='all',resolution=None): + """ + :param samples: the number of a posteriori samples to plot + :param which_data: which if the training data to plot (default all) + :type which_data: 'all' or a slice object to slice self.X, self.Y + :param plot_limits: The limits of the plot. If 1D [xmin,xmax], if 2D [[xmin,ymin],[xmax,ymax]]. Defaluts to data limits + :param which_functions: which of the kernel functions to plot (additively) + :type which_functions: list of bools + :param resolution: the number of intervals to sample the GP on. Defaults to 200 in 1D and 50 (a 50x50 grid) in 2D + + Plot the posterior of the GP. + - In one dimension, the function is plotted with a shaded region identifying two standard deviations. + - In two dimsensions, a contour-plot shows the mean predicted function + - In higher dimensions, we've no implemented this yet !TODO! + + Can plot only part of the data and part of the posterior functions using which_data and which_functions + """ + if which_functions=='all': + which_functions = [True]*self.kern.Nparts + if which_data=='all': + which_data = slice(None) + + X = self.X[which_data,:] + Y = self.Y[which_data,:] + + Xorig = X*self._Xstd + self._Xmean + Yorig = Y*self._Ystd + self._Ymean + if plot_limits is None: + xmin,xmax = Xorig.min(0),Xorig.max(0) + xmin, xmax = xmin-0.2*(xmax-xmin), xmax+0.2*(xmax-xmin) + elif len(plot_limits)==2: + xmin, xmax = plot_limits + else: + raise ValueError, "Bad limits for plotting" + + if self.X.shape[1]==1: + Xnew = np.linspace(xmin,xmax,resolution or 200)[:,None] + #m,v,phi = self.predict(Xnew,slices=which_functions) + #gpplot(Xnew,m,v) + mu_f, var_f, phi_f = self.predict(Xnew,slices=which_functions) + pb.subplot(211) + self.likelihood.plot1Da(X_new=Xnew,Mean_new=mu_f,Var_new=var_f,X_u=self.X,Mean_u=self.mu,Var_u=np.diag(self.Sigma)) + if samples: + s = np.random.multivariate_normal(m.flatten(),v,samples) + pb.plot(Xnew.flatten(),s.T, alpha = 0.4, c='#3465a4', linewidth = 0.8) + pb.xlim(xmin,xmax) + pb.subplot(212) + self.likelihood.plot1Db(self.X,Xnew,phi_f) + + elif self.X.shape[1]==2: + resolution = 50 or resolution + xx,yy = np.mgrid[xmin[0]:xmax[0]:1j*resolution,xmin[1]:xmax[1]:1j*resolution] + Xtest = np.vstack((xx.flatten(),yy.flatten())).T + zz,vv = self.predict(Xtest,slices=which_functions) + zz = zz.reshape(resolution,resolution) + pb.contour(xx,yy,zz,vmin=zz.min(),vmax=zz.max(),cmap=pb.cm.jet) + pb.scatter(Xorig[:,0],Xorig[:,1],40,Yorig,linewidth=0,cmap=pb.cm.jet,vmin=zz.min(),vmax=zz.max()) + pb.xlim(xmin[0],xmax[0]) + pb.ylim(xmin[1],xmax[1]) + + else: + raise NotImplementedError, "Cannot plot GPs with more than two input dimensions" diff --git a/GPy/models/__init__.py b/GPy/models/__init__.py index ab7ff5b4..5f824f2b 100644 --- a/GPy/models/__init__.py +++ b/GPy/models/__init__.py @@ -7,6 +7,7 @@ from sparse_GP_regression import sparse_GP_regression from GPLVM import GPLVM from warped_GP import warpedGP from GP_EP import GP_EP +from GP_EP2 import GP_EP2 from generalized_FITC import generalized_FITC from sparse_GPLVM import sparse_GPLVM from uncollapsed_sparse_GP import uncollapsed_sparse_GP From 6a2e0a1fe554dfe00036b6fdef82c9d437bff3f0 Mon Sep 17 00:00:00 2001 From: Ricardo Andrade Date: Fri, 25 Jan 2013 18:14:28 +0000 Subject: [PATCH 02/10] fixing EP and merging it with GP_regression --- GPy/examples/ep_fix.py | 11 +- GPy/inference/EP.py | 12 +- GPy/inference/likelihoods.py | 31 ++-- GPy/models/GP.py | 312 +++++++++++++++++++++++++++++++++++ GPy/models/GP_EP.py | 2 +- GPy/models/GP_EP2.py | 127 +++++++------- GPy/models/__init__.py | 1 + 7 files changed, 403 insertions(+), 93 deletions(-) create mode 100644 GPy/models/GP.py diff --git a/GPy/examples/ep_fix.py b/GPy/examples/ep_fix.py index e4999f30..2da94335 100644 --- a/GPy/examples/ep_fix.py +++ b/GPy/examples/ep_fix.py @@ -25,14 +25,15 @@ seed=default_seed data = GPy.util.datasets.toy_linear_1d_classification(seed=seed) likelihood = GPy.inference.likelihoods.probit(data['Y'][:, 0:1]) -m = GPy.models.GP_EP2(data['X'],likelihood) +m = GPy.models.GP(data['X'],likelihood=likelihood) -#m.constrain_positive('var') -#m.constrain_positive('len') -#m.tie_param('lengthscale') +m.constrain_positive('var') +m.constrain_positive('len') +m.tie_param('lengthscale') m.approximate_likelihood() +print m.checkgrad() # Optimize and plot -#m.optimize() +m.optimize() #m.em(plot_all=False) # EM algorithm m.plot() diff --git a/GPy/inference/EP.py b/GPy/inference/EP.py index fa691961..f7c163b1 100644 --- a/GPy/inference/EP.py +++ b/GPy/inference/EP.py @@ -60,7 +60,7 @@ class Full(EP): def fit_EP(self): """ The expectation-propagation algorithm. - For nomenclature see Rasmussen & Williams 2006 (pag. 52-60) + For nomenclature see Rasmussen & Williams 2006. """ #Prior distribution parameters: p(f|X) = N(f|0,K) #self.K = self.kernel.K(self.X,self.X) @@ -84,8 +84,6 @@ class Full(EP): phi = np.empty(self.N,dtype=float) mu_hat = np.empty(self.N,dtype=float) sigma2_hat = np.empty(self.N,dtype=float) - self.mu_hat = mu_hat #TODO erase me - self.sigma2_hat = sigma2_hat #TODO erase me #Approximation epsilon_np1 = self.epsilon + 1. @@ -95,21 +93,16 @@ class Full(EP): self.np2 = [self.v_tilde.copy()] while epsilon_np1 > self.epsilon or epsilon_np2 > self.epsilon: update_order = np.arange(self.N) - #random.shuffle(update_order) #TODO uncomment + random.shuffle(update_order) for i in update_order: #Cavity distribution parameters self.tau_[i] = 1./self.Sigma[i,i] - self.eta*self.tau_tilde[i] self.v_[i] = self.mu[i]/self.Sigma[i,i] - self.eta*self.v_tilde[i] #Marginal moments self.Z_hat[i], mu_hat[i], sigma2_hat[i] = self.likelihood.moments_match(i,self.tau_[i],self.v_[i]) - self.mu_hat[i] = mu_hat[i] #TODO erase me - self.sigma2_hat[i] = sigma2_hat[i] #TODO erase me - #if i == 3: - # a = b #Site parameters update Delta_tau = self.delta/self.eta*(1./sigma2_hat[i] - 1./self.Sigma[i,i]) Delta_v = self.delta/self.eta*(mu_hat[i]/sigma2_hat[i] - self.mu[i]/self.Sigma[i,i]) - print Delta_tau self.tau_tilde[i] = self.tau_tilde[i] + Delta_tau self.v_tilde[i] = self.v_tilde[i] + Delta_v #Posterior distribution parameters update @@ -128,6 +121,7 @@ class Full(EP): epsilon_np2 = sum((self.v_tilde-self.np2[-1])**2)/self.N self.np1.append(self.tau_tilde.copy()) self.np2.append(self.v_tilde.copy()) + return self.tau_tilde[:,None], self.v_tilde[:,None], self.Z_hat[:,None], self.tau_[:,None], self.v_[:,None] class DTC(EP): def fit_EP(self): diff --git a/GPy/inference/likelihoods.py b/GPy/inference/likelihoods.py index ff4770f6..29e194e0 100644 --- a/GPy/inference/likelihoods.py +++ b/GPy/inference/likelihoods.py @@ -19,7 +19,7 @@ class likelihood: self.Y = Y self.N = self.Y.shape[0] - def plot1Da(self,X_new,Mean_new,Var_new,X_u,Mean_u,Var_u): + def plot1Da(self,X,mean,var,Z=None,mean_Z=None,var_Z=None): """ Plot the predictive distribution of the GP model for 1-dimensional inputs @@ -30,10 +30,18 @@ class likelihood: :param Mean_u: mean values at X_u :param Var_new: variance values at X_u """ - assert X_new.shape[1] == 1, 'Number of dimensions must be 1' - gpplot(X_new,Mean_new,Var_new) - pb.errorbar(X_u.flatten(),Mean_u.flatten(),2*np.sqrt(Var_u.flatten()),fmt='r+') - pb.plot(X_u,Mean_u,'ro') + assert X.shape[1] == 1, 'Number of dimensions must be 1' + gpplot(X,mean,var.flatten()) + pb.errorbar(Z.flatten(),mean_Z.flatten(),2*np.sqrt(var_Z.flatten()),fmt='r+') + pb.plot(Z,mean_Z,'ro') + + def plot1Db(self,X_obs,X,phi,Z=None): + assert X_obs.shape[1] == 1, 'Number of dimensions must be 1' + gpplot(X,phi,np.zeros(X.shape[0])) + pb.plot(X_obs,(self.Y+1)/2,'kx',mew=1.5) + pb.ylim(-0.2,1.2) + if Z is not None: + pb.plot(Z,Z*0+.5,'r|',mew=1.5,markersize=12) def plot2D(self,X,X_new,F_new,U=None): """ @@ -88,16 +96,11 @@ class probit(likelihood): sigma2_hat = 1./tau_i - (phi/((tau_i**2+tau_i)*Z_hat))*(z+phi/Z_hat) return Z_hat, mu_hat, sigma2_hat - def plot1Db(self,X,X_new,F_new,U=None): - assert X.shape[1] == 1, 'Number of dimensions must be 1' - gpplot(X_new,F_new,np.zeros(X_new.shape[0])) - pb.plot(X,(self.Y+1)/2,'kx',mew=1.5) - pb.ylim(-0.2,1.2) - if U is not None: - pb.plot(U,U*0+.5,'r|',mew=1.5,markersize=12) - def predictive_mean(self,mu,variance): - return stats.norm.cdf(mu/np.sqrt(1+variance)) + def predictive_mean(self,mu,var): + mu = mu.flatten() + var = var.flatten() + return stats.norm.cdf(mu/np.sqrt(1+var)) def _log_likelihood_gradients(): raise NotImplementedError diff --git a/GPy/models/GP.py b/GPy/models/GP.py new file mode 100644 index 00000000..4a8d23e9 --- /dev/null +++ b/GPy/models/GP.py @@ -0,0 +1,312 @@ +# 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 +from .. import kern +from ..core import model +from ..util.linalg import pdinv,mdot +from ..util.plot import gpplot, Tango +from ..inference.EP import Full +from ..inference.likelihoods import likelihood,probit,poisson,gaussian + +class GP(model): + """ + Gaussian Process model for regression + + :param X: input observations + :param Y: observed values + :param kernel: a GPy kernel, defaults to rbf+white + :param normalize_X: whether to normalize the input data before computing (predictions will be in original scales) + :type normalize_X: False|True + :param normalize_Y: whether to normalize the input data before computing (predictions will be in original scales) + :type normalize_Y: False|True + :param Xslices: how the X,Y data co-vary in the kernel (i.e. which "outputs" they correspond to). See (link:slicing) + :rtype: model object + + .. Note:: Multiple independent outputs are allowed using columns of Y + + """ + + def __init__(self,X,Y=None,kernel=None,normalize_X=False,normalize_Y=False, Xslices=None,likelihood=None,epsilon_ep=1e-3,epsion_em=.1,powerep=[1.,1.]): + #TODO: specify beta parameter explicitely + + # parse arguments + self.Xslices = Xslices + self.X = X + self.N, self.Q = self.X.shape + assert len(self.X.shape)==2 + if kernel is None: + kernel = kern.rbf(X.shape[1]) + kern.bias(X.shape[1]) + kern.white(X.shape[1]) + else: + assert isinstance(kernel, kern.kern) + self.kern = kernel + + #here's some simple normalisation + if normalize_X: + self._Xmean = X.mean(0)[None,:] + self._Xstd = X.std(0)[None,:] + self.X = (X.copy() - self._Xmean) / self._Xstd + if hasattr(self,'Z'): + self.Z = (self.Z - self._Xmean) / self._Xstd + else: + self._Xmean = np.zeros((1,self.X.shape[1])) + self._Xstd = np.ones((1,self.X.shape[1])) + + + # Y - likelihood related variables, these might change whether using EP or not + if likelihood is None: + assert Y is not None, "Either Y or likelihood must be defined" + self.likelihood = gaussian(Y) + else: + self.likelihood = likelihood + assert len(self.likelihood.Y.shape)==2 + assert self.X.shape[0] == self.likelihood.Y.shape[0] + self.N, self.D = self.likelihood.Y.shape + + if isinstance(self.likelihood,gaussian): + self.EP = False + self.Y = Y + + #here's some simple normalisation + if normalize_Y: + self._Ymean = Y.mean(0)[None,:] + self._Ystd = Y.std(0)[None,:] + self.Y = (Y.copy()- self._Ymean) / self._Ystd + else: + self._Ymean = np.zeros((1,self.Y.shape[1])) + self._Ystd = np.ones((1,self.Y.shape[1])) + + if self.D > self.N: + # then it's more efficient to store YYT + self.YYT = np.dot(self.Y, self.Y.T) + else: + self.YYT = None + + else: + # Y is defined after approximating the likelihood + self.EP = True + self.eta,self.delta = powerep + self.epsilon_ep = epsilon_ep + self.tau_tilde = np.ones([self.N,self.D]) + self.v_tilde = np.zeros([self.N,self.D]) + self.tau_ = np.ones([self.N,self.D]) + self.v_ = np.zeros([self.N,self.D]) + self.Z_hat = np.ones([self.N,self.D]) + + model.__init__(self) + + def _set_params(self,p): + # TODO: remove beta when using EP + self.kern._set_params_transformed(p) + if not self.EP: + self.K = self.kern.K(self.X,slices1=self.Xslices) + self.Ki, self.L, self.Li, self.K_logdet = pdinv(self.K) + else: + self._ep_covariance() + + def _get_params(self): + # TODO: remove beta when using EP + return self.kern._get_params_transformed() + + def _get_param_names(self): + # TODO: remove beta when using EP + return self.kern._get_param_names_transformed() + + def approximate_likelihood(self): + assert not isinstance(self.likelihood, gaussian), "EP is only available for non-gaussian likelihoods" + self.ep_approx = Full(self.K,self.likelihood,epsilon=self.epsilon_ep,powerep=[self.eta,self.delta]) + self.tau_tilde, self.v_tilde, self.Z_hat, self.tau_, self.v_=self.ep_approx.fit_EP() + # Y: EP likelihood is defined as a regression model for mu_tilde + self.Y = self.v_tilde/self.tau_tilde + self._Ymean = np.zeros((1,self.Y.shape[1])) + self._Ystd = np.ones((1,self.Y.shape[1])) + if self.D > self.N: + # then it's more efficient to store YYT + self.YYT = np.dot(self.Y, self.Y.T) + else: + self.YYT = None + self.mu_ = self.v_/self.tau_ + self._ep_covariance() + + def _ep_covariance(self): + # Kernel plus noise variance term + self.K = self.kern.K(self.X,slices1=self.Xslices) + np.diag(1./self.tau_tilde.flatten()) + self.Ki, self.L, self.Li, self.K_logdet = pdinv(self.K) + + def _model_fit_term(self): + """ + Computes the model fit using YYT if it's available + """ + if self.YYT is None: + return -0.5*np.sum(np.square(np.dot(self.Li,self.Y))) + else: + return -0.5*np.sum(np.multiply(self.Ki, self.YYT)) + + def _normalization_term(self): + """ + Computes the marginal likelihood normalization constants + """ + sigma_sum = 1./self.tau_ + 1./self.tau_tilde + mu_diff_2 = (self.mu_ - self.Y)**2 + penalty_term = np.sum(np.log(self.Z_hat)) + return penalty_term + 0.5*np.sum(np.log(sigma_sum)) + 0.5*np.sum(mu_diff_2/sigma_sum) + + def log_likelihood(self): + """ + The log marginal likelihood for an EP model can be written as the log likelihood of + a regression model for a new variable Y* = v_tilde/tau_tilde, with a covariance + matrix K* = K + diag(1./tau_tilde) plus a normalization term. + """ + complexity_term = -0.5*self.D*self.Kplus_logdet + normalization_term = 0 if self.EP == False else self.normalization_term() + return complexity_term + normalization_term + self._model_fit_term() + + + def log_likelihood(self): + complexity_term = -0.5*self.N*self.D*np.log(2.*np.pi) - 0.5*self.D*self.K_logdet + return complexity_term + self._model_fit_term() + + def dL_dK(self): + if self.YYT is None: + alpha = np.dot(self.Ki,self.Y) + dL_dK = 0.5*(np.dot(alpha,alpha.T)-self.D*self.Ki) + else: + dL_dK = 0.5*(mdot(self.Ki, self.YYT, self.Ki) - self.D*self.Ki) + + return dL_dK + + def _log_likelihood_gradients(self): + return self.kern.dK_dtheta(partial=self.dL_dK(),X=self.X) + + def predict(self,Xnew, slices=None, full_cov=False): + """ + + Predict the function(s) at the new point(s) Xnew. + + Arguments + --------- + :param Xnew: The points at which to make a prediction + :type Xnew: np.ndarray, Nnew x self.Q + :param slices: specifies which outputs kernel(s) the Xnew correspond to (see below) + :type slices: (None, list of slice objects, list of ints) + :param full_cov: whether to return the folll covariance matrix, or just the diagonal + :type full_cov: bool + :rtype: posterior mean, a Numpy array, Nnew x self.D + :rtype: posterior variance, a Numpy array, Nnew x Nnew x (self.D) + + .. Note:: "slices" specifies how the the points X_new co-vary wich the training points. + + - If None, the new points covary throigh every kernel part (default) + - If a list of slices, the i^th slice specifies which data are affected by the i^th kernel part + - If a list of booleans, specifying which kernel parts are active + + If full_cov and self.D > 1, the return shape of var is Nnew x Nnew x self.D. If self.D == 1, the return shape is Nnew x Nnew. + This is to allow for different normalisations of the output dimensions. + + + """ + + #normalise X values + Xnew = (Xnew.copy() - self._Xmean) / self._Xstd + mu, var, phi = self._raw_predict(Xnew, slices, full_cov) + + #un-normalise + mu = mu*self._Ystd + self._Ymean + if full_cov: + if self.D==1: + var *= np.square(self._Ystd) + else: + var = var[:,:,None] * np.square(self._Ystd) + else: + if self.D==1: + var *= np.square(np.squeeze(self._Ystd)) + else: + var = var[:,None] * np.square(self._Ystd) + + return mu,var,phi + + def _raw_predict(self,_Xnew,slices, full_cov=False): + """Internal helper function for making predictions, does not account for normalisation""" + Kx = self.kern.K(self.X,_Xnew, slices1=self.Xslices,slices2=slices) + mu = np.dot(np.dot(Kx.T,self.Ki),self.Y) + KiKx = np.dot(self.Ki,Kx) + if full_cov: + Kxx = self.kern.K(_Xnew, slices1=slices,slices2=slices) + var = Kxx - np.dot(KiKx.T,Kx) + else: + Kxx = self.kern.Kdiag(_Xnew, slices=slices) + var = Kxx - np.sum(np.multiply(KiKx,Kx),0) + phi = None if not self.EP else self.likelihood.predictive_mean(mu,var) + return mu, var, phi + + def plot(self,samples=0,plot_limits=None,which_data='all',which_functions='all',resolution=None): + """ + :param samples: the number of a posteriori samples to plot + :param which_data: which if the training data to plot (default all) + :type which_data: 'all' or a slice object to slice self.X, self.Y + :param plot_limits: The limits of the plot. If 1D [xmin,xmax], if 2D [[xmin,ymin],[xmax,ymax]]. Defaluts to data limits + :param which_functions: which of the kernel functions to plot (additively) + :type which_functions: list of bools + :param resolution: the number of intervals to sample the GP on. Defaults to 200 in 1D and 50 (a 50x50 grid) in 2D + + Plot the posterior of the GP. + - In one dimension, the function is plotted with a shaded region identifying two standard deviations. + - In two dimsensions, a contour-plot shows the mean predicted function + - In higher dimensions, we've no implemented this yet !TODO! + + Can plot only part of the data and part of the posterior functions using which_data and which_functions + """ + if which_functions=='all': + which_functions = [True]*self.kern.Nparts + if which_data=='all': + which_data = slice(None) + + X = self.X[which_data,:] + Y = self.Y[which_data,:] + + Xorig = X*self._Xstd + self._Xmean + Yorig = Y*self._Ystd + self._Ymean if not self.EP else self.likelihood.Y + + if plot_limits is None: + xmin,xmax = Xorig.min(0),Xorig.max(0) + xmin, xmax = xmin-0.2*(xmax-xmin), xmax+0.2*(xmax-xmin) + elif len(plot_limits)==2: + xmin, xmax = plot_limits + else: + raise ValueError, "Bad limits for plotting" + + if self.X.shape[1]==1: + Xnew = np.linspace(xmin,xmax,resolution or 200)[:,None] + m,v,phi = self.predict(Xnew,slices=which_functions) + if self.EP: + pb.subplot(211) + + gpplot(Xnew,m,v) + if samples: + s = np.random.multivariate_normal(m.flatten(),v,samples) + pb.plot(Xnew.flatten(),s.T, alpha = 0.4, c='#3465a4', linewidth = 0.8) + + if not self.EP: + pb.plot(Xorig,Yorig,'kx',mew=1.5) + pb.xlim(xmin,xmax) + else: + pb.xlim(xmin,xmax) + pb.subplot(212) + self.likelihood.plot1Db(self.X,Xnew,phi) + pb.xlim(xmin,xmax) + + elif self.X.shape[1]==2: + resolution = 50 or resolution + xx,yy = np.mgrid[xmin[0]:xmax[0]:1j*resolution,xmin[1]:xmax[1]:1j*resolution] + Xtest = np.vstack((xx.flatten(),yy.flatten())).T + zz,vv,phi = self.predict(Xtest,slices=which_functions) + zz = zz.reshape(resolution,resolution) + pb.contour(xx,yy,zz,vmin=zz.min(),vmax=zz.max(),cmap=pb.cm.jet) + pb.scatter(Xorig[:,0],Xorig[:,1],40,Yorig,linewidth=0,cmap=pb.cm.jet,vmin=zz.min(),vmax=zz.max()) + pb.xlim(xmin[0],xmax[0]) + pb.ylim(xmin[1],xmax[1]) + + else: + raise NotImplementedError, "Cannot plot GPs with more than two input dimensions" diff --git a/GPy/models/GP_EP.py b/GPy/models/GP_EP.py index 302ff366..1c0b9cf6 100644 --- a/GPy/models/GP_EP.py +++ b/GPy/models/GP_EP.py @@ -62,7 +62,7 @@ class GP_EP(model): self.L = jitchol(B) V,info = linalg.flapack.dtrtrs(self.L,self.Sroot_tilde_K,lower=1) self.Sigma = self.K - np.dot(V.T,V) - self.mu = np.dot(self.Sigma,self.ep_approx.v_tilde) + self.mu = np.dot(self.Sigma,self.ep_approx.v_tilde) * self.Z_hat def log_likelihood(self): """ diff --git a/GPy/models/GP_EP2.py b/GPy/models/GP_EP2.py index c68e7b70..ce869951 100644 --- a/GPy/models/GP_EP2.py +++ b/GPy/models/GP_EP2.py @@ -36,14 +36,11 @@ class GP_EP2(model): self.Xslices = Xslices assert isinstance(kernel, kern.kern) self.likelihood = likelihood - #self.Y = self.likelihood.Y #we might not need this self.kern = kernel self.X = X assert len(self.X.shape)==2 - #assert len(self.Y.shape)==2 - #assert self.X.shape[0] == self.Y.shape[0] - #self.N, self.D = self.Y.shape - self.D = 1 + assert self.X.shape[0] == self.likelihood.Y.shape[0] + self.D = self.likelihood.Y.shape[1] self.N, self.Q = self.X.shape #here's some simple normalisation @@ -75,14 +72,17 @@ class GP_EP2(model): """ self.eta,self.delta = powerep self.epsilon_ep = epsilon_ep - self.tau_tilde = np.zeros([self.N,self.D]) + self.tau_tilde = np.ones([self.N,self.D]) self.v_tilde = np.zeros([self.N,self.D]) + self.tau_ = np.ones([self.N,self.D]) + self.v_ = np.zeros([self.N,self.D]) + self.Z_hat = np.ones([self.N,self.D]) model.__init__(self) def _set_params(self,p): self.kern._set_params_transformed(p) self.K = self.kern.K(self.X,slices1=self.Xslices) - self.posterior_params() + self._ep_params() def _get_params(self): return self.kern._get_params_transformed() @@ -92,52 +92,63 @@ class GP_EP2(model): def approximate_likelihood(self): self.ep_approx = Full(self.K,self.likelihood,epsilon=self.epsilon_ep,powerep=[self.eta,self.delta]) - self.ep_approx.fit_EP() - self.tau_tilde = self.ep_approx.tau_tilde[:,None] - self.v_tilde = self.ep_approx.tau_tilde[:,None] - self.posterior_params() - self.Y = self.v_tilde/self.tau_tilde - self._Ymean = np.zeros((1,self.Y.shape[1])) - self._Ystd = np.ones((1,self.Y.shape[1])) - #self.YYT = np.dot(self.Y, self.Y.T) + self.tau_tilde, self.v_tilde, self.Z_hat, self.tau_, self.v_=self.ep_approx.fit_EP() + self._ep_params() - def posterior_params(self): - self.Sroot_tilde_K = np.sqrt(self.tau_tilde.flatten())[:,None]*self.K + def _ep_params(self): + # Posterior mean and Variance computation + self.Sroot_tilde_K = np.sqrt(self.tau_tilde)*self.K B = np.eye(self.N) + np.sqrt(self.tau_tilde.flatten())[None,:]*self.Sroot_tilde_K self.Bi,self.L,self.Li,B_logdet = pdinv(B) V = np.dot(self.Li,self.Sroot_tilde_K) - #V,info = linalg.flapack.dtrtrs(self.L,self.Sroot_tilde_K,lower=1) - self.Sigma = self.K - np.dot(V.T,V) - self.mu = np.dot(self.Sigma,self.v_tilde.flatten()) + self.Sigma = self.K - np.dot(V.T,V) #posterior variance + self.mu = np.dot(self.Sigma,self.v_tilde) #posterior mean + # Kernel plus noise variance term + self.Kplus = self.K + np.diag(1./self.tau_tilde.flatten()) + self.Kplusi,self.Lplus,self.Lplusi,self.Kplus_logdet = pdinv(self.Kplus) + # Y: EP likelihood is defined as a regression model for mu_tilde + self.Y = self.v_tilde/self.tau_tilde + self._Ymean = np.zeros((1,self.Y.shape[1])) + self._Ystd = np.ones((1,self.Y.shape[1])) + self.YYT = None #np.dot(self.Y, self.Y.T) + self.mu_ = self.v_/self.tau_ + def _model_fit_term(self): + """ + Computes the model fit using YYT if it's available + """ + if self.YYT is None: + return -0.5*np.sum(np.square(np.dot(self.Lplusi,self.Y))) + else: + return -0.5*np.sum(np.multiply(self.Kplusi, self.YYT)) - #def _model_fit_term(self): - # """ - # Computes the model fit using YYT if it's available - # """ - # if self.YYT is None: - # return -0.5*np.sum(np.square(np.dot(self.Li,self.Y))) - # else: - # return -0.5*np.sum(np.multiply(self.Ki, self.YYT)) + def _normalization_term(self): + """ + Computes the marginal likelihood normalization constants + """ + sigma_sum = 1./self.tau_ + 1./self.tau_tilde + mu_diff_2 = (self.mu_ - self.Y)**2 + penalty_term = np.sum(np.log(self.Z_hat)) + return penalty_term + 0.5*np.sum(np.log(sigma_sum)) + 0.5*np.sum(mu_diff_2/sigma_sum) def log_likelihood(self): - mu_ = self.ep_approx.v_/self.ep_approx.tau_ - L1 =.5*sum(np.log(1+self.ep_approx.tau_tilde*1./self.ep_approx.tau_))-sum(np.log(np.diag(self.L))) - L2A =.5*np.sum((self.Sigma-np.diag(1./(self.ep_approx.tau_+self.ep_approx.tau_tilde))) * np.dot(self.ep_approx.v_tilde[:,None],self.ep_approx.v_tilde[None,:])) - L2B = .5*np.dot(mu_*(self.ep_approx.tau_/(self.ep_approx.tau_tilde+self.ep_approx.tau_)),self.ep_approx.tau_tilde*mu_ - 2*self.ep_approx.v_tilde) - L3 = sum(np.log(self.ep_approx.Z_hat)) - return L1 + L2A + L2B + L3 + """ + The log marginal likelihood for an EP model can be written as the log likelihood of + a regression model for a new variable Y* = v_tilde/tau_tilde, with a covariance + matrix K* = K + diag(1./tau_tilde) plus a normalization term. + """ + complexity_term = -0.5*self.D*self.Kplus_logdet + return complexity_term + self._model_fit_term() + self._normalization_term() - def dL_dK(self): #FIXME + def dL_dK(self): if self.YYT is None: - alpha = np.dot(self.Ki,self.Y) - dL_dK = 0.5*(np.dot(alpha,alpha.T)-self.D*self.Ki) + alpha = np.dot(self.Kplusi,self.Y) + dL_dK = 0.5*(np.dot(alpha,alpha.T)-self.D*self.Kplusi) else: - dL_dK = 0.5*(mdot(self.Ki, self.YYT, self.Ki) - self.D*self.Ki) - + dL_dK = 0.5*(mdot(self.Kplusi, self.YYT, self.Kplusi) - self.D*self.Kplusi) return dL_dK - def _log_likelihood_gradients(self): #FIXME + def _log_likelihood_gradients(self): return self.kern.dK_dtheta(partial=self.dL_dK(),X=self.X) def predict(self,Xnew, slices=None, full_cov=False): @@ -189,32 +200,20 @@ class GP_EP2(model): def _raw_predict(self,_Xnew,slices, full_cov=False): """Internal helper function for making predictions, does not account for normalisation""" - """ - Kx = self.kern.K(self.X,_Xnew, slices1=self.Xslices,slices2=slices) - mu = np.dot(np.dot(Kx.T,self.Ki),self.Y) - KiKx = np.dot(self.Ki,Kx) + K_x = self.kern.K(self.X,_Xnew,slices1=self.Xslices,slices2=slices) + aux2 = mdot(self.Bi,self.Sroot_tilde_K,self.v_tilde) + zeta = np.sqrt(self.tau_tilde)*aux2 + f = np.dot(K_x.T,self.v_tilde-zeta) + v = mdot(self.Li,np.sqrt(self.tau_tilde)*K_x) if full_cov: - Kxx = self.kern.K(_Xnew, slices1=slices,slices2=slices) - var = Kxx - np.dot(KiKx.T,Kx) + Kxx = self.kern.K(_Xnew,slices1=slices,slices2=slices) + var = Kxx - np.dot(v.T,v) + var_diag = np.diag(var)[:,None] else: Kxx = self.kern.Kdiag(_Xnew, slices=slices) - var = Kxx - np.sum(np.multiply(KiKx,Kx),0) - return mu, var - """ - K_x = self.kern.K(self.X,_Xnew) - Kxx = self.kern.K(_Xnew) - #aux1,info = linalg.flapack.dtrtrs(self.L,np.dot(self.Sroot_tilde_K,self.ep_approx.v_tilde),lower=1) - #aux2,info = linalg.flapack.dtrtrs(self.L.T, aux1,lower=0) - #aux2 = mdot(self.Li.T,self.Li,self.Sroot_tilde_K,self.ep_approx.v_tilde) - aux2 = mdot(self.Bi,self.Sroot_tilde_K,self.ep_approx.v_tilde) - zeta = np.sqrt(self.ep_approx.tau_tilde)*aux2 - f = np.dot(K_x.T,self.ep_approx.v_tilde-zeta) - #v,info = linalg.flapack.dtrtrs(self.L,np.sqrt(self.ep_approx.tau_tilde)[:,None]*K_x,lower=1) - v = mdot(self.Li,np.sqrt(self.ep_approx.tau_tilde)[:,None]*K_x) - variance = Kxx - np.dot(v.T,v) - vdiag = np.diag(variance) - y=self.likelihood.predictive_mean(f,vdiag) - return f,vdiag,y + var_diag = (Kxx - np.sum(v**2,-2))[:,None] + phi = self.likelihood.predictive_mean(f,var_diag) + return f, var_diag, phi def plot(self,samples=0,plot_limits=None,which_data='all',which_functions='all',resolution=None): """ @@ -257,7 +256,7 @@ class GP_EP2(model): #gpplot(Xnew,m,v) mu_f, var_f, phi_f = self.predict(Xnew,slices=which_functions) pb.subplot(211) - self.likelihood.plot1Da(X_new=Xnew,Mean_new=mu_f,Var_new=var_f,X_u=self.X,Mean_u=self.mu,Var_u=np.diag(self.Sigma)) + self.likelihood.plot1Da(X=Xnew,mean=mu_f,var=var_f,Z=self.X,mean_Z=self.mu,var_Z=np.diag(self.Sigma)) if samples: s = np.random.multivariate_normal(m.flatten(),v,samples) pb.plot(Xnew.flatten(),s.T, alpha = 0.4, c='#3465a4', linewidth = 0.8) diff --git a/GPy/models/__init__.py b/GPy/models/__init__.py index 5f824f2b..ca44aab1 100644 --- a/GPy/models/__init__.py +++ b/GPy/models/__init__.py @@ -11,3 +11,4 @@ from GP_EP2 import GP_EP2 from generalized_FITC import generalized_FITC from sparse_GPLVM import sparse_GPLVM from uncollapsed_sparse_GP import uncollapsed_sparse_GP +from GP import GP From 738ca78dac64b0806eeea7bd247849db751e565b Mon Sep 17 00:00:00 2001 From: Ricardo Andrade Date: Fri, 25 Jan 2013 18:24:10 +0000 Subject: [PATCH 03/10] No more GP_EP stuff --- GPy/inference/Expectation_Propagation.py | 240 ------------------- GPy/models/GP_EP.py | 160 ------------- GPy/models/GP_EP2.py | 279 ----------------------- GPy/models/__init__.py | 2 - GPy/models/generalized_FITC.py | 3 +- 5 files changed, 2 insertions(+), 682 deletions(-) delete mode 100644 GPy/inference/Expectation_Propagation.py delete mode 100644 GPy/models/GP_EP.py delete mode 100644 GPy/models/GP_EP2.py diff --git a/GPy/inference/Expectation_Propagation.py b/GPy/inference/Expectation_Propagation.py deleted file mode 100644 index 520fc607..00000000 --- a/GPy/inference/Expectation_Propagation.py +++ /dev/null @@ -1,240 +0,0 @@ -# Copyright (c) 2012, GPy authors (see AUTHORS.txt). -# Licensed under the BSD 3-clause license (see LICENSE.txt) - - -import numpy as np -import random -from scipy import stats, linalg -from .likelihoods import likelihood -from ..core import model -from ..util.linalg import pdinv,mdot,jitchol -from ..util.plot import gpplot -from .. import kern - -class EP_base: - """ - Expectation Propagation. - - This is just the base class for expectation propagation. We'll extend it for full and sparse EP. - """ - def __init__(self,likelihood,epsilon=1e-3,powerep=[1.,1.]): - self.likelihood = likelihood - self.epsilon = epsilon - self.eta, self.delta = powerep - self.jitter = 1e-12 - - #Initial values - Likelihood approximation parameters: - #p(y|f) = t(f|tau_tilde,v_tilde) - self.restart_EP() - - def restart_EP(self): - """ - Set the EP approximation to initial state - """ - self.tau_tilde = np.zeros(self.N) - self.v_tilde = np.zeros(self.N) - self.mu = np.zeros(self.N) - -class Full(EP_base): - """ - :param likelihood: Output's likelihood (e.g. probit) - :type likelihood: GPy.inference.likelihood instance - :param K: prior covariance matrix - :type K: np.ndarray (N x N) - :param likelihood: Output's likelihood (e.g. probit) - :type likelihood: GPy.inference.likelihood instance - :param epsilon: Convergence criterion, maximum squared difference allowed between mean updates to stop iterations (float) - :param powerep: Power-EP parameters (eta,delta) - 2x1 numpy array (floats) - """ - def __init__(self,K,likelihood,*args,**kwargs): - assert K.shape[0] == K.shape[1] - self.K = K - self.N = self.K.shape[0] - EP_base.__init__(self,likelihood,*args,**kwargs) - def fit_EP(self,messages=False): - """ - The expectation-propagation algorithm. - For nomenclature see Rasmussen & Williams 2006 (pag. 52-60) - """ - #Prior distribution parameters: p(f|X) = N(f|0,K) - #self.K = self.kernel.K(self.X,self.X) - - #Initial values - Posterior distribution parameters: q(f|X,Y) = N(f|mu,Sigma) - self.mu=np.zeros(self.N) - self.Sigma=self.K.copy() - - """ - Initial values - Cavity distribution parameters: - q_(f|mu_,sigma2_) = Product{q_i(f|mu_i,sigma2_i)} - sigma_ = 1./tau_ - mu_ = v_/tau_ - """ - - self.tau_ = np.empty(self.N,dtype=np.float64) - self.v_ = np.empty(self.N,dtype=np.float64) - - #Initial values - Marginal moments - z = np.empty(self.N,dtype=np.float64) - self.Z_hat = np.empty(self.N,dtype=np.float64) - phi = np.empty(self.N,dtype=np.float64) - mu_hat = np.empty(self.N,dtype=np.float64) - sigma2_hat = np.empty(self.N,dtype=np.float64) - - #Approximation - epsilon_np1 = self.epsilon + 1. - epsilon_np2 = self.epsilon + 1. - self.iterations = 0 - self.np1 = [self.tau_tilde.copy()] - self.np2 = [self.v_tilde.copy()] - while epsilon_np1 > self.epsilon or epsilon_np2 > self.epsilon: - update_order = np.random.permutation(self.N) - for i in update_order: - #Cavity distribution parameters - self.tau_[i] = 1./self.Sigma[i,i] - self.eta*self.tau_tilde[i] - self.v_[i] = self.mu[i]/self.Sigma[i,i] - self.eta*self.v_tilde[i] - #Marginal moments - self.Z_hat[i], mu_hat[i], sigma2_hat[i] = self.likelihood.moments_match(i,self.tau_[i],self.v_[i]) - #Site parameters update - Delta_tau = self.delta/self.eta*(1./sigma2_hat[i] - 1./self.Sigma[i,i]) - Delta_v = self.delta/self.eta*(mu_hat[i]/sigma2_hat[i] - self.mu[i]/self.Sigma[i,i]) - self.tau_tilde[i] = self.tau_tilde[i] + Delta_tau - self.v_tilde[i] = self.v_tilde[i] + Delta_v - #Posterior distribution parameters update - si=self.Sigma[:,i].reshape(self.N,1) - self.Sigma = self.Sigma - Delta_tau/(1.+ Delta_tau*self.Sigma[i,i])*np.dot(si,si.T) - self.mu = np.dot(self.Sigma,self.v_tilde) - self.iterations += 1 - #Sigma recomptutation with Cholesky decompositon - Sroot_tilde_K = np.sqrt(self.tau_tilde)[:,None]*(self.K) - B = np.eye(self.N) + np.sqrt(self.tau_tilde)[None,:]*Sroot_tilde_K - L = jitchol(B) - V,info = linalg.flapack.dtrtrs(L,Sroot_tilde_K,lower=1) - self.Sigma = self.K - np.dot(V.T,V) - self.mu = np.dot(self.Sigma,self.v_tilde) - epsilon_np1 = np.mean(self.tau_tilde-self.np1[-1]**2) - epsilon_np2 = np.mean(self.v_tilde-self.np2[-1]**2) - self.np1.append(self.tau_tilde.copy()) - self.np2.append(self.v_tilde.copy()) - if messages: - print "EP iteration %i, epsilon %d"%(self.iterations,epsilon_np1) - -class FITC(EP_base): - """ - :param likelihood: Output's likelihood (e.g. probit) - :type likelihood: GPy.inference.likelihood instance - :param Knn_diag: The diagonal elements of Knn is a 1D vector - :param Kmn: The 'cross' variance between inducing inputs and data - :param Kmm: the covariance matrix of the inducing inputs - :param likelihood: Output's likelihood (e.g. probit) - :type likelihood: GPy.inference.likelihood instance - :param epsilon: Convergence criterion, maximum squared difference allowed between mean updates to stop iterations (float) - :param powerep: Power-EP parameters (eta,delta) - 2x1 numpy array (floats) - """ - def __init__(self,likelihood,Knn_diag,Kmn,Kmm,*args,**kwargs): - self.Knn_diag = Knn_diag - self.Kmn = Kmn - self.Kmm = Kmm - self.M = self.Kmn.shape[0] - self.N = self.Kmn.shape[1] - assert self.M <= self.N, 'The number of inducing inputs must be smaller than the number of observations' - assert len(Knn_diag) == self.N, 'Knn_diagonal has size different from N' - EP_base.__init__(self,likelihood,*args,**kwargs) - - def fit_EP(self): - """ - The expectation-propagation algorithm with sparse pseudo-input. - For nomenclature see Naish-Guzman and Holden, 2008. - """ - - """ - Prior approximation parameters: - q(f|X) = int_{df}{N(f|KfuKuu_invu,diag(Kff-Qff)*N(u|0,Kuu)} = N(f|0,Sigma0) - Sigma0 = diag(Knn-Qnn) + Qnn, Qnn = Knm*Kmmi*Kmn - """ - self.Kmmi, self.Kmm_hld = pdinv(self.Kmm) - self.P0 = self.Kmn.T - self.KmnKnm = np.dot(self.P0.T, self.P0) - self.KmmiKmn = np.dot(self.Kmmi,self.P0.T) - self.Qnn_diag = np.sum(self.P0.T*self.KmmiKmn,-2) - self.Diag0 = self.Knn_diag - self.Qnn_diag - self.R0 = jitchol(self.Kmmi).T - - """ - Posterior approximation: q(f|y) = N(f| mu, Sigma) - Sigma = Diag + P*R.T*R*P.T + K - mu = w + P*gamma - """ - self.w = np.zeros(self.N) - self.gamma = np.zeros(self.M) - self.mu = np.zeros(self.N) - self.P = self.P0.copy() - self.R = self.R0.copy() - self.Diag = self.Diag0.copy() - self.Sigma_diag = self.Knn_diag - - """ - Initial values - Cavity distribution parameters: - q_(g|mu_,sigma2_) = Product{q_i(g|mu_i,sigma2_i)} - sigma_ = 1./tau_ - mu_ = v_/tau_ - """ - self.tau_ = np.empty(self.N,dtype=np.float64) - self.v_ = np.empty(self.N,dtype=np.float64) - - #Initial values - Marginal moments - z = np.empty(self.N,dtype=np.float64) - self.Z_hat = np.empty(self.N,dtype=np.float64) - phi = np.empty(self.N,dtype=np.float64) - mu_hat = np.empty(self.N,dtype=np.float64) - sigma2_hat = np.empty(self.N,dtype=np.float64) - - #Approximation - epsilon_np1 = 1 - epsilon_np2 = 1 - self.iterations = 0 - self.np1 = [self.tau_tilde.copy()] - self.np2 = [self.v_tilde.copy()] - while epsilon_np1 > self.epsilon or epsilon_np2 > self.epsilon: - update_order = np.arange(self.N) - random.shuffle(update_order) - for i in update_order: - #Cavity distribution parameters - self.tau_[i] = 1./self.Sigma_diag[i] - self.eta*self.tau_tilde[i] - self.v_[i] = self.mu[i]/self.Sigma_diag[i] - self.eta*self.v_tilde[i] - #Marginal moments - self.Z_hat[i], mu_hat[i], sigma2_hat[i] = self.likelihood.moments_match(i,self.tau_[i],self.v_[i]) - #Site parameters update - Delta_tau = self.delta/self.eta*(1./sigma2_hat[i] - 1./self.Sigma_diag[i]) - Delta_v = self.delta/self.eta*(mu_hat[i]/sigma2_hat[i] - self.mu[i]/self.Sigma_diag[i]) - self.tau_tilde[i] = self.tau_tilde[i] + Delta_tau - self.v_tilde[i] = self.v_tilde[i] + Delta_v - #Posterior distribution parameters update - dtd1 = Delta_tau*self.Diag[i] + 1. - dii = self.Diag[i] - self.Diag[i] = dii - (Delta_tau * dii**2.)/dtd1 - pi_ = self.P[i,:].reshape(1,self.M) - self.P[i,:] = pi_ - (Delta_tau*dii)/dtd1 * pi_ - Rp_i = np.dot(self.R,pi_.T) - RTR = np.dot(self.R.T,np.dot(np.eye(self.M) - Delta_tau/(1.+Delta_tau*self.Sigma_diag[i]) * np.dot(Rp_i,Rp_i.T),self.R)) - self.R = jitchol(RTR).T - self.w[i] = self.w[i] + (Delta_v - Delta_tau*self.w[i])*dii/dtd1 - self.gamma = self.gamma + (Delta_v - Delta_tau*self.mu[i])*np.dot(RTR,self.P[i,:].T) - self.RPT = np.dot(self.R,self.P.T) - self.Sigma_diag = self.Diag + np.sum(self.RPT.T*self.RPT.T,-1) - self.mu = self.w + np.dot(self.P,self.gamma) - self.iterations += 1 - #Sigma recomptutation with Cholesky decompositon - self.Diag = self.Diag0/(1.+ self.Diag0 * self.tau_tilde) - self.P = (self.Diag / self.Diag0)[:,None] * self.P0 - self.RPT0 = np.dot(self.R0,self.P0.T) - L = jitchol(np.eye(self.M) + np.dot(self.RPT0,(1./self.Diag0 - self.Diag/(self.Diag0**2))[:,None]*self.RPT0.T)) - self.R,info = linalg.flapack.dtrtrs(L,self.R0,lower=1) - self.RPT = np.dot(self.R,self.P.T) - self.Sigma_diag = self.Diag + np.sum(self.RPT.T*self.RPT.T,-1) - self.w = self.Diag * self.v_tilde - self.gamma = np.dot(self.R.T, np.dot(self.RPT,self.v_tilde)) - self.mu = self.w + np.dot(self.P,self.gamma) - epsilon_np1 = sum((self.tau_tilde-self.np1[-1])**2)/self.N - epsilon_np2 = sum((self.v_tilde-self.np2[-1])**2)/self.N - self.np1.append(self.tau_tilde.copy()) - self.np2.append(self.v_tilde.copy()) diff --git a/GPy/models/GP_EP.py b/GPy/models/GP_EP.py deleted file mode 100644 index 1c0b9cf6..00000000 --- a/GPy/models/GP_EP.py +++ /dev/null @@ -1,160 +0,0 @@ -# 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 -from scipy import stats, linalg -from .. import kern -from ..inference.Expectation_Propagation import Full -from ..inference.likelihoods import likelihood,probit#,poisson,gaussian -from ..core import model -from ..util.linalg import pdinv,jitchol -from ..util.plot import gpplot - -class GP_EP(model): - def __init__(self,X,likelihood,kernel=None,epsilon_ep=1e-3,epsion_em=.1,powerep=[1.,1.]): - """ - Simple Gaussian Process with Non-Gaussian likelihood - - Arguments - --------- - :param X: input observations (NxD numpy.darray) - :param likelihood: a GPy likelihood (likelihood class) - :param kernel: a GPy kernel (kern class) - :param epsilon_ep: convergence criterion for the Expectation Propagation algorithm, defaults to 0.1 (float) - :param powerep: power-EP parameters [$\eta$,$\delta$], defaults to [1.,1.] (list) - :rtype: GPy model class. - """ - if kernel is None: - kernel = kern.rbf(X.shape[1]) + kern.bias(X.shape[1]) + kern.white(X.shape[1]) - - assert isinstance(kernel,kern.kern), 'kernel is not a kern instance' - self.likelihood = likelihood - self.Y = self.likelihood.Y - self.kernel = kernel - self.X = X - self.N, self.D = self.X.shape - self.eta,self.delta = powerep - self.epsilon_ep = epsilon_ep - self.jitter = 1e-12 - self.K = self.kernel.K(self.X) - model.__init__(self) - - def _set_params(self,p): - self.kernel._set_params_transformed(p) - - def _get_params(self): - return self.kernel._get_params_transformed() - - def _get_param_names(self): - return self.kernel._get_param_names_transformed() - - def approximate_likelihood(self): - self.ep_approx = Full(self.K,self.likelihood,epsilon=self.epsilon_ep,powerep=[self.eta,self.delta]) - self.ep_approx.fit_EP() - - def posterior_param(self): - self.K = self.kernel.K(self.X) - self.Sroot_tilde_K = np.sqrt(self.ep_approx.tau_tilde)[:,None]*self.K - B = np.eye(self.N) + np.sqrt(self.ep_approx.tau_tilde)*self.Sroot_tilde_K - #self.L = np.linalg.cholesky(B) - self.L = jitchol(B) - V,info = linalg.flapack.dtrtrs(self.L,self.Sroot_tilde_K,lower=1) - self.Sigma = self.K - np.dot(V.T,V) - self.mu = np.dot(self.Sigma,self.ep_approx.v_tilde) * self.Z_hat - - def log_likelihood(self): - """ - Returns - ------- - The EP approximation to the log-marginal likelihood - """ - self.posterior_param() - mu_ = self.ep_approx.v_/self.ep_approx.tau_ - L1 =.5*sum(np.log(1+self.ep_approx.tau_tilde*1./self.ep_approx.tau_))-sum(np.log(np.diag(self.L))) - L2A =.5*np.sum((self.Sigma-np.diag(1./(self.ep_approx.tau_+self.ep_approx.tau_tilde))) * np.dot(self.ep_approx.v_tilde[:,None],self.ep_approx.v_tilde[None,:])) - L2B = .5*np.dot(mu_*(self.ep_approx.tau_/(self.ep_approx.tau_tilde+self.ep_approx.tau_)),self.ep_approx.tau_tilde*mu_ - 2*self.ep_approx.v_tilde) - L3 = sum(np.log(self.ep_approx.Z_hat)) - return L1 + L2A + L2B + L3 - - def _log_likelihood_gradients(self): - dK_dp = self.kernel.dK_dtheta(self.X) - self.dK_dp = dK_dp - aux1,info_1 = linalg.flapack.dtrtrs(self.L,np.dot(self.Sroot_tilde_K,self.ep_approx.v_tilde),lower=1) - b = self.ep_approx.v_tilde - np.sqrt(self.ep_approx.tau_tilde)*linalg.flapack.dtrtrs(self.L.T,aux1)[0] - U,info_u = linalg.flapack.dtrtrs(self.L,np.diag(np.sqrt(self.ep_approx.tau_tilde)),lower=1) - dL_dK = 0.5*(np.outer(b,b)-np.dot(U.T,U)) - self.dL_dK = dL_dK - return np.array([np.sum(dK_dpi*dL_dK) for dK_dpi in dK_dp.T]) - - def predict(self,X): - #TODO: check output dimensions - self.posterior_param() - K_x = self.kernel.K(self.X,X) - Kxx = self.kernel.K(X) - aux1,info = linalg.flapack.dtrtrs(self.L,np.dot(self.Sroot_tilde_K,self.ep_approx.v_tilde),lower=1) - aux2,info = linalg.flapack.dtrtrs(self.L.T, aux1,lower=0) - zeta = np.sqrt(self.ep_approx.tau_tilde)*aux2 - f = np.dot(K_x.T,self.ep_approx.v_tilde-zeta) - v,info = linalg.flapack.dtrtrs(self.L,np.sqrt(self.ep_approx.tau_tilde)[:,None]*K_x,lower=1) - variance = Kxx - np.dot(v.T,v) - vdiag = np.diag(variance) - y=self.likelihood.predictive_mean(f,vdiag) - return f,vdiag,y - - def plot(self): - """ - Plot the fitted model: training function values, inducing points used, mean estimate and confidence intervals. - """ - if self.X.shape[1]==1: - pb.figure() - xmin,xmax = self.X.min(),self.X.max() - xmin, xmax = xmin-0.2*(xmax-xmin), xmax+0.2*(xmax-xmin) - Xnew = np.linspace(xmin,xmax,100)[:,None] - mu_f, var_f, mu_phi = self.predict(Xnew) - pb.subplot(211) - self.likelihood.plot1Da(X_new=Xnew,Mean_new=mu_f,Var_new=var_f,X_u=self.X,Mean_u=self.mu,Var_u=np.diag(self.Sigma)) - pb.subplot(212) - self.likelihood.plot1Db(self.X,Xnew,mu_phi) - elif self.X.shape[1]==2: - pb.figure() - x1min,x1max = self.X[:,0].min(0),self.X[:,0].max(0) - x2min,x2max = self.X[:,1].min(0),self.X[:,1].max(0) - x1min, x1max = x1min-0.2*(x1max-x1min), x1max+0.2*(x1max-x1min) - x2min, x2max = x2min-0.2*(x2max-x2min), x2max+0.2*(x1max-x1min) - axis1 = np.linspace(x1min,x1max,50) - axis2 = np.linspace(x2min,x2max,50) - XX1, XX2 = [e.flatten() for e in np.meshgrid(axis1,axis2)] - Xnew = np.c_[XX1.flatten(),XX2.flatten()] - f,v,p = self.predict(Xnew) - self.likelihood.plot2D(self.X,Xnew,p) - else: - raise NotImplementedError, "Cannot plot GPs with more than two input dimensions" - - def em(self,max_f_eval=1e4,epsilon=.1,plot_all=False): #TODO check this makes sense - """ - Fits sparse_EP and optimizes the hyperparametes iteratively until convergence is achieved. - """ - self.epsilon_em = epsilon - log_likelihood_change = self.epsilon_em + 1. - self.parameters_path = [self.kernel._get_params()] - self.approximate_likelihood() - self.site_approximations_path = [[self.ep_approx.tau_tilde,self.ep_approx.v_tilde]] - self.log_likelihood_path = [self.log_likelihood()] - iteration = 0 - while log_likelihood_change > self.epsilon_em: - print 'EM iteration', iteration - self.optimize(max_f_eval = max_f_eval) - log_likelihood_new = self.log_likelihood() - log_likelihood_change = log_likelihood_new - self.log_likelihood_path[-1] - if log_likelihood_change < 0: - print 'log_likelihood decrement' - self.kernel._set_params_transformed(self.parameters_path[-1]) - self.kernM._set_params_transformed(self.parameters_path[-1]) - else: - self.approximate_likelihood() - self.log_likelihood_path.append(self.log_likelihood()) - self.parameters_path.append(self.kernel._get_params()) - self.site_approximations_path.append([self.ep_approx.tau_tilde,self.ep_approx.v_tilde]) - iteration += 1 diff --git a/GPy/models/GP_EP2.py b/GPy/models/GP_EP2.py deleted file mode 100644 index ce869951..00000000 --- a/GPy/models/GP_EP2.py +++ /dev/null @@ -1,279 +0,0 @@ -# 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 -from scipy import stats, linalg -from .. import kern -from ..inference.EP import Full -from ..inference.likelihoods import likelihood,probit,poisson,gaussian -from ..core import model -from ..util.linalg import pdinv,mdot #,jitchol -from ..util.plot import gpplot, Tango - -class GP_EP2(model): - def __init__(self,X,likelihood,kernel=None,normalize_X=False,Xslices=None,epsilon_ep=1e-3,epsion_em=.1,powerep=[1.,1.]): - """ - Simple Gaussian Process with Non-Gaussian likelihood - - Arguments - --------- - :param X: input observations (NxD numpy.darray) - :param likelihood: a GPy likelihood (likelihood class) - :param kernel: a GPy kernel, defaults to rbf+white - :param normalize_X: whether to normalize the input data before computing (predictions will be in original scales) - :type normalize_X: False|True - :param epsilon_ep: convergence criterion for the Expectation Propagation algorithm, defaults to 1e-3 - :param powerep: power-EP parameters [$\eta$,$\delta$], defaults to [1.,1.] (list) - :param Xslices: how the X,Y data co-vary in the kernel (i.e. which "outputs" they correspond to). See (link:slicing) - :rtype: model object. - """ - #.. Note:: Multiple independent outputs are allowed using columns of Y #TODO add this note? - if kernel is None: - kernel = kern.rbf(X.shape[1]) + kern.bias(X.shape[1]) + kern.white(X.shape[1]) - - # parse arguments - self.Xslices = Xslices - assert isinstance(kernel, kern.kern) - self.likelihood = likelihood - self.kern = kernel - self.X = X - assert len(self.X.shape)==2 - assert self.X.shape[0] == self.likelihood.Y.shape[0] - self.D = self.likelihood.Y.shape[1] - self.N, self.Q = self.X.shape - - #here's some simple normalisation - if normalize_X: - self._Xmean = X.mean(0)[None,:] - self._Xstd = X.std(0)[None,:] - self.X = (X.copy() - self._Xmean) / self._Xstd - if hasattr(self,'Z'): - self.Z = (self.Z - self._Xmean) / self._Xstd - else: - self._Xmean = np.zeros((1,self.X.shape[1])) - self._Xstd = np.ones((1,self.X.shape[1])) - - #THIS PART IS NOT NEEDED - """ - if normalize_Y: - self._Ymean = Y.mean(0)[None,:] - self._Ystd = Y.std(0)[None,:] - self.Y = (Y.copy()- self._Ymean) / self._Ystd - else: - self._Ymean = np.zeros((1,self.Y.shape[1])) - self._Ystd = np.ones((1,self.Y.shape[1])) - - if self.D > self.N: - # then it's more efficient to store YYT - self.YYT = np.dot(self.Y, self.Y.T) - else: - self.YYT = None - """ - self.eta,self.delta = powerep - self.epsilon_ep = epsilon_ep - self.tau_tilde = np.ones([self.N,self.D]) - self.v_tilde = np.zeros([self.N,self.D]) - self.tau_ = np.ones([self.N,self.D]) - self.v_ = np.zeros([self.N,self.D]) - self.Z_hat = np.ones([self.N,self.D]) - model.__init__(self) - - def _set_params(self,p): - self.kern._set_params_transformed(p) - self.K = self.kern.K(self.X,slices1=self.Xslices) - self._ep_params() - - def _get_params(self): - return self.kern._get_params_transformed() - - def _get_param_names(self): - return self.kern._get_param_names_transformed() - - def approximate_likelihood(self): - self.ep_approx = Full(self.K,self.likelihood,epsilon=self.epsilon_ep,powerep=[self.eta,self.delta]) - self.tau_tilde, self.v_tilde, self.Z_hat, self.tau_, self.v_=self.ep_approx.fit_EP() - self._ep_params() - - def _ep_params(self): - # Posterior mean and Variance computation - self.Sroot_tilde_K = np.sqrt(self.tau_tilde)*self.K - B = np.eye(self.N) + np.sqrt(self.tau_tilde.flatten())[None,:]*self.Sroot_tilde_K - self.Bi,self.L,self.Li,B_logdet = pdinv(B) - V = np.dot(self.Li,self.Sroot_tilde_K) - self.Sigma = self.K - np.dot(V.T,V) #posterior variance - self.mu = np.dot(self.Sigma,self.v_tilde) #posterior mean - # Kernel plus noise variance term - self.Kplus = self.K + np.diag(1./self.tau_tilde.flatten()) - self.Kplusi,self.Lplus,self.Lplusi,self.Kplus_logdet = pdinv(self.Kplus) - # Y: EP likelihood is defined as a regression model for mu_tilde - self.Y = self.v_tilde/self.tau_tilde - self._Ymean = np.zeros((1,self.Y.shape[1])) - self._Ystd = np.ones((1,self.Y.shape[1])) - self.YYT = None #np.dot(self.Y, self.Y.T) - self.mu_ = self.v_/self.tau_ - - def _model_fit_term(self): - """ - Computes the model fit using YYT if it's available - """ - if self.YYT is None: - return -0.5*np.sum(np.square(np.dot(self.Lplusi,self.Y))) - else: - return -0.5*np.sum(np.multiply(self.Kplusi, self.YYT)) - - def _normalization_term(self): - """ - Computes the marginal likelihood normalization constants - """ - sigma_sum = 1./self.tau_ + 1./self.tau_tilde - mu_diff_2 = (self.mu_ - self.Y)**2 - penalty_term = np.sum(np.log(self.Z_hat)) - return penalty_term + 0.5*np.sum(np.log(sigma_sum)) + 0.5*np.sum(mu_diff_2/sigma_sum) - - def log_likelihood(self): - """ - The log marginal likelihood for an EP model can be written as the log likelihood of - a regression model for a new variable Y* = v_tilde/tau_tilde, with a covariance - matrix K* = K + diag(1./tau_tilde) plus a normalization term. - """ - complexity_term = -0.5*self.D*self.Kplus_logdet - return complexity_term + self._model_fit_term() + self._normalization_term() - - def dL_dK(self): - if self.YYT is None: - alpha = np.dot(self.Kplusi,self.Y) - dL_dK = 0.5*(np.dot(alpha,alpha.T)-self.D*self.Kplusi) - else: - dL_dK = 0.5*(mdot(self.Kplusi, self.YYT, self.Kplusi) - self.D*self.Kplusi) - return dL_dK - - def _log_likelihood_gradients(self): - return self.kern.dK_dtheta(partial=self.dL_dK(),X=self.X) - - def predict(self,Xnew, slices=None, full_cov=False): - """ - - Predict the function(s) at the new point(s) Xnew. - - Arguments - --------- - :param Xnew: The points at which to make a prediction - :type Xnew: np.ndarray, Nnew x self.Q - :param slices: specifies which outputs kernel(s) the Xnew correspond to (see below) - :type slices: (None, list of slice objects, list of ints) - :param full_cov: whether to return the folll covariance matrix, or just the diagonal - :type full_cov: bool - :rtype: posterior mean, a Numpy array, Nnew x self.D - :rtype: posterior variance, a Numpy array, Nnew x Nnew x (self.D) - - .. Note:: "slices" specifies how the the points X_new co-vary wich the training points. - - - If None, the new points covary throigh every kernel part (default) - - If a list of slices, the i^th slice specifies which data are affected by the i^th kernel part - - If a list of booleans, specifying which kernel parts are active - - If full_cov and self.D > 1, the return shape of var is Nnew x Nnew x self.D. If self.D == 1, the return shape is Nnew x Nnew. - This is to allow for different normalisations of the output dimensions. - - - """ - - #normalise X values - Xnew = (Xnew.copy() - self._Xmean) / self._Xstd - mu, var, phi = self._raw_predict(Xnew, slices, full_cov) - - #un-normalise - mu = mu*self._Ystd + self._Ymean - if full_cov: - if self.D==1: - var *= np.square(self._Ystd) - else: - var = var[:,:,None] * np.square(self._Ystd) - else: - if self.D==1: - var *= np.square(np.squeeze(self._Ystd)) - else: - var = var[:,None] * np.square(self._Ystd) - - return mu,var,phi - - def _raw_predict(self,_Xnew,slices, full_cov=False): - """Internal helper function for making predictions, does not account for normalisation""" - K_x = self.kern.K(self.X,_Xnew,slices1=self.Xslices,slices2=slices) - aux2 = mdot(self.Bi,self.Sroot_tilde_K,self.v_tilde) - zeta = np.sqrt(self.tau_tilde)*aux2 - f = np.dot(K_x.T,self.v_tilde-zeta) - v = mdot(self.Li,np.sqrt(self.tau_tilde)*K_x) - if full_cov: - Kxx = self.kern.K(_Xnew,slices1=slices,slices2=slices) - var = Kxx - np.dot(v.T,v) - var_diag = np.diag(var)[:,None] - else: - Kxx = self.kern.Kdiag(_Xnew, slices=slices) - var_diag = (Kxx - np.sum(v**2,-2))[:,None] - phi = self.likelihood.predictive_mean(f,var_diag) - return f, var_diag, phi - - def plot(self,samples=0,plot_limits=None,which_data='all',which_functions='all',resolution=None): - """ - :param samples: the number of a posteriori samples to plot - :param which_data: which if the training data to plot (default all) - :type which_data: 'all' or a slice object to slice self.X, self.Y - :param plot_limits: The limits of the plot. If 1D [xmin,xmax], if 2D [[xmin,ymin],[xmax,ymax]]. Defaluts to data limits - :param which_functions: which of the kernel functions to plot (additively) - :type which_functions: list of bools - :param resolution: the number of intervals to sample the GP on. Defaults to 200 in 1D and 50 (a 50x50 grid) in 2D - - Plot the posterior of the GP. - - In one dimension, the function is plotted with a shaded region identifying two standard deviations. - - In two dimsensions, a contour-plot shows the mean predicted function - - In higher dimensions, we've no implemented this yet !TODO! - - Can plot only part of the data and part of the posterior functions using which_data and which_functions - """ - if which_functions=='all': - which_functions = [True]*self.kern.Nparts - if which_data=='all': - which_data = slice(None) - - X = self.X[which_data,:] - Y = self.Y[which_data,:] - - Xorig = X*self._Xstd + self._Xmean - Yorig = Y*self._Ystd + self._Ymean - if plot_limits is None: - xmin,xmax = Xorig.min(0),Xorig.max(0) - xmin, xmax = xmin-0.2*(xmax-xmin), xmax+0.2*(xmax-xmin) - elif len(plot_limits)==2: - xmin, xmax = plot_limits - else: - raise ValueError, "Bad limits for plotting" - - if self.X.shape[1]==1: - Xnew = np.linspace(xmin,xmax,resolution or 200)[:,None] - #m,v,phi = self.predict(Xnew,slices=which_functions) - #gpplot(Xnew,m,v) - mu_f, var_f, phi_f = self.predict(Xnew,slices=which_functions) - pb.subplot(211) - self.likelihood.plot1Da(X=Xnew,mean=mu_f,var=var_f,Z=self.X,mean_Z=self.mu,var_Z=np.diag(self.Sigma)) - if samples: - s = np.random.multivariate_normal(m.flatten(),v,samples) - pb.plot(Xnew.flatten(),s.T, alpha = 0.4, c='#3465a4', linewidth = 0.8) - pb.xlim(xmin,xmax) - pb.subplot(212) - self.likelihood.plot1Db(self.X,Xnew,phi_f) - - elif self.X.shape[1]==2: - resolution = 50 or resolution - xx,yy = np.mgrid[xmin[0]:xmax[0]:1j*resolution,xmin[1]:xmax[1]:1j*resolution] - Xtest = np.vstack((xx.flatten(),yy.flatten())).T - zz,vv = self.predict(Xtest,slices=which_functions) - zz = zz.reshape(resolution,resolution) - pb.contour(xx,yy,zz,vmin=zz.min(),vmax=zz.max(),cmap=pb.cm.jet) - pb.scatter(Xorig[:,0],Xorig[:,1],40,Yorig,linewidth=0,cmap=pb.cm.jet,vmin=zz.min(),vmax=zz.max()) - pb.xlim(xmin[0],xmax[0]) - pb.ylim(xmin[1],xmax[1]) - - else: - raise NotImplementedError, "Cannot plot GPs with more than two input dimensions" diff --git a/GPy/models/__init__.py b/GPy/models/__init__.py index ca44aab1..cc2f62d6 100644 --- a/GPy/models/__init__.py +++ b/GPy/models/__init__.py @@ -6,8 +6,6 @@ from GP_regression import GP_regression from sparse_GP_regression import sparse_GP_regression from GPLVM import GPLVM from warped_GP import warpedGP -from GP_EP import GP_EP -from GP_EP2 import GP_EP2 from generalized_FITC import generalized_FITC from sparse_GPLVM import sparse_GPLVM from uncollapsed_sparse_GP import uncollapsed_sparse_GP diff --git a/GPy/models/generalized_FITC.py b/GPy/models/generalized_FITC.py index a5ed8d0a..57ae2407 100644 --- a/GPy/models/generalized_FITC.py +++ b/GPy/models/generalized_FITC.py @@ -9,7 +9,8 @@ from .. import kern from ..core import model from ..util.linalg import pdinv,mdot from ..util.plot import gpplot -from ..inference.Expectation_Propagation import FITC +#from ..inference.Expectation_Propagation import FITC +from ..inference.EP import FITC from ..inference.likelihoods import likelihood,probit class generalized_FITC(model): From fad0e07624971b0e381db34806b1b27ae7d27fcb Mon Sep 17 00:00:00 2001 From: Ricardo Date: Mon, 28 Jan 2013 00:16:23 +0000 Subject: [PATCH 04/10] Sparse EP --- GPy/examples/ep_fix.py | 5 +- GPy/examples/poisson.py | 50 +++++++ GPy/examples/sparse_ep_fix.py | 76 ++++++++++ GPy/inference/EP.py | 9 +- GPy/models/GP.py | 8 +- GPy/models/__init__.py | 1 + GPy/models/sparse_GP.py | 258 ++++++++++++++++++++++++++++++++++ 7 files changed, 399 insertions(+), 8 deletions(-) create mode 100644 GPy/examples/poisson.py create mode 100644 GPy/examples/sparse_ep_fix.py create mode 100644 GPy/models/sparse_GP.py diff --git a/GPy/examples/ep_fix.py b/GPy/examples/ep_fix.py index 2da94335..9b35b3ff 100644 --- a/GPy/examples/ep_fix.py +++ b/GPy/examples/ep_fix.py @@ -10,6 +10,7 @@ import numpy as np import GPy pb.ion() +pb.close('all') default_seed=10000 model_type='Full' @@ -26,11 +27,13 @@ data = GPy.util.datasets.toy_linear_1d_classification(seed=seed) likelihood = GPy.inference.likelihoods.probit(data['Y'][:, 0:1]) m = GPy.models.GP(data['X'],likelihood=likelihood) +#m = GPy.models.GP(data['X'],Y=likelihood.Y) m.constrain_positive('var') m.constrain_positive('len') m.tie_param('lengthscale') -m.approximate_likelihood() +if not isinstance(m.likelihood,GPy.inference.likelihoods.gaussian): + m.approximate_likelihood() print m.checkgrad() # Optimize and plot m.optimize() diff --git a/GPy/examples/poisson.py b/GPy/examples/poisson.py new file mode 100644 index 00000000..5a1cc6af --- /dev/null +++ b/GPy/examples/poisson.py @@ -0,0 +1,50 @@ +# Copyright (c) 2012, GPy authors (see AUTHORS.txt). +# Licensed under the BSD 3-clause license (see LICENSE.txt) + + +""" +Simple Gaussian Processes classification +""" +import pylab as pb +import numpy as np +import GPy +pb.ion() + +pb.close('all') +default_seed=10000 + +model_type='Full' +inducing=4 +seed=default_seed +"""Simple 1D classification example. +:param model_type: type of model to fit ['Full', 'FITC', 'DTC']. +:param seed : seed value for data generation (default is 4). +:type seed: int +:param inducing : number of inducing variables (only used for 'FITC' or 'DTC'). +:type inducing: int +""" + +X = np.arange(0,100,5)[:,None] +F = np.round(np.sin(X/18.) + .1*X) +E = np.random.randint(-3,3,20)[:,None] +Y = F + E +pb.plot(X,F,'k-') +pb.plot(X,Y,'ro') +pb.figure() +likelihood = GPy.inference.likelihoods.poisson(Y,scale=4.) + +m = GPy.models.GP(X,likelihood=likelihood) +#m = GPy.models.GP(data['X'],Y=likelihood.Y) + +m.constrain_positive('var') +m.constrain_positive('len') +m.tie_param('lengthscale') +if not isinstance(m.likelihood,GPy.inference.likelihoods.gaussian): + m.approximate_likelihood() +print m.checkgrad() +# Optimize and plot +m.optimize() +#m.em(plot_all=False) # EM algorithm +m.plot() + +print(m) diff --git a/GPy/examples/sparse_ep_fix.py b/GPy/examples/sparse_ep_fix.py new file mode 100644 index 00000000..738a82e6 --- /dev/null +++ b/GPy/examples/sparse_ep_fix.py @@ -0,0 +1,76 @@ +# Copyright (c) 2012, GPy authors (see AUTHORS.txt). +# Licensed under the BSD 3-clause license (see LICENSE.txt) + + +import numpy as np +""" +Sparse Gaussian Processes regression with an RBF kernel +""" +import pylab as pb +import numpy as np +import GPy +np.random.seed(2) +pb.ion() +N = 500 +M = 5 + +###################################### +## 1 dimensional example + +# sample inputs and outputs +X = np.random.uniform(-3.,3.,(N,1)) +#Y = np.sin(X)+np.random.randn(N,1)*0.05 +F = np.sin(X)+np.random.randn(N,1)*0.05 +Y = np.ones([F.shape[0],1]) +Y[F<0] = -1 +likelihood = GPy.inference.likelihoods.probit(Y) + +# construct kernel +rbf = GPy.kern.rbf(1) +noise = GPy.kern.white(1) +kernel = rbf + noise + +# create simple GP model +#m1 = GPy.models.sparse_GP_regression(X, Y, kernel, M=M) +m1 = GPy.models.sparse_GP(X, kernel, M=M,likelihood= likelihood) + +# contrain all parameters to be positive +m1.constrain_positive('(variance|lengthscale|precision)') +#m1.constrain_positive('(variance|lengthscale)') +#m1.constrain_fixed('prec',10.) + + +#check gradient FIXME unit test please +m1.checkgrad() +# optimize and plot +m1.optimize('tnc', messages = 1) +m1.plot() +# print(m1) + +###################################### +## 2 dimensional example + +# # sample inputs and outputs +# X = np.random.uniform(-3.,3.,(N,2)) +# Y = np.sin(X[:,0:1]) * np.sin(X[:,1:2])+np.random.randn(N,1)*0.05 + +# # construct kernel +# rbf = GPy.kern.rbf(2) +# noise = GPy.kern.white(2) +# kernel = rbf + noise + +# # create simple GP model +# m2 = GPy.models.sparse_GP_regression(X,Y,kernel, M = 50) +# create simple GP model + +# # contrain all parameters to be positive (but not inducing inputs) +# m2.constrain_positive('(variance|lengthscale|precision)') + +# #check gradient FIXME unit test please +# m2.checkgrad() + +# # optimize and plot +# pb.figure() +# m2.optimize('tnc', messages = 1) +# m2.plot() +# print(m2) diff --git a/GPy/inference/EP.py b/GPy/inference/EP.py index f7c163b1..751d5ca8 100644 --- a/GPy/inference/EP.py +++ b/GPy/inference/EP.py @@ -9,7 +9,7 @@ from ..util.plot import gpplot from .. import kern class EP: - def __init__(self,covariance,likelihood,Kmn=None,Knn_diag=None,epsilon=1e-3,powerep=[1.,1.]): + def __init__(self,covariance,likelihood,Kmn=None,Knn_diag=None,epsilon=1e-3,power_ep=[1.,1.]): """ Expectation Propagation @@ -19,7 +19,7 @@ class EP: likelihood : Output's likelihood (likelihood class) kernel : a GPy kernel (kern class) inducing : Either an array specifying the inducing points location or a sacalar defining their number. None value for using a non-sparse model is used. - powerep : Power-EP parameters (eta,delta) - 2x1 numpy array (floats) + power_ep : Power-EP parameters (eta,delta) - 2x1 numpy array (floats) epsilon : Convergence criterion, maximum squared difference allowed between mean updates to stop iterations (float) """ self.likelihood = likelihood @@ -38,7 +38,7 @@ class EP: assert len(Knn_diag) == self.N, 'Knn_diagonal has size different from N' self.epsilon = epsilon - self.eta, self.delta = powerep + self.eta, self.delta = power_ep self.jitter = 1e-12 """ @@ -110,6 +110,7 @@ class Full(EP): self.Sigma = self.Sigma - Delta_tau/(1.+ Delta_tau*self.Sigma[i,i])*np.dot(si,si.T) self.mu = np.dot(self.Sigma,self.v_tilde) self.iterations += 1 + print self.tau_tilde[i] #TODO erase me #Sigma recomptutation with Cholesky decompositon Sroot_tilde_K = np.sqrt(self.tau_tilde)[:,None]*(self.K) B = np.eye(self.N) + np.sqrt(self.tau_tilde)[None,:]*Sroot_tilde_K @@ -206,6 +207,7 @@ class DTC(EP): epsilon_np2 = sum((self.v_tilde-self.np2[-1])**2)/self.N self.np1.append(self.tau_tilde.copy()) self.np2.append(self.v_tilde.copy()) + return self.tau_tilde[:,None], self.v_tilde[:,None], self.Z_hat[:,None], self.tau_[:,None], self.v_[:,None] class FITC(EP): def fit_EP(self): @@ -306,3 +308,4 @@ class FITC(EP): epsilon_np2 = sum((self.v_tilde-self.np2[-1])**2)/self.N self.np1.append(self.tau_tilde.copy()) self.np2.append(self.v_tilde.copy()) + return self.tau_tilde[:,None], self.v_tilde[:,None], self.Z_hat[:,None], self.tau_[:,None], self.v_[:,None] diff --git a/GPy/models/GP.py b/GPy/models/GP.py index 4a8d23e9..ccfe95c7 100644 --- a/GPy/models/GP.py +++ b/GPy/models/GP.py @@ -29,8 +29,8 @@ class GP(model): """ - def __init__(self,X,Y=None,kernel=None,normalize_X=False,normalize_Y=False, Xslices=None,likelihood=None,epsilon_ep=1e-3,epsion_em=.1,powerep=[1.,1.]): - #TODO: specify beta parameter explicitely + def __init__(self,X,Y=None,kernel=None,normalize_X=False,normalize_Y=False, Xslices=None,likelihood=None,epsilon_ep=1e-3,epsion_em=.1,power_ep=[1.,1.]): + #TODO: make beta parameter explicit # parse arguments self.Xslices = Xslices @@ -87,7 +87,7 @@ class GP(model): else: # Y is defined after approximating the likelihood self.EP = True - self.eta,self.delta = powerep + self.eta,self.delta = power_ep self.epsilon_ep = epsilon_ep self.tau_tilde = np.ones([self.N,self.D]) self.v_tilde = np.zeros([self.N,self.D]) @@ -116,7 +116,7 @@ class GP(model): def approximate_likelihood(self): assert not isinstance(self.likelihood, gaussian), "EP is only available for non-gaussian likelihoods" - self.ep_approx = Full(self.K,self.likelihood,epsilon=self.epsilon_ep,powerep=[self.eta,self.delta]) + self.ep_approx = Full(self.K,self.likelihood,epsilon=self.epsilon_ep,power_ep=[self.eta,self.delta]) self.tau_tilde, self.v_tilde, self.Z_hat, self.tau_, self.v_=self.ep_approx.fit_EP() # Y: EP likelihood is defined as a regression model for mu_tilde self.Y = self.v_tilde/self.tau_tilde diff --git a/GPy/models/__init__.py b/GPy/models/__init__.py index cc2f62d6..a839f827 100644 --- a/GPy/models/__init__.py +++ b/GPy/models/__init__.py @@ -10,3 +10,4 @@ from generalized_FITC import generalized_FITC from sparse_GPLVM import sparse_GPLVM from uncollapsed_sparse_GP import uncollapsed_sparse_GP from GP import GP +from sparse_GP import sparse_GP diff --git a/GPy/models/sparse_GP.py b/GPy/models/sparse_GP.py new file mode 100644 index 00000000..1164a1af --- /dev/null +++ b/GPy/models/sparse_GP.py @@ -0,0 +1,258 @@ +# 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 +from ..util.linalg import mdot, jitchol, chol_inv, pdinv +from ..util.plot import gpplot +from .. import kern +from GP import GP +from ..inference.EP import Full +from ..inference.likelihoods import likelihood,probit,poisson,gaussian + +#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): + """ + Variational sparse GP model (Regression) + + :param X: inputs + :type X: np.ndarray (N x Q) + :param Y: observed data + :type Y: np.ndarray of observations (N x D) + :param kernel : the kernel/covariance function. See link kernels + :type kernel: a GPy kernel + :param Z: inducing inputs (optional, see note) + :type Z: np.ndarray (M x Q) | None + :param X_uncertainty: The uncertainty in the measurements of X (Gaussian variance) + :type X_uncertainty: np.ndarray (N x Q) | None + :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 beta: noise precision. TODO> ignore beta if doing EP + :type beta: float + :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,Y,kernel=None, X_uncertainty=None, beta=100., Z=None,Zslices=None,M=10,normalize_X=False,normalize_Y=False,likelihood=None,method_ep='DTC',epsilon_ep=1e-3,epsilon_em=.1,power_ep=[1.,1.]): + + if Z is None: + self.Z = np.random.permutation(X.copy())[:M] + self.M = M + else: + assert Z.shape[1]==X.shape[1] + self.Z = Z + self.M = Z.shape[0] + if X_uncertainty is None: + self.has_uncertain_inputs=False + else: + assert X_uncertainty.shape==X.shape + self.has_uncertain_inputs=True + self.X_uncertainty = X_uncertainty + + + self.beta = beta #FIXME + GP.__init__(self, X, Y, kernel=kernel, normalize_X=normalize_X, normalize_Y=normalize_Y,likelihood=likelihood,epsilon_ep=epsilon_ep,epsion_em=epsilon_em,power_ep=power_ep) + self.beta = beta if isinstance(likelihood,gaussian) else self.tau_tilde #TODO this should be defined in GP.__init__ + + + #normalise X uncertainty also + if self.has_uncertain_inputs: + self.X_uncertainty /= np.square(self._Xstd) + + def _set_params(self, p): + if not self.EP: + self.Z = p[:self.M*self.Q].reshape(self.M, self.Q) + self.beta = p[self.M*self.Q] + self.kern._set_params(p[self.Z.size + 1:]) + self.beta2 = self.beta**2 + self._compute_kernel_matrices() + self._computations() + else: + self.Z = p[:self.M*self.Q].reshape(self.M, self.Q) + self.kern._set_params(p[self.Z.size:]) + #self._compute_kernel_matrices() this is replaced by _ep_covariance + self._ep_covariance() + self._ep_computations() + + def approximate_likelihood(self): + assert not isinstance(self.likelihood, gaussian), "EP is only available for non-gaussian likelihoods" + if self.ep_proxy == 'DTC': + self.ep_approx = DTC(self.Kmm,self.likelihood,self.psi1,epsilon=self.epsilon_ep,power_ep=[self.eta,self.delta]) + elif self.ep_proxy == 'FITC': + self.Knn_diag = self.kern.psi0(self.Z,self.X, self.X_uncertainty) #TODO psi0 already calculates this + self.ep_approx = FITC(self.Kmm,self.likelihood,self.psi1,self.Knn_diag,epsilon=self.epsilon_ep,power_ep=[self.eta,self.delta]) + else: + self.ep_approx = Full(self.X,self.likelihood,self.kernel,inducing=None,epsilon=self.epsilon_ep,power_ep=[self.eta,self.delta]) + self.beta, self.v_tilde, self.Z_hat, self.tau_, self.v_=self.ep_approx.fit_EP() + # Y: EP likelihood is defined as a regression model for mu_tilde + self.Y = self.v_tilde/self.beta + self._Ymean = np.zeros((1,self.Y.shape[1])) + self._Ystd = np.ones((1,self.Y.shape[1])) + self.trbetaYYT = np.sum(self.beta*np.square(self.Y)) + if self.D > self.N: + # then it's more efficient to store YYT + self.YYT = np.dot(self.Y, self.Y.T) + else: + self.YYT = None + self.mu_ = self.v_/self.tau_ + self._ep_covariance() + self._computations() + + def _ep_covariance(self): + self.Kmm = self.kern.K(self.Z) + if self.has_uncertain_inputs: + self.psi0 = self.kern.psi0(self.Z,self.X, self.X_uncertainty).sum() + self.psi1 = self.kern.psi1(self.Z,self.X, self.X_uncertainty).T + self.psi2 = self.kern.psi2(self.Z,self.X, self.X_uncertainty) #FIXME include beta + else: + #self.psi0 = self.kern.Kdiag(self.X,slices=self.Xslices).sum() + self.Knn_diag = self.kern.Kdiag(self.X,slices=self.Xslices) + self.psi0 = (self.beta*self.Knn_diag).sum() #TODO check dimensions + self.psi1 = self.kern.K(self.Z,self.X) + #self.psi2 = np.dot(self.psi1,self.psi1.T) + self.psi2 = np.dot(self.psi1,self.beta*self.psi1.T) + + def _compute_kernel_matrices(self): + # kernel computations, using BGPLVM notation + #TODO: slices for psi statistics (easy enough) + + self.Kmm = self.kern.K(self.Z) + if self.has_uncertain_inputs: + self.psi0 = self.kern.psi0(self.Z,self.X, self.X_uncertainty).sum() + self.psi1 = self.kern.psi1(self.Z,self.X, self.X_uncertainty).T + self.psi2 = self.kern.psi2(self.Z,self.X, self.X_uncertainty) + else: + self.psi0 = self.kern.Kdiag(self.X,slices=self.Xslices).sum() + self.psi1 = self.kern.K(self.Z,self.X) + self.psi2 = np.dot(self.psi1,self.psi1.T) + + def _ep_computations(self): + # TODO find routine to multiply triangular matrices + self.V = self.beta*self.Y + self.psi1V = np.dot(self.psi1, self.V) + self.psi1VVpsi1 = np.dot(self.psi1V, self.psi1V.T) + self.Kmmi, self.Lm, self.Lmi, self.Kmm_logdet = pdinv(self.Kmm) + #self.A = mdot(self.Lmi, self.beta*self.psi2, self.Lmi.T) + self.A = mdot(self.Lmi, self.psi2, self.Lmi.T) + self.B = np.eye(self.M) + self.A + self.Bi, self.LB, self.LBi, self.B_logdet = pdinv(self.B) + self.LLambdai = np.dot(self.LBi, self.Lmi) + #self.trace_K = self.psi0 - np.sum(np.dot(self.Lmi,self.psi1)**2,-1) #TODO check + self.trace_K = self.psi0 - np.trace(self.A) + self.LBL_inv = mdot(self.Lmi.T, self.Bi, self.Lmi) + self.C = mdot(self.LLambdai, self.psi1V) + self.G = mdot(self.LBL_inv, self.psi1VVpsi1, self.LBL_inv.T) + + # Compute dL_dpsi + #self.dL_dpsi0 = - 0.5 * self.D * self.beta * np.ones(self.N) + self.dL_dpsi0 = - 0.5 * self.D * self.beta.flatten() * np.ones(self.N) #TODO check + self.dL_dpsi1 = mdot(self.LLambdai.T,self.C,self.V.T) + #self.dL_dpsi2 = - 0.5 * self.beta * (self.D*(self.LBL_inv - self.Kmmi) + self.G) + self.dL_dpsi2 = - 0.5 * self.beta * (self.D*(self.LBL_inv - self.Kmmi) + self.G) + + # Compute dL_dKmm + self.dL_dKmm = -0.5 * self.D * mdot(self.Lmi.T, self.A, self.Lmi) # dB + self.dL_dKmm += -0.5 * self.D * (- self.LBL_inv - 2.*self.beta*mdot(self.LBL_inv, self.psi2, self.Kmmi) + self.Kmmi) # dC + self.dL_dKmm += np.dot(np.dot(self.G,self.beta*self.psi2) - np.dot(self.LBL_inv, self.psi1VVpsi1), self.Kmmi) + 0.5*self.G # dE + + def _get_params(self): + if not self.EP: + return np.hstack([self.Z.flatten(),self.beta,self.kern._get_params_transformed()]) + else: + return np.hstack([self.Z.flatten(),self.kern._get_params_transformed()]) + + def _get_param_names(self): + if not self.EP: + return sum([['iip_%i_%i'%(i,j) for i in range(self.Z.shape[0])] for j in range(self.Z.shape[1])],[]) + ['noise_precision']+self.kern._get_param_names_transformed() + else: + return sum([['iip_%i_%i'%(i,j) for i in range(self.Z.shape[0])] for j in range(self.Z.shape[1])],[]) + self.kern._get_param_names_transformed() + + def log_likelihood(self): + """ + Compute the (lower bound on the) log marginal likelihood + """ + beta_logdet = self.N*self.D*np.log(self.beta) if not self.EP else self.D*np.sum(np.log(self.beta)) + A = -0.5*self.N*self.D*(np.log(2.*np.pi)) - 0.5*beta_logdet + B = -0.5*self.beta*self.D*self.trace_K if not self.EP else -0.5*self.D*self.trace_K + C = -0.5*self.D * self.B_logdet + D = -0.5*self.beta*self.trYYT if not self.EP else -0.5*self.trbetaYYT + E = +0.5*np.sum(self.psi1VVpsi1 * self.LBL_inv) + return A+B+C+D+E + + def dL_dbeta(self): + """ + Compute the gradient of the log likelihood wrt beta. + """ + #TODO: suport heteroscedatic noise + dA_dbeta = 0.5 * self.N*self.D/self.beta + dB_dbeta = - 0.5 * self.D * self.trace_K + dC_dbeta = - 0.5 * self.D * np.sum(self.Bi*self.A)/self.beta + dD_dbeta = - 0.5 * self.trYYT + tmp = mdot(self.LBi.T, self.LLambdai, self.psi1V) + dE_dbeta = (np.sum(np.square(self.C)) - 0.5 * np.sum(self.A * np.dot(tmp, tmp.T)))/self.beta + + return np.squeeze(dA_dbeta + dB_dbeta + dC_dbeta + dD_dbeta + dE_dbeta) + + 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_uncertainty) + dL_dtheta += self.kern.dpsi1_dtheta(self.dL_dpsi1.T,self.Z,self.X, self.X_uncertainty) + dL_dtheta += self.kern.dpsi2_dtheta(self.dL_dpsi2,self.Z,self.X, self.X_uncertainty) # for multiple_beta, dL_dpsi2 will be a different shape + else: + #re-cast computations in psi2 back to psi1: + dL_dpsi1 = self.dL_dpsi1 + 2.*np.dot(self.dL_dpsi2,self.psi1) + dL_dtheta += self.kern.dK_dtheta(dL_dpsi1,self.Z,self.X) + 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 + """ + dL_dZ = 2.*self.kern.dK_dX(self.dL_dKmm,self.Z,)#factor of two becase of vertical and horizontal 'stripes' in dKmm_dZ + if self.has_uncertain_inputs: + dL_dZ += self.kern.dpsi1_dZ(self.dL_dpsi1.T,self.Z,self.X, self.X_uncertainty) + dL_dZ += self.kern.dpsi2_dZ(self.dL_dpsi2,self.Z,self.X, self.X_uncertainty) + else: + #re-cast computations in psi2 back to psi1: + dL_dpsi1 = self.dL_dpsi1 + 2.*np.dot(self.dL_dpsi2,self.psi1) + dL_dZ += self.kern.dK_dX(dL_dpsi1,self.Z,self.X) + return dL_dZ + + def _log_likelihood_gradients(self): + return np.hstack([self.dL_dZ().flatten(), self.dL_dbeta(), self.dL_dtheta()]) + + def _raw_predict(self, Xnew, slices, full_cov=False): + """Internal helper function for making predictions, does not account for normalisation""" + Kx = self.kern.K(self.Z, Xnew) + mu = mdot(Kx.T, self.LBL_inv, self.psi1V) + if full_cov: + noise_term = np.eye(Xnew.shape[0])/self.beta if not self.EP else 0 + Kxx = self.kern.K(Xnew) + var = Kxx - mdot(Kx.T, (self.Kmmi - self.LBL_inv), Kx) + noise_term + else: + noise_term = 1./self.beta if not self.EP else 0 + Kxx = self.kern.Kdiag(Xnew) + var = Kxx - np.sum(Kx*np.dot(self.Kmmi - self.LBL_inv, Kx),0) + noise_term + return mu,var + + def plot(self, *args, **kwargs): + """ + Plot the fitted model: just call the GP_regression plot function and then add inducing inputs + """ + GP_regression.plot(self,*args,**kwargs) + if self.Q==1: + pb.plot(self.Z,self.Z*0+pb.ylim()[0],'k|',mew=1.5,markersize=12) + if self.has_uncertain_inputs: + pb.errorbar(self.X[:,0], pb.ylim()[0]+np.zeros(self.N), xerr=2*np.sqrt(self.X_uncertainty.flatten())) + if self.Q==2: + pb.plot(self.Z[:,0],self.Z[:,1],'wo') From 29ec128c9d6620b20989c9bdb27de95c098927ef Mon Sep 17 00:00:00 2001 From: Ricardo Andrade Date: Mon, 28 Jan 2013 17:47:08 +0000 Subject: [PATCH 05/10] Other changes. --- GPy/examples/ep_fix.py | 12 ++-- GPy/examples/poisson.py | 2 +- GPy/examples/sparse_ep_fix.py | 34 +-------- GPy/inference/EP.py | 9 ++- GPy/inference/likelihoods.py | 32 ++++++++- GPy/models/GP.py | 92 +++++++++++-------------- GPy/models/sparse_GP.py | 126 +++++++++++++++++++++------------- 7 files changed, 164 insertions(+), 143 deletions(-) diff --git a/GPy/examples/ep_fix.py b/GPy/examples/ep_fix.py index 9b35b3ff..c4e025dd 100644 --- a/GPy/examples/ep_fix.py +++ b/GPy/examples/ep_fix.py @@ -11,11 +11,9 @@ import GPy pb.ion() pb.close('all') -default_seed=10000 model_type='Full' inducing=4 -seed=default_seed """Simple 1D classification example. :param model_type: type of model to fit ['Full', 'FITC', 'DTC']. :param seed : seed value for data generation (default is 4). @@ -23,21 +21,19 @@ seed=default_seed :param inducing : number of inducing variables (only used for 'FITC' or 'DTC'). :type inducing: int """ -data = GPy.util.datasets.toy_linear_1d_classification(seed=seed) +data = GPy.util.datasets.toy_linear_1d_classification(seed=0) likelihood = GPy.inference.likelihoods.probit(data['Y'][:, 0:1]) m = GPy.models.GP(data['X'],likelihood=likelihood) -#m = GPy.models.GP(data['X'],Y=likelihood.Y) +#m = GPy.models.GP(data['X'],likelihood.Y) -m.constrain_positive('var') -m.constrain_positive('len') -m.tie_param('lengthscale') +m.ensure_default_constraints() if not isinstance(m.likelihood,GPy.inference.likelihoods.gaussian): m.approximate_likelihood() print m.checkgrad() # Optimize and plot m.optimize() #m.em(plot_all=False) # EM algorithm -m.plot() +m.plot(samples=3) print(m) diff --git a/GPy/examples/poisson.py b/GPy/examples/poisson.py index 5a1cc6af..71d80b30 100644 --- a/GPy/examples/poisson.py +++ b/GPy/examples/poisson.py @@ -31,7 +31,7 @@ Y = F + E pb.plot(X,F,'k-') pb.plot(X,Y,'ro') pb.figure() -likelihood = GPy.inference.likelihoods.poisson(Y,scale=4.) +likelihood = GPy.inference.likelihoods.poisson(Y,scale=6.) m = GPy.models.GP(X,likelihood=likelihood) #m = GPy.models.GP(data['X'],Y=likelihood.Y) diff --git a/GPy/examples/sparse_ep_fix.py b/GPy/examples/sparse_ep_fix.py index 738a82e6..7e3f1fc3 100644 --- a/GPy/examples/sparse_ep_fix.py +++ b/GPy/examples/sparse_ep_fix.py @@ -31,46 +31,18 @@ noise = GPy.kern.white(1) kernel = rbf + noise # create simple GP model -#m1 = GPy.models.sparse_GP_regression(X, Y, kernel, M=M) -m1 = GPy.models.sparse_GP(X, kernel, M=M,likelihood= likelihood) +#m1 = GPy.models.sparse_GP(X, Y, kernel, M=M) +m1 = GPy.models.sparse_GP(X,Y=None, kernel=kernel, M=M,likelihood= likelihood) +print m1.checkgrad() # contrain all parameters to be positive m1.constrain_positive('(variance|lengthscale|precision)') #m1.constrain_positive('(variance|lengthscale)') #m1.constrain_fixed('prec',10.) - #check gradient FIXME unit test please -m1.checkgrad() # optimize and plot m1.optimize('tnc', messages = 1) m1.plot() # print(m1) -###################################### -## 2 dimensional example - -# # sample inputs and outputs -# X = np.random.uniform(-3.,3.,(N,2)) -# Y = np.sin(X[:,0:1]) * np.sin(X[:,1:2])+np.random.randn(N,1)*0.05 - -# # construct kernel -# rbf = GPy.kern.rbf(2) -# noise = GPy.kern.white(2) -# kernel = rbf + noise - -# # create simple GP model -# m2 = GPy.models.sparse_GP_regression(X,Y,kernel, M = 50) -# create simple GP model - -# # contrain all parameters to be positive (but not inducing inputs) -# m2.constrain_positive('(variance|lengthscale|precision)') - -# #check gradient FIXME unit test please -# m2.checkgrad() - -# # optimize and plot -# pb.figure() -# m2.optimize('tnc', messages = 1) -# m2.plot() -# print(m2) diff --git a/GPy/inference/EP.py b/GPy/inference/EP.py index 751d5ca8..5d571888 100644 --- a/GPy/inference/EP.py +++ b/GPy/inference/EP.py @@ -110,7 +110,6 @@ class Full(EP): self.Sigma = self.Sigma - Delta_tau/(1.+ Delta_tau*self.Sigma[i,i])*np.dot(si,si.T) self.mu = np.dot(self.Sigma,self.v_tilde) self.iterations += 1 - print self.tau_tilde[i] #TODO erase me #Sigma recomptutation with Cholesky decompositon Sroot_tilde_K = np.sqrt(self.tau_tilde)[:,None]*(self.K) B = np.eye(self.N) + np.sqrt(self.tau_tilde)[None,:]*Sroot_tilde_K @@ -122,7 +121,13 @@ class Full(EP): epsilon_np2 = sum((self.v_tilde-self.np2[-1])**2)/self.N self.np1.append(self.tau_tilde.copy()) self.np2.append(self.v_tilde.copy()) - return self.tau_tilde[:,None], self.v_tilde[:,None], self.Z_hat[:,None], self.tau_[:,None], self.v_[:,None] + + #Variables to be called from GP + mu_tilde = self.v_tilde/self.tau_tilde #When calling EP, this variable is used instead of Y in the GP model + sigma_sum = 1./self.tau_ + 1./self.tau_tilde + mu_diff_2 = (self.v_/self.tau_ - mu_tilde)**2 + Z_ep = np.sum(np.log(self.Z_hat)) + 0.5*np.sum(np.log(sigma_sum)) + 0.5*np.sum(mu_diff_2/sigma_sum) #Normalization constant + return self.tau_tilde[:,None], mu_tilde[:,None], Z_ep class DTC(EP): def fit_EP(self): diff --git a/GPy/inference/likelihoods.py b/GPy/inference/likelihoods.py index 7f5d9140..864afa57 100644 --- a/GPy/inference/likelihoods.py +++ b/GPy/inference/likelihoods.py @@ -21,6 +21,27 @@ class likelihood: self.location = location self.scale = scale + def plot1D(self,X,mean,var,Z=None,mean_Z=None,var_Z=None,samples=0): + """ + Plot the predictive distribution of the GP model for 1-dimensional inputs + + :param X: The points at which to make a prediction + :param Mean: mean values at X + :param Var: variance values at X + :param Z: Set of points to be highlighted in the plot, i.e. inducing points + :param mean_Z: mean values at Z + :param var_Z: variance values at Z + :samples: Number of samples to plot + """ + assert X.shape[1] == 1, 'Number of dimensions must be 1' + gpplot(X,mean,var.flatten()) + if samples: #NOTE why don't we put samples as a parameter of gpplot + s = np.random.multivariate_normal(mean.flatten(),np.diag(var),samples) + pb.plot(X.flatten(),s.T, alpha = 0.4, c='#3465a4', linewidth = 0.8) + #pb.subplot(211) + #self.plot1Da(X,mean,var,Z,mean_Z,var_Z) + + def plot1Da(self,X,mean,var,Z=None,mean_Z=None,var_Z=None): """ Plot the predictive distribution of the GP model for 1-dimensional inputs @@ -37,6 +58,7 @@ class likelihood: pb.errorbar(Z.flatten(),mean_Z.flatten(),2*np.sqrt(var_Z.flatten()),fmt='r+') pb.plot(Z,mean_Z,'ro') + """ def plot1Db(self,X_obs,X,phi,Z=None): assert X_obs.shape[1] == 1, 'Number of dimensions must be 1' gpplot(X,phi,np.zeros(X.shape[0])) @@ -45,6 +67,7 @@ class likelihood: if Z is not None: pb.plot(Z,Z*0+.5,'r|',mew=1.5,markersize=12) + """ def plot2D(self,X,X_new,F_new,U=None): """ Predictive distribution of the fitted GP model for 2-dimensional inputs @@ -98,7 +121,6 @@ class probit(likelihood): sigma2_hat = 1./tau_i - (phi/((tau_i**2+tau_i)*Z_hat))*(z+phi/Z_hat) return Z_hat, mu_hat, sigma2_hat - def predictive_mean(self,mu,var): mu = mu.flatten() var = var.flatten() @@ -107,6 +129,14 @@ class probit(likelihood): def _log_likelihood_gradients(): raise NotImplementedError + def plot(self,X,phi,X_obs,Z=None): + assert X_obs.shape[1] == 1, 'Number of dimensions must be 1' + gpplot(X,phi,np.zeros(X.shape[0])) + pb.plot(X_obs,(self.Y+1)/2,'kx',mew=1.5) + if Z is not None: + pb.plot(Z,Z*0+.5,'r|',mew=1.5,markersize=12) + pb.ylim(-0.2,1.2) + class poisson(likelihood): """ Poisson likelihood diff --git a/GPy/models/GP.py b/GPy/models/GP.py index ccfe95c7..3a9f6de8 100644 --- a/GPy/models/GP.py +++ b/GPy/models/GP.py @@ -24,13 +24,18 @@ class GP(model): :type normalize_Y: False|True :param Xslices: how the X,Y data co-vary in the kernel (i.e. which "outputs" they correspond to). See (link:slicing) :rtype: model object + :parm likelihood: a GPy likelihood, defaults to gaussian + :param epsilon_ep: convergence criterion for the Expectation Propagation algorithm, defaults to 0.1 + :param powerep: power-EP parameters [$\eta$,$\delta$], defaults to [1.,1.] + :type powerep: list .. Note:: Multiple independent outputs are allowed using columns of Y """ + #TODO: make beta parameter explicit + #TODO: when using EP, predict needs to return 3 values otherwise it just needs 2. At the moment predict returns 3 values in any case. - def __init__(self,X,Y=None,kernel=None,normalize_X=False,normalize_Y=False, Xslices=None,likelihood=None,epsilon_ep=1e-3,epsion_em=.1,power_ep=[1.,1.]): - #TODO: make beta parameter explicit + def __init__(self,X,Y=None,kernel=None,normalize_X=False,normalize_Y=False, Xslices=None,likelihood=None,epsilon_ep=1e-3,epsilon_em=.1,power_ep=[1.,1.]): # parse arguments self.Xslices = Xslices @@ -54,7 +59,6 @@ class GP(model): self._Xmean = np.zeros((1,self.X.shape[1])) self._Xstd = np.ones((1,self.X.shape[1])) - # Y - likelihood related variables, these might change whether using EP or not if likelihood is None: assert Y is not None, "Either Y or likelihood must be defined" @@ -68,8 +72,9 @@ class GP(model): if isinstance(self.likelihood,gaussian): self.EP = False self.Y = Y + self.beta = 100.#FIXME beta should be an explicit parameter for this model - #here's some simple normalisation + # Here's some simple normalisation if normalize_Y: self._Ymean = Y.mean(0)[None,:] self._Ystd = Y.std(0)[None,:] @@ -89,50 +94,43 @@ class GP(model): self.EP = True self.eta,self.delta = power_ep self.epsilon_ep = epsilon_ep - self.tau_tilde = np.ones([self.N,self.D]) - self.v_tilde = np.zeros([self.N,self.D]) - self.tau_ = np.ones([self.N,self.D]) - self.v_ = np.zeros([self.N,self.D]) - self.Z_hat = np.ones([self.N,self.D]) + self.beta = np.ones([self.N,self.D]) + self.Z_ep = 0 + self.Y = None + self._Ymean = np.zeros((1,self.D)) + self._Ystd = np.ones((1,self.D)) model.__init__(self) def _set_params(self,p): - # TODO: remove beta when using EP + # TODO: add beta when not using EP self.kern._set_params_transformed(p) - if not self.EP: - self.K = self.kern.K(self.X,slices1=self.Xslices) - self.Ki, self.L, self.Li, self.K_logdet = pdinv(self.K) - else: - self._ep_covariance() + self.K = self.kern.K(self.X,slices1=self.Xslices) + if self.EP: + self.K += np.diag(1./self.beta.flatten()) + #else: + # self.beta = p[-1] + self.Ki, self.L, self.Li, self.K_logdet = pdinv(self.K) def _get_params(self): - # TODO: remove beta when using EP + # TODO: add beta when not using EP return self.kern._get_params_transformed() def _get_param_names(self): - # TODO: remove beta when using EP + # TODO: add beta when not using EP return self.kern._get_param_names_transformed() def approximate_likelihood(self): assert not isinstance(self.likelihood, gaussian), "EP is only available for non-gaussian likelihoods" - self.ep_approx = Full(self.K,self.likelihood,epsilon=self.epsilon_ep,power_ep=[self.eta,self.delta]) - self.tau_tilde, self.v_tilde, self.Z_hat, self.tau_, self.v_=self.ep_approx.fit_EP() - # Y: EP likelihood is defined as a regression model for mu_tilde - self.Y = self.v_tilde/self.tau_tilde - self._Ymean = np.zeros((1,self.Y.shape[1])) - self._Ystd = np.ones((1,self.Y.shape[1])) + self.ep_approx = Full(self.K,self.likelihood,epsilon = self.epsilon_ep,power_ep=[self.eta,self.delta]) + self.beta, self.Y, self.Z_ep = self.ep_approx.fit_EP() if self.D > self.N: # then it's more efficient to store YYT self.YYT = np.dot(self.Y, self.Y.T) else: self.YYT = None - self.mu_ = self.v_/self.tau_ - self._ep_covariance() - - def _ep_covariance(self): # Kernel plus noise variance term - self.K = self.kern.K(self.X,slices1=self.Xslices) + np.diag(1./self.tau_tilde.flatten()) + self.K = self.kern.K(self.X,slices1=self.Xslices) + np.diag(1./self.beta.flatten()) self.Ki, self.L, self.Li, self.K_logdet = pdinv(self.K) def _model_fit_term(self): @@ -144,25 +142,16 @@ class GP(model): else: return -0.5*np.sum(np.multiply(self.Ki, self.YYT)) - def _normalization_term(self): - """ - Computes the marginal likelihood normalization constants - """ - sigma_sum = 1./self.tau_ + 1./self.tau_tilde - mu_diff_2 = (self.mu_ - self.Y)**2 - penalty_term = np.sum(np.log(self.Z_hat)) - return penalty_term + 0.5*np.sum(np.log(sigma_sum)) + 0.5*np.sum(mu_diff_2/sigma_sum) - def log_likelihood(self): """ The log marginal likelihood for an EP model can be written as the log likelihood of a regression model for a new variable Y* = v_tilde/tau_tilde, with a covariance matrix K* = K + diag(1./tau_tilde) plus a normalization term. """ - complexity_term = -0.5*self.D*self.Kplus_logdet - normalization_term = 0 if self.EP == False else self.normalization_term() - return complexity_term + normalization_term + self._model_fit_term() - + L = -0.5*selff.D*self.K_logdet + self.model_fit_term() + if self.EP: + L += self.normalisation_term() + return L def log_likelihood(self): complexity_term = -0.5*self.N*self.D*np.log(2.*np.pi) - 0.5*self.D*self.K_logdet @@ -174,7 +163,6 @@ class GP(model): dL_dK = 0.5*(np.dot(alpha,alpha.T)-self.D*self.Ki) else: dL_dK = 0.5*(mdot(self.Ki, self.YYT, self.Ki) - self.D*self.Ki) - return dL_dK def _log_likelihood_gradients(self): @@ -267,7 +255,7 @@ class GP(model): Y = self.Y[which_data,:] Xorig = X*self._Xstd + self._Xmean - Yorig = Y*self._Ystd + self._Ymean if not self.EP else self.likelihood.Y + Yorig = Y*self._Ystd + self._Ymean #NOTE For EP this is v_tilde/beta if plot_limits is None: xmin,xmax = Xorig.min(0),Xorig.max(0) @@ -282,19 +270,17 @@ class GP(model): m,v,phi = self.predict(Xnew,slices=which_functions) if self.EP: pb.subplot(211) - gpplot(Xnew,m,v) - if samples: - s = np.random.multivariate_normal(m.flatten(),v,samples) - pb.plot(Xnew.flatten(),s.T, alpha = 0.4, c='#3465a4', linewidth = 0.8) - if not self.EP: - pb.plot(Xorig,Yorig,'kx',mew=1.5) - pb.xlim(xmin,xmax) - else: - pb.xlim(xmin,xmax) + if samples: #NOTE why don't we put samples as a parameter of gpplot + s = np.random.multivariate_normal(m.flatten(),np.diag(v),samples) + pb.plot(Xnew.flatten(),s.T, alpha = 0.4, c='#3465a4', linewidth = 0.8) + pb.plot(Xorig,Yorig,'kx',mew=1.5) + pb.xlim(xmin,xmax) + + if self.EP: pb.subplot(212) - self.likelihood.plot1Db(self.X,Xnew,phi) + self.likelihood.plot(Xnew,phi,self.X) pb.xlim(xmin,xmax) elif self.X.shape[1]==2: diff --git a/GPy/models/sparse_GP.py b/GPy/models/sparse_GP.py index 1164a1af..655f6252 100644 --- a/GPy/models/sparse_GP.py +++ b/GPy/models/sparse_GP.py @@ -37,7 +37,7 @@ class sparse_GP(GP): :type normalize_(X|Y): bool """ - def __init__(self,X,Y,kernel=None, X_uncertainty=None, beta=100., Z=None,Zslices=None,M=10,normalize_X=False,normalize_Y=False,likelihood=None,method_ep='DTC',epsilon_ep=1e-3,epsilon_em=.1,power_ep=[1.,1.]): + def __init__(self,X,Y=None,kernel=None, X_uncertainty=None, beta=100., Z=None,Zslices=None,M=10,normalize_X=False,normalize_Y=False,likelihood=None,method_ep='DTC',epsilon_ep=1e-3,epsilon_em=.1,power_ep=[1.,1.]): if Z is None: self.Z = np.random.permutation(X.copy())[:M] @@ -53,10 +53,8 @@ class sparse_GP(GP): self.has_uncertain_inputs=True self.X_uncertainty = X_uncertainty - - self.beta = beta #FIXME - GP.__init__(self, X, Y, kernel=kernel, normalize_X=normalize_X, normalize_Y=normalize_Y,likelihood=likelihood,epsilon_ep=epsilon_ep,epsion_em=epsilon_em,power_ep=power_ep) - self.beta = beta if isinstance(likelihood,gaussian) else self.tau_tilde #TODO this should be defined in GP.__init__ + GP.__init__(self, X=X, Y=Y, kernel=kernel, normalize_X=normalize_X, normalize_Y=normalize_Y,likelihood=likelihood,epsilon_ep=epsilon_ep,epsilon_em=epsilon_em,power_ep=power_ep) + self.trYYT = np.sum(np.square(self.Y)) if not self.EP else None #normalise X uncertainty also @@ -74,10 +72,55 @@ class sparse_GP(GP): else: self.Z = p[:self.M*self.Q].reshape(self.M, self.Q) self.kern._set_params(p[self.Z.size:]) - #self._compute_kernel_matrices() this is replaced by _ep_covariance - self._ep_covariance() + #self._compute_kernel_matrices() this is replaced by _ep_kernel_matrices + self._ep_kernel_matrices() self._ep_computations() + def _compute_kernel_matrices(self): + # kernel computations, using BGPLVM notation + #TODO: slices for psi statistics (easy enough) + + self.Kmm = self.kern.K(self.Z) + if self.has_uncertain_inputs: + if self.hetero_noise: + raise NotImplementedError, "uncertain ips and het noise not yet supported" + else: + self.psi0 = self.kern.psi0(self.Z,self.X, self.X_uncertainty).sum() + self.psi1 = self.kern.psi1(self.Z,self.X, self.X_uncertainty).T + self.psi2 = self.kern.psi2(self.Z,self.X, self.X_uncertainty) + else: + if self.hetero_noise: + print "rick's stuff here" + else: + self.psi0 = self.kern.Kdiag(self.X,slices=self.Xslices).sum() + self.psi1 = self.kern.K(self.Z,self.X) + self.psi2 = np.dot(self.psi1,self.psi1.T) + + def _computations(self): + # TODO find routine to multiply triangular matrices + self.V = self.beta*self.Y + self.psi1V = np.dot(self.psi1, self.V) + self.psi1VVpsi1 = np.dot(self.psi1V, self.psi1V.T) + self.Kmmi, self.Lm, self.Lmi, self.Kmm_logdet = pdinv(self.Kmm) + self.A = mdot(self.Lmi, self.beta*self.psi2, self.Lmi.T) + self.B = np.eye(self.M) + self.A + self.Bi, self.LB, self.LBi, self.B_logdet = pdinv(self.B) + self.LLambdai = np.dot(self.LBi, self.Lmi) + self.trace_K = self.psi0 - np.trace(self.A)/self.beta + self.LBL_inv = mdot(self.Lmi.T, self.Bi, self.Lmi) + self.C = mdot(self.LLambdai, self.psi1V) + self.G = mdot(self.LBL_inv, self.psi1VVpsi1, self.LBL_inv.T) + + # Compute dL_dpsi + self.dL_dpsi0 = - 0.5 * self.D * self.beta * np.ones(self.N) + self.dL_dpsi1 = mdot(self.LLambdai.T,self.C,self.V.T) + self.dL_dpsi2 = - 0.5 * self.beta * (self.D*(self.LBL_inv - self.Kmmi) + self.G) + + # Compute dL_dKmm + self.dL_dKmm = -0.5 * self.D * mdot(self.Lmi.T, self.A, self.Lmi) # dB + self.dL_dKmm += -0.5 * self.D * (- self.LBL_inv - 2.*self.beta*mdot(self.LBL_inv, self.psi2, self.Kmmi) + self.Kmmi) # dC + self.dL_dKmm += np.dot(np.dot(self.G,self.beta*self.psi2) - np.dot(self.LBL_inv, self.psi1VVpsi1), self.Kmmi) + 0.5*self.G # dE + def approximate_likelihood(self): assert not isinstance(self.likelihood, gaussian), "EP is only available for non-gaussian likelihoods" if self.ep_proxy == 'DTC': @@ -88,6 +131,22 @@ class sparse_GP(GP): else: self.ep_approx = Full(self.X,self.likelihood,self.kernel,inducing=None,epsilon=self.epsilon_ep,power_ep=[self.eta,self.delta]) self.beta, self.v_tilde, self.Z_hat, self.tau_, self.v_=self.ep_approx.fit_EP() + self._ep_kernel_matrices() + self._computations() + + def _ep_kernel_matrices(self): + self.Kmm = self.kern.K(self.Z) + if self.has_uncertain_inputs: + self.psi0 = self.kern.psi0(self.Z,self.X, self.X_uncertainty).sum() + self.psi1 = self.kern.psi1(self.Z,self.X, self.X_uncertainty).T + self.psi2 = self.kern.psi2(self.Z,self.X, self.X_uncertainty) #FIXME include beta + else: + self.psi0 = self.kern.Kdiag(self.X,slices=self.Xslices) + self.psi1 = self.kern.K(self.Z,self.X) + self.psi2 = np.dot(self.psi1,self.psi1.T) + self.psi2_beta_scaled = np.dot(self.psi1,self.beta*self.psi1.T) + + def _ep_computations(self): # Y: EP likelihood is defined as a regression model for mu_tilde self.Y = self.v_tilde/self.beta self._Ymean = np.zeros((1,self.Y.shape[1])) @@ -99,50 +158,17 @@ class sparse_GP(GP): else: self.YYT = None self.mu_ = self.v_/self.tau_ - self._ep_covariance() - self._computations() - - def _ep_covariance(self): - self.Kmm = self.kern.K(self.Z) - if self.has_uncertain_inputs: - self.psi0 = self.kern.psi0(self.Z,self.X, self.X_uncertainty).sum() - self.psi1 = self.kern.psi1(self.Z,self.X, self.X_uncertainty).T - self.psi2 = self.kern.psi2(self.Z,self.X, self.X_uncertainty) #FIXME include beta - else: - #self.psi0 = self.kern.Kdiag(self.X,slices=self.Xslices).sum() - self.Knn_diag = self.kern.Kdiag(self.X,slices=self.Xslices) - self.psi0 = (self.beta*self.Knn_diag).sum() #TODO check dimensions - self.psi1 = self.kern.K(self.Z,self.X) - #self.psi2 = np.dot(self.psi1,self.psi1.T) - self.psi2 = np.dot(self.psi1,self.beta*self.psi1.T) - - def _compute_kernel_matrices(self): - # kernel computations, using BGPLVM notation - #TODO: slices for psi statistics (easy enough) - - self.Kmm = self.kern.K(self.Z) - if self.has_uncertain_inputs: - self.psi0 = self.kern.psi0(self.Z,self.X, self.X_uncertainty).sum() - self.psi1 = self.kern.psi1(self.Z,self.X, self.X_uncertainty).T - self.psi2 = self.kern.psi2(self.Z,self.X, self.X_uncertainty) - else: - self.psi0 = self.kern.Kdiag(self.X,slices=self.Xslices).sum() - self.psi1 = self.kern.K(self.Z,self.X) - self.psi2 = np.dot(self.psi1,self.psi1.T) - - def _ep_computations(self): # TODO find routine to multiply triangular matrices self.V = self.beta*self.Y self.psi1V = np.dot(self.psi1, self.V) self.psi1VVpsi1 = np.dot(self.psi1V, self.psi1V.T) self.Kmmi, self.Lm, self.Lmi, self.Kmm_logdet = pdinv(self.Kmm) #self.A = mdot(self.Lmi, self.beta*self.psi2, self.Lmi.T) - self.A = mdot(self.Lmi, self.psi2, self.Lmi.T) + self.A = mdot(self.Lmi, self.psi2_beta_scaled, self.Lmi.T) self.B = np.eye(self.M) + self.A self.Bi, self.LB, self.LBi, self.B_logdet = pdinv(self.B) self.LLambdai = np.dot(self.LBi, self.Lmi) - #self.trace_K = self.psi0 - np.sum(np.dot(self.Lmi,self.psi1)**2,-1) #TODO check - self.trace_K = self.psi0 - np.trace(self.A) + self.trace_K = self.psi0.sum() - np.trace(self.A) self.LBL_inv = mdot(self.Lmi.T, self.Bi, self.Lmi) self.C = mdot(self.LLambdai, self.psi1V) self.G = mdot(self.LBL_inv, self.psi1VVpsi1, self.LBL_inv.T) @@ -176,10 +202,15 @@ class sparse_GP(GP): Compute the (lower bound on the) log marginal likelihood """ beta_logdet = self.N*self.D*np.log(self.beta) if not self.EP else self.D*np.sum(np.log(self.beta)) - A = -0.5*self.N*self.D*(np.log(2.*np.pi)) - 0.5*beta_logdet - B = -0.5*self.beta*self.D*self.trace_K if not self.EP else -0.5*self.D*self.trace_K + if self.hetero_noise: + A = foo + B = bar + D = -0.5*self.trbetaYYT + else: + A = -0.5*self.N*self.D*(np.log(2.*np.pi)) - 0.5*beta_logdet + B = -0.5*self.beta*self.D*self.trace_K if not self.EP else -0.5*self.D*self.trace_K + D = -0.5*self.beta*self.trYYT C = -0.5*self.D * self.B_logdet - D = -0.5*self.beta*self.trYYT if not self.EP else -0.5*self.trbetaYYT E = +0.5*np.sum(self.psi1VVpsi1 * self.LBL_inv) return A+B+C+D+E @@ -243,13 +274,14 @@ class sparse_GP(GP): noise_term = 1./self.beta if not self.EP else 0 Kxx = self.kern.Kdiag(Xnew) var = Kxx - np.sum(Kx*np.dot(self.Kmmi - self.LBL_inv, Kx),0) + noise_term - return mu,var + return mu,var,None#TODO add phi for EP def plot(self, *args, **kwargs): """ Plot the fitted model: just call the GP_regression plot function and then add inducing inputs """ - GP_regression.plot(self,*args,**kwargs) + #GP_regression.plot(self,*args,**kwargs) + GP.plot(self,*args,**kwargs) if self.Q==1: pb.plot(self.Z,self.Z*0+pb.ylim()[0],'k|',mew=1.5,markersize=12) if self.has_uncertain_inputs: From 7737cecf6db40188ceaf626e2287d380c6705e0e Mon Sep 17 00:00:00 2001 From: Ricardo Andrade Date: Mon, 28 Jan 2013 18:01:55 +0000 Subject: [PATCH 06/10] EM algorithm --- GPy/examples/ep_fix.py | 1 + GPy/models/GP.py | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/GPy/examples/ep_fix.py b/GPy/examples/ep_fix.py index c4e025dd..49ebd5aa 100644 --- a/GPy/examples/ep_fix.py +++ b/GPy/examples/ep_fix.py @@ -35,5 +35,6 @@ print m.checkgrad() m.optimize() #m.em(plot_all=False) # EM algorithm m.plot(samples=3) +m.EM() print(m) diff --git a/GPy/models/GP.py b/GPy/models/GP.py index 3a9f6de8..51da0490 100644 --- a/GPy/models/GP.py +++ b/GPy/models/GP.py @@ -229,6 +229,33 @@ class GP(model): phi = None if not self.EP else self.likelihood.predictive_mean(mu,var) return mu, var, phi + def EM(self,max_f_eval=20,epsilon=.1,plot_all=False): #TODO check this makes sense + """ + Fits sparse_EP and optimizes the hyperparametes iteratively until convergence is achieved. + """ + self.epsilon_em = epsilon + log_likelihood_change = self.epsilon_em + 1. + self.parameters_path = [self._get_params()] + self.approximate_likelihood() + self.site_approximations_path = [[self.ep_approx.tau_tilde,self.ep_approx.v_tilde]] + self.log_likelihood_path = [self.log_likelihood()] + iteration = 0 + while log_likelihood_change > self.epsilon_em: + print 'EM iteration', iteration + self.optimize(max_f_eval = max_f_eval) + log_likelihood_new = self.log_likelihood() + log_likelihood_change = log_likelihood_new - self.log_likelihood_path[-1] + if log_likelihood_change < 0: + print 'log_likelihood decrement' + self._set_params(self.parameters_path[-1]) + self.kern._set_params(self.parameters_path[-1]) + else: + self.approximate_likelihood() + self.log_likelihood_path.append(self.log_likelihood()) + self.parameters_path.append(self._get_params()) + self.site_approximations_path.append([self.ep_approx.tau_tilde,self.ep_approx.v_tilde]) + iteration += 1 + def plot(self,samples=0,plot_limits=None,which_data='all',which_functions='all',resolution=None): """ :param samples: the number of a posteriori samples to plot From d9a3226f4989c15ccb1f23b3daf6c76db5c46b8e Mon Sep 17 00:00:00 2001 From: Ricardo Andrade Date: Tue, 29 Jan 2013 12:06:34 +0000 Subject: [PATCH 07/10] EM algorithm for EP. --- GPy/core/model.py | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/GPy/core/model.py b/GPy/core/model.py index 4a1791bd..b6b280a1 100644 --- a/GPy/core/model.py +++ b/GPy/core/model.py @@ -381,6 +381,43 @@ class model(parameterised): print grad_string print '' - return False return True + + def EM(self,epsilon=.1,**kwargs): + """ + Expectation maximization for Expectation Propagation. + + kwargs are passed to the optimize function. They can be: + + :epsilon: convergence criterion + :max_f_eval: maximum number of function evaluations + :messages: whether to display during optimisation + :param optimzer: whice optimizer to use (defaults to self.preferred optimizer) + :type optimzer: string TODO: valid strings? + + """ + assert self.EP, "EM not available for gaussian likelihood" + log_change = epsilon + 1. + self.log_likelihood_record = [] + self.gp_params_record = [] + self.ep_params_record = [] + iteration = 0 + last_value = -np.exp(1000) + while log_change > epsilon or not iteration: + print 'EM iteration %s' %iteration + self.approximate_likelihood() + self.optimize(**kwargs) + new_value = self.log_likelihood() + log_change = new_value - last_value + if log_change > epsilon: + self.log_likelihood_record.append(new_value) + self.gp_params_record.append(self._get_params()) + self.ep_params_record.append((self.beta,self.Y,self.Z_ep)) + last_value = new_value + else: + convergence = False + self.beta, self.Y, self.Z_ep = self.ep_params_record[-1] + self._set_params(self.gp_params_record[-1]) + print "Log-likelihood decrement: %s \nLast iteration discarded." %log_change + iteration += 1 From 691aeeaf22ca28f28190af3ce8ba02d0d0205e94 Mon Sep 17 00:00:00 2001 From: Ricardo Andrade Date: Tue, 29 Jan 2013 12:07:19 +0000 Subject: [PATCH 08/10] GP model works now. --- GPy/models/GP.py | 36 +++++------------------------------- 1 file changed, 5 insertions(+), 31 deletions(-) diff --git a/GPy/models/GP.py b/GPy/models/GP.py index 51da0490..95145978 100644 --- a/GPy/models/GP.py +++ b/GPy/models/GP.py @@ -13,7 +13,7 @@ from ..inference.likelihoods import likelihood,probit,poisson,gaussian class GP(model): """ - Gaussian Process model for regression + Gaussian Process model for regression and EP :param X: input observations :param Y: observed values @@ -35,7 +35,7 @@ class GP(model): #TODO: make beta parameter explicit #TODO: when using EP, predict needs to return 3 values otherwise it just needs 2. At the moment predict returns 3 values in any case. - def __init__(self,X,Y=None,kernel=None,normalize_X=False,normalize_Y=False, Xslices=None,likelihood=None,epsilon_ep=1e-3,epsilon_em=.1,power_ep=[1.,1.]): + def __init__(self,X,Y=None,kernel=None,normalize_X=False,normalize_Y=False, Xslices=None,likelihood=None,epsilon_ep=1e-3,power_ep=[1.,1.]): # parse arguments self.Xslices = Xslices @@ -121,6 +121,9 @@ class GP(model): return self.kern._get_param_names_transformed() def approximate_likelihood(self): + """ + Approximates a non-gaussian likelihood using Expectation Propagation + """ assert not isinstance(self.likelihood, gaussian), "EP is only available for non-gaussian likelihoods" self.ep_approx = Full(self.K,self.likelihood,epsilon = self.epsilon_ep,power_ep=[self.eta,self.delta]) self.beta, self.Y, self.Z_ep = self.ep_approx.fit_EP() @@ -170,7 +173,6 @@ class GP(model): def predict(self,Xnew, slices=None, full_cov=False): """ - Predict the function(s) at the new point(s) Xnew. Arguments @@ -193,7 +195,6 @@ class GP(model): If full_cov and self.D > 1, the return shape of var is Nnew x Nnew x self.D. If self.D == 1, the return shape is Nnew x Nnew. This is to allow for different normalisations of the output dimensions. - """ #normalise X values @@ -229,33 +230,6 @@ class GP(model): phi = None if not self.EP else self.likelihood.predictive_mean(mu,var) return mu, var, phi - def EM(self,max_f_eval=20,epsilon=.1,plot_all=False): #TODO check this makes sense - """ - Fits sparse_EP and optimizes the hyperparametes iteratively until convergence is achieved. - """ - self.epsilon_em = epsilon - log_likelihood_change = self.epsilon_em + 1. - self.parameters_path = [self._get_params()] - self.approximate_likelihood() - self.site_approximations_path = [[self.ep_approx.tau_tilde,self.ep_approx.v_tilde]] - self.log_likelihood_path = [self.log_likelihood()] - iteration = 0 - while log_likelihood_change > self.epsilon_em: - print 'EM iteration', iteration - self.optimize(max_f_eval = max_f_eval) - log_likelihood_new = self.log_likelihood() - log_likelihood_change = log_likelihood_new - self.log_likelihood_path[-1] - if log_likelihood_change < 0: - print 'log_likelihood decrement' - self._set_params(self.parameters_path[-1]) - self.kern._set_params(self.parameters_path[-1]) - else: - self.approximate_likelihood() - self.log_likelihood_path.append(self.log_likelihood()) - self.parameters_path.append(self._get_params()) - self.site_approximations_path.append([self.ep_approx.tau_tilde,self.ep_approx.v_tilde]) - iteration += 1 - def plot(self,samples=0,plot_limits=None,which_data='all',which_functions='all',resolution=None): """ :param samples: the number of a posteriori samples to plot From 9972862ea22164a89e05b1667a45cbadf8d780e9 Mon Sep 17 00:00:00 2001 From: Ricardo Andrade Date: Tue, 29 Jan 2013 12:08:50 +0000 Subject: [PATCH 09/10] Test file. --- GPy/examples/ep_fix.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/GPy/examples/ep_fix.py b/GPy/examples/ep_fix.py index 49ebd5aa..1d7b4741 100644 --- a/GPy/examples/ep_fix.py +++ b/GPy/examples/ep_fix.py @@ -26,15 +26,14 @@ likelihood = GPy.inference.likelihoods.probit(data['Y'][:, 0:1]) m = GPy.models.GP(data['X'],likelihood=likelihood) #m = GPy.models.GP(data['X'],likelihood.Y) - m.ensure_default_constraints() + +# Optimize and plot if not isinstance(m.likelihood,GPy.inference.likelihoods.gaussian): m.approximate_likelihood() -print m.checkgrad() -# Optimize and plot -m.optimize() -#m.em(plot_all=False) # EM algorithm -m.plot(samples=3) +#m.optimize() m.EM() +print m.log_likelihood() +m.plot(samples=3) print(m) From 01f0378f840821fdac8acc0652be213ef77a536f Mon Sep 17 00:00:00 2001 From: Ricardo Andrade Date: Tue, 29 Jan 2013 12:23:49 +0000 Subject: [PATCH 10/10] Other change. --- GPy/examples/ep_fix.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/GPy/examples/ep_fix.py b/GPy/examples/ep_fix.py index 1d7b4741..8041cc91 100644 --- a/GPy/examples/ep_fix.py +++ b/GPy/examples/ep_fix.py @@ -29,8 +29,8 @@ m = GPy.models.GP(data['X'],likelihood=likelihood) m.ensure_default_constraints() # Optimize and plot -if not isinstance(m.likelihood,GPy.inference.likelihoods.gaussian): - m.approximate_likelihood() +#if not isinstance(m.likelihood,GPy.inference.likelihoods.gaussian): +# m.approximate_likelihood() #m.optimize() m.EM()