From 7dca4218fccac9972ae87760bc1c9ab00e87e30e Mon Sep 17 00:00:00 2001 From: vsaase Date: Wed, 13 Apr 2016 21:46:07 +0200 Subject: [PATCH 1/4] added precomputed kernel class --- GPy/kern/__init__.py | 2 +- GPy/kern/src/static.py | 50 +++++++++++++++++++++++++++++++++++++ GPy/testing/kernel_tests.py | 9 +++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/GPy/kern/__init__.py b/GPy/kern/__init__.py index 3c3de65c..7f44b6a9 100644 --- a/GPy/kern/__init__.py +++ b/GPy/kern/__init__.py @@ -10,7 +10,7 @@ from .src.add import Add from .src.prod import Prod from .src.rbf import RBF from .src.linear import Linear, LinearFull -from .src.static import Bias, White, Fixed, WhiteHeteroscedastic +from .src.static import Bias, White, Fixed, WhiteHeteroscedastic, Precomputed from .src.brownian import Brownian from .src.stationary import Exponential, OU, Matern32, Matern52, ExpQuad, RatQuad, Cosine from .src.mlp import MLP diff --git a/GPy/kern/src/static.py b/GPy/kern/src/static.py index 18f7605f..c2f6b129 100644 --- a/GPy/kern/src/static.py +++ b/GPy/kern/src/static.py @@ -192,3 +192,53 @@ class Fixed(Static): def update_gradients_expectations(self, dL_dpsi0, dL_dpsi1, dL_dpsi2, Z, variational_posterior): self.variance.gradient = dL_dpsi0.sum() +class Precomputed(Fixed): + def __init__(self, input_dim, covariance_matrix, variance=1., active_dims=None, name='precomputed'): + """ + Class for precomputed kernels, indexed by X + + Usage example: + + import numpy as np + from GPy.models import GPClassification + from GPy.kern import Precomputed + from sklearn.cross_validation import LeaveOneOut + + n = 10 + d = 100 + X = np.arange(n).reshape((n,1)) # column vector of indices + y = 2*np.random.binomial(1,0.5,(n,1))-1 + X0 = np.random.randn(n,d) + k = np.dot(X0,X0.T) + kern = Precomputed(1,k) # k is a n x n covariance matrix + + cv = LeaveOneOut(n) + ypred = y.copy() + for train, test in cv: + m = GPClassification(X[train], y[train], kernel=kern) + m.optimize() + ypred[test] = 2*(m.predict(X[test])[0]>0.5)-1 + + :param input_dim: the number of input dimensions + :type input_dim: int + :param variance: the variance of the kernel + :type variance: float + """ + super(Precomputed, self).__init__(input_dim, covariance_matrix, variance, active_dims, name) + def K(self, X, X2=None): + if X2 is None: + return self.variance * self.fixed_K[X[:,0].astype('int')][:,X[:,0].astype('int')] + else: + return self.variance * self.fixed_K[X[:,0].astype('int')][:,X2[:,0].astype('int')] + + def Kdiag(self, X): + return self.variance * self.fixed_K[X[:,0].astype('int')][:,X[:,0].astype('int')].diagonal() + + def update_gradients_full(self, dL_dK, X, X2=None): + if X2 is None: + self.variance.gradient = np.einsum('ij,ij', dL_dK, self.fixed_K[X[:,0].astype('int')][:,X[:,0].astype('int')]) + else: + self.variance.gradient = np.einsum('ij,ij', dL_dK, self.fixed_K[X[:,0].astype('int')][:,X2[:,0].astype('int')]) + + def update_gradients_diag(self, dL_dKdiag, X): + self.variance.gradient = np.einsum('i,ii', dL_dKdiag, self.fixed_K[X[:,0].astype('int')][:,X[:,0].astype('int')]) \ No newline at end of file diff --git a/GPy/testing/kernel_tests.py b/GPy/testing/kernel_tests.py index 6b620406..cab0c3e9 100644 --- a/GPy/testing/kernel_tests.py +++ b/GPy/testing/kernel_tests.py @@ -339,6 +339,15 @@ class KernelGradientTestsContinuous(unittest.TestCase): k = GPy.kern.StdPeriodic(self.D) k.randomize() self.assertTrue(check_kernel_gradient_functions(k, X=self.X, X2=self.X2, verbose=verbose)) + + def test_Precomputed(self): + Xall = np.concatenate([self.X, self.X2]) + cov = np.dot(Xall, Xall.T) + X = np.arange(self.N).reshape(1,self.N) + X2 = np.arange(self.N,2*self.N+10).reshape(1,self.N+10) + k = GPy.kern.Precomputed(1, cov) + k.randomize() + self.assertTrue(check_kernel_gradient_functions(k, X=X, X2=X2, verbose=verbose)) class KernelTestsMiscellaneous(unittest.TestCase): def setUp(self): From 87af7e252594b49111ad211f537368d77b53e4e0 Mon Sep 17 00:00:00 2001 From: Max Zwiessele Date: Thu, 21 Apr 2016 12:31:00 +0100 Subject: [PATCH 2/4] [static] added fixed tests --- GPy/kern/src/static.py | 12 ++++++------ GPy/testing/kernel_tests.py | 9 ++++++--- README.md | 4 ++-- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/GPy/kern/src/static.py b/GPy/kern/src/static.py index 24099dbb..3ce0dc0a 100644 --- a/GPy/kern/src/static.py +++ b/GPy/kern/src/static.py @@ -195,15 +195,15 @@ class Fixed(Static): class Precomputed(Fixed): def __init__(self, input_dim, covariance_matrix, variance=1., active_dims=None, name='precomputed'): """ - Class for precomputed kernels, indexed by X - + Class for precomputed kernels, indexed by columns in X + Usage example: - + import numpy as np from GPy.models import GPClassification from GPy.kern import Precomputed from sklearn.cross_validation import LeaveOneOut - + n = 10 d = 100 X = np.arange(n).reshape((n,1)) # column vector of indices @@ -211,14 +211,14 @@ class Precomputed(Fixed): X0 = np.random.randn(n,d) k = np.dot(X0,X0.T) kern = Precomputed(1,k) # k is a n x n covariance matrix - + cv = LeaveOneOut(n) ypred = y.copy() for train, test in cv: m = GPClassification(X[train], y[train], kernel=kern) m.optimize() ypred[test] = 2*(m.predict(X[test])[0]>0.5)-1 - + :param input_dim: the number of input dimensions :type input_dim: int :param variance: the variance of the kernel diff --git a/GPy/testing/kernel_tests.py b/GPy/testing/kernel_tests.py index fa2cdc28..b834ba9f 100644 --- a/GPy/testing/kernel_tests.py +++ b/GPy/testing/kernel_tests.py @@ -2,11 +2,14 @@ # Licensed under the BSD 3-clause license (see LICENSE.txt) import unittest -import numpy as np +from unittest.case import skip + import GPy from GPy.core.parameterization.param import Param +import numpy as np + from ..util.config import config -from unittest.case import skip + verbose = 0 @@ -347,7 +350,7 @@ class KernelGradientTestsContinuous(unittest.TestCase): k = GPy.kern.StdPeriodic(self.D) k.randomize() self.assertTrue(check_kernel_gradient_functions(k, X=self.X, X2=self.X2, verbose=verbose)) - + def test_Precomputed(self): Xall = np.concatenate([self.X, self.X2]) cov = np.dot(Xall, Xall.T) diff --git a/README.md b/README.md index d0257bab..fceab117 100644 --- a/README.md +++ b/README.md @@ -41,10 +41,10 @@ Python 2.7, 3.4 and higher ## Citation @Misc{gpy2014, - author = {{The GPy authors}}, + author = {{GPy}}, title = {{GPy}: A Gaussian process framework in python}, howpublished = {\url{http://github.com/SheffieldML/GPy}}, - year = {2012--2015} + year = {since 2012} } ### Pronounciation: From 78f7ef3e43d04254946fb96792dad1dfad6c2797 Mon Sep 17 00:00:00 2001 From: Max Zwiessele Date: Thu, 21 Apr 2016 12:34:12 +0100 Subject: [PATCH 3/4] [travis] condition --- .travis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index a634751a..71d7bda6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -48,10 +48,10 @@ before_deploy: - make html - cd ../ - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; - then + then export DIST='sdist'; elif [[ "$TRAVIS_OS_NAME" == "osx" ]]; - then + then export DIST='bdist_wheel'; fi; @@ -63,6 +63,6 @@ deploy: on: tags: true branch: deploy - condition: "$TRAVIS_OS_NAME" == "osx" || ( "$TRAVIS_OS_NAME" == "linux" && "$PYTHON_VERSION" == "2.7" ) + #condition: "$TRAVIS_OS_NAME" == "osx" || ( "$TRAVIS_OS_NAME" == "linux" && "$PYTHON_VERSION" == "2.7" ) distributions: $DIST skip_cleanup: true From 3c2edf852b72ae0d4e66a778d0887ae098bed054 Mon Sep 17 00:00:00 2001 From: Max Zwiessele Date: Thu, 21 Apr 2016 12:54:20 +0100 Subject: [PATCH 4/4] [statespace] less restrictive test for regular statespace model --- GPy/testing/gpy_kernels_state_space_tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GPy/testing/gpy_kernels_state_space_tests.py b/GPy/testing/gpy_kernels_state_space_tests.py index 1dd8dc93..03eb3a85 100644 --- a/GPy/testing/gpy_kernels_state_space_tests.py +++ b/GPy/testing/gpy_kernels_state_space_tests.py @@ -230,7 +230,7 @@ class StateSpaceKernelsTests(np.testing.TestCase): use_cython=False, optimize_max_iters=10, check_gradients=True, predict_X=X, gp_kernel=gp_kernel, - mean_compare_decimal=5, var_compare_decimal=5) + mean_compare_decimal=2, var_compare_decimal=2) ss_kernel, gp_kernel = get_new_kernels() self.run_for_model(X, Y, ss_kernel, kalman_filter_type = 'svd',