mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-05-08 03:22:38 +02:00
Merge branch 'devel' of https://github.com/SheffieldML/GPy into devel
This commit is contained in:
commit
e9b4e3c770
7 changed files with 43 additions and 19 deletions
|
|
@ -89,6 +89,13 @@ class Param(OptimizationHandlable, ObsAr):
|
||||||
def param_array(self):
|
def param_array(self):
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
@property
|
||||||
|
def values(self):
|
||||||
|
"""
|
||||||
|
Return self as numpy array view
|
||||||
|
"""
|
||||||
|
return self.view(np.ndarray)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def gradient(self):
|
def gradient(self):
|
||||||
"""
|
"""
|
||||||
|
|
@ -99,11 +106,11 @@ class Param(OptimizationHandlable, ObsAr):
|
||||||
"""
|
"""
|
||||||
if getattr(self, '_gradient_array_', None) is None:
|
if getattr(self, '_gradient_array_', None) is None:
|
||||||
self._gradient_array_ = numpy.empty(self._realshape_, dtype=numpy.float64)
|
self._gradient_array_ = numpy.empty(self._realshape_, dtype=numpy.float64)
|
||||||
return self._gradient_array_[self._current_slice_]
|
return self._gradient_array_#[self._current_slice_]
|
||||||
|
|
||||||
@gradient.setter
|
@gradient.setter
|
||||||
def gradient(self, val):
|
def gradient(self, val):
|
||||||
self._gradient_array_[self._current_slice_] = val
|
self._gradient_array_[:] = val
|
||||||
|
|
||||||
#===========================================================================
|
#===========================================================================
|
||||||
# Array operations -> done
|
# Array operations -> done
|
||||||
|
|
@ -114,7 +121,10 @@ class Param(OptimizationHandlable, ObsAr):
|
||||||
#if not reduce(lambda a, b: a or numpy.any(b is Ellipsis), s, False) and len(s) <= self.ndim:
|
#if not reduce(lambda a, b: a or numpy.any(b is Ellipsis), s, False) and len(s) <= self.ndim:
|
||||||
# s += (Ellipsis,)
|
# s += (Ellipsis,)
|
||||||
new_arr = super(Param, self).__getitem__(s, *args, **kwargs)
|
new_arr = super(Param, self).__getitem__(s, *args, **kwargs)
|
||||||
try: new_arr._current_slice_ = s; new_arr._original_ = self.base is new_arr.base
|
try:
|
||||||
|
new_arr._current_slice_ = s
|
||||||
|
new_arr._gradient_array_ = self.gradient[s]
|
||||||
|
new_arr._original_ = self.base is new_arr.base
|
||||||
except AttributeError: pass # returning 0d array or float, double etc
|
except AttributeError: pass # returning 0d array or float, double etc
|
||||||
return new_arr
|
return new_arr
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -713,6 +713,10 @@ class Parameterizable(OptimizationHandlable):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def param_array(self):
|
def param_array(self):
|
||||||
|
"""
|
||||||
|
Array representing the parameters of this class.
|
||||||
|
There is only one copy of all parameters in memory, two during optimization.
|
||||||
|
"""
|
||||||
if self._param_array_ is None:
|
if self._param_array_ is None:
|
||||||
self._param_array_ = np.empty(self.size, dtype=np.float64)
|
self._param_array_ = np.empty(self.size, dtype=np.float64)
|
||||||
return self._param_array_
|
return self._param_array_
|
||||||
|
|
|
||||||
|
|
@ -100,6 +100,9 @@ class VariationalPosterior(Parameterized):
|
||||||
n.__dict__.update(dc)
|
n.__dict__.update(dc)
|
||||||
n._parameters_[dc['mean']._parent_index_] = dc['mean']
|
n._parameters_[dc['mean']._parent_index_] = dc['mean']
|
||||||
n._parameters_[dc['variance']._parent_index_] = dc['variance']
|
n._parameters_[dc['variance']._parent_index_] = dc['variance']
|
||||||
|
n._gradient_array_ = None
|
||||||
|
oversize = self.size - self.mean.size - self.variance.size
|
||||||
|
n.size = n.mean.size + n.variance.size + oversize
|
||||||
n.ndim = n.mean.ndim
|
n.ndim = n.mean.ndim
|
||||||
n.shape = n.mean.shape
|
n.shape = n.mean.shape
|
||||||
n.num_data = n.mean.shape[0]
|
n.num_data = n.mean.shape[0]
|
||||||
|
|
|
||||||
|
|
@ -408,13 +408,13 @@ def stick(kernel=None, optimize=True, verbose=True, plot=True):
|
||||||
data = GPy.util.datasets.osu_run1()
|
data = GPy.util.datasets.osu_run1()
|
||||||
# optimize
|
# optimize
|
||||||
m = GPy.models.GPLVM(data['Y'], 2, kernel=kernel)
|
m = GPy.models.GPLVM(data['Y'], 2, kernel=kernel)
|
||||||
if optimize: m.optimize(messages=verbose, max_f_eval=10000)
|
if optimize: m.optimize('bfgs', messages=verbose, max_f_eval=10000)
|
||||||
if plot:
|
if plot:
|
||||||
plt.clf
|
plt.clf
|
||||||
ax = m.plot_latent()
|
ax = m.plot_latent()
|
||||||
y = m.Y[0, :]
|
y = m.Y[0, :]
|
||||||
data_show = GPy.plotting.matplot_dep.visualize.stick_show(y[None, :], connect=data['connect'])
|
data_show = GPy.plotting.matplot_dep.visualize.stick_show(y[None, :], connect=data['connect'])
|
||||||
vis = GPy.plotting.matplot_dep.visualize.lvm(m.X[0, :].copy(), m, data_show, latent_axes=ax)
|
vis = GPy.plotting.matplot_dep.visualize.lvm(m.X[:1, :].copy(), m, data_show, latent_axes=ax)
|
||||||
raw_input('Press enter to finish')
|
raw_input('Press enter to finish')
|
||||||
|
|
||||||
return m
|
return m
|
||||||
|
|
@ -475,24 +475,28 @@ def robot_wireless(optimize=True, verbose=True, plot=True):
|
||||||
def stick_bgplvm(model=None, optimize=True, verbose=True, plot=True):
|
def stick_bgplvm(model=None, optimize=True, verbose=True, plot=True):
|
||||||
from GPy.models import BayesianGPLVM
|
from GPy.models import BayesianGPLVM
|
||||||
from matplotlib import pyplot as plt
|
from matplotlib import pyplot as plt
|
||||||
|
import numpy as np
|
||||||
import GPy
|
import GPy
|
||||||
|
|
||||||
data = GPy.util.datasets.osu_run1()
|
data = GPy.util.datasets.osu_run1()
|
||||||
Q = 6
|
Q = 6
|
||||||
kernel = GPy.kern.RBF(Q, ARD=True) + GPy.kern.Bias(Q, _np.exp(-2)) + GPy.kern.White(Q, _np.exp(-2))
|
kernel = GPy.kern.RBF(Q, lengthscale=np.repeat(.5, Q), ARD=True)
|
||||||
m = BayesianGPLVM(data['Y'], Q, init="PCA", num_inducing=20, kernel=kernel)
|
m = BayesianGPLVM(data['Y'], Q, init="PCA", num_inducing=20, kernel=kernel)
|
||||||
|
|
||||||
|
m.data = data
|
||||||
|
m.likelihood.variance = 0.001
|
||||||
|
|
||||||
# optimize
|
# optimize
|
||||||
m.ensure_default_constraints()
|
if optimize: m.optimize('bfgs', messages=verbose, max_iters=800, xtol=1e-300, ftol=1e-300)
|
||||||
if optimize: m.optimize('scg', messages=verbose, max_iters=200, xtol=1e-300, ftol=1e-300)
|
|
||||||
m._set_params(m._get_params())
|
|
||||||
if plot:
|
if plot:
|
||||||
plt.clf, (latent_axes, sense_axes) = plt.subplots(1, 2)
|
plt.clf, (latent_axes, sense_axes) = plt.subplots(1, 2)
|
||||||
plt.sca(latent_axes)
|
plt.sca(latent_axes)
|
||||||
m.plot_latent()
|
m.plot_latent(ax=latent_axes)
|
||||||
y = m.likelihood.Y[0, :].copy()
|
y = m.Y[:1, :].copy()
|
||||||
data_show = GPy.plotting.matplot_dep.visualize.stick_show(y[None, :], connect=data['connect'])
|
data_show = GPy.plotting.matplot_dep.visualize.stick_show(y, connect=data['connect'])
|
||||||
GPy.plotting.matplot_dep.visualize.lvm_dimselect(m.X[0, :].copy(), m, data_show, latent_axes=latent_axes, sense_axes=sense_axes)
|
GPy.plotting.matplot_dep.visualize.lvm_dimselect(m.X.mean[:1, :].copy(), m, data_show, latent_axes=latent_axes, sense_axes=sense_axes)
|
||||||
raw_input('Press enter to finish')
|
plt.draw()
|
||||||
|
#raw_input('Press enter to finish')
|
||||||
|
|
||||||
return m
|
return m
|
||||||
|
|
||||||
|
|
@ -509,7 +513,7 @@ def cmu_mocap(subject='35', motion=['01'], in_place=True, optimize=True, verbose
|
||||||
if optimize: m.optimize(messages=verbose, max_f_eval=10000)
|
if optimize: m.optimize(messages=verbose, max_f_eval=10000)
|
||||||
if plot:
|
if plot:
|
||||||
ax = m.plot_latent()
|
ax = m.plot_latent()
|
||||||
y = m.likelihood.Y[0, :]
|
y = m.Y[0, :]
|
||||||
data_show = GPy.plotting.matplot_dep.visualize.skeleton_show(y[None, :], data['skel'])
|
data_show = GPy.plotting.matplot_dep.visualize.skeleton_show(y[None, :], data['skel'])
|
||||||
lvm_visualizer = GPy.plotting.matplot_dep.visualize.lvm(m.X[0, :].copy(), m, data_show, ax)
|
lvm_visualizer = GPy.plotting.matplot_dep.visualize.lvm(m.X[0, :].copy(), m, data_show, ax)
|
||||||
raw_input('Press enter to finish')
|
raw_input('Press enter to finish')
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ class BayesianGPLVM(SparseGP):
|
||||||
assert Z.shape[1] == X.shape[1]
|
assert Z.shape[1] == X.shape[1]
|
||||||
|
|
||||||
if kernel is None:
|
if kernel is None:
|
||||||
kernel = kern.RBF(input_dim, lengthscale=fracs, ARD=True) # + kern.white(input_dim)
|
kernel = kern.RBF(input_dim, lengthscale=1./fracs, ARD=True) # + kern.white(input_dim)
|
||||||
|
|
||||||
if likelihood is None:
|
if likelihood is None:
|
||||||
likelihood = Gaussian()
|
likelihood = Gaussian()
|
||||||
|
|
|
||||||
|
|
@ -103,7 +103,6 @@ class lvm(matplotlib_show):
|
||||||
else:
|
else:
|
||||||
vals = param_to_array(model.X)
|
vals = param_to_array(model.X)
|
||||||
|
|
||||||
vals = param_to_array(vals)
|
|
||||||
matplotlib_show.__init__(self, vals, axes=latent_axes)
|
matplotlib_show.__init__(self, vals, axes=latent_axes)
|
||||||
|
|
||||||
if isinstance(latent_axes,mpl.axes.Axes):
|
if isinstance(latent_axes,mpl.axes.Axes):
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,11 @@ def initialize_latent(init, input_dim, Y):
|
||||||
p = pca(Y)
|
p = pca(Y)
|
||||||
PC = p.project(Y, min(input_dim, Y.shape[1]))
|
PC = p.project(Y, min(input_dim, Y.shape[1]))
|
||||||
Xr[:PC.shape[0], :PC.shape[1]] = PC
|
Xr[:PC.shape[0], :PC.shape[1]] = PC
|
||||||
|
var = p.fracs[:input_dim]
|
||||||
else:
|
else:
|
||||||
var = Xr.var(0)
|
var = Xr.var(0)
|
||||||
return Xr, var/var.max()
|
|
||||||
return Xr, p.fracs[:input_dim]
|
Xr -= Xr.mean(0)
|
||||||
|
Xr /= Xr.var(0)
|
||||||
|
|
||||||
|
return Xr, var/var.max()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue