2012-11-29 16:39:20 +00:00
|
|
|
# Copyright (c) 2012, GPy authors (see AUTHORS.txt).
|
|
|
|
|
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
|
|
|
|
|
|
|
|
|
|
2012-11-29 16:32:48 +00:00
|
|
|
import numpy as np
|
|
|
|
|
import pylab as pb
|
|
|
|
|
import sys, pdb
|
|
|
|
|
from .. import kern
|
|
|
|
|
from ..core import model
|
|
|
|
|
from ..util.linalg import pdinv, PCA
|
2013-02-04 16:15:54 +00:00
|
|
|
from GP import GP
|
|
|
|
|
from ..likelihoods import Gaussian
|
2012-11-29 16:32:48 +00:00
|
|
|
|
2013-02-04 16:15:54 +00:00
|
|
|
class GPLVM(GP):
|
2012-11-29 16:32:48 +00:00
|
|
|
"""
|
|
|
|
|
Gaussian Process Latent Variable Model
|
|
|
|
|
|
|
|
|
|
:param Y: observed data
|
|
|
|
|
:type Y: np.ndarray
|
|
|
|
|
:param Q: latent dimensionality
|
|
|
|
|
:type Q: int
|
|
|
|
|
:param init: initialisation method for the latent space
|
|
|
|
|
:type init: 'PCA'|'random'
|
|
|
|
|
|
|
|
|
|
"""
|
2013-02-04 16:15:54 +00:00
|
|
|
def __init__(self, Y, Q, init='PCA', X = None, kernel=None, **kwargs):
|
2012-12-07 15:16:52 +00:00
|
|
|
if X is None:
|
|
|
|
|
X = self.initialise_latent(init, Q, Y)
|
2013-02-04 16:15:54 +00:00
|
|
|
if kernel is None:
|
|
|
|
|
kernel = kern.rbf(Q) + kern.bias(Q)
|
|
|
|
|
likelihood = Gaussian(Y)
|
|
|
|
|
GP.__init__(self, X, likelihood, kernel, **kwargs)
|
2012-11-29 16:32:48 +00:00
|
|
|
|
|
|
|
|
def initialise_latent(self, init, Q, Y):
|
|
|
|
|
if init == 'PCA':
|
|
|
|
|
return PCA(Y, Q)[0]
|
|
|
|
|
else:
|
|
|
|
|
return np.random.randn(Y.shape[0], Q)
|
|
|
|
|
|
2013-01-18 12:31:37 +00:00
|
|
|
def _get_param_names(self):
|
2013-02-19 14:36:55 +00:00
|
|
|
return sum([['X_%i_%i'%(n,q) for q in range(self.Q)] for n in range(self.N)],[]) + GP._get_param_names(self)
|
2012-11-29 16:32:48 +00:00
|
|
|
|
2013-01-18 12:31:37 +00:00
|
|
|
def _get_params(self):
|
2013-02-04 16:15:54 +00:00
|
|
|
return np.hstack((self.X.flatten(), GP._get_params(self)))
|
2012-11-29 16:32:48 +00:00
|
|
|
|
2013-01-18 12:31:37 +00:00
|
|
|
def _set_params(self,x):
|
2012-11-29 16:32:48 +00:00
|
|
|
self.X = x[:self.X.size].reshape(self.N,self.Q).copy()
|
2013-02-04 16:15:54 +00:00
|
|
|
GP._set_params(self, x[self.X.size:])
|
2012-11-29 16:32:48 +00:00
|
|
|
|
2013-01-18 13:47:37 +00:00
|
|
|
def _log_likelihood_gradients(self):
|
2013-02-04 16:15:54 +00:00
|
|
|
dL_dX = 2.*self.kern.dK_dX(self.dL_dK,self.X)
|
2012-11-29 16:32:48 +00:00
|
|
|
|
2013-02-04 16:15:54 +00:00
|
|
|
return np.hstack((dL_dX.flatten(),GP._log_likelihood_gradients(self)))
|
2012-11-29 16:32:48 +00:00
|
|
|
|
|
|
|
|
def plot(self):
|
|
|
|
|
assert self.Y.shape[1]==2
|
2013-01-18 17:47:50 +00:00
|
|
|
pb.scatter(self.Y[:,0],self.Y[:,1],40,self.X[:,0].copy(),linewidth=0,cmap=pb.cm.jet)
|
2012-11-29 16:32:48 +00:00
|
|
|
Xnew = np.linspace(self.X.min(),self.X.max(),200)[:,None]
|
|
|
|
|
mu, var = self.predict(Xnew)
|
|
|
|
|
pb.plot(mu[:,0], mu[:,1],'k',linewidth=1.5)
|
|
|
|
|
|
|
|
|
|
def plot_latent(self):
|
|
|
|
|
raise NotImplementedError
|