From 433b2131654d8cc84f646b822f9a3875d83a312c Mon Sep 17 00:00:00 2001 From: James Hensman Date: Thu, 13 Mar 2014 17:02:29 +0000 Subject: [PATCH 1/3] independent output gradients --- GPy/kern/_src/independent_outputs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GPy/kern/_src/independent_outputs.py b/GPy/kern/_src/independent_outputs.py index 0cbd5be4..438168a0 100644 --- a/GPy/kern/_src/independent_outputs.py +++ b/GPy/kern/_src/independent_outputs.py @@ -83,7 +83,7 @@ class IndependentOutputs(Kern): target = np.zeros_like(X) slices = index_to_slices(X[:,self.index_dim]) if X2 is None: - [[np.copyto(target[s,self.kern.active_dims], self.kern.gradients_X(dL_dK[s,s],X[s],X[ss])) for s, ss in product(slices_i, slices_i)] for slices_i in slices] + [[np.copyto(target[s,self.kern.active_dims], self.kern.gradients_X(dL_dK[s,ss],X[s],X[ss])) for s, ss in itertools.product(slices_i, slices_i)] for slices_i in slices] else: X2,slices2 = X2[:,:self.index_dim],index_to_slices(X2[:,-1]) [[[np.copyto(target[s,:self.index_dim], self.kern.gradients_X(dL_dK[s,s2], X[s], X2[s2])) for s in slices_i] for s2 in slices_j] for slices_i,slices_j in zip(slices,slices2)] From f50b121d4d4ca70f44150c4fd2400e20092ecbfe Mon Sep 17 00:00:00 2001 From: James Hensman Date: Thu, 13 Mar 2014 17:05:46 +0000 Subject: [PATCH 2/3] Alans change to checkgrad --- GPy/core/model.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/GPy/core/model.py b/GPy/core/model.py index c2a9ed23..6d90e13a 100644 --- a/GPy/core/model.py +++ b/GPy/core/model.py @@ -15,7 +15,7 @@ import itertools class Model(Parameterized): _fail_count = 0 # Count of failed optimization steps (see objective) _allowed_failures = 10 # number of allowed failures - + def __init__(self, name): super(Model, self).__init__(name) # Parameterized.__init__(self) self.optimization_runs = [] @@ -27,7 +27,7 @@ class Model(Parameterized): def _log_likelihood_gradients(self): return self.gradient - + def _getstate(self): """ Get the current state of the class. @@ -231,7 +231,7 @@ class Model(Parameterized): raise RuntimeError, "Cannot optimize, when everything is fixed" if self.size == 0: raise RuntimeError, "Model without parameters cannot be minimized" - + if optimizer is None: optimizer = self.preferred_optimizer @@ -301,9 +301,8 @@ class Model(Parameterized): denominator = (2 * np.dot(dx, gradient)) global_ratio = (f1 - f2) / np.where(denominator==0., 1e-32, denominator) - gloabl_diff = (f1 - f2) - denominator - return (np.abs(1. - global_ratio) < tolerance) or (np.abs(gloabl_diff) == 0) + return np.abs(1. - global_ratio) < tolerance) else: # check the gradient of each parameter individually, and do some pretty printing try: @@ -349,7 +348,7 @@ class Model(Parameterized): xx[xind] -= 2.*step f2 = self.objective_function(xx) numerical_gradient = (f1 - f2) / (2 * step) - if np.all(gradient[xind]==0): ratio = (f1-f2) == gradient[xind] + if np.all(gradient[xind]==0): ratio = (f1-f2) == gradient[xind] else: ratio = (f1 - f2) / (2 * step * gradient[xind]) difference = np.abs((f1 - f2) / 2 / step - gradient[xind]) From da4303f71be27788c02acbefaaf0cb5d04891d0d Mon Sep 17 00:00:00 2001 From: James Hensman Date: Fri, 14 Mar 2014 10:29:14 +0000 Subject: [PATCH 3/3] bugfix for grad_dict --- GPy/core/model.py | 2 +- GPy/core/sparse_gp.py | 2 +- GPy/testing/kernel_tests.py | 2 +- GPy/util/caching.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/GPy/core/model.py b/GPy/core/model.py index 6d90e13a..0990e7f1 100644 --- a/GPy/core/model.py +++ b/GPy/core/model.py @@ -302,7 +302,7 @@ class Model(Parameterized): denominator = (2 * np.dot(dx, gradient)) global_ratio = (f1 - f2) / np.where(denominator==0., 1e-32, denominator) - return np.abs(1. - global_ratio) < tolerance) + return np.abs(1. - global_ratio) < tolerance else: # check the gradient of each parameter individually, and do some pretty printing try: diff --git a/GPy/core/sparse_gp.py b/GPy/core/sparse_gp.py index 23f8e690..d137ceff 100644 --- a/GPy/core/sparse_gp.py +++ b/GPy/core/sparse_gp.py @@ -60,7 +60,7 @@ class SparseGP(GP): dL_dKmm = self.grad_dict.pop('dL_dKmm') self.kern.update_gradients_full(dL_dKmm, self.Z, None) target = self.kern.gradient.copy() - self.kern.update_gradients_expectations(variational_posterior=self.X, Z=self.Z, dL_dpsi0=grad_dict['dL_dpsi0'], dL_dpsi1=grad_dict['dL_dpsi1'], dL_dpsi2=grad_dict['dL_dpsi2']) + self.kern.update_gradients_expectations(variational_posterior=self.X, Z=self.Z, dL_dpsi0=self.grad_dict['dL_dpsi0'], dL_dpsi1=self.grad_dict['dL_dpsi1'], dL_dpsi2=self.grad_dict['dL_dpsi2']) self.kern.gradient += target #gradients wrt Z diff --git a/GPy/testing/kernel_tests.py b/GPy/testing/kernel_tests.py index d54b3871..2a35ad3b 100644 --- a/GPy/testing/kernel_tests.py +++ b/GPy/testing/kernel_tests.py @@ -252,7 +252,7 @@ class KernelGradientTestsContinuous(unittest.TestCase): k.randomize() self.assertTrue(check_kernel_gradient_functions(k, X=self.X, X2=self.X2, verbose=verbose)) -#TODO: turn off grad checkingwrt X for indexed kernels liek coregionalize +#TODO: turn off grad checkingwrt X for indexed kernels like coregionalize # class KernelGradientTestsContinuous1D(unittest.TestCase): # def setUp(self): # self.N, self.D = 100, 1 diff --git a/GPy/util/caching.py b/GPy/util/caching.py index 792d82e2..ea09292a 100644 --- a/GPy/util/caching.py +++ b/GPy/util/caching.py @@ -48,7 +48,7 @@ class Cacher(object): if k in kw and kw[k] is not None: return self.operation(*args, **kw) # TODO: WARNING !!! Cache OFFSWITCH !!! WARNING - return self.operation(*args) + #return self.operation(*args) #if the result is cached, return the cached computation state = [all(a is b for a, b in itertools.izip_longest(args, cached_i)) for cached_i in self.cached_inputs]