GPy/GPy/models/gplvm.py

84 lines
3 KiB
Python
Raw Normal View History

2013-06-25 17:43:42 +01:00
# ## Copyright (c) 2012, GPy authors (see AUTHORS.txt).
2013-06-05 14:11:49 +01:00
# Licensed under the BSD 3-clause license (see LICENSE.txt)
import numpy as np
from .. import kern
2014-01-24 15:48:45 +00:00
from ..core import GP, Param
2013-06-05 14:11:49 +01:00
from ..likelihoods import Gaussian
from .. import util
class GPLVM(GP):
"""
Gaussian Process Latent Variable Model
"""
2013-12-07 18:45:24 +00:00
def __init__(self, Y, input_dim, init='PCA', X=None, kernel=None, normalize_Y=False, name="gplvm"):
2014-01-24 15:56:27 +00:00
"""
:param Y: observed data
:type Y: np.ndarray
:param input_dim: latent dimensionality
:type input_dim: int
:param init: initialisation method for the latent space
:type init: 'PCA'|'random'
"""
2013-06-05 14:11:49 +01:00
if X is None:
2014-02-24 14:47:43 +00:00
from ..util.initialization import initialize_latent
2014-03-24 11:16:57 +00:00
X, fracs = initialize_latent(init, input_dim, Y)
else:
fracs = np.ones(input_dim)
2013-06-05 14:11:49 +01:00
if kernel is None:
2014-03-24 11:16:57 +00:00
kernel = kern.RBF(input_dim, lengthscale=fracs, ARD=input_dim > 1) + kern.Bias(input_dim, np.exp(-2))
2014-01-24 15:56:27 +00:00
likelihood = Gaussian()
super(GPLVM, self).__init__(X, Y, kernel, likelihood, name='GPLVM')
2014-02-24 14:47:43 +00:00
self.X = Param('latent_mean', X)
self.link_parameter(self.X, index=0)
2013-06-05 14:11:49 +01:00
2014-01-24 15:56:27 +00:00
def parameters_changed(self):
2014-02-24 14:47:43 +00:00
super(GPLVM, self).parameters_changed()
2014-03-13 09:07:56 +00:00
self.X.gradient = self.kern.gradients_X(self.grad_dict['dL_dK'], self.X, None)
2014-01-24 15:56:27 +00:00
def jacobian(self,X):
2014-08-13 10:36:54 +01:00
J = np.zeros((X.shape[0],X.shape[1],self.output_dim))
for i in range(self.output_dim):
2014-08-13 10:36:54 +01:00
J[:,:,i] = self.kern.gradients_X(self.posterior.woodbury_vector[:,i:i+1], X, self.X)
return J
2014-01-24 15:56:27 +00:00
def magnification(self,X):
target=np.zeros(X.shape[0])
#J = np.zeros((X.shape[0],X.shape[1],self.output_dim))
J = self.jacobian(X)
for i in range(X.shape[0]):
2014-09-12 11:51:51 +01:00
target[i]=np.sqrt(np.linalg.det(np.dot(J[i,:,:],np.transpose(J[i,:,:]))))
return target
2013-06-05 14:11:49 +01:00
def plot(self):
2013-06-25 17:43:42 +01:00
assert self.likelihood.Y.shape[1] == 2
pb.scatter(self.likelihood.Y[:, 0], self.likelihood.Y[:, 1], 40, self.X[:, 0].copy(), linewidth=0, cmap=pb.cm.jet) # @UndefinedVariable
2013-06-25 17:43:42 +01:00
Xnew = np.linspace(self.X.min(), self.X.max(), 200)[:, None]
2014-03-20 16:20:39 +00:00
mu, _ = self.predict(Xnew)
2014-09-12 11:51:51 +01:00
import pylab as pb
2013-06-25 17:43:42 +01:00
pb.plot(mu[:, 0], mu[:, 1], 'k', linewidth=1.5)
2013-06-05 14:11:49 +01:00
2014-03-20 16:20:39 +00:00
def plot_latent(self, labels=None, which_indices=None,
resolution=50, ax=None, marker='o', s=40,
fignum=None, legend=True,
plot_limits=None,
aspect='auto', updates=False, **kwargs):
import sys
assert "matplotlib" in sys.modules, "matplotlib package has not been imported."
2014-02-24 14:47:43 +00:00
from ..plotting.matplot_dep import dim_reduction_plots
2014-03-20 16:20:39 +00:00
return dim_reduction_plots.plot_latent(self, labels, which_indices,
resolution, ax, marker, s,
fignum, False, legend,
plot_limits, aspect, updates, **kwargs)
def plot_magnification(self, *args, **kwargs):
return util.plot_latent.plot_magnification(self, *args, **kwargs)