mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-06-08 15:05:15 +02:00
Added optimize and plot for classification, non_gaussian and stochastic examples
This commit is contained in:
parent
68ece19211
commit
3cd808cccc
3 changed files with 132 additions and 121 deletions
|
|
@ -6,12 +6,11 @@
|
||||||
Gaussian Processes classification
|
Gaussian Processes classification
|
||||||
"""
|
"""
|
||||||
import pylab as pb
|
import pylab as pb
|
||||||
import numpy as np
|
|
||||||
import GPy
|
import GPy
|
||||||
|
|
||||||
default_seed = 10000
|
default_seed = 10000
|
||||||
|
|
||||||
def oil(num_inducing=50, max_iters=100, kernel=None):
|
def oil(num_inducing=50, max_iters=100, kernel=None, optimize=True, plot=True):
|
||||||
"""
|
"""
|
||||||
Run a Gaussian process classification on the three phase oil data. The demonstration calls the basic GP classification model and uses EP to approximate the likelihood.
|
Run a Gaussian process classification on the three phase oil data. The demonstration calls the basic GP classification model and uses EP to approximate the likelihood.
|
||||||
|
|
||||||
|
|
@ -33,6 +32,7 @@ def oil(num_inducing=50, max_iters=100, kernel=None):
|
||||||
m.update_likelihood_approximation()
|
m.update_likelihood_approximation()
|
||||||
|
|
||||||
# Optimize
|
# Optimize
|
||||||
|
if optimize:
|
||||||
m.optimize(max_iters=max_iters)
|
m.optimize(max_iters=max_iters)
|
||||||
print(m)
|
print(m)
|
||||||
|
|
||||||
|
|
@ -41,7 +41,7 @@ def oil(num_inducing=50, max_iters=100, kernel=None):
|
||||||
GPy.util.classification.conf_matrix(probs, Ytest)
|
GPy.util.classification.conf_matrix(probs, Ytest)
|
||||||
return m
|
return m
|
||||||
|
|
||||||
def toy_linear_1d_classification(seed=default_seed):
|
def toy_linear_1d_classification(seed=default_seed, optimize=True, plot=True):
|
||||||
"""
|
"""
|
||||||
Simple 1D classification example using EP approximation
|
Simple 1D classification example using EP approximation
|
||||||
|
|
||||||
|
|
@ -58,6 +58,7 @@ def toy_linear_1d_classification(seed=default_seed):
|
||||||
m = GPy.models.GPClassification(data['X'], Y)
|
m = GPy.models.GPClassification(data['X'], Y)
|
||||||
|
|
||||||
# Optimize
|
# Optimize
|
||||||
|
if optimize:
|
||||||
#m.update_likelihood_approximation()
|
#m.update_likelihood_approximation()
|
||||||
# Parameters optimization:
|
# Parameters optimization:
|
||||||
#m.optimize()
|
#m.optimize()
|
||||||
|
|
@ -65,14 +66,15 @@ def toy_linear_1d_classification(seed=default_seed):
|
||||||
m.pseudo_EM()
|
m.pseudo_EM()
|
||||||
|
|
||||||
# Plot
|
# Plot
|
||||||
|
if plot:
|
||||||
fig, axes = pb.subplots(2, 1)
|
fig, axes = pb.subplots(2, 1)
|
||||||
m.plot_f(ax=axes[0])
|
m.plot_f(ax=axes[0])
|
||||||
m.plot(ax=axes[1])
|
m.plot(ax=axes[1])
|
||||||
print(m)
|
|
||||||
|
|
||||||
|
print m
|
||||||
return m
|
return m
|
||||||
|
|
||||||
def toy_linear_1d_classification_laplace(seed=default_seed):
|
def toy_linear_1d_classification_laplace(seed=default_seed, optimize=True, plot=True):
|
||||||
"""
|
"""
|
||||||
Simple 1D classification example using Laplace approximation
|
Simple 1D classification example using Laplace approximation
|
||||||
|
|
||||||
|
|
@ -90,24 +92,25 @@ def toy_linear_1d_classification_laplace(seed=default_seed):
|
||||||
|
|
||||||
# Model definition
|
# Model definition
|
||||||
m = GPy.models.GPClassification(data['X'], Y, likelihood=laplace_likelihood)
|
m = GPy.models.GPClassification(data['X'], Y, likelihood=laplace_likelihood)
|
||||||
|
|
||||||
print m
|
print m
|
||||||
|
|
||||||
# Optimize
|
# Optimize
|
||||||
|
if optimize:
|
||||||
#m.update_likelihood_approximation()
|
#m.update_likelihood_approximation()
|
||||||
# Parameters optimization:
|
# Parameters optimization:
|
||||||
m.optimize('bfgs', messages=1)
|
m.optimize('bfgs', messages=1)
|
||||||
#m.pseudo_EM()
|
#m.pseudo_EM()
|
||||||
|
|
||||||
# Plot
|
# Plot
|
||||||
|
if plot:
|
||||||
fig, axes = pb.subplots(2, 1)
|
fig, axes = pb.subplots(2, 1)
|
||||||
m.plot_f(ax=axes[0])
|
m.plot_f(ax=axes[0])
|
||||||
m.plot(ax=axes[1])
|
m.plot(ax=axes[1])
|
||||||
print(m)
|
|
||||||
|
|
||||||
|
print m
|
||||||
return m
|
return m
|
||||||
|
|
||||||
|
def sparse_toy_linear_1d_classification(num_inducing=10, seed=default_seed, optimize=True, plot=True):
|
||||||
def sparse_toy_linear_1d_classification(num_inducing=10,seed=default_seed):
|
|
||||||
"""
|
"""
|
||||||
Sparse 1D classification example
|
Sparse 1D classification example
|
||||||
|
|
||||||
|
|
@ -125,20 +128,22 @@ def sparse_toy_linear_1d_classification(num_inducing=10,seed=default_seed):
|
||||||
m['.*len'] = 4.
|
m['.*len'] = 4.
|
||||||
|
|
||||||
# Optimize
|
# Optimize
|
||||||
|
if optimize:
|
||||||
#m.update_likelihood_approximation()
|
#m.update_likelihood_approximation()
|
||||||
# Parameters optimization:
|
# Parameters optimization:
|
||||||
#m.optimize()
|
#m.optimize()
|
||||||
m.pseudo_EM()
|
m.pseudo_EM()
|
||||||
|
|
||||||
# Plot
|
# Plot
|
||||||
|
if plot:
|
||||||
fig, axes = pb.subplots(2, 1)
|
fig, axes = pb.subplots(2, 1)
|
||||||
m.plot_f(ax=axes[0])
|
m.plot_f(ax=axes[0])
|
||||||
m.plot(ax=axes[1])
|
m.plot(ax=axes[1])
|
||||||
print(m)
|
|
||||||
|
|
||||||
|
print m
|
||||||
return m
|
return m
|
||||||
|
|
||||||
def toy_heaviside(seed=default_seed):
|
def toy_heaviside(seed=default_seed, optimize=True, plot=True):
|
||||||
"""
|
"""
|
||||||
Simple 1D classification example using a heavy side gp transformation
|
Simple 1D classification example using a heavy side gp transformation
|
||||||
|
|
||||||
|
|
@ -157,20 +162,22 @@ def toy_heaviside(seed=default_seed):
|
||||||
m = GPy.models.GPClassification(data['X'], likelihood=likelihood)
|
m = GPy.models.GPClassification(data['X'], likelihood=likelihood)
|
||||||
|
|
||||||
# Optimize
|
# Optimize
|
||||||
|
if optimize:
|
||||||
m.update_likelihood_approximation()
|
m.update_likelihood_approximation()
|
||||||
# Parameters optimization:
|
# Parameters optimization:
|
||||||
m.optimize()
|
m.optimize()
|
||||||
#m.pseudo_EM()
|
#m.pseudo_EM()
|
||||||
|
|
||||||
# Plot
|
# Plot
|
||||||
|
if plot:
|
||||||
fig, axes = pb.subplots(2, 1)
|
fig, axes = pb.subplots(2, 1)
|
||||||
m.plot_f(ax=axes[0])
|
m.plot_f(ax=axes[0])
|
||||||
m.plot(ax=axes[1])
|
m.plot(ax=axes[1])
|
||||||
print(m)
|
|
||||||
|
|
||||||
|
print m
|
||||||
return m
|
return m
|
||||||
|
|
||||||
def crescent_data(model_type='Full', num_inducing=10, seed=default_seed, kernel=None):
|
def crescent_data(model_type='Full', num_inducing=10, seed=default_seed, kernel=None, optimize=True, plot=True):
|
||||||
"""
|
"""
|
||||||
Run a Gaussian process classification on the crescent data. The demonstration calls the basic GP classification model and uses EP to approximate the likelihood.
|
Run a Gaussian process classification on the crescent data. The demonstration calls the basic GP classification model and uses EP to approximate the likelihood.
|
||||||
|
|
||||||
|
|
@ -197,8 +204,11 @@ def crescent_data(model_type='Full', num_inducing=10, seed=default_seed, kernel=
|
||||||
m = GPy.models.FITCClassification(data['X'], Y, kernel=kernel, num_inducing=num_inducing)
|
m = GPy.models.FITCClassification(data['X'], Y, kernel=kernel, num_inducing=num_inducing)
|
||||||
m['.*len'] = 3.
|
m['.*len'] = 3.
|
||||||
|
|
||||||
|
if optimize:
|
||||||
m.pseudo_EM()
|
m.pseudo_EM()
|
||||||
print(m)
|
|
||||||
|
if plot:
|
||||||
m.plot()
|
m.plot()
|
||||||
|
|
||||||
|
print m
|
||||||
return m
|
return m
|
||||||
|
|
|
||||||
|
|
@ -114,7 +114,7 @@ def student_t_approx(optimize=True, plot=True):
|
||||||
|
|
||||||
return m1, m2, m3, m4
|
return m1, m2, m3, m4
|
||||||
|
|
||||||
def boston_example():
|
def boston_example(optimize=True, plot=True):
|
||||||
import sklearn
|
import sklearn
|
||||||
from sklearn.cross_validation import KFold
|
from sklearn.cross_validation import KFold
|
||||||
optimizer='bfgs'
|
optimizer='bfgs'
|
||||||
|
|
@ -143,7 +143,6 @@ def boston_example():
|
||||||
noise = 1e-1 #np.exp(-2)
|
noise = 1e-1 #np.exp(-2)
|
||||||
rbf_len = 0.5
|
rbf_len = 0.5
|
||||||
data_axis_plot = 4
|
data_axis_plot = 4
|
||||||
plot = False
|
|
||||||
kernelstu = GPy.kern.rbf(X.shape[1]) + GPy.kern.white(X.shape[1]) + GPy.kern.bias(X.shape[1])
|
kernelstu = GPy.kern.rbf(X.shape[1]) + GPy.kern.white(X.shape[1]) + GPy.kern.bias(X.shape[1])
|
||||||
kernelgp = GPy.kern.rbf(X.shape[1]) + GPy.kern.white(X.shape[1]) + GPy.kern.bias(X.shape[1])
|
kernelgp = GPy.kern.rbf(X.shape[1]) + GPy.kern.white(X.shape[1]) + GPy.kern.bias(X.shape[1])
|
||||||
|
|
||||||
|
|
@ -158,17 +157,13 @@ def boston_example():
|
||||||
mgp['rbf_len'] = rbf_len
|
mgp['rbf_len'] = rbf_len
|
||||||
mgp['noise'] = noise
|
mgp['noise'] = noise
|
||||||
print mgp
|
print mgp
|
||||||
|
if optimize:
|
||||||
mgp.optimize(optimizer=optimizer, messages=messages)
|
mgp.optimize(optimizer=optimizer, messages=messages)
|
||||||
Y_test_pred = mgp.predict(X_test)
|
Y_test_pred = mgp.predict(X_test)
|
||||||
score_folds[1, n] = rmse(Y_test, Y_test_pred[0])
|
score_folds[1, n] = rmse(Y_test, Y_test_pred[0])
|
||||||
pred_density[1, n] = np.mean(mgp.log_predictive_density(X_test, Y_test))
|
pred_density[1, n] = np.mean(mgp.log_predictive_density(X_test, Y_test))
|
||||||
print mgp
|
print mgp
|
||||||
print pred_density
|
print pred_density
|
||||||
if plot:
|
|
||||||
plt.figure()
|
|
||||||
plt.scatter(X_test[:, data_axis_plot], Y_test_pred[0])
|
|
||||||
plt.scatter(X_test[:, data_axis_plot], Y_test, c='r', marker='x')
|
|
||||||
plt.title('GP gauss')
|
|
||||||
|
|
||||||
print "Gaussian Laplace GP"
|
print "Gaussian Laplace GP"
|
||||||
N, D = Y_train.shape
|
N, D = Y_train.shape
|
||||||
|
|
@ -181,20 +176,13 @@ def boston_example():
|
||||||
mg['rbf_len'] = rbf_len
|
mg['rbf_len'] = rbf_len
|
||||||
mg['noise'] = noise
|
mg['noise'] = noise
|
||||||
print mg
|
print mg
|
||||||
try:
|
if optimize:
|
||||||
mg.optimize(optimizer=optimizer, messages=messages)
|
mg.optimize(optimizer=optimizer, messages=messages)
|
||||||
except Exception:
|
|
||||||
print "Blew up"
|
|
||||||
Y_test_pred = mg.predict(X_test)
|
Y_test_pred = mg.predict(X_test)
|
||||||
score_folds[2, n] = rmse(Y_test, Y_test_pred[0])
|
score_folds[2, n] = rmse(Y_test, Y_test_pred[0])
|
||||||
pred_density[2, n] = np.mean(mg.log_predictive_density(X_test, Y_test))
|
pred_density[2, n] = np.mean(mg.log_predictive_density(X_test, Y_test))
|
||||||
print pred_density
|
print pred_density
|
||||||
print mg
|
print mg
|
||||||
if plot:
|
|
||||||
plt.figure()
|
|
||||||
plt.scatter(X_test[:, data_axis_plot], Y_test_pred[0])
|
|
||||||
plt.scatter(X_test[:, data_axis_plot], Y_test, c='r', marker='x')
|
|
||||||
plt.title('Lap gauss')
|
|
||||||
|
|
||||||
for stu_num, df in enumerate(degrees_freedoms):
|
for stu_num, df in enumerate(degrees_freedoms):
|
||||||
#Student T
|
#Student T
|
||||||
|
|
@ -208,16 +196,25 @@ def boston_example():
|
||||||
mstu_t['rbf_len'] = rbf_len
|
mstu_t['rbf_len'] = rbf_len
|
||||||
mstu_t['t_noise'] = noise
|
mstu_t['t_noise'] = noise
|
||||||
print mstu_t
|
print mstu_t
|
||||||
try:
|
if optimize:
|
||||||
mstu_t.optimize(optimizer=optimizer, messages=messages)
|
mstu_t.optimize(optimizer=optimizer, messages=messages)
|
||||||
except Exception:
|
|
||||||
print "Blew up"
|
|
||||||
Y_test_pred = mstu_t.predict(X_test)
|
Y_test_pred = mstu_t.predict(X_test)
|
||||||
score_folds[3+stu_num, n] = rmse(Y_test, Y_test_pred[0])
|
score_folds[3+stu_num, n] = rmse(Y_test, Y_test_pred[0])
|
||||||
pred_density[3+stu_num, n] = np.mean(mstu_t.log_predictive_density(X_test, Y_test))
|
pred_density[3+stu_num, n] = np.mean(mstu_t.log_predictive_density(X_test, Y_test))
|
||||||
print pred_density
|
print pred_density
|
||||||
print mstu_t
|
print mstu_t
|
||||||
|
|
||||||
if plot:
|
if plot:
|
||||||
|
plt.figure()
|
||||||
|
plt.scatter(X_test[:, data_axis_plot], Y_test_pred[0])
|
||||||
|
plt.scatter(X_test[:, data_axis_plot], Y_test, c='r', marker='x')
|
||||||
|
plt.title('GP gauss')
|
||||||
|
|
||||||
|
plt.figure()
|
||||||
|
plt.scatter(X_test[:, data_axis_plot], Y_test_pred[0])
|
||||||
|
plt.scatter(X_test[:, data_axis_plot], Y_test, c='r', marker='x')
|
||||||
|
plt.title('Lap gauss')
|
||||||
|
|
||||||
plt.figure()
|
plt.figure()
|
||||||
plt.scatter(X_test[:, data_axis_plot], Y_test_pred[0])
|
plt.scatter(X_test[:, data_axis_plot], Y_test_pred[0])
|
||||||
plt.scatter(X_test[:, data_axis_plot], Y_test, c='r', marker='x')
|
plt.scatter(X_test[:, data_axis_plot], Y_test, c='r', marker='x')
|
||||||
|
|
@ -226,6 +223,7 @@ def boston_example():
|
||||||
print "Average scores: {}".format(np.mean(score_folds, 1))
|
print "Average scores: {}".format(np.mean(score_folds, 1))
|
||||||
print "Average pred density: {}".format(np.mean(pred_density, 1))
|
print "Average pred density: {}".format(np.mean(pred_density, 1))
|
||||||
|
|
||||||
|
if plot:
|
||||||
#Plotting
|
#Plotting
|
||||||
stu_t_legends = ['Student T, df={}'.format(df) for df in degrees_freedoms]
|
stu_t_legends = ['Student T, df={}'.format(df) for df in degrees_freedoms]
|
||||||
legends = ['Baseline', 'Gaussian', 'Laplace Approx Gaussian'] + stu_t_legends
|
legends = ['Baseline', 'Gaussian', 'Laplace Approx Gaussian'] + stu_t_legends
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ import pylab as pb
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import GPy
|
import GPy
|
||||||
|
|
||||||
def toy_1d():
|
def toy_1d(optimize=True, plot=True):
|
||||||
N = 2000
|
N = 2000
|
||||||
M = 20
|
M = 20
|
||||||
|
|
||||||
|
|
@ -20,15 +20,18 @@ def toy_1d():
|
||||||
|
|
||||||
m.param_steplength = 1e-4
|
m.param_steplength = 1e-4
|
||||||
|
|
||||||
|
if plot:
|
||||||
fig = pb.figure()
|
fig = pb.figure()
|
||||||
ax = fig.add_subplot(111)
|
ax = fig.add_subplot(111)
|
||||||
def cb():
|
def cb(foo):
|
||||||
ax.cla()
|
ax.cla()
|
||||||
m.plot(ax=ax,Z_height=-3)
|
m.plot(ax=ax,Z_height=-3)
|
||||||
ax.set_ylim(-3,3)
|
ax.set_ylim(-3,3)
|
||||||
fig.canvas.draw()
|
fig.canvas.draw()
|
||||||
|
|
||||||
|
if optimize:
|
||||||
m.optimize(500, callback=cb, callback_interval=1)
|
m.optimize(500, callback=cb, callback_interval=1)
|
||||||
|
|
||||||
|
if plot:
|
||||||
m.plot_traces()
|
m.plot_traces()
|
||||||
return m
|
return m
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue