From dc33aa1b8c334ed5cd05d574aa4690a636a0cc17 Mon Sep 17 00:00:00 2001 From: James Hensman Date: Thu, 30 May 2013 09:29:26 +0100 Subject: [PATCH] used scipy.weave to improve the speed of rbf grads for large number of input dimensions with ARD, wqe have approx tenfold speedup. --- GPy/kern/rbf.py | 52 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/GPy/kern/rbf.py b/GPy/kern/rbf.py index c413b469..8b644241 100644 --- a/GPy/kern/rbf.py +++ b/GPy/kern/rbf.py @@ -56,6 +56,13 @@ class rbf(kernpart): self._Z, self._mu, self._S = np.empty(shape=(3,1)) self._X, self._X2, self._params = np.empty(shape=(3,1)) + #a set of optional args to pass to weave + self.weave_options = {'headers' : [''], + 'extra_compile_args': ['-fopenmp -O3'], #-march=native'], + 'extra_link_args' : ['-lgomp']} + + + def _get_params(self): return np.hstack((self.variance,self.lengthscale)) @@ -85,8 +92,43 @@ class rbf(kernpart): self._K_computations(X,X2) target[0] += np.sum(self._K_dvar*dL_dK) if self.ARD: - if X2 is None: X2 = X - [np.add(target[1+q:2+q],(self.variance/self.lengthscale[q]**3)*np.sum(self._K_dvar*dL_dK*np.square(X[:,q][:,None]-X2[:,q][None,:])),target[1+q:2+q]) for q in range(self.D)] + dvardLdK = self._K_dvar*dL_dK + var_len3 = self.variance/np.power(self.lengthscale,3) + if X2 is None: + #save computation for the symmetrical case + dvardLdK += dvardLdK.T + code = """ + int q,i,j; + double tmp; + for(q=0; q'], - 'extra_compile_args': ['-fopenmp -O3'], #-march=native'], - 'extra_link_args' : ['-lgomp']} - N,Q = mu.shape M = Zhat.shape[0] @@ -288,6 +326,6 @@ class rbf(kernpart): """ 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) + type_converters=weave.converters.blitz,**self.weave_options) return mudist,mudist_sq, psi2_exponent, psi2