From 48b0ac6399d8c9e0df0114daaf4c21d9ca1cb879 Mon Sep 17 00:00:00 2001 From: James Hensman Date: Wed, 10 Apr 2013 20:02:22 +0100 Subject: [PATCH] some minor improvements in visualize --- GPy/examples/dimensionality_reduction.py | 8 ++++---- GPy/kern/rbf.py | 9 ++++----- GPy/models/Bayesian_GPLVM.py | 1 + GPy/util/visualize.py | 17 ++++++++++------- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/GPy/examples/dimensionality_reduction.py b/GPy/examples/dimensionality_reduction.py index 61a4abd8..f2558341 100644 --- a/GPy/examples/dimensionality_reduction.py +++ b/GPy/examples/dimensionality_reduction.py @@ -60,7 +60,7 @@ def GPLVM_oil_100(optimize=True,M=15): m.plot_latent(labels=m.data_labels) return m -def BGPLVM_oil(optimize=True,N=100,Q=10,M=15): +def BGPLVM_oil(optimize=True,N=100,Q=10,M=15,max_f_eval=300): data = GPy.util.datasets.oil() # create simple GP model @@ -72,10 +72,10 @@ def BGPLVM_oil(optimize=True,N=100,Q=10,M=15): if optimize: m.constrain_fixed('noise',0.05) m.ensure_default_constraints() - m.optimize('scg',messages=1) + m.optimize('scg',messages=1,max_f_eval=max(80,max_f_eval)) m.unconstrain('noise') m.constrain_positive('noise') - m.optimize('scg',messages=1) + m.optimize('scg',messages=1,max_f_eval=max(0,max_f_eval-80)) else: m.ensure_default_constraints() @@ -120,7 +120,7 @@ def brendan_faces(): def stick(): data = GPy.util.datasets.stick() m = GPy.models.GPLVM(data['Y'], 2) - + # optimize m.ensure_default_constraints() m.optimize(messages=1, max_f_eval=10000) diff --git a/GPy/kern/rbf.py b/GPy/kern/rbf.py index a26bb79c..84521cf9 100644 --- a/GPy/kern/rbf.py +++ b/GPy/kern/rbf.py @@ -173,7 +173,7 @@ class rbf(kernpart): """Think N,M,M,Q """ self._psi_computations(Z,mu,S) tmp = self._psi2[:,:,:,None]/self.lengthscale2/self._psi2_denom - target_mu += (dL_dpsi2[:,:,:,None]*-tmp*2.*self._psi2_mudist).sum(1).sum(1) + target_mu += -2.*(dL_dpsi2[:,:,:,None]*tmp*self._psi2_mudist).sum(1).sum(1) target_S += (dL_dpsi2[:,:,:,None]*tmp*(2.*self._psi2_mudist_sq-1)).sum(1).sum(1) @@ -207,7 +207,6 @@ class rbf(kernpart): if not (np.all(Z==self._Z) and np.all(mu==self._mu) and np.all(S==self._S)): #something's changed. recompute EVERYTHING - #TODO: make more efficient for large Q (using NDL's dot product trick) #psi1 self._psi1_denom = S[:,None,:]/self.lengthscale2 + 1. self._psi1_dist = Z[None,:,:]-mu[:,None,:] @@ -250,7 +249,7 @@ class rbf(kernpart): _psi2_denom = self._psi2_denom.squeeze() code = """ double tmp; - + #pragma omp parallel for private(tmp) for (int n=0; n #include """ - weave.inline(code, support_code=support_code, libraries=['gomp'], + weave.inline(code, support_code=support_code, libraries=['gomp'], arg_names=['N','M','Q','mu','Zhat','mudist_sq','mudist','lengthscale2','_psi2_denom','psi2_Zdist_sq','psi2_exponent','half_log_psi2_denom','psi2','variance_sq'], type_converters=weave.converters.blitz,**weave_options) - + return mudist,mudist_sq, psi2_exponent, psi2 diff --git a/GPy/models/Bayesian_GPLVM.py b/GPy/models/Bayesian_GPLVM.py index ba9603bb..ee485e76 100644 --- a/GPy/models/Bayesian_GPLVM.py +++ b/GPy/models/Bayesian_GPLVM.py @@ -95,3 +95,4 @@ class Bayesian_GPLVM(sparse_GP, GPLVM): 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 diff --git a/GPy/util/visualize.py b/GPy/util/visualize.py index dde9cd32..f9538942 100644 --- a/GPy/util/visualize.py +++ b/GPy/util/visualize.py @@ -4,7 +4,7 @@ import GPy import numpy as np class lvm: - def __init__(self, model, data_visualize, latent_axis): + def __init__(self, model, data_visualize, latent_axis, latent_index=[0,1], latent_dim=2): self.cid = latent_axis.figure.canvas.mpl_connect('button_press_event', self.on_click) self.cid = latent_axis.figure.canvas.mpl_connect('motion_notify_event', self.on_move) self.data_visualize = data_visualize @@ -12,6 +12,8 @@ class lvm: self.latent_axis = latent_axis self.called = False self.move_on = False + self.latent_index = latent_index + self.latent_dim = latent_dim def on_click(self, event): #print 'click', event.xdata, event.ydata @@ -32,7 +34,8 @@ class lvm: if self.called and self.move_on: # Call modify code on move #print 'move', event.xdata, event.ydata - latent_values = np.array((event.xdata, event.ydata)) + latent_values = np.zeros((1,self.latent_dim)) + latent_values[0,self.latent_index] = np.array([event.xdata, event.ydata]) y = self.model.predict(latent_values)[0] self.data_visualize.modify(y) #print 'y', y @@ -45,7 +48,7 @@ class data_show: # If no axes are defined, create some. if axis==None: fig = plt.figure() - self.axis = fig.add_subplot(111) + self.axis = fig.add_subplot(111) else: self.axis = axis @@ -57,7 +60,7 @@ class vector_show(data_show): def __init__(self, vals, axis=None): data_show.__init__(self, vals, axis) self.vals = vals.T - self.handle = plt.plot(np.arange(0, len(vals))[:, None], self.vals)[0] + self.handle = self.axis.plot(np.arange(0, len(vals))[:, None], self.vals)[0] def modify(self, vals): xdata, ydata = self.handle.get_data() @@ -84,7 +87,7 @@ class image_show(data_show): self.handle.set_array(self.vals) #self.axis.figure.canvas.draw() plt.show() - + def set_image(self, vals): self.vals = np.reshape(vals, self.dimensions, order='F') if self.transpose: @@ -94,7 +97,7 @@ class image_show(data_show): #if self.invert: # self.vals = -self.vals -class stick_show(data_show): +class stick_show(data_show): """Show a three dimensional point cloud as a figure. Connect elements of the figure together using the matrix connect.""" def __init__(self, vals, axis=None, connect=None): @@ -159,6 +162,6 @@ class stick_show(data_show): self.line_handle = self.axis.plot(np.array(x), np.array(y), np.array(z), 'b-') self.axis.figure.canvas.draw() - +