mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-06-08 15:05:15 +02:00
fixed coregionalize cython
This commit is contained in:
parent
9e4c33910f
commit
1ebbc49fdd
5 changed files with 437 additions and 232 deletions
|
|
@ -73,8 +73,8 @@ class Coregionalize(Kern):
|
|||
|
||||
def _K_cython(self, X, X2=None):
|
||||
if X2 is None:
|
||||
return coregionalize_cython.K_symmetric(self.B, X[:,0])
|
||||
return coregionalize_cython.K_asymmetric(self.B, X[:,0], X2[:,0])
|
||||
return coregionalize_cython.K_symmetric(self.B, np.asarray(X, dtype=np.int64)[:,0])
|
||||
return coregionalize_cython.K_asymmetric(self.B, np.asarray(X, dtype=np.int64)[:,0], np.asarray(X2, dtype=np.int64)[:,0])
|
||||
|
||||
|
||||
def Kdiag(self, X):
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -1,5 +1,5 @@
|
|||
#cython: boundscheck=False
|
||||
#cython: wraparound=False
|
||||
#cython: boundscheck=True
|
||||
#cython: wraparound=True
|
||||
import cython
|
||||
import numpy as np
|
||||
cimport numpy as np
|
||||
|
|
@ -25,9 +25,9 @@ def gradient_reduce(int D, np.ndarray[double, ndim=2] dL_dK, np.ndarray[np.int64
|
|||
cdef np.ndarray[np.double_t, ndim=2] dL_dK_small = np.zeros((D, D))
|
||||
cdef int N = index.size
|
||||
cdef int M = index2.size
|
||||
for i in range(M):
|
||||
for j in range(N):
|
||||
dL_dK_small[index[j],index2[i]] += dL_dK[i,j];
|
||||
for i in range(N):
|
||||
for j in range(M):
|
||||
dL_dK_small[index2[j],index[i]] += dL_dK[i,j];
|
||||
return dL_dK_small
|
||||
|
||||
|
||||
|
|
|
|||
57
GPy/testing/cython_tests.py
Normal file
57
GPy/testing/cython_tests.py
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
import numpy as np
|
||||
import scipy as sp
|
||||
from GPy.util import choleskies
|
||||
import GPy
|
||||
|
||||
"""
|
||||
These tests make sure that the opure python and cython codes work the same
|
||||
"""
|
||||
|
||||
class CythonTestChols(np.testing.TestCase):
|
||||
def setUp(self):
|
||||
self.flat = np.random.randn(45, 5)
|
||||
self.triang = np.dstack([np.eye(20)[:,:,None] for i in range(3)])
|
||||
def test_flat_to_triang(self):
|
||||
L1 = choleskies._flat_to_triang_pure(self.flat)
|
||||
L2 = choleskies._flat_to_triang_cython(self.flat)
|
||||
np.testing.assert_allclose(L1, L2)
|
||||
def test_triang_to_flat(self):
|
||||
A1 = choleskies._triang_to_flat_pure(self.triang)
|
||||
A2 = choleskies._triang_to_flat_cython(self.triang)
|
||||
np.testing.assert_allclose(A1, A2)
|
||||
|
||||
class test_stationary(np.testing.TestCase):
|
||||
def setUp(self):
|
||||
self.k = GPy.kern.RBF(10)
|
||||
self.X = np.random.randn(300,10)
|
||||
self.Z = np.random.randn(20,10)
|
||||
self.dKxx = np.random.randn(300,300)
|
||||
self.dKzz = np.random.randn(20,20)
|
||||
self.dKxz = np.random.randn(300,20)
|
||||
|
||||
def test_square_gradX(self):
|
||||
g1 = self.k._gradients_X_cython(self.dKxx, self.X)
|
||||
g2 = self.k._gradients_X_pure(self.dKxx, self.X)
|
||||
np.testing.assert_allclose(g1, g2)
|
||||
|
||||
def test_rect_gradx(self):
|
||||
g1 = self.k._gradients_X_cython(self.dKxz, self.X, self.Z)
|
||||
g2 = self.k._gradients_X_pure(self.dKxz, self.X, self.Z)
|
||||
np.testing.assert_allclose(g1, g2)
|
||||
|
||||
def test_square_lengthscales(self):
|
||||
g1 = self.k._lengthscale_grads_pure(self.dKxx, self.X, self.X)
|
||||
g2 = self.k._lengthscale_grads_cython(self.dKxx, self.X, self.X)
|
||||
np.testing.assert_allclose(g1, g2)
|
||||
|
||||
def test_rect_lengthscales(self):
|
||||
g1 = self.k._lengthscale_grads_pure(self.dKxz, self.X, self.Z)
|
||||
g2 = self.k._lengthscale_grads_cython(self.dKxz, self.X, self.Z)
|
||||
np.testing.assert_allclose(g1, g2)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -366,9 +366,9 @@ class KernelTestsNonContinuous(unittest.TestCase):
|
|||
X2 = self.X2[self.X2[:,-1]!=2]
|
||||
self.assertTrue(check_kernel_gradient_functions(kern, X=X, X2=X2, verbose=verbose, fixed_X_dims=-1))
|
||||
|
||||
class Coregionalize_weave_test(unittest.TestCase):
|
||||
class Coregionalize_cython_test(unittest.TestCase):
|
||||
"""
|
||||
Make sure that the coregionalize kernel work with and without weave enabled
|
||||
Make sure that the coregionalize kernel work with and without cython enabled
|
||||
"""
|
||||
def setUp(self):
|
||||
self.k = GPy.kern.Coregionalize(1, output_dim=12)
|
||||
|
|
@ -378,36 +378,42 @@ class Coregionalize_weave_test(unittest.TestCase):
|
|||
|
||||
def test_sym(self):
|
||||
dL_dK = np.random.randn(self.N1, self.N1)
|
||||
GPy.util.config.config.set('weave', 'working', 'True')
|
||||
K_weave = self.k.K(self.X)
|
||||
GPy.util.config.config.set('cython', 'working', 'True')
|
||||
K_cython = self.k.K(self.X)
|
||||
self.k.update_gradients_full(dL_dK, self.X)
|
||||
grads_weave = self.k.gradient.copy()
|
||||
grads_cython = self.k.gradient.copy()
|
||||
|
||||
GPy.util.config.config.set('weave', 'working', 'False')
|
||||
GPy.util.config.config.set('cython', 'working', 'False')
|
||||
K_numpy = self.k.K(self.X)
|
||||
self.k.update_gradients_full(dL_dK, self.X)
|
||||
grads_numpy = self.k.gradient.copy()
|
||||
|
||||
self.assertTrue(np.allclose(K_numpy, K_weave))
|
||||
self.assertTrue(np.allclose(grads_numpy, grads_weave))
|
||||
self.assertTrue(np.allclose(K_numpy, K_cython))
|
||||
self.assertTrue(np.allclose(grads_numpy, grads_cython))
|
||||
|
||||
#reset the cython state for any other tests
|
||||
GPy.util.config.config.set('cython', 'working', 'true')
|
||||
|
||||
def test_nonsym(self):
|
||||
dL_dK = np.random.randn(self.N1, self.N2)
|
||||
GPy.util.config.config.set('weave', 'working', 'True')
|
||||
K_weave = self.k.K(self.X, self.X2)
|
||||
GPy.util.config.config.set('cython', 'working', 'True')
|
||||
K_cython = self.k.K(self.X, self.X2)
|
||||
self.k.gradient = 0.
|
||||
self.k.update_gradients_full(dL_dK, self.X, self.X2)
|
||||
grads_weave = self.k.gradient.copy()
|
||||
grads_cython = self.k.gradient.copy()
|
||||
|
||||
GPy.util.config.config.set('weave', 'working', 'False')
|
||||
GPy.util.config.config.set('cython', 'working', 'False')
|
||||
K_numpy = self.k.K(self.X, self.X2)
|
||||
self.k.gradient = 0.
|
||||
self.k.update_gradients_full(dL_dK, self.X, self.X2)
|
||||
grads_numpy = self.k.gradient.copy()
|
||||
|
||||
self.assertTrue(np.allclose(K_numpy, K_weave))
|
||||
self.assertTrue(np.allclose(grads_numpy, grads_weave))
|
||||
self.assertTrue(np.allclose(K_numpy, K_cython))
|
||||
self.assertTrue(np.allclose(grads_numpy, grads_cython))
|
||||
|
||||
#reset the cython state for any other tests
|
||||
GPy.util.config.config.set('cython', 'working', 'true')
|
||||
|
||||
#reset the weave state for any other tests
|
||||
GPy.util.config.config.set('weave', 'working', 'False')
|
||||
|
||||
|
||||
class KernelTestsProductWithZeroValues(unittest.TestCase):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue