mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-05-24 14:15:14 +02:00
merged master
This commit is contained in:
commit
6c4528e4da
53 changed files with 1242 additions and 821 deletions
|
|
@ -1,28 +0,0 @@
|
|||
# Copyright (c) 2012, GPy authors (see AUTHORS.txt).
|
||||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
|
||||
|
||||
import numpy as np
|
||||
import pylab as pb
|
||||
import GPy
|
||||
np.random.seed(1)
|
||||
print "GPLVM with RBF kernel"
|
||||
|
||||
N = 100
|
||||
Q = 1
|
||||
D = 2
|
||||
X = np.random.rand(N, Q)
|
||||
k = GPy.kern.rbf(Q, 1.0, 2.0) + GPy.kern.white(Q, 0.00001)
|
||||
K = k.K(X)
|
||||
Y = np.random.multivariate_normal(np.zeros(N),K,D).T
|
||||
|
||||
m = GPy.models.GPLVM(Y, Q)
|
||||
m.constrain_positive('(rbf|bias|white)')
|
||||
|
||||
pb.figure()
|
||||
m.plot()
|
||||
pb.title('PCA initialisation')
|
||||
pb.figure()
|
||||
m.optimize(messages = 1)
|
||||
m.plot()
|
||||
pb.title('After optimisation')
|
||||
|
|
@ -1,51 +0,0 @@
|
|||
# Copyright (c) 2012, GPy authors (see AUTHORS.txt).
|
||||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
|
||||
|
||||
"""
|
||||
Simple Gaussian Processes regression with an RBF kernel
|
||||
"""
|
||||
import pylab as pb
|
||||
import numpy as np
|
||||
import GPy
|
||||
pb.ion()
|
||||
pb.close('all')
|
||||
|
||||
|
||||
######################################
|
||||
## 1 dimensional example
|
||||
|
||||
# sample inputs and outputs
|
||||
X = np.random.uniform(-3.,3.,(20,1))
|
||||
Y = np.sin(X)+np.random.randn(20,1)*0.05
|
||||
|
||||
# create simple GP model
|
||||
m = GPy.models.GP_regression(X,Y)
|
||||
|
||||
# contrain all parameters to be positive
|
||||
m.constrain_positive('')
|
||||
|
||||
# optimize and plot
|
||||
m.optimize('tnc', max_f_eval = 1000)
|
||||
m.plot()
|
||||
print(m)
|
||||
|
||||
######################################
|
||||
## 2 dimensional example
|
||||
|
||||
# sample inputs and outputs
|
||||
X = np.random.uniform(-3.,3.,(40,2))
|
||||
Y = np.sin(X[:,0:1]) * np.sin(X[:,1:2])+np.random.randn(40,1)*0.05
|
||||
|
||||
# create simple GP model
|
||||
m = GPy.models.GP_regression(X,Y)
|
||||
|
||||
# contrain all parameters to be positive
|
||||
m.constrain_positive('')
|
||||
# optimize and plot
|
||||
pb.figure()
|
||||
m.optimize('tnc', max_f_eval = 1000)
|
||||
m.plot()
|
||||
print(m)
|
||||
|
||||
|
||||
|
|
@ -1,33 +0,0 @@
|
|||
# Copyright (c) 2012, GPy authors (see AUTHORS.txt).
|
||||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
|
||||
|
||||
"""
|
||||
Simple one-dimensional Gaussian Processes with assorted kernel functions
|
||||
"""
|
||||
import pylab as pb
|
||||
import numpy as np
|
||||
import GPy
|
||||
|
||||
# sample inputs and outputs
|
||||
D = 1
|
||||
X = np.random.randn(10,D)*2
|
||||
X = np.linspace(-1.5,1.5,5)[:,None]
|
||||
X = np.append(X,[[5]],0)
|
||||
Y = np.sin(np.pi*X/2) #+np.random.randn(X.shape[0],1)*0.05
|
||||
|
||||
models = [GPy.models.GP_regression(X,Y, k) for k in (GPy.kern.rbf(D), GPy.kern.Matern52(D), GPy.kern.Matern32(D), GPy.kern.exponential(D), GPy.kern.linear(D) + GPy.kern.white(D), GPy.kern.bias(D) + GPy.kern.white(D))]
|
||||
|
||||
pb.figure(figsize=(12,8))
|
||||
for i,m in enumerate(models):
|
||||
m.constrain_positive('')
|
||||
m.optimize()
|
||||
pb.subplot(3,2,i+1)
|
||||
m.plot()
|
||||
#pb.title(m.kern.parts[0].name)
|
||||
|
||||
GPy.util.plot.align_subplots(3,2,(-3,6),(-2.5,2.5))
|
||||
|
||||
pb.show()
|
||||
|
||||
|
||||
0
GPy/examples/__init__.py
Normal file
0
GPy/examples/__init__.py
Normal file
|
|
@ -8,8 +8,6 @@ Simple Gaussian Processes classification
|
|||
import pylab as pb
|
||||
import numpy as np
|
||||
import GPy
|
||||
pb.ion()
|
||||
pb.close('all')
|
||||
|
||||
default_seed=10000
|
||||
######################################
|
||||
|
|
@ -27,7 +25,7 @@ def crescent_data(model_type='Full', inducing=10, seed=default_seed):
|
|||
likelihood = GPy.inference.likelihoods.probit(data['Y'])
|
||||
|
||||
if model_type=='Full':
|
||||
m = GPy.models.simple_GP_EP(data['X'],likelihood)
|
||||
m = GPy.models.GP_EP(data['X'],likelihood)
|
||||
else:
|
||||
# create sparse GP EP model
|
||||
m = GPy.models.sparse_GP_EP(data['X'],likelihood=likelihood,inducing=inducing,ep_proxy=model_type)
|
||||
|
|
@ -49,7 +47,7 @@ def oil():
|
|||
likelihood = GPy.inference.likelihoods.probit(data['Y'][:, 0:1])
|
||||
|
||||
# create simple GP model
|
||||
m = GPy.models.simple_GP_EP(data['X'],likelihood)
|
||||
m = GPy.models.GP_EP(data['X'],likelihood)
|
||||
|
||||
# contrain all parameters to be positive
|
||||
m.constrain_positive('')
|
||||
|
|
|
|||
|
|
@ -1,53 +0,0 @@
|
|||
# Copyright (c) 2012, GPy authors (see AUTHORS.txt).
|
||||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
|
||||
|
||||
import cPickle as pickle
|
||||
import numpy as np
|
||||
import pylab as pb
|
||||
import GPy
|
||||
import pylab as plt
|
||||
np.random.seed(1)
|
||||
|
||||
def plot_oil(X, theta, labels, label):
|
||||
plt.figure()
|
||||
X = X[:,np.argsort(theta)[:2]]
|
||||
flow_type = (X[labels[:,0]==1])
|
||||
plt.plot(flow_type[:,0], flow_type[:,1], 'rx')
|
||||
flow_type = (X[labels[:,1]==1])
|
||||
plt.plot(flow_type[:,0], flow_type[:,1], 'gx')
|
||||
flow_type = (X[labels[:,2]==1])
|
||||
plt.plot(flow_type[:,0], flow_type[:,1], 'bx')
|
||||
plt.title(label)
|
||||
|
||||
data = pickle.load(open('../util/datasets/oil_flow_3classes.pickle', 'r'))
|
||||
|
||||
Y = data['DataTrn']
|
||||
N, D = Y.shape
|
||||
selected = np.random.permutation(N)[:200]
|
||||
labels = data['DataTrnLbls'][selected]
|
||||
Y = Y[selected]
|
||||
N, D = Y.shape
|
||||
Y -= Y.mean(axis=0)
|
||||
Y /= Y.std(axis=0)
|
||||
|
||||
Q = 2
|
||||
m1 = GPy.models.sparse_GPLVM(Y, Q, M = 15)
|
||||
m1.constrain_positive('(rbf|bias|noise)')
|
||||
m1.constrain_bounded('white', 1e-6, 1.0)
|
||||
|
||||
plot_oil(m1.X, np.array([1,1]), labels, 'PCA initialization')
|
||||
# m.optimize(messages = True)
|
||||
m1.optimize('bfgs', messages = True)
|
||||
plot_oil(m1.X, np.array([1,1]), labels, 'sparse GPLVM')
|
||||
# pb.figure()
|
||||
# m.plot()
|
||||
# pb.title('PCA initialisation')
|
||||
# pb.figure()
|
||||
# m.optimize(messages = 1)
|
||||
# m.plot()
|
||||
# pb.title('After optimisation')
|
||||
m = GPy.models.GPLVM(Y, Q)
|
||||
m.constrain_positive('(white|rbf|bias|noise)')
|
||||
m.optimize()
|
||||
plot_oil(m.X, np.array([1,1]), labels, 'GPLVM')
|
||||
|
|
@ -8,8 +8,6 @@ Gaussian Processes regression examples
|
|||
import pylab as pb
|
||||
import numpy as np
|
||||
import GPy
|
||||
pb.ion()
|
||||
pb.close('all')
|
||||
|
||||
|
||||
def toy_rbf_1d():
|
||||
|
|
@ -19,10 +17,8 @@ def toy_rbf_1d():
|
|||
# create simple GP model
|
||||
m = GPy.models.GP_regression(data['X'],data['Y'])
|
||||
|
||||
# contrain all parameters to be positive
|
||||
m.constrain_positive('')
|
||||
|
||||
# optimize
|
||||
m.ensure_default_constraints()
|
||||
m.optimize()
|
||||
|
||||
# plot
|
||||
|
|
@ -37,10 +33,8 @@ def rogers_girolami_olympics():
|
|||
# create simple GP model
|
||||
m = GPy.models.GP_regression(data['X'],data['Y'])
|
||||
|
||||
# contrain all parameters to be positive
|
||||
m.constrain_positive('')
|
||||
|
||||
# optimize
|
||||
m.ensure_default_constraints()
|
||||
m.optimize()
|
||||
|
||||
# plot
|
||||
|
|
@ -48,6 +42,10 @@ def rogers_girolami_olympics():
|
|||
print(m)
|
||||
return m
|
||||
|
||||
def della_gatta_TRP63_gene_expression(number=942):
|
||||
"""Run a standard Gaussian process regression on the della Gatta et al TRP63 Gene Expression data set for a given gene number."""
|
||||
|
||||
|
||||
def toy_rbf_1d_50():
|
||||
"""Run a simple demonstration of a standard Gaussian process fitting it to data sampled from an RBF covariance."""
|
||||
data = GPy.util.datasets.toy_rbf_1d_50()
|
||||
|
|
@ -55,10 +53,8 @@ def toy_rbf_1d_50():
|
|||
# create simple GP model
|
||||
m = GPy.models.GP_regression(data['X'],data['Y'])
|
||||
|
||||
# contrain all parameters to be positive
|
||||
m.constrain_positive('')
|
||||
|
||||
# optimize
|
||||
m.ensure_default_constraints()
|
||||
m.optimize()
|
||||
|
||||
# plot
|
||||
|
|
@ -73,11 +69,95 @@ def silhouette():
|
|||
# create simple GP model
|
||||
m = GPy.models.GP_regression(data['X'],data['Y'])
|
||||
|
||||
# contrain all parameters to be positive
|
||||
m.constrain_positive('')
|
||||
|
||||
# optimize
|
||||
m.ensure_default_constraints()
|
||||
m.optimize()
|
||||
|
||||
print(m)
|
||||
return m
|
||||
|
||||
|
||||
def multiple_optima(gene_number=937,resolution=80, model_restarts=10, seed=10000):
|
||||
"""Show an example of a multimodal error surface for Gaussian process regression. Gene 939 has bimodal behaviour where the noisey mode is higher."""
|
||||
|
||||
# Contour over a range of length scales and signal/noise ratios.
|
||||
length_scales = np.linspace(0.1, 60., resolution)
|
||||
log_SNRs = np.linspace(-3., 4., resolution)
|
||||
|
||||
data = GPy.util.datasets.della_gatta_TRP63_gene_expression(gene_number)
|
||||
# Sub sample the data to ensure multiple optima
|
||||
#data['Y'] = data['Y'][0::2, :]
|
||||
#data['X'] = data['X'][0::2, :]
|
||||
|
||||
# Remove the mean (no bias kernel to ensure signal/noise is in RBF/white)
|
||||
data['Y'] = data['Y'] - np.mean(data['Y'])
|
||||
|
||||
lls = GPy.examples.regression.contour_data(data, length_scales, log_SNRs, GPy.kern.rbf)
|
||||
pb.contour(length_scales, log_SNRs, np.exp(lls), 20)
|
||||
ax = pb.gca()
|
||||
pb.xlabel('length scale')
|
||||
pb.ylabel('log_10 SNR')
|
||||
|
||||
xlim = ax.get_xlim()
|
||||
ylim = ax.get_ylim()
|
||||
|
||||
# Now run a few optimizations
|
||||
models = []
|
||||
optim_point_x = np.empty(2)
|
||||
optim_point_y = np.empty(2)
|
||||
np.random.seed(seed=seed)
|
||||
for i in range(0, model_restarts):
|
||||
kern = GPy.kern.rbf(1, variance=np.random.exponential(1.), lengthscale=np.random.exponential(50.)) + GPy.kern.white(1,variance=np.random.exponential(1.))
|
||||
|
||||
m = GPy.models.GP_regression(data['X'],data['Y'], kernel=kern)
|
||||
optim_point_x[0] = m.get('rbf_lengthscale')
|
||||
optim_point_y[0] = np.log10(m.get('rbf_variance')) - np.log10(m.get('white_variance'));
|
||||
|
||||
# optimize
|
||||
m.ensure_default_constraints()
|
||||
m.optimize(xtol=1e-6,ftol=1e-6)
|
||||
|
||||
optim_point_x[1] = m.get('rbf_lengthscale')
|
||||
optim_point_y[1] = np.log10(m.get('rbf_variance')) - np.log10(m.get('white_variance'));
|
||||
|
||||
pb.arrow(optim_point_x[0], optim_point_y[0], optim_point_x[1]-optim_point_x[0], optim_point_y[1]-optim_point_y[0], label=str(i), head_length=1, head_width=0.5, fc='k', ec='k')
|
||||
models.append(m)
|
||||
|
||||
ax.set_xlim(xlim)
|
||||
ax.set_ylim(ylim)
|
||||
return (models, lls)
|
||||
|
||||
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.
|
||||
|
||||
:data_set: A data set from the utils.datasets director.
|
||||
:length_scales: a list of length scales to explore for the contour plot.
|
||||
:log_SNRs: a list of base 10 logarithm signal to noise ratios to explore for the contour plot.
|
||||
:signal_kernel: a kernel to use for the 'signal' portion of the data."""
|
||||
|
||||
lls = []
|
||||
total_var = np.var(data['Y'])
|
||||
for log_SNR in log_SNRs:
|
||||
SNR = 10**log_SNR
|
||||
length_scale_lls = []
|
||||
for length_scale in length_scales:
|
||||
noise_var = 1.
|
||||
signal_var = SNR
|
||||
noise_var = noise_var/(noise_var + signal_var)*total_var
|
||||
signal_var = signal_var/(noise_var + signal_var)*total_var
|
||||
|
||||
signal_kernel = signal_kernel_call(1, variance=signal_var, lengthscale=length_scale)
|
||||
noise_kernel = GPy.kern.white(1, variance=noise_var)
|
||||
kernel = signal_kernel + noise_kernel
|
||||
K = kernel.K(data['X'])
|
||||
total_var = (np.dot(np.dot(data['Y'].T,GPy.util.linalg.pdinv(K)[0]), data['Y'])/data['Y'].shape[0])[0,0]
|
||||
noise_var *= total_var
|
||||
signal_var *= total_var
|
||||
|
||||
kernel = signal_kernel_call(1, variance=signal_var, lengthscale=length_scale) + GPy.kern.white(1, variance=noise_var)
|
||||
|
||||
model = GPy.models.GP_regression(data['X'], data['Y'], kernel=kernel)
|
||||
model.constrain_positive('')
|
||||
length_scale_lls.append(model.log_likelihood())
|
||||
lls.append(length_scale_lls)
|
||||
return np.array(lls)
|
||||
|
|
|
|||
|
|
@ -10,11 +10,11 @@ print "sparse GPLVM with RBF kernel"
|
|||
|
||||
N = 100
|
||||
M = 4
|
||||
Q = 1
|
||||
Q = 2
|
||||
D = 2
|
||||
#generate GPLVM-like data
|
||||
X = np.random.rand(N, Q)
|
||||
k = GPy.kern.rbf(Q, 1.0, 2.0) + GPy.kern.white(Q, 0.00001)
|
||||
k = GPy.kern.rbf(Q,1.,2*np.ones((1,))) + GPy.kern.white(Q, 0.00001)
|
||||
K = k.K(X)
|
||||
Y = np.random.multivariate_normal(np.zeros(N),K,D).T
|
||||
|
||||
|
|
|
|||
25
GPy/examples/unsupervised.py
Normal file
25
GPy/examples/unsupervised.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
"""
|
||||
Usupervised learning with Gaussian Processes.
|
||||
"""
|
||||
import pylab as pb
|
||||
import numpy as np
|
||||
import GPy
|
||||
|
||||
|
||||
######################################
|
||||
## Oil data subsampled to 100 points.
|
||||
def oil_100():
|
||||
data = GPy.util.datasets.oil_100()
|
||||
|
||||
# create simple GP model
|
||||
m = GPy.models.GPLVM(data['X'], 2)
|
||||
|
||||
|
||||
# optimize
|
||||
m.ensure_default_constraints()
|
||||
m.optimize()
|
||||
|
||||
# plot
|
||||
print(m)
|
||||
return m
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue