mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-06-05 14:55:15 +02:00
Merge branch 'devel' of github.com:SheffieldML/GPy into devel
This commit is contained in:
commit
1b253ba685
11 changed files with 266 additions and 165 deletions
|
|
@ -14,6 +14,7 @@ import itertools
|
|||
from matplotlib.colors import colorConverter
|
||||
from matplotlib.figure import SubplotParams
|
||||
from GPy.inference.optimization import SCG
|
||||
from GPy.util import plot_latent
|
||||
|
||||
class Bayesian_GPLVM(sparse_GP, GPLVM):
|
||||
"""
|
||||
|
|
@ -37,6 +38,7 @@ class Bayesian_GPLVM(sparse_GP, GPLVM):
|
|||
|
||||
if X == None:
|
||||
X = self.initialise_latent(init, Q, likelihood.Y)
|
||||
self.init = init
|
||||
|
||||
if X_variance is None:
|
||||
X_variance = np.clip((np.ones_like(X) * 0.5) + .01 * np.random.randn(*X.shape), 0.001, 1)
|
||||
|
|
@ -177,18 +179,8 @@ class Bayesian_GPLVM(sparse_GP, GPLVM):
|
|||
self.dbound_dZtheta = sparse_GP._log_likelihood_gradients(self)
|
||||
return np.hstack((self.dbound_dmuS.flatten(), self.dbound_dZtheta))
|
||||
|
||||
def plot_latent(self, which_indices=None, *args, **kwargs):
|
||||
|
||||
if which_indices is None:
|
||||
try:
|
||||
input_1, input_2 = np.argsort(self.input_sensitivity())[:2]
|
||||
except:
|
||||
raise ValueError, "cannot Atomatically determine which dimensions to plot, please pass 'which_indices'"
|
||||
else:
|
||||
input_1, input_2 = which_indices
|
||||
ax = GPLVM.plot_latent(self, which_indices=[input_1, input_2], *args, **kwargs)
|
||||
ax.plot(self.Z[:, input_1], self.Z[:, input_2], '^w')
|
||||
return ax
|
||||
def plot_latent(self, *args, **kwargs):
|
||||
plot_latent.plot_latent_indices(self, *args, **kwargs)
|
||||
|
||||
def do_test_latents(self, Y):
|
||||
"""
|
||||
|
|
@ -200,21 +192,21 @@ class Bayesian_GPLVM(sparse_GP, GPLVM):
|
|||
assert not self.likelihood.is_heteroscedastic
|
||||
N_test = Y.shape[0]
|
||||
Q = self.Z.shape[1]
|
||||
means = np.zeros((N_test,Q))
|
||||
covars = np.zeros((N_test,Q))
|
||||
means = np.zeros((N_test, Q))
|
||||
covars = np.zeros((N_test, Q))
|
||||
|
||||
dpsi0 = - 0.5 * self.D * self.likelihood.precision
|
||||
dpsi2 = self.dL_dpsi2[0][None,:,:] # TODO: this may change if we ignore het. likelihoods
|
||||
V = self.likelihood.precision*Y
|
||||
dpsi1 = np.dot(self.Cpsi1V,V.T)
|
||||
dpsi0 = -0.5 * self.D * self.likelihood.precision
|
||||
dpsi2 = self.dL_dpsi2[0][None, :, :] # TODO: this may change if we ignore het. likelihoods
|
||||
V = self.likelihood.precision * Y
|
||||
dpsi1 = np.dot(self.Cpsi1V, V.T)
|
||||
|
||||
start = np.zeros(self.Q*2)
|
||||
start = np.zeros(self.Q * 2)
|
||||
|
||||
for n,dpsi1_n in enumerate(dpsi1.T[:,:,None]):
|
||||
args = (self.kern,self.Z,dpsi0,dpsi1_n,dpsi2)
|
||||
xopt,fopt,neval,status = SCG(f=latent_cost, gradf=latent_grad, x=start, optargs=args, display = False)
|
||||
for n, dpsi1_n in enumerate(dpsi1.T[:, :, None]):
|
||||
args = (self.kern, self.Z, dpsi0, dpsi1_n, dpsi2)
|
||||
xopt, fopt, neval, status = SCG(f=latent_cost, gradf=latent_grad, x=start, optargs=args, display=False)
|
||||
|
||||
mu,log_S = xopt.reshape(2,1,-1)
|
||||
mu, log_S = xopt.reshape(2, 1, -1)
|
||||
means[n] = mu[0].copy()
|
||||
covars[n] = np.exp(log_S[0]).copy()
|
||||
|
||||
|
|
@ -262,6 +254,14 @@ class Bayesian_GPLVM(sparse_GP, GPLVM):
|
|||
fig.tight_layout(h_pad=.01) # , rect=(0, 0, 1, .95))
|
||||
return fig
|
||||
|
||||
def __getstate__(self):
|
||||
return (self.likelihood, self.Q, self.X, self.X_variance,
|
||||
self.init, self.M, self.Z, self.kern,
|
||||
self.oldpsave, self._debug)
|
||||
|
||||
def __setstate__(self, state):
|
||||
self.__init__(*state)
|
||||
|
||||
def _debug_filter_params(self, x):
|
||||
start, end = 0, self.X.size,
|
||||
X = x[start:end].reshape(self.N, self.Q)
|
||||
|
|
@ -523,59 +523,59 @@ class Bayesian_GPLVM(sparse_GP, GPLVM):
|
|||
|
||||
|
||||
|
||||
def latent_cost_and_grad(mu_S, kern,Z, dL_dpsi0, dL_dpsi1, dL_dpsi2):
|
||||
def latent_cost_and_grad(mu_S, kern, Z, dL_dpsi0, dL_dpsi1, dL_dpsi2):
|
||||
"""
|
||||
objective function for fitting the latent variables for test points
|
||||
(negative log-likelihood: should be minimised!)
|
||||
"""
|
||||
mu,log_S = mu_S.reshape(2,1,-1)
|
||||
mu, log_S = mu_S.reshape(2, 1, -1)
|
||||
S = np.exp(log_S)
|
||||
|
||||
psi0 = kern.psi0(Z,mu,S)
|
||||
psi1 = kern.psi1(Z,mu,S)
|
||||
psi2 = kern.psi2(Z,mu,S)
|
||||
psi0 = kern.psi0(Z, mu, S)
|
||||
psi1 = kern.psi1(Z, mu, S)
|
||||
psi2 = kern.psi2(Z, mu, S)
|
||||
|
||||
lik = dL_dpsi0*psi0 + np.dot(dL_dpsi1.flatten(),psi1.flatten()) + np.dot(dL_dpsi2.flatten(),psi2.flatten()) - 0.5*np.sum(np.square(mu) + S) + 0.5*np.sum(log_S)
|
||||
lik = dL_dpsi0 * psi0 + np.dot(dL_dpsi1.flatten(), psi1.flatten()) + np.dot(dL_dpsi2.flatten(), psi2.flatten()) - 0.5 * np.sum(np.square(mu) + S) + 0.5 * np.sum(log_S)
|
||||
|
||||
mu0, S0 = kern.dpsi0_dmuS(dL_dpsi0,Z,mu,S)
|
||||
mu1, S1 = kern.dpsi1_dmuS(dL_dpsi1,Z,mu,S)
|
||||
mu2, S2 = kern.dpsi2_dmuS(dL_dpsi2,Z,mu,S)
|
||||
mu0, S0 = kern.dpsi0_dmuS(dL_dpsi0, Z, mu, S)
|
||||
mu1, S1 = kern.dpsi1_dmuS(dL_dpsi1, Z, mu, S)
|
||||
mu2, S2 = kern.dpsi2_dmuS(dL_dpsi2, Z, mu, S)
|
||||
|
||||
dmu = mu0 + mu1 + mu2 - mu
|
||||
#dS = S0 + S1 + S2 -0.5 + .5/S
|
||||
dlnS = S*(S0 + S1 + S2 -0.5) + .5
|
||||
return -lik,-np.hstack((dmu.flatten(),dlnS.flatten()))
|
||||
# dS = S0 + S1 + S2 -0.5 + .5/S
|
||||
dlnS = S * (S0 + S1 + S2 - 0.5) + .5
|
||||
return -lik, -np.hstack((dmu.flatten(), dlnS.flatten()))
|
||||
|
||||
def latent_cost(mu_S, kern,Z, dL_dpsi0, dL_dpsi1, dL_dpsi2):
|
||||
def latent_cost(mu_S, kern, Z, dL_dpsi0, dL_dpsi1, dL_dpsi2):
|
||||
"""
|
||||
objective function for fitting the latent variables (negative log-likelihood: should be minimised!)
|
||||
This is the same as latent_cost_and_grad but only for the objective
|
||||
"""
|
||||
mu,log_S = mu_S.reshape(2,1,-1)
|
||||
mu, log_S = mu_S.reshape(2, 1, -1)
|
||||
S = np.exp(log_S)
|
||||
|
||||
psi0 = kern.psi0(Z,mu,S)
|
||||
psi1 = kern.psi1(Z,mu,S)
|
||||
psi2 = kern.psi2(Z,mu,S)
|
||||
psi0 = kern.psi0(Z, mu, S)
|
||||
psi1 = kern.psi1(Z, mu, S)
|
||||
psi2 = kern.psi2(Z, mu, S)
|
||||
|
||||
lik = dL_dpsi0*psi0 + np.dot(dL_dpsi1.flatten(),psi1.flatten()) + np.dot(dL_dpsi2.flatten(),psi2.flatten()) - 0.5*np.sum(np.square(mu) + S) + 0.5*np.sum(log_S)
|
||||
lik = dL_dpsi0 * psi0 + np.dot(dL_dpsi1.flatten(), psi1.flatten()) + np.dot(dL_dpsi2.flatten(), psi2.flatten()) - 0.5 * np.sum(np.square(mu) + S) + 0.5 * np.sum(log_S)
|
||||
return -float(lik)
|
||||
|
||||
def latent_grad(mu_S, kern,Z, dL_dpsi0, dL_dpsi1, dL_dpsi2):
|
||||
def latent_grad(mu_S, kern, Z, dL_dpsi0, dL_dpsi1, dL_dpsi2):
|
||||
"""
|
||||
This is the same as latent_cost_and_grad but only for the grad
|
||||
"""
|
||||
mu,log_S = mu_S.reshape(2,1,-1)
|
||||
mu, log_S = mu_S.reshape(2, 1, -1)
|
||||
S = np.exp(log_S)
|
||||
|
||||
mu0, S0 = kern.dpsi0_dmuS(dL_dpsi0,Z,mu,S)
|
||||
mu1, S1 = kern.dpsi1_dmuS(dL_dpsi1,Z,mu,S)
|
||||
mu2, S2 = kern.dpsi2_dmuS(dL_dpsi2,Z,mu,S)
|
||||
mu0, S0 = kern.dpsi0_dmuS(dL_dpsi0, Z, mu, S)
|
||||
mu1, S1 = kern.dpsi1_dmuS(dL_dpsi1, Z, mu, S)
|
||||
mu2, S2 = kern.dpsi2_dmuS(dL_dpsi2, Z, mu, S)
|
||||
|
||||
dmu = mu0 + mu1 + mu2 - mu
|
||||
#dS = S0 + S1 + S2 -0.5 + .5/S
|
||||
dlnS = S*(S0 + S1 + S2 -0.5) + .5
|
||||
# dS = S0 + S1 + S2 -0.5 + .5/S
|
||||
dlnS = S * (S0 + S1 + S2 - 0.5) + .5
|
||||
|
||||
return -np.hstack((dmu.flatten(),dlnS.flatten()))
|
||||
return -np.hstack((dmu.flatten(), dlnS.flatten()))
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -11,6 +11,8 @@ from ..util.linalg import pdinv, PCA
|
|||
from GP import GP
|
||||
from ..likelihoods import Gaussian
|
||||
from .. import util
|
||||
from GPy.util import plot_latent
|
||||
|
||||
|
||||
class GPLVM(GP):
|
||||
"""
|
||||
|
|
@ -60,65 +62,5 @@ class GPLVM(GP):
|
|||
mu, var, upper, lower = self.predict(Xnew)
|
||||
pb.plot(mu[:,0], mu[:,1],'k',linewidth=1.5)
|
||||
|
||||
def plot_latent(self, labels=None, which_indices=None, resolution=50, ax=None):
|
||||
"""
|
||||
:param labels: a np.array of size self.N containing labels for the points (can be number, strings, etc)
|
||||
:param resolution: the resolution of the grid on which to evaluate the predictive variance
|
||||
"""
|
||||
if ax is None:
|
||||
ax = pb.gca()
|
||||
util.plot.Tango.reset()
|
||||
|
||||
if labels is None:
|
||||
labels = np.ones(self.N)
|
||||
if which_indices is None:
|
||||
if self.Q==1:
|
||||
input_1 = 0
|
||||
input_2 = None
|
||||
if self.Q==2:
|
||||
input_1, input_2 = 0,1
|
||||
else:
|
||||
try:
|
||||
input_1, input_2 = np.argsort(self.input_sensitivity())[:2]
|
||||
except:
|
||||
raise ValueError, "cannot Atomatically determine which dimensions to plot, please pass 'which_indices'"
|
||||
else:
|
||||
input_1, input_2 = which_indices
|
||||
|
||||
#first, plot the output variance as a function of the latent space
|
||||
Xtest, xx,yy,xmin,xmax = util.plot.x_frame2D(self.X[:,[input_1, input_2]],resolution=resolution)
|
||||
Xtest_full = np.zeros((Xtest.shape[0], self.X.shape[1]))
|
||||
Xtest_full[:, :2] = Xtest
|
||||
mu, var, low, up = self.predict(Xtest_full)
|
||||
var = var[:, :1]
|
||||
ax.imshow(var.reshape(resolution, resolution).T,
|
||||
extent=[xmin[0], xmax[0], xmin[1], xmax[1]], cmap=pb.cm.binary,interpolation='bilinear',origin='lower')
|
||||
|
||||
for i,ul in enumerate(np.unique(labels)):
|
||||
if type(ul) is np.string_:
|
||||
this_label = ul
|
||||
elif type(ul) is np.int64:
|
||||
this_label = 'class %i'%ul
|
||||
else:
|
||||
this_label = 'class %i'%i
|
||||
|
||||
index = np.nonzero(labels==ul)[0]
|
||||
if self.Q==1:
|
||||
x = self.X[index,input_1]
|
||||
y = np.zeros(index.size)
|
||||
else:
|
||||
x = self.X[index,input_1]
|
||||
y = self.X[index,input_2]
|
||||
ax.plot(x,y,marker='o',color=util.plot.Tango.nextMedium(),mew=0,label=this_label,linewidth=0)
|
||||
|
||||
ax.set_xlabel('latent dimension %i'%input_1)
|
||||
ax.set_ylabel('latent dimension %i'%input_2)
|
||||
|
||||
if not np.all(labels==1.):
|
||||
ax.legend(loc=0,numpoints=1)
|
||||
|
||||
ax.set_xlim(xmin[0],xmax[0])
|
||||
ax.set_ylim(xmin[1],xmax[1])
|
||||
ax.grid(b=False) # remove the grid if present, it doesn't look good
|
||||
ax.set_aspect('auto') # set a nice aspect ratio
|
||||
return ax
|
||||
def plot_latent(self, *args, **kwargs):
|
||||
util.plot_latent.plot_latent(self, *args, **kwargs)
|
||||
Loading…
Add table
Add a link
Reference in a new issue