From 2dcfabf300d8692adc0b4eb175b925979e3f0ca2 Mon Sep 17 00:00:00 2001 From: Alan Saul Date: Thu, 30 Apr 2015 13:59:00 +0100 Subject: [PATCH 1/3] Added another einsum operation --- GPy/testing/linalg_test.py | 7 +++++++ GPy/util/linalg.py | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/GPy/testing/linalg_test.py b/GPy/testing/linalg_test.py index 81cb0368..ad524156 100644 --- a/GPy/testing/linalg_test.py +++ b/GPy/testing/linalg_test.py @@ -50,3 +50,10 @@ class LinalgTests(np.testing.TestCase): pure = np.einsum('ijk,jlk->il', A, B) quick = GPy.util.linalg.ijk_jlk_to_il(A,B) np.testing.assert_allclose(pure, quick) + + def test_einsum_ijk_ljk_to_ilk(self): + A = np.random.randn(150, 20, 5) + B = np.random.randn(20, 30, 5) + pure = np.einsum('ijk,jlk->il', A, B) + quick = GPy.util.linalg.ijk_jlk_to_il(A,B) + np.testing.assert_allclose(pure, quick) diff --git a/GPy/util/linalg.py b/GPy/util/linalg.py index 285701e7..bc047654 100644 --- a/GPy/util/linalg.py +++ b/GPy/util/linalg.py @@ -465,3 +465,14 @@ def ijk_jlk_to_il(A, B): res = np.zeros((A.shape[0], B.shape[1])) [np.add(np.dot(A[:,:,k], B[:,:,k]), res, res) for k in range(B.shape[-1])] return res + +def ijk_ljk_to_ilk(A, B): + """ + Faster version of einsum np.einsum('ijk,ljk->ilk', A, B) + + I.e A.dot(B.T) for every dimension + """ + res = np.empty((A.shape[0], B.shape[0], A.shape[-1])) + [np.dot(A[:,:,i], B[:,:,i].T, res[i,:,:]) for i in range(A.shape[0])] + res = res.swapaxes(0,2) + return res From 16e6d393176328312b53ce5ef5569846317c0392 Mon Sep 17 00:00:00 2001 From: Alan Saul Date: Thu, 30 Apr 2015 14:22:46 +0100 Subject: [PATCH 2/3] Fixed linalg for general matricies --- GPy/testing/linalg_test.py | 7 ++++--- GPy/util/linalg.py | 8 ++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/GPy/testing/linalg_test.py b/GPy/testing/linalg_test.py index ad524156..ec3aca5a 100644 --- a/GPy/testing/linalg_test.py +++ b/GPy/testing/linalg_test.py @@ -53,7 +53,8 @@ class LinalgTests(np.testing.TestCase): def test_einsum_ijk_ljk_to_ilk(self): A = np.random.randn(150, 20, 5) - B = np.random.randn(20, 30, 5) - pure = np.einsum('ijk,jlk->il', A, B) - quick = GPy.util.linalg.ijk_jlk_to_il(A,B) + B = np.random.randn(150, 20, 5) + #B = A.copy() + pure = np.einsum('ijk,ljk->ilk', A, B) + quick = GPy.util.linalg.ijk_ljk_to_ilk(A,B) np.testing.assert_allclose(pure, quick) diff --git a/GPy/util/linalg.py b/GPy/util/linalg.py index bc047654..634a1e0d 100644 --- a/GPy/util/linalg.py +++ b/GPy/util/linalg.py @@ -463,7 +463,7 @@ def ijk_jlk_to_il(A, B): Faster version of einsum einsum('ijk,jlk->il', A,B) """ res = np.zeros((A.shape[0], B.shape[1])) - [np.add(np.dot(A[:,:,k], B[:,:,k]), res, res) for k in range(B.shape[-1])] + [np.add(np.dot(A[:,:,k], B[:,:,k]), res, out=res) for k in range(B.shape[-1])] return res def ijk_ljk_to_ilk(A, B): @@ -472,7 +472,7 @@ def ijk_ljk_to_ilk(A, B): I.e A.dot(B.T) for every dimension """ - res = np.empty((A.shape[0], B.shape[0], A.shape[-1])) - [np.dot(A[:,:,i], B[:,:,i].T, res[i,:,:]) for i in range(A.shape[0])] - res = res.swapaxes(0,2) + res = np.zeros((A.shape[-1], A.shape[0], B.shape[0])) + [np.dot(A[:,:,i], B[:,:,i].T, out=res[i,:,:]) for i in range(A.shape[-1])] + res = res.swapaxes(0, 2).swapaxes(0,1) return res From 435308d5dafbba8e179424d8e5964c8d5846ded9 Mon Sep 17 00:00:00 2001 From: mzwiessele Date: Thu, 30 Apr 2015 15:28:16 +0200 Subject: [PATCH 3/3] [changepoint] cp is array --- GPy/kern/_src/basis_funcs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GPy/kern/_src/basis_funcs.py b/GPy/kern/_src/basis_funcs.py index e18be3fb..a6c1f36c 100644 --- a/GPy/kern/_src/basis_funcs.py +++ b/GPy/kern/_src/basis_funcs.py @@ -121,7 +121,7 @@ class LinearSlopeBasisFuncKernel(BasisFuncKernel): class ChangePointBasisFuncKernel(BasisFuncKernel): def __init__(self, input_dim, changepoint, variance=1., active_dims=None, ARD=False, name='changepoint'): - self.changepoint = changepoint + self.changepoint = np.array(changepoint) super(ChangePointBasisFuncKernel, self).__init__(input_dim, variance, active_dims, ARD, name) @Cache_this(limit=3, ignore_args=())