mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-04-26 13:26:22 +02:00
Merge branch 'master' of github.com:SheffieldML/GPy
This commit is contained in:
commit
9b8c4eae25
8 changed files with 90 additions and 42 deletions
|
|
@ -194,7 +194,7 @@ def multiple_optima(gene_number=937,resolution=80, model_restarts=10, seed=10000
|
||||||
# Remove the mean (no bias kernel to ensure signal/noise is in RBF/white)
|
# Remove the mean (no bias kernel to ensure signal/noise is in RBF/white)
|
||||||
data['Y'] = data['Y'] - np.mean(data['Y'])
|
data['Y'] = data['Y'] - np.mean(data['Y'])
|
||||||
|
|
||||||
lls = GPy.examples.regression.contour_data(data, length_scales, log_SNRs, GPy.kern.rbf)
|
lls = GPy.examples.regression._contour_data(data, length_scales, log_SNRs, GPy.kern.rbf)
|
||||||
pb.contour(length_scales, log_SNRs, np.exp(lls), 20)
|
pb.contour(length_scales, log_SNRs, np.exp(lls), 20)
|
||||||
ax = pb.gca()
|
ax = pb.gca()
|
||||||
pb.xlabel('length scale')
|
pb.xlabel('length scale')
|
||||||
|
|
@ -229,7 +229,7 @@ def multiple_optima(gene_number=937,resolution=80, model_restarts=10, seed=10000
|
||||||
ax.set_ylim(ylim)
|
ax.set_ylim(ylim)
|
||||||
return (models, lls)
|
return (models, lls)
|
||||||
|
|
||||||
def contour_data(data, length_scales, log_SNRs, signal_kernel_call=GPy.kern.rbf):
|
def _contour_data(data, length_scales, log_SNRs, signal_kernel_call=GPy.kern.rbf):
|
||||||
"""Evaluate the GP objective function for a given data set for a range of signal to noise ratios and a range of lengthscales.
|
"""Evaluate the GP objective function for a given data set for a range of signal to noise ratios and a range of lengthscales.
|
||||||
|
|
||||||
:data_set: A data set from the utils.datasets director.
|
:data_set: A data set from the utils.datasets director.
|
||||||
|
|
|
||||||
|
|
@ -6,14 +6,14 @@
|
||||||
Code of Tutorials
|
Code of Tutorials
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def tuto_GP_regression():
|
|
||||||
"""The detailed explanations of the commands used in this file can be found in the tutorial section"""
|
|
||||||
|
|
||||||
import pylab as pb
|
import pylab as pb
|
||||||
pb.ion()
|
pb.ion()
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import GPy
|
import GPy
|
||||||
|
|
||||||
|
def tuto_GP_regression():
|
||||||
|
"""The detailed explanations of the commands used in this file can be found in the tutorial section"""
|
||||||
|
|
||||||
X = np.random.uniform(-3.,3.,(20,1))
|
X = np.random.uniform(-3.,3.,(20,1))
|
||||||
Y = np.sin(X) + np.random.randn(20,1)*0.05
|
Y = np.sin(X) + np.random.randn(20,1)*0.05
|
||||||
|
|
||||||
|
|
@ -39,11 +39,6 @@ def tuto_GP_regression():
|
||||||
# 2-dimensional example #
|
# 2-dimensional example #
|
||||||
###########################
|
###########################
|
||||||
|
|
||||||
import pylab as pb
|
|
||||||
pb.ion()
|
|
||||||
import numpy as np
|
|
||||||
import GPy
|
|
||||||
|
|
||||||
# sample inputs and outputs
|
# sample inputs and outputs
|
||||||
X = np.random.uniform(-3.,3.,(50,2))
|
X = np.random.uniform(-3.,3.,(50,2))
|
||||||
Y = np.sin(X[:,0:1]) * np.sin(X[:,1:2])+np.random.randn(50,1)*0.05
|
Y = np.sin(X[:,0:1]) * np.sin(X[:,1:2])+np.random.randn(50,1)*0.05
|
||||||
|
|
@ -67,9 +62,6 @@ def tuto_GP_regression():
|
||||||
|
|
||||||
def tuto_kernel_overview():
|
def tuto_kernel_overview():
|
||||||
"""The detailed explanations of the commands used in this file can be found in the tutorial section"""
|
"""The detailed explanations of the commands used in this file can be found in the tutorial section"""
|
||||||
import pylab as pb
|
|
||||||
import numpy as np
|
|
||||||
import GPy
|
|
||||||
pb.ion()
|
pb.ion()
|
||||||
|
|
||||||
ker1 = GPy.kern.rbf(1) # Equivalent to ker1 = GPy.kern.rbf(D=1, variance=1., lengthscale=1.)
|
ker1 = GPy.kern.rbf(1) # Equivalent to ker1 = GPy.kern.rbf(D=1, variance=1., lengthscale=1.)
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ class rbf(kernpart):
|
||||||
|
|
||||||
.. math::
|
.. math::
|
||||||
|
|
||||||
k(r) = \sigma^2 \exp(- \frac{1}{2}r^2) \ \ \ \ \ \\text{ where } r^2 = \sum_{i=1}^d \frac{ (x_i-x^\prime_i)^2}{\ell_i^2}}
|
k(r) = \sigma^2 \exp \\bigg(- \\frac{1}{2} r^2 \\bigg) \ \ \ \ \ \\text{ where } r^2 = \sum_{i=1}^d \\frac{ (x_i-x^\prime_i)^2}{\ell_i^2}
|
||||||
|
|
||||||
where \ell_i is the lengthscale, \sigma^2 the variance and d the dimensionality of the input.
|
where \ell_i is the lengthscale, \sigma^2 the variance and d the dimensionality of the input.
|
||||||
|
|
||||||
|
|
@ -55,7 +55,6 @@ class rbf(kernpart):
|
||||||
self._X, self._X2, self._params = np.empty(shape=(3,1))
|
self._X, self._X2, self._params = np.empty(shape=(3,1))
|
||||||
|
|
||||||
def _get_params(self):
|
def _get_params(self):
|
||||||
foo
|
|
||||||
return np.hstack((self.variance,self.lengthscale))
|
return np.hstack((self.variance,self.lengthscale))
|
||||||
|
|
||||||
def _set_params(self,x):
|
def _set_params(self,x):
|
||||||
|
|
|
||||||
|
|
@ -83,3 +83,7 @@ class Bayesian_GPLVM(sparse_GP, GPLVM):
|
||||||
|
|
||||||
def _log_likelihood_gradients(self):
|
def _log_likelihood_gradients(self):
|
||||||
return np.hstack((self.dL_dmuS().flatten(), sparse_GP._log_likelihood_gradients(self)))
|
return np.hstack((self.dL_dmuS().flatten(), sparse_GP._log_likelihood_gradients(self)))
|
||||||
|
|
||||||
|
def plot_latent(self, *args, **kwargs):
|
||||||
|
input_1, input_2 = GPLVM.plot_latent(*args, **kwargs)
|
||||||
|
pb.plot(m.Z[:, input_1], m.Z[:, input_2], '^w')
|
||||||
|
|
|
||||||
|
|
@ -117,6 +117,4 @@ class GPLVM(GP):
|
||||||
pb.xlim(xmin[0],xmax[0])
|
pb.xlim(xmin[0],xmax[0])
|
||||||
pb.ylim(xmin[1],xmax[1])
|
pb.ylim(xmin[1],xmax[1])
|
||||||
|
|
||||||
|
return input_1, input_2
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -55,3 +55,7 @@ class sparse_GPLVM(sparse_GP_regression, GPLVM):
|
||||||
#passing Z without a small amout of jitter will induce the white kernel where we don;t want it!
|
#passing Z without a small amout of jitter will induce the white kernel where we don;t want it!
|
||||||
mu, var, upper, lower = sparse_GP_regression.predict(self, self.Z+np.random.randn(*self.Z.shape)*0.0001)
|
mu, var, upper, lower = sparse_GP_regression.predict(self, self.Z+np.random.randn(*self.Z.shape)*0.0001)
|
||||||
pb.plot(mu[:, 0] , mu[:, 1], 'ko')
|
pb.plot(mu[:, 0] , mu[:, 1], 'ko')
|
||||||
|
|
||||||
|
def plot_latent(self, *args, **kwargs):
|
||||||
|
input_1, input_2 = GPLVM.plot_latent(*args, **kwargs)
|
||||||
|
pb.plot(m.Z[:, input_1], m.Z[:, input_2], '^w')
|
||||||
|
|
|
||||||
|
|
@ -4,22 +4,73 @@
|
||||||
import unittest
|
import unittest
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import GPy
|
import GPy
|
||||||
|
import inspect
|
||||||
|
import pkgutil
|
||||||
|
import os
|
||||||
|
import random
|
||||||
|
|
||||||
|
|
||||||
class ExamplesTests(unittest.TestCase):
|
class ExamplesTests(unittest.TestCase):
|
||||||
def test_check_model_returned(self):
|
def _checkgrad(self, model):
|
||||||
pass
|
self.assertTrue(model.checkgrad())
|
||||||
|
|
||||||
def test_model_checkgrads(self):
|
def _model_instance(self, model):
|
||||||
pass
|
self.assertTrue(isinstance(model, GPy.models))
|
||||||
|
|
||||||
def test_all_examples(self):
|
"""
|
||||||
pass
|
def model_instance_generator(model):
|
||||||
#Load models
|
def check_model_returned(self):
|
||||||
|
self._model_instance(model)
|
||||||
|
return check_model_returned
|
||||||
|
|
||||||
#Loop through models
|
def checkgrads_generator(model):
|
||||||
#for model in models:
|
def model_checkgrads(self):
|
||||||
#self.assertTrue(m.checkgrad())
|
self._checkgrad(model)
|
||||||
|
return model_checkgrads
|
||||||
|
"""
|
||||||
|
|
||||||
|
def model_checkgrads(model):
|
||||||
|
model.randomize()
|
||||||
|
assert model.checkgrad()
|
||||||
|
|
||||||
|
|
||||||
|
def model_instance(model):
|
||||||
|
assert isinstance(model, GPy.core.model)
|
||||||
|
|
||||||
|
|
||||||
|
def test_models():
|
||||||
|
examples_path = os.path.dirname(GPy.examples.__file__)
|
||||||
|
#Load modules
|
||||||
|
for loader, module_name, is_pkg in pkgutil.iter_modules([examples_path]):
|
||||||
|
#Load examples
|
||||||
|
module_examples = loader.find_module(module_name).load_module(module_name)
|
||||||
|
print "MODULE", module_examples
|
||||||
|
print "Before"
|
||||||
|
print inspect.getmembers(module_examples, predicate=inspect.isfunction)
|
||||||
|
functions = [ func for func in inspect.getmembers(module_examples, predicate=inspect.isfunction) if func[0].startswith('_') is False ][::-1]
|
||||||
|
print "After"
|
||||||
|
print functions
|
||||||
|
for example in functions:
|
||||||
|
print "Testing example: ", example[0]
|
||||||
|
#Generate model
|
||||||
|
model = example[1]()
|
||||||
|
print model
|
||||||
|
|
||||||
|
#Create tests for instance check
|
||||||
|
"""
|
||||||
|
test = model_instance_generator(model)
|
||||||
|
test.__name__ = 'test_instance_%s' % example[0]
|
||||||
|
setattr(ExamplesTests, test.__name__, test)
|
||||||
|
|
||||||
|
#Create tests for checkgrads check
|
||||||
|
test = checkgrads_generator(model)
|
||||||
|
test.__name__ = 'test_checkgrads_%s' % example[0]
|
||||||
|
setattr(ExamplesTests, test.__name__, test)
|
||||||
|
"""
|
||||||
|
model_checkgrads.description = 'test_checkgrads_%s' % example[0]
|
||||||
|
yield model_checkgrads, model
|
||||||
|
model_instance.description = 'test_instance_%s' % example[0]
|
||||||
|
yield model_instance, model
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print "Running unit tests, please be (very) patient..."
|
print "Running unit tests, please be (very) patient..."
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ We advise the reader to start with copy-pasting an existing kernel and to modify
|
||||||
|
|
||||||
**Header**
|
**Header**
|
||||||
|
|
||||||
The header is similar to all kernels::
|
The header is similar to all kernels: ::
|
||||||
|
|
||||||
from kernpart import kernpart
|
from kernpart import kernpart
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue