mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-05-24 14:15:14 +02:00
Merge branch 'devel' of https://github.com/SheffieldML/GPy into devel
This commit is contained in:
commit
2e6cac0d34
8 changed files with 87 additions and 57 deletions
|
|
@ -397,13 +397,17 @@ class Model(Parameterized):
|
|||
return np.nan
|
||||
return 0.5 * self._get_params().size * np.log(2 * np.pi) + self.log_likelihood() - hld
|
||||
|
||||
def __str__(self):
|
||||
s = Parameterized.__str__(self).split('\n')
|
||||
def __str__(self, names=None):
|
||||
if names is None:
|
||||
names = self._get_print_names()
|
||||
s = Parameterized.__str__(self, names=names).split('\n')
|
||||
# add priors to the string
|
||||
if self.priors is not None:
|
||||
strs = [str(p) if p is not None else '' for p in self.priors]
|
||||
else:
|
||||
strs = [''] * len(self._get_params())
|
||||
strs = [''] * len(self._get_param_names())
|
||||
name_indices = self.grep_param_names("|".join(names))
|
||||
strs = np.array(strs)[name_indices]
|
||||
width = np.array(max([len(p) for p in strs] + [5])) + 4
|
||||
|
||||
log_like = self.log_likelihood()
|
||||
|
|
|
|||
|
|
@ -27,6 +27,9 @@ class Parameterized(object):
|
|||
|
||||
def _get_param_names(self):
|
||||
raise NotImplementedError, "this needs to be implemented to use the Parameterized class"
|
||||
def _get_print_names(self):
|
||||
""" Override for which names to print out, when using print m """
|
||||
return self._get_param_names()
|
||||
|
||||
def pickle(self, filename, protocol=None):
|
||||
if protocol is None:
|
||||
|
|
@ -333,19 +336,26 @@ class Parameterized(object):
|
|||
n = [nn for i, nn in enumerate(n) if not i in remove]
|
||||
return n
|
||||
|
||||
def __str__(self, nw=30):
|
||||
@property
|
||||
def all(self):
|
||||
return self.__str__(self._get_param_names())
|
||||
|
||||
|
||||
def __str__(self, names=None, nw=30):
|
||||
"""
|
||||
Return a string describing the parameter names and their ties and constraints
|
||||
"""
|
||||
names = self._get_param_names()
|
||||
if names is None:
|
||||
names = self._get_print_names()
|
||||
name_indices = self.grep_param_names("|".join(names))
|
||||
N = len(names)
|
||||
|
||||
if not N:
|
||||
return "This object has no free parameters."
|
||||
header = ['Name', 'Value', 'Constraints', 'Ties']
|
||||
values = self._get_params() # map(str,self._get_params())
|
||||
values = self._get_params()[name_indices] # map(str,self._get_params())
|
||||
# sort out the constraints
|
||||
constraints = [''] * len(names)
|
||||
constraints = [''] * len(self._get_param_names())
|
||||
for i, t in zip(self.constrained_indices, self.constraints):
|
||||
for ii in i:
|
||||
constraints[ii] = t.__str__()
|
||||
|
|
|
|||
|
|
@ -194,6 +194,9 @@ class SparseGP(GPBase):
|
|||
return sum([['iip_%i_%i' % (i, j) for j in range(self.Z.shape[1])] for i in range(self.Z.shape[0])], [])\
|
||||
+ self.kern._get_param_names_transformed() + self.likelihood._get_param_names()
|
||||
|
||||
def _get_print_names(self):
|
||||
return self.kern._get_param_names_transformed() + self.likelihood._get_param_names()
|
||||
|
||||
def update_likelihood_approximation(self):
|
||||
"""
|
||||
Approximates a non-gaussian likelihood using Expectation Propagation
|
||||
|
|
|
|||
|
|
@ -110,7 +110,7 @@ class kern(Parameterized):
|
|||
transOffset = offset_copy(ax.transData, fig=fig,
|
||||
x=0., y= -2., units='points')
|
||||
transOffsetUp = offset_copy(ax.transData, fig=fig,
|
||||
x=0., y=2., units='points')
|
||||
x=0., y=1., units='points')
|
||||
for bar in bars:
|
||||
for patch, num in zip(bar.patches, np.arange(len(bar.patches))):
|
||||
height = patch.get_height()
|
||||
|
|
@ -119,7 +119,7 @@ class kern(Parameterized):
|
|||
c = 'w'
|
||||
t = TextPath((0, 0), "${xi}$".format(xi=xi), rotation=0, usetex=True, ha='center')
|
||||
transform = transOffset
|
||||
if patch.get_extents().height <= t.get_extents().height + 2:
|
||||
if patch.get_extents().height <= t.get_extents().height + 3:
|
||||
va = 'bottom'
|
||||
c = 'k'
|
||||
transform = transOffsetUp
|
||||
|
|
|
|||
|
|
@ -57,6 +57,7 @@ class BayesianGPLVM(SparseGP, GPLVM):
|
|||
return SparseGP.getstate(self) + [self.init]
|
||||
|
||||
def setstate(self, state):
|
||||
self._const_jitter = None
|
||||
self.init = state.pop()
|
||||
SparseGP.setstate(self, state)
|
||||
|
||||
|
|
@ -65,6 +66,9 @@ class BayesianGPLVM(SparseGP, GPLVM):
|
|||
S_names = sum([['X_variance_%i_%i' % (n, q) for q in range(self.input_dim)] for n in range(self.num_data)], [])
|
||||
return (X_names + S_names + SparseGP._get_param_names(self))
|
||||
|
||||
def _get_print_names(self):
|
||||
return SparseGP._get_print_names(self)
|
||||
|
||||
def _get_params(self):
|
||||
"""
|
||||
Horizontally stacks the parameters in order to present them to the optimizer.
|
||||
|
|
|
|||
|
|
@ -163,17 +163,28 @@ class MRD(Model):
|
|||
self._init_X(initx, self.likelihood_list)
|
||||
self._init_Z(initz, self.X)
|
||||
|
||||
def _get_param_names(self):
|
||||
# X_names = sum([['X_%i_%i' % (n, q) for q in range(self.input_dim)] for n in range(self.num_data)], [])
|
||||
# S_names = sum([['X_variance_%i_%i' % (n, q) for q in range(self.input_dim)] for n in range(self.num_data)], [])
|
||||
def _get_latent_param_names(self):
|
||||
n1 = self.gref._get_param_names()
|
||||
n1var = n1[:self.NQ * 2 + self.MQ]
|
||||
return n1var
|
||||
|
||||
|
||||
def _get_kernel_names(self):
|
||||
map_names = lambda ns, name: map(lambda x: "{1}_{0}".format(*x),
|
||||
itertools.izip(ns,
|
||||
itertools.repeat(name)))
|
||||
return list(itertools.chain(n1var, *(map_names(\
|
||||
SparseGP._get_param_names(g)[self.MQ:], n) \
|
||||
for g, n in zip(self.bgplvms, self.names))))
|
||||
kernel_names = (map_names(SparseGP._get_param_names(g)[self.MQ:], n) for g, n in zip(self.bgplvms, self.names))
|
||||
return kernel_names
|
||||
|
||||
def _get_param_names(self):
|
||||
# X_names = sum([['X_%i_%i' % (n, q) for q in range(self.input_dim)] for n in range(self.num_data)], [])
|
||||
# S_names = sum([['X_variance_%i_%i' % (n, q) for q in range(self.input_dim)] for n in range(self.num_data)], [])
|
||||
n1var = self._get_latent_param_names()
|
||||
kernel_names = self._get_kernel_names()
|
||||
return list(itertools.chain(n1var, *kernel_names))
|
||||
|
||||
def _get_print_names(self):
|
||||
return list(itertools.chain(*self._get_kernel_names()))
|
||||
|
||||
def _get_params(self):
|
||||
"""
|
||||
|
|
@ -329,7 +340,9 @@ class MRD(Model):
|
|||
"""
|
||||
if titles is None:
|
||||
titles = [r'${}$'.format(name) for name in self.names]
|
||||
ymax = reduce(max, [numpy.ceil(max(g.input_sensitivity())) for g in self.bgplvms])
|
||||
def plotf(i, g, ax):
|
||||
ax.set_ylim([0,ymax])
|
||||
g.kern.plot_ARD(ax=ax, title=titles[i], *args, **kwargs)
|
||||
fig = self._handle_plotting(fignum, ax, plotf, sharex=sharex, sharey=sharey)
|
||||
return fig
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ class GradientTests(unittest.TestCase):
|
|||
self.X2D = np.random.uniform(-3., 3., (40, 2))
|
||||
self.Y2D = np.sin(self.X2D[:, 0:1]) * np.sin(self.X2D[:, 1:2]) + np.random.randn(40, 1) * 0.05
|
||||
|
||||
def check_model_with_white(self, kern, model_type='GPRegression', dimension=1, uncertain_inputs=False):
|
||||
def check_model(self, kern, model_type='GPRegression', dimension=1, uncertain_inputs=False):
|
||||
# Get the correct gradients
|
||||
if dimension == 1:
|
||||
X = self.X1D
|
||||
|
|
@ -34,8 +34,8 @@ class GradientTests(unittest.TestCase):
|
|||
# Get model type (GPRegression, SparseGPRegression, etc)
|
||||
model_fit = getattr(GPy.models, model_type)
|
||||
|
||||
noise = GPy.kern.white(dimension)
|
||||
kern = kern + noise
|
||||
# noise = GPy.kern.white(dimension)
|
||||
kern = kern # + noise
|
||||
if uncertain_inputs:
|
||||
m = model_fit(X, Y, kernel=kern, X_variance=np.random.rand(X.shape[0], X.shape[1]))
|
||||
else:
|
||||
|
|
@ -47,135 +47,135 @@ class GradientTests(unittest.TestCase):
|
|||
def test_GPRegression_rbf_1d(self):
|
||||
''' Testing the GP regression with rbf kernel with white kernel on 1d data '''
|
||||
rbf = GPy.kern.rbf(1)
|
||||
self.check_model_with_white(rbf, model_type='GPRegression', dimension=1)
|
||||
self.check_model(rbf, model_type='GPRegression', dimension=1)
|
||||
|
||||
def test_GPRegression_rbf_2D(self):
|
||||
''' Testing the GP regression with rbf and white kernel on 2d data '''
|
||||
''' Testing the GP regression with rbf kernel on 2d data '''
|
||||
rbf = GPy.kern.rbf(2)
|
||||
self.check_model_with_white(rbf, model_type='GPRegression', dimension=2)
|
||||
self.check_model(rbf, model_type='GPRegression', dimension=2)
|
||||
|
||||
def test_GPRegression_rbf_ARD_2D(self):
|
||||
''' Testing the GP regression with rbf and white kernel on 2d data '''
|
||||
''' Testing the GP regression with rbf kernel on 2d data '''
|
||||
k = GPy.kern.rbf(2, ARD=True)
|
||||
self.check_model_with_white(k, model_type='GPRegression', dimension=2)
|
||||
self.check_model(k, model_type='GPRegression', dimension=2)
|
||||
|
||||
def test_GPRegression_mlp_1d(self):
|
||||
''' Testing the GP regression with mlp kernel with white kernel on 1d data '''
|
||||
mlp = GPy.kern.mlp(1)
|
||||
self.check_model_with_white(mlp, model_type='GPRegression', dimension=1)
|
||||
self.check_model(mlp, model_type='GPRegression', dimension=1)
|
||||
|
||||
def test_GPRegression_poly_1d(self):
|
||||
''' Testing the GP regression with polynomial kernel with white kernel on 1d data '''
|
||||
mlp = GPy.kern.poly(1, degree=5)
|
||||
self.check_model_with_white(mlp, model_type='GPRegression', dimension=1)
|
||||
self.check_model(mlp, model_type='GPRegression', dimension=1)
|
||||
|
||||
def test_GPRegression_matern52_1D(self):
|
||||
''' Testing the GP regression with matern52 kernel on 1d data '''
|
||||
matern52 = GPy.kern.Matern52(1)
|
||||
self.check_model_with_white(matern52, model_type='GPRegression', dimension=1)
|
||||
self.check_model(matern52, model_type='GPRegression', dimension=1)
|
||||
|
||||
def test_GPRegression_matern52_2D(self):
|
||||
''' Testing the GP regression with matern52 kernel on 2d data '''
|
||||
matern52 = GPy.kern.Matern52(2)
|
||||
self.check_model_with_white(matern52, model_type='GPRegression', dimension=2)
|
||||
self.check_model(matern52, model_type='GPRegression', dimension=2)
|
||||
|
||||
def test_GPRegression_matern52_ARD_2D(self):
|
||||
''' Testing the GP regression with matern52 kernel on 2d data '''
|
||||
matern52 = GPy.kern.Matern52(2, ARD=True)
|
||||
self.check_model_with_white(matern52, model_type='GPRegression', dimension=2)
|
||||
self.check_model(matern52, model_type='GPRegression', dimension=2)
|
||||
|
||||
def test_GPRegression_matern32_1D(self):
|
||||
''' Testing the GP regression with matern32 kernel on 1d data '''
|
||||
matern32 = GPy.kern.Matern32(1)
|
||||
self.check_model_with_white(matern32, model_type='GPRegression', dimension=1)
|
||||
self.check_model(matern32, model_type='GPRegression', dimension=1)
|
||||
|
||||
def test_GPRegression_matern32_2D(self):
|
||||
''' Testing the GP regression with matern32 kernel on 2d data '''
|
||||
matern32 = GPy.kern.Matern32(2)
|
||||
self.check_model_with_white(matern32, model_type='GPRegression', dimension=2)
|
||||
self.check_model(matern32, model_type='GPRegression', dimension=2)
|
||||
|
||||
def test_GPRegression_matern32_ARD_2D(self):
|
||||
''' Testing the GP regression with matern32 kernel on 2d data '''
|
||||
matern32 = GPy.kern.Matern32(2, ARD=True)
|
||||
self.check_model_with_white(matern32, model_type='GPRegression', dimension=2)
|
||||
self.check_model(matern32, model_type='GPRegression', dimension=2)
|
||||
|
||||
def test_GPRegression_exponential_1D(self):
|
||||
''' Testing the GP regression with exponential kernel on 1d data '''
|
||||
exponential = GPy.kern.exponential(1)
|
||||
self.check_model_with_white(exponential, model_type='GPRegression', dimension=1)
|
||||
self.check_model(exponential, model_type='GPRegression', dimension=1)
|
||||
|
||||
def test_GPRegression_exponential_2D(self):
|
||||
''' Testing the GP regression with exponential kernel on 2d data '''
|
||||
exponential = GPy.kern.exponential(2)
|
||||
self.check_model_with_white(exponential, model_type='GPRegression', dimension=2)
|
||||
self.check_model(exponential, model_type='GPRegression', dimension=2)
|
||||
|
||||
def test_GPRegression_exponential_ARD_2D(self):
|
||||
''' Testing the GP regression with exponential kernel on 2d data '''
|
||||
exponential = GPy.kern.exponential(2, ARD=True)
|
||||
self.check_model_with_white(exponential, model_type='GPRegression', dimension=2)
|
||||
self.check_model(exponential, model_type='GPRegression', dimension=2)
|
||||
|
||||
def test_GPRegression_bias_kern_1D(self):
|
||||
''' Testing the GP regression with bias kernel on 1d data '''
|
||||
bias = GPy.kern.bias(1)
|
||||
self.check_model_with_white(bias, model_type='GPRegression', dimension=1)
|
||||
self.check_model(bias, model_type='GPRegression', dimension=1)
|
||||
|
||||
def test_GPRegression_bias_kern_2D(self):
|
||||
''' Testing the GP regression with bias kernel on 2d data '''
|
||||
bias = GPy.kern.bias(2)
|
||||
self.check_model_with_white(bias, model_type='GPRegression', dimension=2)
|
||||
self.check_model(bias, model_type='GPRegression', dimension=2)
|
||||
|
||||
def test_GPRegression_linear_kern_1D_ARD(self):
|
||||
''' Testing the GP regression with linear kernel on 1d data '''
|
||||
linear = GPy.kern.linear(1, ARD=True)
|
||||
self.check_model_with_white(linear, model_type='GPRegression', dimension=1)
|
||||
self.check_model(linear, model_type='GPRegression', dimension=1)
|
||||
|
||||
def test_GPRegression_linear_kern_2D_ARD(self):
|
||||
''' Testing the GP regression with linear kernel on 2d data '''
|
||||
linear = GPy.kern.linear(2, ARD=True)
|
||||
self.check_model_with_white(linear, model_type='GPRegression', dimension=2)
|
||||
self.check_model(linear, model_type='GPRegression', dimension=2)
|
||||
|
||||
def test_GPRegression_linear_kern_1D(self):
|
||||
''' Testing the GP regression with linear kernel on 1d data '''
|
||||
linear = GPy.kern.linear(1)
|
||||
self.check_model_with_white(linear, model_type='GPRegression', dimension=1)
|
||||
self.check_model(linear, model_type='GPRegression', dimension=1)
|
||||
|
||||
def test_GPRegression_linear_kern_2D(self):
|
||||
''' Testing the GP regression with linear kernel on 2d data '''
|
||||
linear = GPy.kern.linear(2)
|
||||
self.check_model_with_white(linear, model_type='GPRegression', dimension=2)
|
||||
self.check_model(linear, model_type='GPRegression', dimension=2)
|
||||
|
||||
def test_SparseGPRegression_rbf_white_kern_1d(self):
|
||||
''' Testing the sparse GP regression with rbf kernel with white kernel on 1d data '''
|
||||
rbf = GPy.kern.rbf(1)
|
||||
self.check_model_with_white(rbf, model_type='SparseGPRegression', dimension=1)
|
||||
self.check_model(rbf, model_type='SparseGPRegression', dimension=1)
|
||||
|
||||
def test_SparseGPRegression_rbf_white_kern_2D(self):
|
||||
''' Testing the sparse GP regression with rbf and white kernel on 2d data '''
|
||||
''' Testing the sparse GP regression with rbf kernel on 2d data '''
|
||||
rbf = GPy.kern.rbf(2)
|
||||
self.check_model_with_white(rbf, model_type='SparseGPRegression', dimension=2)
|
||||
self.check_model(rbf, model_type='SparseGPRegression', dimension=2)
|
||||
|
||||
def test_SparseGPRegression_rbf_linear_white_kern_1D(self):
|
||||
''' Testing the sparse GP regression with rbf and white kernel on 2d data '''
|
||||
''' Testing the sparse GP regression with rbf kernel on 2d data '''
|
||||
rbflin = GPy.kern.rbf(1) + GPy.kern.linear(1)
|
||||
self.check_model_with_white(rbflin, model_type='SparseGPRegression', dimension=1)
|
||||
self.check_model(rbflin, model_type='SparseGPRegression', dimension=1)
|
||||
|
||||
def test_SparseGPRegression_rbf_linear_white_kern_2D(self):
|
||||
''' Testing the sparse GP regression with rbf and white kernel on 2d data '''
|
||||
''' Testing the sparse GP regression with rbf kernel on 2d data '''
|
||||
rbflin = GPy.kern.rbf(2) + GPy.kern.linear(2)
|
||||
self.check_model_with_white(rbflin, model_type='SparseGPRegression', dimension=2)
|
||||
self.check_model(rbflin, model_type='SparseGPRegression', dimension=2)
|
||||
|
||||
def test_SparseGPRegression_rbf_linear_white_kern_2D_uncertain_inputs(self):
|
||||
''' Testing the sparse GP regression with rbf, linear and white kernel on 2d data with uncertain inputs'''
|
||||
''' Testing the sparse GP regression with rbf, linear kernel on 2d data with uncertain inputs'''
|
||||
rbflin = GPy.kern.rbf(2) + GPy.kern.linear(2)
|
||||
self.check_model_with_white(rbflin, model_type='SparseGPRegression', dimension=2, uncertain_inputs=1)
|
||||
self.check_model(rbflin, model_type='SparseGPRegression', dimension=2, uncertain_inputs=1)
|
||||
|
||||
def test_SparseGPRegression_rbf_linear_white_kern_1D_uncertain_inputs(self):
|
||||
''' Testing the sparse GP regression with rbf, linear and white kernel on 1d data with uncertain inputs'''
|
||||
''' Testing the sparse GP regression with rbf, linear kernel on 1d data with uncertain inputs'''
|
||||
rbflin = GPy.kern.rbf(1) + GPy.kern.linear(1)
|
||||
self.check_model_with_white(rbflin, model_type='SparseGPRegression', dimension=1, uncertain_inputs=1)
|
||||
self.check_model(rbflin, model_type='SparseGPRegression', dimension=1, uncertain_inputs=1)
|
||||
|
||||
def test_GPLVM_rbf_bias_white_kern_2D(self):
|
||||
""" Testing GPLVM with rbf + bias and white kernel """
|
||||
""" Testing GPLVM with rbf + bias kernel """
|
||||
N, input_dim, D = 50, 1, 2
|
||||
X = np.random.rand(N, input_dim)
|
||||
k = GPy.kern.rbf(input_dim, 0.5, 0.9 * np.ones((1,))) + GPy.kern.bias(input_dim, 0.1) + GPy.kern.white(input_dim, 0.05)
|
||||
|
|
@ -185,7 +185,7 @@ class GradientTests(unittest.TestCase):
|
|||
self.assertTrue(m.checkgrad())
|
||||
|
||||
def test_GPLVM_rbf_linear_white_kern_2D(self):
|
||||
""" Testing GPLVM with rbf + bias and white kernel """
|
||||
""" Testing GPLVM with rbf + bias kernel """
|
||||
N, input_dim, D = 50, 1, 2
|
||||
X = np.random.rand(N, input_dim)
|
||||
k = GPy.kern.linear(input_dim) + GPy.kern.bias(input_dim, 0.1) + GPy.kern.white(input_dim, 0.05)
|
||||
|
|
|
|||
|
|
@ -1,15 +1,11 @@
|
|||
import os
|
||||
import pylab as pb
|
||||
import numpy as np
|
||||
import GPy
|
||||
import scipy.sparse
|
||||
import scipy.io
|
||||
import cPickle as pickle
|
||||
import urllib as url
|
||||
import zipfile
|
||||
import tarfile
|
||||
import gzip
|
||||
import zlib
|
||||
import datetime
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue