From ca5067c61421187dfa140d1b105c54b122fdfa39 Mon Sep 17 00:00:00 2001 From: Zhenwen Dai Date: Mon, 1 Sep 2014 15:23:41 +0100 Subject: [PATCH 1/4] improve debug helper --- GPy/core/sparse_gp_mpi.py | 3 +++ GPy/util/debug.py | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/GPy/core/sparse_gp_mpi.py b/GPy/core/sparse_gp_mpi.py index 7910cb71..f0465f38 100644 --- a/GPy/core/sparse_gp_mpi.py +++ b/GPy/core/sparse_gp_mpi.py @@ -67,6 +67,9 @@ class SparseGP_MPI(SparseGP): del dc['N_range'] del dc['N_list'] del dc['Y_local'] + if 'normalizer' not in dc: + dc['normalizer'] = None + dc['Y_normalized'] = dc['Y'] return dc #===================================================== diff --git a/GPy/util/debug.py b/GPy/util/debug.py index a5796e80..cd4a0c2d 100644 --- a/GPy/util/debug.py +++ b/GPy/util/debug.py @@ -26,11 +26,14 @@ def checkFullRank(m, tol=1e-10, name=None, force_check=False): print 'The size of '+name+'is too big to check (>=10000)!' return True - s = np.linalg.eigvals(m) + s = np.real(np.linalg.eigvals(m)) if s.min()/s.max() Date: Mon, 1 Sep 2014 18:39:43 +0100 Subject: [PATCH 2/4] more for debug --- .../latent_function_inference/var_dtc_parallel.py | 14 +++++++++++--- GPy/util/debug.py | 3 --- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/GPy/inference/latent_function_inference/var_dtc_parallel.py b/GPy/inference/latent_function_inference/var_dtc_parallel.py index b834d942..ab4074f4 100644 --- a/GPy/inference/latent_function_inference/var_dtc_parallel.py +++ b/GPy/inference/latent_function_inference/var_dtc_parallel.py @@ -170,12 +170,14 @@ class VarDTC_minibatch(LatentFunctionInference): Kmm = kern.K(Z).copy() diag.add(Kmm, self.const_jitter) - checkFullRank(Kmm) + r1 = checkFullRank(Kmm,name='Kmm') Lm = jitchol(Kmm) LmInvPsi2LmInvT = backsub_both_sides(Lm,psi2_full,transpose='right') Lambda = np.eye(Kmm.shape[0])+LmInvPsi2LmInvT - checkFullRank(Lambda) + r2 = checkFullRank(Lambda,name='Lambda') + if (not r1) or (not r2): + raise LL = jitchol(Lambda) LL = np.dot(Lm,LL) b,_ = dtrtrs(LL, psi1Y_full.T) @@ -339,7 +341,13 @@ def update_gradients(model, mpi_comm=None): Y = model.Y_local X = model.X[model.N_range[0]:model.N_range[1]] - model._log_marginal_likelihood, dL_dKmm, model.posterior = model.inference_method.inference_likelihood(model.kern, X, model.Z, model.likelihood, Y) + try: + model._log_marginal_likelihood, dL_dKmm, model.posterior = model.inference_method.inference_likelihood(model.kern, X, model.Z, model.likelihood, Y) + except Exception: + if model.mpi_comm is None or model.mpi_comm.rank==0: + import time + model.pickle('model_'+str(int(time.time()))+'.pickle') + raise het_noise = model.likelihood.variance.size > 1 diff --git a/GPy/util/debug.py b/GPy/util/debug.py index cd4a0c2d..b676d028 100644 --- a/GPy/util/debug.py +++ b/GPy/util/debug.py @@ -31,9 +31,6 @@ def checkFullRank(m, tol=1e-10, name=None, force_check=False): if s.min()/s.max() Date: Tue, 2 Sep 2014 11:52:09 +0100 Subject: [PATCH 3/4] Allow the default constraint of a Param object to be 'fixed' --- GPy/core/model.py | 1 + GPy/core/parameterization/parameter_core.py | 7 ++++--- GPy/core/parameterization/variational.py | 11 +++++++---- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/GPy/core/model.py b/GPy/core/model.py index 00c6d5ff..0fdf4800 100644 --- a/GPy/core/model.py +++ b/GPy/core/model.py @@ -303,6 +303,7 @@ class Model(Parameterized): denominator = (2 * np.dot(dx, gradient)) global_ratio = (f1 - f2) / np.where(denominator == 0., 1e-32, denominator) global_diff = np.abs(f1 - f2) < tolerance and np.allclose(gradient, 0, atol=tolerance) + print self.mpi_comm.rank,global_ratio,global_diff if global_ratio is np.nan: global_ratio = 0 return np.abs(1. - global_ratio) < tolerance or global_diff diff --git a/GPy/core/parameterization/parameter_core.py b/GPy/core/parameterization/parameter_core.py index 815f069b..82c494b2 100644 --- a/GPy/core/parameterization/parameter_core.py +++ b/GPy/core/parameterization/parameter_core.py @@ -13,7 +13,7 @@ Observable Pattern for patameterization """ -from transformations import Logexp, NegativeLogexp, Logistic, __fixed__, FIXED, UNFIXED +from transformations import Transformation,Logexp, NegativeLogexp, Logistic, __fixed__, FIXED, UNFIXED import numpy as np import re import logging @@ -541,7 +541,8 @@ class Indexable(Nameable, Observable): Constrain the parameter to the given :py:class:`GPy.core.transformations.Transformation`. """ - self.param_array[...] = transform.initialize(self.param_array) + if isinstance(transform, Transformation): + self.param_array[...] = transform.initialize(self.param_array) reconstrained = self.unconstrain() added = self._add_to_index_operations(self.constraints, reconstrained, transform, warning) self.notify_observers(self, None if trigger_parent else -np.inf) @@ -617,7 +618,7 @@ class Indexable(Nameable, Observable): """ Helper preventing copy code. This adds the given what (transformation, prior etc) to parameter index operations which. - revonstrained are reconstrained indices. + reconstrained are reconstrained indices. warn when reconstraining parameters if warning is True. TODO: find out which parameters have changed specifically """ diff --git a/GPy/core/parameterization/variational.py b/GPy/core/parameterization/variational.py index 5412a70d..2afeafbb 100644 --- a/GPy/core/parameterization/variational.py +++ b/GPy/core/parameterization/variational.py @@ -7,7 +7,7 @@ Created on 6 Nov 2013 import numpy as np from parameterized import Parameterized from param import Param -from transformations import Logexp, Logistic +from transformations import Logexp, Logistic,__fixed__ class VariationalPrior(Parameterized): def __init__(self, name='latent space', **kw): @@ -35,12 +35,15 @@ class NormalPrior(VariationalPrior): class SpikeAndSlabPrior(VariationalPrior): def __init__(self, pi=None, learnPi=False, variance = 1.0, name='SpikeAndSlabPrior', **kw): - super(VariationalPrior, self).__init__(name=name, **kw) - self.pi = Param('pi', pi, Logistic(1e-10,1.-1e-10)) + super(SpikeAndSlabPrior, self).__init__(name=name, **kw) self.variance = Param('variance',variance) self.learnPi = learnPi if learnPi: - self.add_parameters(self.pi) + self.pi = Param('Pi', pi, Logistic(1e-10,1.-1e-10)) + else: + self.pi = Param('Pi', pi, __fixed__) + self.add_parameter(self.pi) + def KL_divergence(self, variational_posterior): mu = variational_posterior.mean From e46f3b342e3f8f08d9d32a63f22996f99d4d6f7f Mon Sep 17 00:00:00 2001 From: Zhenwen Dai Date: Tue, 2 Sep 2014 12:33:52 +0100 Subject: [PATCH 4/4] remove the print message in model.checkgrad --- GPy/core/model.py | 3 +-- GPy/core/sparse_gp_mpi.py | 2 +- GPy/models/ss_gplvm.py | 4 ++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/GPy/core/model.py b/GPy/core/model.py index 0fdf4800..3863ac42 100644 --- a/GPy/core/model.py +++ b/GPy/core/model.py @@ -291,7 +291,7 @@ class Model(Parameterized): # just check the global ratio dx = np.zeros(x.shape) dx[transformed_index] = step * (np.sign(np.random.uniform(-1, 1, transformed_index.size)) if transformed_index.size != 2 else 1.) - + # evaulate around the point x f1 = self._objective(x + dx) f2 = self._objective(x - dx) @@ -303,7 +303,6 @@ class Model(Parameterized): denominator = (2 * np.dot(dx, gradient)) global_ratio = (f1 - f2) / np.where(denominator == 0., 1e-32, denominator) global_diff = np.abs(f1 - f2) < tolerance and np.allclose(gradient, 0, atol=tolerance) - print self.mpi_comm.rank,global_ratio,global_diff if global_ratio is np.nan: global_ratio = 0 return np.abs(1. - global_ratio) < tolerance or global_diff diff --git a/GPy/core/sparse_gp_mpi.py b/GPy/core/sparse_gp_mpi.py index f0465f38..73a37862 100644 --- a/GPy/core/sparse_gp_mpi.py +++ b/GPy/core/sparse_gp_mpi.py @@ -46,7 +46,7 @@ class SparseGP_MPI(SparseGP): self.add_parameter(self.X, index=0) if variational_prior is not None: self.add_parameter(variational_prior) - self.X.fix() +# self.X.fix() self.mpi_comm = mpi_comm # Manage the data (Y) division diff --git a/GPy/models/ss_gplvm.py b/GPy/models/ss_gplvm.py index 7913b605..4ea4f297 100644 --- a/GPy/models/ss_gplvm.py +++ b/GPy/models/ss_gplvm.py @@ -70,8 +70,8 @@ class SSGPLVM(SparseGP_MPI): X = SpikeAndSlabPosterior(X, X_variance, gamma) super(SSGPLVM,self).__init__(X, Y, Z, kernel, likelihood, variational_prior=self.variational_prior, inference_method=inference_method, name=name, mpi_comm=mpi_comm, normalizer=normalizer, **kwargs) - self.X.unfix() - self.X.variance.constrain_positive() +# self.X.unfix() +# self.X.variance.constrain_positive() if self.group_spike: [self.X.gamma[:,i].tie('tieGamma'+str(i)) for i in xrange(self.X.gamma.shape[1])] # Tie columns together