mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-06-11 15:15:15 +02:00
format on save
This commit is contained in:
parent
b0c5e137a2
commit
1df86e2216
2 changed files with 58 additions and 51 deletions
|
|
@ -5,6 +5,7 @@ import unittest
|
|||
import numpy as np
|
||||
import GPy
|
||||
|
||||
|
||||
class PEPgradienttest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
######################################
|
||||
|
|
@ -13,22 +14,25 @@ class PEPgradienttest(unittest.TestCase):
|
|||
|
||||
N = 20
|
||||
# sample inputs and outputs
|
||||
self.X1D = np.random.uniform(-3., 3., (N, 1))
|
||||
self.X1D = np.random.uniform(-3.0, 3.0, (N, 1))
|
||||
self.Y1D = np.sin(self.X1D) + np.random.randn(N, 1) * 0.05
|
||||
|
||||
######################################
|
||||
# # 2 dimensional example
|
||||
|
||||
# sample inputs and outputs
|
||||
self.X2D = np.random.uniform(-3., 3., (N, 2))
|
||||
self.Y2D = np.sin(self.X2D[:, 0:1]) * np.sin(self.X2D[:, 1:2]) + np.random.randn(N, 1) * 0.05
|
||||
self.X2D = np.random.uniform(-3.0, 3.0, (N, 2))
|
||||
self.Y2D = (
|
||||
np.sin(self.X2D[:, 0:1]) * np.sin(self.X2D[:, 1:2])
|
||||
+ np.random.randn(N, 1) * 0.05
|
||||
)
|
||||
|
||||
#######################################
|
||||
# # more datapoints, check in alpha limits, the log marginal likelihood
|
||||
# # is consistent with FITC and VFE/Var_DTC
|
||||
M = 5
|
||||
np.random.seed(42)
|
||||
self.X1 = np.c_[np.linspace(-1., 1., N)]
|
||||
self.X1 = np.c_[np.linspace(-1.0, 1.0, N)]
|
||||
self.Y1 = np.sin(self.X1) + np.random.randn(N, 1) * 0.05
|
||||
self.kernel = GPy.kern.RBF(input_dim=1, lengthscale=0.5, variance=1)
|
||||
self.Z = np.random.uniform(-1, 1, (M, 1))
|
||||
|
|
@ -36,59 +40,52 @@ class PEPgradienttest(unittest.TestCase):
|
|||
|
||||
def test_pep_1d_gradients(self):
|
||||
m = GPy.models.SparseGPRegression(self.X1D, self.Y1D)
|
||||
m.inference_method = GPy.inference.latent_function_inference.PEP(alpha=np.random.rand())
|
||||
self.assertTrue(m.checkgrad())
|
||||
m.inference_method = GPy.inference.latent_function_inference.PEP(
|
||||
alpha=np.random.rand()
|
||||
)
|
||||
assert m.checkgrad()
|
||||
|
||||
def test_pep_2d_gradients(self):
|
||||
m = GPy.models.SparseGPRegression(self.X2D, self.Y2D)
|
||||
m.inference_method = GPy.inference.latent_function_inference.PEP(alpha=np.random.rand())
|
||||
self.assertTrue(m.checkgrad())
|
||||
m.inference_method = GPy.inference.latent_function_inference.PEP(
|
||||
alpha=np.random.rand()
|
||||
)
|
||||
assert m.checkgrad()
|
||||
|
||||
def test_pep_vfe_consistency(self):
|
||||
vfe_model = GPy.models.SparseGPRegression(
|
||||
self.X1,
|
||||
self.Y1,
|
||||
kernel=self.kernel,
|
||||
Z=self.Z
|
||||
self.X1, self.Y1, kernel=self.kernel, Z=self.Z
|
||||
)
|
||||
vfe_model.inference_method = GPy.inference.latent_function_inference.VarDTC()
|
||||
vfe_model.Gaussian_noise.variance = self.lik_noise_var
|
||||
vfe_lml = vfe_model.log_likelihood()
|
||||
|
||||
pep_model = GPy.models.SparseGPRegression(
|
||||
self.X1,
|
||||
self.Y1,
|
||||
kernel=self.kernel,
|
||||
Z=self.Z
|
||||
self.X1, self.Y1, kernel=self.kernel, Z=self.Z
|
||||
)
|
||||
pep_model.inference_method = GPy.inference.latent_function_inference.PEP(
|
||||
alpha=1e-5
|
||||
)
|
||||
pep_model.inference_method = GPy.inference.latent_function_inference.PEP(alpha=1e-5)
|
||||
pep_model.Gaussian_noise.variance = self.lik_noise_var
|
||||
pep_lml = pep_model.log_likelihood()
|
||||
|
||||
self.assertAlmostEqual(vfe_lml[0, 0], pep_lml[0], delta=abs(0.01*pep_lml[0]))
|
||||
self.assertAlmostEqual(vfe_lml[0, 0], pep_lml[0], delta=abs(0.01 * pep_lml[0]))
|
||||
|
||||
def test_pep_fitc_consistency(self):
|
||||
fitc_model = GPy.models.SparseGPRegression(
|
||||
self.X1D,
|
||||
self.Y1D,
|
||||
kernel=self.kernel,
|
||||
Z=self.Z
|
||||
self.X1D, self.Y1D, kernel=self.kernel, Z=self.Z
|
||||
)
|
||||
fitc_model.inference_method = GPy.inference.latent_function_inference.FITC()
|
||||
fitc_model.Gaussian_noise.variance = self.lik_noise_var
|
||||
fitc_lml = fitc_model.log_likelihood()
|
||||
|
||||
pep_model = GPy.models.SparseGPRegression(
|
||||
self.X1D,
|
||||
self.Y1D,
|
||||
kernel=self.kernel,
|
||||
Z=self.Z
|
||||
self.X1D, self.Y1D, kernel=self.kernel, Z=self.Z
|
||||
)
|
||||
pep_model.inference_method = GPy.inference.latent_function_inference.PEP(
|
||||
alpha=1
|
||||
)
|
||||
pep_model.inference_method = GPy.inference.latent_function_inference.PEP(alpha=1)
|
||||
pep_model.Gaussian_noise.variance = self.lik_noise_var
|
||||
pep_lml = pep_model.log_likelihood()
|
||||
|
||||
self.assertAlmostEqual(fitc_lml, pep_lml[0], delta=abs(0.001*pep_lml[0]))
|
||||
|
||||
|
||||
|
||||
self.assertAlmostEqual(fitc_lml, pep_lml[0], delta=abs(0.001 * pep_lml[0]))
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
'''
|
||||
"""
|
||||
Created on 13 Mar 2014
|
||||
|
||||
@author: maxz
|
||||
'''
|
||||
"""
|
||||
import unittest, itertools
|
||||
#import cPickle as pickle
|
||||
|
||||
# import cPickle as pickle
|
||||
import pickle
|
||||
import numpy as np
|
||||
import tempfile
|
||||
|
|
@ -14,28 +15,37 @@ from GPy.models.gp_regression import GPRegression
|
|||
import GPy
|
||||
from nose import SkipTest
|
||||
|
||||
|
||||
def toy_model():
|
||||
X = np.linspace(0,1,50)[:, None]
|
||||
X = np.linspace(0, 1, 50)[:, None]
|
||||
Y = np.sin(X)
|
||||
m = GPRegression(X=X, Y=Y)
|
||||
return m
|
||||
|
||||
|
||||
class ListDictTestCase(unittest.TestCase):
|
||||
def assertListDictEquals(self, d1, d2, msg=None):
|
||||
#py3 fix
|
||||
#for k,v in d1.iteritems():
|
||||
for k,v in d1.items():
|
||||
# py3 fix
|
||||
# for k,v in d1.iteritems():
|
||||
for k, v in d1.items():
|
||||
self.assertListEqual(list(v), list(d2[k]), msg)
|
||||
|
||||
def assertArrayListEquals(self, l1, l2):
|
||||
for a1, a2 in zip(l1,l2):
|
||||
for a1, a2 in zip(l1, l2):
|
||||
np.testing.assert_array_equal(a1, a2)
|
||||
|
||||
|
||||
class Test(ListDictTestCase):
|
||||
@SkipTest
|
||||
def test_load_pickle(self):
|
||||
import os
|
||||
m = GPy.load(os.path.join(os.path.abspath(os.path.split(__file__)[0]), 'pickle_test.pickle'))
|
||||
self.assertTrue(m.checkgrad())
|
||||
|
||||
m = GPy.load(
|
||||
os.path.join(
|
||||
os.path.abspath(os.path.split(__file__)[0]), "pickle_test.pickle"
|
||||
)
|
||||
)
|
||||
assert m.checkgrad()
|
||||
self.assertEqual(m.log_likelihood(), -4.7351019830022087)
|
||||
|
||||
def test_model(self):
|
||||
|
|
@ -47,8 +57,8 @@ class Test(ListDictTestCase):
|
|||
self.assertIsNot(par.param_array, pcopy.param_array)
|
||||
self.assertIsNot(par.gradient_full, pcopy.gradient_full)
|
||||
self.assertTrue(pcopy.checkgrad())
|
||||
self.assert_(np.any(pcopy.gradient!=0.0))
|
||||
with tempfile.TemporaryFile('w+b') as f:
|
||||
self.assert_(np.any(pcopy.gradient != 0.0))
|
||||
with tempfile.TemporaryFile("w+b") as f:
|
||||
par.pickle(f)
|
||||
f.seek(0)
|
||||
pcopy = pickle.load(f)
|
||||
|
|
@ -66,10 +76,10 @@ class Test(ListDictTestCase):
|
|||
self.assertIsNot(par.param_array, pcopy.param_array)
|
||||
self.assertIsNot(par.gradient_full, pcopy.gradient_full)
|
||||
self.assertTrue(pcopy.checkgrad())
|
||||
self.assert_(np.any(pcopy.gradient!=0.0))
|
||||
self.assert_(np.any(pcopy.gradient != 0.0))
|
||||
np.testing.assert_allclose(pcopy.param_array, par.param_array, atol=1e-6)
|
||||
par.randomize()
|
||||
with tempfile.TemporaryFile('w+b') as f:
|
||||
with tempfile.TemporaryFile("w+b") as f:
|
||||
par.pickle(f)
|
||||
f.seek(0)
|
||||
pcopy = pickle.load(f)
|
||||
|
|
@ -79,9 +89,9 @@ class Test(ListDictTestCase):
|
|||
self.assert_(pcopy.checkgrad())
|
||||
|
||||
def test_posterior(self):
|
||||
X = np.random.randn(3,5)
|
||||
X = np.random.randn(3, 5)
|
||||
Xv = np.random.rand(*X.shape)
|
||||
par = NormalPosterior(X,Xv)
|
||||
par = NormalPosterior(X, Xv)
|
||||
par.gradient = 10
|
||||
pcopy = par.copy()
|
||||
pcopy.gradient = 10
|
||||
|
|
@ -90,7 +100,7 @@ class Test(ListDictTestCase):
|
|||
self.assertSequenceEqual(str(par), str(pcopy))
|
||||
self.assertIsNot(par.param_array, pcopy.param_array)
|
||||
self.assertIsNot(par.gradient_full, pcopy.gradient_full)
|
||||
with tempfile.TemporaryFile('w+b') as f:
|
||||
with tempfile.TemporaryFile("w+b") as f:
|
||||
par.pickle(f)
|
||||
f.seek(0)
|
||||
pcopy = pickle.load(f)
|
||||
|
|
@ -111,8 +121,8 @@ class Test(ListDictTestCase):
|
|||
self.assertIsNot(par.gradient_full, pcopy.gradient_full)
|
||||
self.assertTrue(par.checkgrad())
|
||||
self.assertTrue(pcopy.checkgrad())
|
||||
self.assert_(np.any(pcopy.gradient!=0.0))
|
||||
with tempfile.TemporaryFile('w+b') as f:
|
||||
self.assert_(np.any(pcopy.gradient != 0.0))
|
||||
with tempfile.TemporaryFile("w+b") as f:
|
||||
par.pickle(f)
|
||||
f.seek(0)
|
||||
pcopy = pickle.load(f)
|
||||
|
|
@ -126,5 +136,5 @@ class Test(ListDictTestCase):
|
|||
|
||||
|
||||
if __name__ == "__main__":
|
||||
#import sys;sys.argv = ['', 'Test.test_parameter_index_operations']
|
||||
# import sys;sys.argv = ['', 'Test.test_parameter_index_operations']
|
||||
unittest.main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue