From 8b197b79a079583dbbc4c3ff8f7ddf2ff086e9c5 Mon Sep 17 00:00:00 2001 From: James Hensman Date: Fri, 28 Feb 2014 17:35:00 +0000 Subject: [PATCH 1/3] stability in stationary) --- GPy/kern/_src/stationary.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/GPy/kern/_src/stationary.py b/GPy/kern/_src/stationary.py index 5f88c3b0..44e17d8a 100644 --- a/GPy/kern/_src/stationary.py +++ b/GPy/kern/_src/stationary.py @@ -87,7 +87,9 @@ class Stationary(Kern): """ if X2 is None: Xsq = np.sum(np.square(X),1) - return np.sqrt(-2.*tdot(X) + (Xsq[:,None] + Xsq[None,:])) + r2 = -2.*tdot(X) + (Xsq[:,None] + Xsq[None,:]) + util.diag.view(r2)[:,]= 0. # force diagnoal to be zero: sometime numerically a little negative + return np.sqrt(r2) else: X1sq = np.sum(np.square(X),1) X2sq = np.sum(np.square(X2),1) From 32c6237672c8aef7616ec2b90b737799febbc441 Mon Sep 17 00:00:00 2001 From: Zhenwen Dai Date: Fri, 28 Feb 2014 18:11:00 +0000 Subject: [PATCH 2/3] changes on rbf --- GPy/kern/_src/rbf.py | 1 - 1 file changed, 1 deletion(-) diff --git a/GPy/kern/_src/rbf.py b/GPy/kern/_src/rbf.py index 7bf0adeb..24b70671 100644 --- a/GPy/kern/_src/rbf.py +++ b/GPy/kern/_src/rbf.py @@ -257,7 +257,6 @@ class RBF(Stationary): #allocate memory for the things we want to compute mudist = np.empty((N, M, M, Q)) mudist_sq = np.empty((N, M, M, Q)) - exponent = np.zeros((N,M,M)) psi2 = np.empty((N, M, M)) l2 = self.lengthscale **2 From 496624af784c18c1ff65f5b0ea2c4dab34c2eb3b Mon Sep 17 00:00:00 2001 From: James Hensman Date: Fri, 28 Feb 2014 21:23:47 +0000 Subject: [PATCH 3/3] weaving a faster rbf --- GPy/kern/_src/rbf.py | 52 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/GPy/kern/_src/rbf.py b/GPy/kern/_src/rbf.py index baa5b932..007bac77 100644 --- a/GPy/kern/_src/rbf.py +++ b/GPy/kern/_src/rbf.py @@ -91,13 +91,11 @@ class RBF(Stationary): #from psi2 S = variational_posterior.variance _, Zdist_sq, _, mudist_sq, psi2 = self._psi2computations(Z, variational_posterior) - d_length = 2.*psi2[:, :, :, None] * (Zdist_sq * (2.*S[:,None,None,:]/l2 + 1.) + mudist_sq + S[:, None, None, :] / l2) / (2.*S[:,None,None,:] + l2)*self.lengthscale - dpsi2_dlength = d_length * dL_dpsi2[:, :, :, None] if not self.ARD: - self.lengthscale.gradient += dpsi2_dlength.sum() + self.lengthscale.gradient += self._weave_psi2_lengthscale_grads(dL_dpsi2, psi2, Zdist_sq, S, mudist_sq, l2).sum() else: - self.lengthscale.gradient += dpsi2_dlength.sum(0).sum(0).sum(0) + self.lengthscale.gradient += self._weave_psi2_lengthscale_grads(dL_dpsi2, psi2, Zdist_sq, S, mudist_sq, l2) self.variance.gradient += 2.*np.sum(dL_dpsi2 * psi2)/self.variance @@ -224,7 +222,7 @@ class RBF(Stationary): code = """ double tmp, exponent_tmp; - //#pragma omp parallel for private(tmp, exponent_tmp) + #pragma omp parallel for private(tmp, exponent_tmp) for (int n=0; nl', dL_dpsi2, psi2, Zdist_sq * (2.*S[:,None,None,:]/l2 + 1.) + mudist_sq + S[:, None, None, :] / l2, 1./(2.*S + l2))*self.lengthscale + + result = np.zeros(self.input_dim) + code = """ + double tmp; + for(int q=0; q + #include + """ + N,Q = S.shape + M = psi2.shape[-1] + + S = param_to_array(S) + weave.inline(code, support_code=support_code, libraries=['gomp'], + arg_names=['psi2', 'dL_dpsi2', 'N', 'M', 'Q', 'mudist_sq', 'l2', 'Zdist_sq', 'S', 'result'], + type_converters=weave.converters.blitz, **self.weave_options) + + return 2.*result*self.lengthscale