mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-06-08 15:05:15 +02:00
Nparams > num_params and Nparam_tranformed > num_params_transformed
This commit is contained in:
parent
35c2a8b521
commit
db78b233b8
25 changed files with 119 additions and 121 deletions
|
|
@ -33,8 +33,8 @@ class GP(GPBase):
|
|||
self._set_params(self._get_params())
|
||||
|
||||
def _set_params(self, p):
|
||||
self.kern._set_params_transformed(p[:self.kern.Nparam_transformed()])
|
||||
self.likelihood._set_params(p[self.kern.Nparam_transformed():])
|
||||
self.kern._set_params_transformed(p[:self.kern.num_params_transformed()])
|
||||
self.likelihood._set_params(p[self.kern.num_params_transformed():])
|
||||
|
||||
self.K = self.kern.K(self.X)
|
||||
self.K += self.likelihood.covariance_matrix
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ class model(parameterised):
|
|||
self.optimization_runs = []
|
||||
self.sampling_runs = []
|
||||
self.preferred_optimizer = 'scg'
|
||||
#self._set_params(self._get_params()) has been taken out as it should only be called on leaf nodes
|
||||
# self._set_params(self._get_params()) has been taken out as it should only be called on leaf nodes
|
||||
def _get_params(self):
|
||||
raise NotImplementedError, "this needs to be implemented to use the model class"
|
||||
def _set_params(self, x):
|
||||
|
|
@ -65,7 +65,7 @@ class model(parameterised):
|
|||
if len(tie_matches) > 1:
|
||||
raise ValueError, "cannot place Prior across multiple ties"
|
||||
elif len(tie_matches) == 1:
|
||||
which = which[:1] # just place a Prior object on the first parameter
|
||||
which = which[:1] # just place a Prior object on the first parameter
|
||||
|
||||
|
||||
# check constraints are okay
|
||||
|
|
@ -147,10 +147,10 @@ class model(parameterised):
|
|||
if self.priors is not None:
|
||||
[np.put(x, i, p.rvs(1)) for i, p in enumerate(self.priors) if not p is None]
|
||||
self._set_params(x)
|
||||
self._set_params_transformed(self._get_params_transformed()) # makes sure all of the tied parameters get the same init (since there's only one prior object...)
|
||||
self._set_params_transformed(self._get_params_transformed()) # makes sure all of the tied parameters get the same init (since there's only one prior object...)
|
||||
|
||||
|
||||
def optimize_restarts(self, Nrestarts=10, robust=False, verbose=True, parallel=False, num_processes=None, **kwargs):
|
||||
def optimize_restarts(self, num_restarts=10, robust=False, verbose=True, parallel=False, num_processes=None, **kwargs):
|
||||
"""
|
||||
Perform random restarts of the model, and set the model to the best
|
||||
seen solution.
|
||||
|
|
@ -179,19 +179,19 @@ class model(parameterised):
|
|||
try:
|
||||
jobs = []
|
||||
pool = mp.Pool(processes=num_processes)
|
||||
for i in range(Nrestarts):
|
||||
for i in range(num_restarts):
|
||||
self.randomize()
|
||||
job = pool.apply_async(opt_wrapper, args=(self,), kwds=kwargs)
|
||||
jobs.append(job)
|
||||
|
||||
pool.close() # signal that no more data coming in
|
||||
pool.join() # wait for all the tasks to complete
|
||||
pool.close() # signal that no more data coming in
|
||||
pool.join() # wait for all the tasks to complete
|
||||
except KeyboardInterrupt:
|
||||
print "Ctrl+c received, terminating and joining pool."
|
||||
pool.terminate()
|
||||
pool.join()
|
||||
|
||||
for i in range(Nrestarts):
|
||||
for i in range(num_restarts):
|
||||
try:
|
||||
if not parallel:
|
||||
self.randomize()
|
||||
|
|
@ -200,10 +200,10 @@ class model(parameterised):
|
|||
self.optimization_runs.append(jobs[i].get())
|
||||
|
||||
if verbose:
|
||||
print("Optimization restart {0}/{1}, f = {2}".format(i + 1, Nrestarts, self.optimization_runs[-1].f_opt))
|
||||
print("Optimization restart {0}/{1}, f = {2}".format(i + 1, num_restarts, self.optimization_runs[-1].f_opt))
|
||||
except Exception as e:
|
||||
if robust:
|
||||
print("Warning - optimization restart {0}/{1} failed".format(i + 1, Nrestarts))
|
||||
print("Warning - optimization restart {0}/{1} failed".format(i + 1, num_restarts))
|
||||
else:
|
||||
raise e
|
||||
|
||||
|
|
@ -222,7 +222,7 @@ class model(parameterised):
|
|||
currently_constrained = self.all_constrained_indices()
|
||||
to_make_positive = []
|
||||
for s in positive_strings:
|
||||
for i in self.grep_param_names(".*"+s):
|
||||
for i in self.grep_param_names(".*" + s):
|
||||
if not (i in currently_constrained):
|
||||
to_make_positive.append(i)
|
||||
if len(to_make_positive):
|
||||
|
|
@ -240,13 +240,13 @@ class model(parameterised):
|
|||
Gets the gradients from the likelihood and the priors.
|
||||
"""
|
||||
self._set_params_transformed(x)
|
||||
obj_grads = - self._transform_gradients(self._log_likelihood_gradients() + self._log_prior_gradients())
|
||||
obj_grads = -self._transform_gradients(self._log_likelihood_gradients() + self._log_prior_gradients())
|
||||
return obj_grads
|
||||
|
||||
def objective_and_gradients(self, x):
|
||||
self._set_params_transformed(x)
|
||||
obj_f = -self.log_likelihood() - self.log_prior()
|
||||
obj_grads = - self._transform_gradients(self._log_likelihood_gradients() + self._log_prior_gradients())
|
||||
obj_grads = -self._transform_gradients(self._log_likelihood_gradients() + self._log_prior_gradients())
|
||||
return obj_f, obj_grads
|
||||
|
||||
def optimize(self, optimizer=None, start=None, **kwargs):
|
||||
|
|
@ -315,7 +315,7 @@ class model(parameterised):
|
|||
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_params())
|
||||
width = np.array(max([len(p) for p in strs] + [5])) + 4
|
||||
|
||||
log_like = self.log_likelihood()
|
||||
|
|
@ -474,8 +474,8 @@ class model(parameterised):
|
|||
ll_change = new_ll - last_ll
|
||||
|
||||
if ll_change < 0:
|
||||
self.likelihood = last_approximation # restore previous likelihood approximation
|
||||
self._set_params(last_params) # restore model parameters
|
||||
self.likelihood = last_approximation # restore previous likelihood approximation
|
||||
self._set_params(last_params) # restore model parameters
|
||||
print "Log-likelihood decrement: %s \nLast likelihood update discarded." % ll_change
|
||||
stop = True
|
||||
else:
|
||||
|
|
|
|||
|
|
@ -6,8 +6,6 @@ import numpy as np
|
|||
import re
|
||||
import copy
|
||||
import cPickle
|
||||
import os
|
||||
from ..util.squashers import sigmoid
|
||||
import warnings
|
||||
import transformations
|
||||
|
||||
|
|
@ -113,7 +111,7 @@ class parameterised(object):
|
|||
if hasattr(self, 'prior'):
|
||||
pass
|
||||
|
||||
self._set_params_transformed(self._get_params_transformed()) # sets tied parameters to single value
|
||||
self._set_params_transformed(self._get_params_transformed()) # sets tied parameters to single value
|
||||
|
||||
def untie_everything(self):
|
||||
"""Unties all parameters by setting tied_indices to an empty list."""
|
||||
|
|
@ -145,7 +143,7 @@ class parameterised(object):
|
|||
else:
|
||||
return np.nonzero([regexp.match(name) for name in names])[0]
|
||||
|
||||
def Nparam_transformed(self):
|
||||
def num_params_transformed(self):
|
||||
removed = 0
|
||||
for tie in self.tied_indices:
|
||||
removed += tie.size - 1
|
||||
|
|
@ -159,18 +157,18 @@ class parameterised(object):
|
|||
"""Unconstrain matching parameters. does not untie parameters"""
|
||||
matches = self.grep_param_names(regexp)
|
||||
|
||||
#tranformed contraints:
|
||||
# tranformed contraints:
|
||||
for match in matches:
|
||||
self.constrained_indices = [i[i<>match] for i in self.constrained_indices]
|
||||
self.constrained_indices = [i[i <> match] for i in self.constrained_indices]
|
||||
|
||||
#remove empty constraints
|
||||
tmp = zip(*[(i,t) for i,t in zip(self.constrained_indices,self.constraints) if len(i)])
|
||||
# remove empty constraints
|
||||
tmp = zip(*[(i, t) for i, t in zip(self.constrained_indices, self.constraints) if len(i)])
|
||||
if tmp:
|
||||
self.constrained_indices, self.constraints = zip(*[(i,t) for i,t in zip(self.constrained_indices,self.constraints) if len(i)])
|
||||
self.constrained_indices, self.constraints = zip(*[(i, t) for i, t in zip(self.constrained_indices, self.constraints) if len(i)])
|
||||
self.constrained_indices, self.constraints = list(self.constrained_indices), list(self.constraints)
|
||||
|
||||
# fixed:
|
||||
self.fixed_values = [np.delete(values, np.nonzero(np.sum(indices[:, None] == matches[None, :], 1))[0]) for indices,values in zip(self.fixed_indices,self.fixed_values)]
|
||||
self.fixed_values = [np.delete(values, np.nonzero(np.sum(indices[:, None] == matches[None, :], 1))[0]) for indices, values in zip(self.fixed_indices, self.fixed_values)]
|
||||
self.fixed_indices = [np.delete(indices, np.nonzero(np.sum(indices[:, None] == matches[None, :], 1))[0]) for indices in self.fixed_indices]
|
||||
|
||||
# remove empty elements
|
||||
|
|
@ -189,7 +187,7 @@ class parameterised(object):
|
|||
""" Set positive constraints. """
|
||||
self.constrain(regexp, transformations.logexp())
|
||||
|
||||
def constrain_bounded(self, regexp,lower, upper):
|
||||
def constrain_bounded(self, regexp, lower, upper):
|
||||
""" Set bounded constraints. """
|
||||
self.constrain(regexp, transformations.logistic(lower, upper))
|
||||
|
||||
|
|
@ -199,8 +197,8 @@ class parameterised(object):
|
|||
else:
|
||||
return np.empty(shape=(0,))
|
||||
|
||||
def constrain(self,regexp,transform):
|
||||
assert isinstance(transform,transformations.transformation)
|
||||
def constrain(self, regexp, transform):
|
||||
assert isinstance(transform, transformations.transformation)
|
||||
|
||||
matches = self.grep_param_names(regexp)
|
||||
overlap = set(matches).intersection(set(self.all_constrained_indices()))
|
||||
|
|
@ -251,7 +249,7 @@ class parameterised(object):
|
|||
def _get_params_transformed(self):
|
||||
"""use self._get_params to get the 'true' parameters of the model, which are then tied, constrained and fixed"""
|
||||
x = self._get_params()
|
||||
[np.put(x,i,t.finv(x[i])) for i,t in zip(self.constrained_indices,self.constraints)]
|
||||
[np.put(x, i, t.finv(x[i])) for i, t in zip(self.constrained_indices, self.constraints)]
|
||||
|
||||
to_remove = self.fixed_indices + [t[1:] for t in self.tied_indices]
|
||||
if len(to_remove):
|
||||
|
|
@ -263,7 +261,7 @@ class parameterised(object):
|
|||
""" takes the vector x, which is then modified (by untying, reparameterising or inserting fixed values), and then call self._set_params"""
|
||||
self._set_params(self._untransform_params(x))
|
||||
|
||||
def _untransform_params(self,x):
|
||||
def _untransform_params(self, x):
|
||||
"""
|
||||
The transformation required for _set_params_transformed.
|
||||
|
||||
|
|
@ -290,9 +288,9 @@ class parameterised(object):
|
|||
[np.put(xx, i, v) for i, v in zip(self.fixed_indices, self.fixed_values)]
|
||||
[np.put(xx, i, v) for i, v in [(t[1:], xx[t[0]]) for t in self.tied_indices] ]
|
||||
|
||||
[np.put(xx,i,t.f(xx[i])) for i,t in zip(self.constrained_indices, self.constraints)]
|
||||
if hasattr(self,'debug'):
|
||||
stop
|
||||
[np.put(xx, i, t.f(xx[i])) for i, t in zip(self.constrained_indices, self.constraints)]
|
||||
if hasattr(self, 'debug'):
|
||||
stop # @UndefinedVariable
|
||||
|
||||
return xx
|
||||
|
||||
|
|
@ -316,7 +314,7 @@ class parameterised(object):
|
|||
remove = np.hstack((remove, np.hstack(self.fixed_indices)))
|
||||
|
||||
# add markers to show that some variables are constrained
|
||||
for i,t in zip(self.constrained_indices,self.constraints):
|
||||
for i, t in zip(self.constrained_indices, self.constraints):
|
||||
for ii in i:
|
||||
n[ii] = n[ii] + t.__str__()
|
||||
|
||||
|
|
@ -333,10 +331,10 @@ class parameterised(object):
|
|||
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() # map(str,self._get_params())
|
||||
# sort out the constraints
|
||||
constraints = [''] * len(names)
|
||||
for i,t in zip(self.constrained_indices,self.constraints):
|
||||
for i, t in zip(self.constrained_indices, self.constraints):
|
||||
for ii in i:
|
||||
constraints[ii] = t.__str__()
|
||||
for i in self.fixed_indices:
|
||||
|
|
@ -354,7 +352,7 @@ class parameterised(object):
|
|||
max_constraint = max([len(constraints[i]) for i in range(len(constraints))] + [len(header[2])])
|
||||
max_ties = max([len(ties[i]) for i in range(len(ties))] + [len(header[3])])
|
||||
cols = np.array([max_names, max_values, max_constraint, max_ties]) + 4
|
||||
columns = cols.sum()
|
||||
# columns = cols.sum()
|
||||
|
||||
header_string = ["{h:^{col}}".format(h=header[i], col=cols[i]) for i in range(len(cols))]
|
||||
header_string = map(lambda x: '|'.join(x), [header_string])
|
||||
|
|
|
|||
|
|
@ -153,8 +153,8 @@ class SparseGP(GPBase):
|
|||
|
||||
def _set_params(self, p):
|
||||
self.Z = p[:self.num_inducing * self.output_dim].reshape(self.num_inducing, self.input_dim)
|
||||
self.kern._set_params(p[self.Z.size:self.Z.size + self.kern.Nparam])
|
||||
self.likelihood._set_params(p[self.Z.size + self.kern.Nparam:])
|
||||
self.kern._set_params(p[self.Z.size:self.Z.size + self.kern.num_params])
|
||||
self.likelihood._set_params(p[self.Z.size + self.kern.num_params:])
|
||||
self._compute_kernel_matrices()
|
||||
self._computations()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue