mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-05-27 14:25:16 +02:00
[paramz] fully integrated all tests running
This commit is contained in:
parent
e49c75ce2e
commit
dce82847a7
78 changed files with 1581 additions and 1222 deletions
|
|
@ -4,8 +4,6 @@ import warnings
|
|||
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
||||
|
||||
from . import core
|
||||
from .core.parameterization import transformations, priors
|
||||
constraints = transformations
|
||||
from . import models
|
||||
from . import mappings
|
||||
from . import inference
|
||||
|
|
@ -13,16 +11,16 @@ from . import util
|
|||
from . import examples
|
||||
from . import likelihoods
|
||||
from . import testing
|
||||
from numpy.testing import Tester
|
||||
from . import kern
|
||||
from . import plotting
|
||||
|
||||
# Direct imports for convenience:
|
||||
from .core import Model
|
||||
from .core.parameterization import Param, Parameterized, ObsAr
|
||||
from .core import ProbabilisticModel, priors
|
||||
from paramz import Param, Parameterized, ObsAr, transformations as constraints
|
||||
|
||||
from .__version__ import __version__
|
||||
|
||||
from numpy.testing import Tester
|
||||
#@nottest
|
||||
try:
|
||||
#Get rid of nose dependency by only ignoring if you have nose installed
|
||||
|
|
|
|||
|
|
@ -1,10 +1,8 @@
|
|||
# Copyright (c) 2012-2014, GPy authors (see AUTHORS.txt).
|
||||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
|
||||
from GPy.core.probabilistic_model import *
|
||||
from .parameterization.parameterized import adjust_name_for_printing, Parameterizable
|
||||
from .parameterization.param import Param, ParamConcatenation
|
||||
from .parameterization.observable_array import ObsAr
|
||||
from .probabilistic_model import ProbabilisticModel
|
||||
from .parameterization import Param, Parameterized
|
||||
|
||||
from .gp import GP
|
||||
from .svgp import SVGP
|
||||
|
|
|
|||
|
|
@ -2,21 +2,20 @@
|
|||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
|
||||
import numpy as np
|
||||
import sys
|
||||
from .. import kern
|
||||
from GPy.core.probabilistic_model import Model
|
||||
from .parameterization import ObsAr
|
||||
from .probabilistic_model import ProbabilisticModel
|
||||
from paramz import ObsAr
|
||||
from .mapping import Mapping
|
||||
from .. import likelihoods
|
||||
from ..inference.latent_function_inference import exact_gaussian_inference, expectation_propagation
|
||||
from .parameterization.variational import VariationalPosterior
|
||||
from .variational import VariationalPosterior
|
||||
|
||||
import logging
|
||||
import warnings
|
||||
from GPy.util.normalizer import MeanNorm
|
||||
logger = logging.getLogger("GP")
|
||||
|
||||
class GP(Model):
|
||||
class GP(ProbabilisticModel):
|
||||
"""
|
||||
General purpose Gaussian process model
|
||||
|
||||
|
|
|
|||
5
GPy/core/parameterization/__init__.py
Normal file
5
GPy/core/parameterization/__init__.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
# Copyright (c) 2012-2014, GPy authors (see AUTHORS.txt).
|
||||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
|
||||
from .param import Param
|
||||
from .parameterized import Parameterized
|
||||
8
GPy/core/parameterization/param.py
Normal file
8
GPy/core/parameterization/param.py
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
# Copyright (c) 2014, Max Zwiessele
|
||||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
|
||||
from paramz import Param
|
||||
from .priorizable import Priorizable
|
||||
|
||||
class Param(Param, Priorizable):
|
||||
pass
|
||||
52
GPy/core/parameterization/parameterized.py
Normal file
52
GPy/core/parameterization/parameterized.py
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
# Copyright (c) 2014, Max Zwiessele, James Hensman
|
||||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
|
||||
from paramz import Parameterized
|
||||
from .priorizable import Priorizable
|
||||
|
||||
import logging
|
||||
logger = logging.getLogger("parameters changed meta")
|
||||
|
||||
class Parameterized(Parameterized, Priorizable):
|
||||
"""
|
||||
Parameterized class
|
||||
|
||||
Say m is a handle to a parameterized class.
|
||||
|
||||
Printing parameters:
|
||||
|
||||
- print m: prints a nice summary over all parameters
|
||||
- print m.name: prints details for param with name 'name'
|
||||
- print m[regexp]: prints details for all the parameters
|
||||
which match (!) regexp
|
||||
- print m['']: prints details for all parameters
|
||||
|
||||
Fields:
|
||||
|
||||
Name: The name of the param, can be renamed!
|
||||
Value: Shape or value, if one-valued
|
||||
Constrain: constraint of the param, curly "{c}" brackets indicate
|
||||
some parameters are constrained by c. See detailed print
|
||||
to get exact constraints.
|
||||
Tied_to: which paramter it is tied to.
|
||||
|
||||
Getting and setting parameters:
|
||||
|
||||
Set all values in param to one:
|
||||
|
||||
m.name.to.param = 1
|
||||
|
||||
Handling of constraining, fixing and tieing parameters:
|
||||
|
||||
You can constrain parameters by calling the constrain on the param itself, e.g:
|
||||
|
||||
- m.name[:,1].constrain_positive()
|
||||
- m.name[0].tie_to(m.name[1])
|
||||
|
||||
Fixing parameters will fix them to the value they are right now. If you change
|
||||
the parameters value, the param will be fixed to the new value!
|
||||
|
||||
If you want to operate on all parameters use m[''] to wildcard select all paramters
|
||||
and concatenate them. Printing m[''] will result in printing of all parameters in detail.
|
||||
"""
|
||||
pass
|
||||
76
GPy/core/parameterization/priorizable.py
Normal file
76
GPy/core/parameterization/priorizable.py
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
# Copyright (c) 2012-2014, GPy authors (see AUTHORS.txt).
|
||||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
import numpy as np
|
||||
from paramz.transformations import Transformation, __fixed__
|
||||
from paramz.core.parameter_core import Parameterizable
|
||||
|
||||
class Priorizable(Parameterizable):
|
||||
def __init__(self, name, default_prior=None, *a, **kw):
|
||||
super(Priorizable, self).__init__(name=name, *a, **kw)
|
||||
self._default_prior_ = default_prior
|
||||
from paramz.core.index_operations import ParameterIndexOperations
|
||||
self.add_index_operation('priors', ParameterIndexOperations())
|
||||
if self._default_prior_ is not None:
|
||||
self.set_prior(self._default_prior_)
|
||||
|
||||
#===========================================================================
|
||||
# Prior Operations
|
||||
#===========================================================================
|
||||
def set_prior(self, prior, warning=True):
|
||||
"""
|
||||
Set the prior for this object to prior.
|
||||
:param :class:`~GPy.priors.Prior` prior: a prior to set for this parameter
|
||||
:param bool warning: whether to warn if another prior was set for this parameter
|
||||
"""
|
||||
repriorized = self.unset_priors()
|
||||
self._add_to_index_operations(self.priors, repriorized, prior, warning)
|
||||
|
||||
from paramz.domains import _REAL, _POSITIVE, _NEGATIVE
|
||||
if prior.domain is _POSITIVE:
|
||||
self.constrain_positive(warning)
|
||||
elif prior.domain is _NEGATIVE:
|
||||
self.constrain_negative(warning)
|
||||
elif prior.domain is _REAL:
|
||||
rav_i = self._raveled_index()
|
||||
assert all(all(False if c is __fixed__ else c.domain is _REAL for c in con) for con in self.constraints.properties_for(rav_i)), 'Domain of prior and constraint have to match, please unconstrain if you REALLY wish to use this prior'
|
||||
|
||||
def unset_priors(self, *priors):
|
||||
"""
|
||||
Un-set all priors given (in *priors) from this parameter handle.
|
||||
"""
|
||||
return self._remove_from_index_operations(self.priors, priors)
|
||||
|
||||
def log_prior(self):
|
||||
"""evaluate the prior"""
|
||||
if self.priors.size == 0:
|
||||
return 0.
|
||||
x = self.param_array
|
||||
#evaluate the prior log densities
|
||||
log_p = reduce(lambda a, b: a + b, (p.lnpdf(x[ind]).sum() for p, ind in self.priors.items()), 0)
|
||||
|
||||
#account for the transformation by evaluating the log Jacobian (where things are transformed)
|
||||
log_j = 0.
|
||||
priored_indexes = np.hstack([i for p, i in self.priors.items()])
|
||||
for c,j in self.constraints.items():
|
||||
if not isinstance(c, Transformation):continue
|
||||
for jj in j:
|
||||
if jj in priored_indexes:
|
||||
log_j += c.log_jacobian(x[jj])
|
||||
return log_p + log_j
|
||||
|
||||
def _log_prior_gradients(self):
|
||||
"""evaluate the gradients of the priors"""
|
||||
if self.priors.size == 0:
|
||||
return 0.
|
||||
x = self.param_array
|
||||
ret = np.zeros(x.size)
|
||||
#compute derivate of prior density
|
||||
[np.put(ret, ind, p.lnpdf_grad(x[ind])) for p, ind in self.priors.items()]
|
||||
#add in jacobian derivatives if transformed
|
||||
priored_indexes = np.hstack([i for p, i in self.priors.items()])
|
||||
for c,j in self.constraints.items():
|
||||
if not isinstance(c, Transformation):continue
|
||||
for jj in j:
|
||||
if jj in priored_indexes:
|
||||
ret[jj] += c.log_jacobian_grad(x[jj])
|
||||
return ret
|
||||
4
GPy/core/parameterization/transformations.py
Normal file
4
GPy/core/parameterization/transformations.py
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
# Copyright (c) 2014, Max Zwiessele, James Hensman
|
||||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
|
||||
from paramz.transformations import *
|
||||
1311
GPy/core/priors.py
Normal file
1311
GPy/core/priors.py
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -1,22 +1,9 @@
|
|||
# Copyright (c) 2012-2014, GPy authors (see AUTHORS.txt).
|
||||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
from .parameterization.priorizable import Priorizable
|
||||
from paramz import Model
|
||||
|
||||
|
||||
from .. import likelihoods
|
||||
from ..inference import optimization
|
||||
from ..util.misc import opt_wrapper
|
||||
from .parameterization import Parameterized
|
||||
import multiprocessing as mp
|
||||
import numpy as np
|
||||
from numpy.linalg.linalg import LinAlgError
|
||||
import itertools
|
||||
import sys
|
||||
from .verbose_optimization import VerboseOptimization
|
||||
# import numdifftools as ndt
|
||||
from functools import reduce
|
||||
from paramz.model import Model
|
||||
|
||||
class ProbabilisticModel(Model):
|
||||
class ProbabilisticModel(Model, Priorizable):
|
||||
|
||||
def __init__(self, name):
|
||||
super(ProbabilisticModel, self).__init__(name) # Parameterized.__init__(self)
|
||||
|
|
@ -25,8 +12,7 @@ class ProbabilisticModel(Model):
|
|||
raise NotImplementedError("this needs to be implemented to use the model class")
|
||||
|
||||
def _log_likelihood_gradients(self):
|
||||
return self.gradient.copy()
|
||||
|
||||
return self.gradient#.copy()
|
||||
|
||||
def objective_function(self):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -6,11 +6,9 @@ from .gp import GP
|
|||
from .parameterization.param import Param
|
||||
from ..inference.latent_function_inference import var_dtc
|
||||
from .. import likelihoods
|
||||
from .parameterization.variational import VariationalPosterior, NormalPosterior
|
||||
from ..util.linalg import mdot
|
||||
from .variational import VariationalPosterior
|
||||
|
||||
import logging
|
||||
import itertools
|
||||
logger = logging.getLogger("sparse gp")
|
||||
|
||||
class SparseGP(GP):
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ class SVGP(SparseGP):
|
|||
#create the SVI inference method
|
||||
inf_method = svgp_inf()
|
||||
|
||||
SparseGP.__init__(self, X_batch, Y_batch, Z, kernel, likelihood, mean_function=mean_function, inference_method=inf_method,
|
||||
super(SVGP, self).__init__(X_batch, Y_batch, Z, kernel, likelihood, mean_function=mean_function, inference_method=inf_method,
|
||||
name=name, Y_metadata=Y_metadata, normalizer=False)
|
||||
|
||||
#assume the number of latent functions is one per col of Y unless specified
|
||||
|
|
|
|||
|
|
@ -5,11 +5,9 @@ Created on 6 Nov 2013
|
|||
'''
|
||||
|
||||
import numpy as np
|
||||
from .parameterized import Parameterized
|
||||
from .param import Param
|
||||
from .transformations import Logexp, Logistic,__fixed__
|
||||
from GPy.util.misc import param_to_array
|
||||
from GPy.util.caching import Cache_this
|
||||
from .parameterization.parameterized import Parameterized
|
||||
from .parameterization.param import Param
|
||||
from paramz.transformations import Logexp, Logistic,__fixed__
|
||||
|
||||
class VariationalPrior(Parameterized):
|
||||
def __init__(self, name='latent space', **kw):
|
||||
|
|
|
|||
|
|
@ -1,185 +0,0 @@
|
|||
# Copyright (c) 2012-2014, Max Zwiessele.
|
||||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
|
||||
from __future__ import print_function
|
||||
import numpy as np
|
||||
import sys
|
||||
import time
|
||||
import datetime
|
||||
|
||||
def exponents(fnow, current_grad):
|
||||
exps = [np.abs(np.float(fnow)), 1 if current_grad is np.nan else current_grad]
|
||||
return np.sign(exps) * np.log10(exps).astype(int)
|
||||
|
||||
class VerboseOptimization(object):
|
||||
def __init__(self, model, opt, maxiters, verbose=False, current_iteration=0, ipython_notebook=True, clear_after_finish=False):
|
||||
self.verbose = verbose
|
||||
if self.verbose:
|
||||
self.model = model
|
||||
self.iteration = current_iteration
|
||||
self.p_iter = self.iteration
|
||||
self.maxiters = maxiters
|
||||
self.len_maxiters = len(str(maxiters))
|
||||
self.opt_name = opt.opt_name
|
||||
self.model.add_observer(self, self.print_status)
|
||||
self.status = 'running'
|
||||
self.clear = clear_after_finish
|
||||
|
||||
self.update()
|
||||
|
||||
try:
|
||||
from IPython.display import display
|
||||
from IPython.html.widgets import IntProgress, HTML, Box, VBox, HBox, FlexBox
|
||||
self.text = HTML(width='100%')
|
||||
self.progress = IntProgress(min=0, max=maxiters)
|
||||
#self.progresstext = Text(width='100%', disabled=True, value='0/{}'.format(maxiters))
|
||||
self.model_show = HTML()
|
||||
self.ipython_notebook = ipython_notebook
|
||||
except:
|
||||
# Not in Ipython notebook
|
||||
self.ipython_notebook = False
|
||||
|
||||
if self.ipython_notebook:
|
||||
left_col = VBox(children=[self.progress, self.text], padding=2, width='40%')
|
||||
right_col = Box(children=[self.model_show], padding=2, width='60%')
|
||||
self.hor_align = FlexBox(children = [left_col, right_col], width='100%', orientation='horizontal')
|
||||
|
||||
display(self.hor_align)
|
||||
|
||||
try:
|
||||
self.text.set_css('width', '100%')
|
||||
left_col.set_css({
|
||||
'padding': '2px',
|
||||
'width': "100%",
|
||||
})
|
||||
|
||||
right_col.set_css({
|
||||
'padding': '2px',
|
||||
})
|
||||
|
||||
self.hor_align.set_css({
|
||||
'width': "100%",
|
||||
})
|
||||
|
||||
self.hor_align.remove_class('vbox')
|
||||
self.hor_align.add_class('hbox')
|
||||
|
||||
left_col.add_class("box-flex1")
|
||||
right_col.add_class('box-flex0')
|
||||
|
||||
except:
|
||||
pass
|
||||
|
||||
#self.text.add_class('box-flex2')
|
||||
#self.progress.add_class('box-flex1')
|
||||
else:
|
||||
self.exps = exponents(self.fnow, self.current_gradient)
|
||||
print('Running {} Code:'.format(self.opt_name))
|
||||
print(' {3:7s} {0:{mi}s} {1:11s} {2:11s}'.format("i", "f", "|g|", "runtime", mi=self.len_maxiters))
|
||||
|
||||
def __enter__(self):
|
||||
self.start = time.time()
|
||||
self._time = self.start
|
||||
return self
|
||||
|
||||
def print_out(self, seconds):
|
||||
if seconds<60:
|
||||
ms = (seconds%1)*100
|
||||
self.timestring = "{s:0>2d}s{ms:0>2d}".format(s=int(seconds), ms=int(ms))
|
||||
else:
|
||||
m, s = divmod(seconds, 60)
|
||||
if m>59:
|
||||
h, m = divmod(m, 60)
|
||||
if h>23:
|
||||
d, h = divmod(h, 24)
|
||||
self.timestring = '{d:0>2d}d{h:0>2d}h{m:0>2d}'.format(m=int(m), h=int(h), d=int(d))
|
||||
else:
|
||||
self.timestring = '{h:0>2d}h{m:0>2d}m{s:0>2d}'.format(m=int(m), s=int(s), h=int(h))
|
||||
else:
|
||||
ms = (seconds%1)*100
|
||||
self.timestring = '{m:0>2d}m{s:0>2d}s{ms:0>2d}'.format(m=int(m), s=int(s), ms=int(ms))
|
||||
if self.ipython_notebook:
|
||||
names_vals = [['optimizer', "{:s}".format(self.opt_name)],
|
||||
['runtime', "{:>s}".format(self.timestring)],
|
||||
['evaluation', "{:>0{l}}".format(self.iteration, l=self.len_maxiters)],
|
||||
['objective', "{: > 12.3E}".format(self.fnow)],
|
||||
['||gradient||', "{: >+12.3E}".format(float(self.current_gradient))],
|
||||
['status', "{:s}".format(self.status)],
|
||||
]
|
||||
#message = "Lik:{:5.3E} Grad:{:5.3E} Lik:{:5.3E} Len:{!s}".format(float(m.log_likelihood()), np.einsum('i,i->', grads, grads), float(m.likelihood.variance), " ".join(["{:3.2E}".format(l) for l in m.kern.lengthscale.values]))
|
||||
html_begin = """<style type="text/css">
|
||||
.tg-opt {font-family:"Courier New", Courier, monospace !important;padding:2px 3px;word-break:normal;border-collapse:collapse;border-spacing:0;border-color:#DCDCDC;margin:0px auto;width:100%;}
|
||||
.tg-opt td{font-family:"Courier New", Courier, monospace !important;font-weight:bold;color:#444;background-color:#F7FDFA;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#DCDCDC;}
|
||||
.tg-opt th{font-family:"Courier New", Courier, monospace !important;font-weight:normal;color:#fff;background-color:#26ADE4;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#DCDCDC;}
|
||||
.tg-opt .tg-left{font-family:"Courier New", Courier, monospace !important;font-weight:normal;text-align:left;}
|
||||
.tg-opt .tg-right{font-family:"Courier New", Courier, monospace !important;font-weight:normal;text-align:right;}
|
||||
</style>
|
||||
<table class="tg-opt">"""
|
||||
html_end = "</table>"
|
||||
html_body = ""
|
||||
for name, val in names_vals:
|
||||
html_body += "<tr>"
|
||||
html_body += "<td class='tg-left'>{}</td>".format(name)
|
||||
html_body += "<td class='tg-right'>{}</td>".format(val)
|
||||
html_body += "</tr>"
|
||||
self.text.value = html_begin + html_body + html_end
|
||||
self.progress.value = (self.iteration+1)
|
||||
#self.progresstext.value = '0/{}'.format((self.iteration+1))
|
||||
self.model_show.value = self.model._repr_html_()
|
||||
else:
|
||||
n_exps = exponents(self.fnow, self.current_gradient)
|
||||
if self.iteration - self.p_iter >= 20 * np.random.rand():
|
||||
a = self.iteration >= self.p_iter * 2.78
|
||||
b = np.any(n_exps < self.exps)
|
||||
if a or b:
|
||||
self.p_iter = self.iteration
|
||||
print('')
|
||||
if b:
|
||||
self.exps = n_exps
|
||||
print('\r', end=' ')
|
||||
print('{3:} {0:>0{mi}g} {1:> 12e} {2:> 12e}'.format(self.iteration, float(self.fnow), float(self.current_gradient), "{:>8s}".format(self.timestring), mi=self.len_maxiters), end=' ') # print 'Iteration:', iteration, ' Objective:', fnow, ' Scale:', beta, '\r',
|
||||
sys.stdout.flush()
|
||||
|
||||
def print_status(self, me, which=None):
|
||||
self.update()
|
||||
|
||||
t = time.time()
|
||||
seconds = t-self.start
|
||||
#sys.stdout.write(" "*len(self.message))
|
||||
if t-self._time > .3 or seconds < .3:
|
||||
self.print_out(seconds)
|
||||
self._time = t
|
||||
|
||||
self.iteration += 1
|
||||
|
||||
def update(self):
|
||||
self.fnow = self.model.objective_function()
|
||||
if self.model.obj_grads is not None:
|
||||
grad = self.model.obj_grads
|
||||
self.current_gradient = np.dot(grad, grad)
|
||||
else:
|
||||
self.current_gradient = np.nan
|
||||
|
||||
def finish(self, opt):
|
||||
self.status = opt.status
|
||||
if self.verbose and self.ipython_notebook:
|
||||
if 'conv' in self.status.lower():
|
||||
self.progress.bar_style = 'success'
|
||||
elif self.iteration >= self.maxiters:
|
||||
self.progress.bar_style = 'warning'
|
||||
else:
|
||||
self.progress.bar_style = 'danger'
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
if self.verbose:
|
||||
self.stop = time.time()
|
||||
self.model.remove_observer(self)
|
||||
self.print_out(self.stop - self.start)
|
||||
|
||||
if not self.ipython_notebook:
|
||||
print()
|
||||
print('Runtime: {}'.format("{:>9s}".format(self.timestring)))
|
||||
print('Optimization status: {0}'.format(self.status))
|
||||
print()
|
||||
elif self.clear:
|
||||
self.hor_align.close()
|
||||
|
|
@ -2,7 +2,7 @@
|
|||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
import numpy as np
|
||||
from ...util.linalg import jitchol, DSYR, dtrtrs, dtrtri
|
||||
from ...core.parameterization.observable_array import ObsAr
|
||||
from paramz import ObsAr
|
||||
from . import ExactGaussianInference, VarDTC
|
||||
from ...util import diag
|
||||
|
||||
|
|
|
|||
|
|
@ -2,10 +2,9 @@
|
|||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
|
||||
import numpy as np
|
||||
from ...core import Model
|
||||
from ...core.parameterization import variational
|
||||
from ...core import ProbabilisticModel
|
||||
from ...core import variational
|
||||
from ...util.linalg import tdot
|
||||
from GPy.core.parameterization.variational import VariationalPosterior
|
||||
|
||||
def infer_newX(model, Y_new, optimize=True, init='L2'):
|
||||
"""
|
||||
|
|
@ -27,7 +26,7 @@ def infer_newX(model, Y_new, optimize=True, init='L2'):
|
|||
|
||||
return infr_m.X, infr_m
|
||||
|
||||
class InferenceX(Model):
|
||||
class InferenceX(ProbabilisticModel):
|
||||
"""
|
||||
The model class for inference of new X with given new Y. (replacing the "do_test_latent" in Bayesian GPLVM)
|
||||
It is a tiny inference model created from the original GP model. The kernel, likelihood (only Gaussian is supported at the moment)
|
||||
|
|
@ -62,14 +61,12 @@ class InferenceX(Model):
|
|||
# self.kern.GPU(True)
|
||||
from copy import deepcopy
|
||||
self.posterior = deepcopy(model.posterior)
|
||||
from ...core.parameterization.variational import VariationalPosterior
|
||||
if isinstance(model.X, VariationalPosterior):
|
||||
if isinstance(model.X, variational.VariationalPosterior):
|
||||
self.uncertain_input = True
|
||||
from ...models.ss_gplvm import IBPPrior
|
||||
from ...models.ss_mrd import IBPPrior_SSMRD
|
||||
if isinstance(model.variational_prior, IBPPrior) or isinstance(model.variational_prior, IBPPrior_SSMRD):
|
||||
from ...core.parameterization.variational import SpikeAndSlabPrior
|
||||
self.variational_prior = SpikeAndSlabPrior(pi=0.5, learnPi=False, group_spike=False)
|
||||
self.variational_prior = variational.SpikeAndSlabPrior(pi=0.5, learnPi=False, group_spike=False)
|
||||
else:
|
||||
self.variational_prior = model.variational_prior.copy()
|
||||
else:
|
||||
|
|
@ -105,17 +102,16 @@ class InferenceX(Model):
|
|||
idx = dist.argmin(axis=1)
|
||||
|
||||
from ...models import SSGPLVM
|
||||
from ...util.misc import param_to_array
|
||||
if isinstance(model, SSGPLVM):
|
||||
X = variational.SpikeAndSlabPosterior(param_to_array(model.X.mean[idx]), param_to_array(model.X.variance[idx]), param_to_array(model.X.gamma[idx]))
|
||||
X = variational.SpikeAndSlabPosterior((model.X.mean[idx].values), (model.X.variance[idx].values), (model.X.gamma[idx].values))
|
||||
if model.group_spike:
|
||||
X.gamma.fix()
|
||||
else:
|
||||
if self.uncertain_input and self.sparse_gp:
|
||||
X = variational.NormalPosterior(param_to_array(model.X.mean[idx]), param_to_array(model.X.variance[idx]))
|
||||
X = variational.NormalPosterior((model.X.mean[idx].values), (model.X.variance[idx].values))
|
||||
else:
|
||||
from ...core import Param
|
||||
X = Param('latent mean',param_to_array(model.X[idx]).copy())
|
||||
X = Param('latent mean',(model.X[idx].values).copy())
|
||||
|
||||
return X
|
||||
|
||||
|
|
@ -160,8 +156,7 @@ class InferenceX(Model):
|
|||
self.X.gradient = X_grad
|
||||
|
||||
if self.uncertain_input:
|
||||
from ...core.parameterization.variational import SpikeAndSlabPrior
|
||||
if isinstance(self.variational_prior, SpikeAndSlabPrior):
|
||||
if isinstance(self.variational_prior, variational.SpikeAndSlabPrior):
|
||||
# Update Log-likelihood
|
||||
KL_div = self.variational_prior.KL_divergence(self.X)
|
||||
# update for the KL divergence
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
from .posterior import Posterior
|
||||
from ...util.linalg import mdot, jitchol, backsub_both_sides, tdot, dtrtrs, dtrtri, dpotri, dpotrs, symmetrify
|
||||
from ...util import diag
|
||||
from ...core.parameterization.variational import VariationalPosterior
|
||||
from ...core.variational import VariationalPosterior
|
||||
import numpy as np
|
||||
from . import LatentFunctionInference
|
||||
log_2_pi = np.log(2*np.pi)
|
||||
|
|
@ -23,8 +23,7 @@ class VarDTC(LatentFunctionInference):
|
|||
"""
|
||||
const_jitter = 1e-8
|
||||
def __init__(self, limit=1):
|
||||
#self._YYTfactor_cache = caching.cache()
|
||||
from ...util.caching import Cacher
|
||||
from paramz.caching import Cacher
|
||||
self.limit = limit
|
||||
self.get_trYYT = Cacher(self._get_trYYT, limit)
|
||||
self.get_YYTfactor = Cacher(self._get_YYTfactor, limit)
|
||||
|
|
@ -45,7 +44,7 @@ class VarDTC(LatentFunctionInference):
|
|||
def __setstate__(self, state):
|
||||
# has to be overridden, as Cacher objects cannot be pickled.
|
||||
self.limit = state
|
||||
from ...util.caching import Cacher
|
||||
from paramz.caching import Cacher
|
||||
self.get_trYYT = Cacher(self._get_trYYT, self.limit)
|
||||
self.get_YYTfactor = Cacher(self._get_YYTfactor, self.limit)
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
from .posterior import Posterior
|
||||
from ...util.linalg import jitchol, backsub_both_sides, tdot, dtrtrs, dtrtri,pdinv
|
||||
from ...util import diag
|
||||
from ...core.parameterization.variational import VariationalPosterior
|
||||
from ...core.variational import VariationalPosterior
|
||||
import numpy as np
|
||||
from . import LatentFunctionInference
|
||||
log_2_pi = np.log(2*np.pi)
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
from .kern import Kern
|
||||
from .independent_outputs import index_to_slices
|
||||
from ...core.parameterization import Param
|
||||
from ...core.parameterization.transformations import Logexp
|
||||
from paramz.transformations import Logexp
|
||||
import numpy as np
|
||||
|
||||
class ODE_UY(Kern):
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
from .kern import Kern
|
||||
from ...core.parameterization import Param
|
||||
from ...core.parameterization.transformations import Logexp
|
||||
from paramz.transformations import Logexp
|
||||
import numpy as np
|
||||
from .independent_outputs import index_to_slices
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
from .kern import Kern
|
||||
from ...core.parameterization import Param
|
||||
from ...core.parameterization.transformations import Logexp
|
||||
from paramz.transformations import Logexp
|
||||
import numpy as np
|
||||
from .independent_outputs import index_to_slices
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
from .kern import Kern
|
||||
from ...core.parameterization import Param
|
||||
from ...core.parameterization.transformations import Logexp
|
||||
from paramz.transformations import Logexp
|
||||
import numpy as np
|
||||
from .independent_outputs import index_to_slices
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
import numpy as np
|
||||
import itertools
|
||||
from ...util.caching import Cache_this
|
||||
from paramz.caching import Cache_this
|
||||
from .kern import CombinationKernel, Kern
|
||||
from functools import reduce
|
||||
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@
|
|||
import numpy as np
|
||||
from .kern import Kern
|
||||
from ...core.parameterization.param import Param
|
||||
from ...core.parameterization.transformations import Logexp
|
||||
from ...util.caching import Cache_this
|
||||
from paramz.transformations import Logexp
|
||||
from paramz.caching import Cache_this
|
||||
from ...util.linalg import tdot, mdot
|
||||
|
||||
class BasisFuncKernel(Kern):
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
from .kern import Kern
|
||||
from ...core.parameterization import Param
|
||||
from ...core.parameterization.transformations import Logexp
|
||||
from paramz.transformations import Logexp
|
||||
import numpy as np
|
||||
|
||||
class Brownian(Kern):
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
from .kern import Kern
|
||||
import numpy as np
|
||||
from ...core.parameterization import Param
|
||||
from ...core.parameterization.transformations import Logexp
|
||||
from paramz.transformations import Logexp
|
||||
from ...util.config import config # for assesing whether to use cython
|
||||
try:
|
||||
from . import coregionalize_cython
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@ import numpy as np
|
|||
from scipy.special import wofz
|
||||
from .kern import Kern
|
||||
from ...core.parameterization import Param
|
||||
from ...core.parameterization.transformations import Logexp
|
||||
from ...util.caching import Cache_this
|
||||
from paramz.transformations import Logexp
|
||||
from paramz.caching import Cache_this
|
||||
|
||||
class EQ_ODE2(Kern):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@
|
|||
import sys
|
||||
import numpy as np
|
||||
from ...core.parameterization.parameterized import Parameterized
|
||||
from ...core.parameterization.observable_array import ObsAr
|
||||
from ...util.caching import Cache_this
|
||||
from paramz.core.observable_array import ObsAr
|
||||
from paramz.caching import Cache_this
|
||||
from .kernel_slice_operations import KernCallsViaSlicerMeta
|
||||
from functools import reduce
|
||||
import six
|
||||
|
|
@ -30,18 +30,16 @@ class Kern(Parameterized):
|
|||
tight dimensionality of inputs.
|
||||
You most likely want this to be the integer telling the number of
|
||||
input dimensions of the kernel.
|
||||
If this is not an integer (!) we will work on the whole input matrix X,
|
||||
and not check whether dimensions match or not (!).
|
||||
|
||||
_all_dims_active:
|
||||
active_dims:
|
||||
|
||||
is the active_dimensions of inputs X we will work on.
|
||||
All kernels will get sliced Xes as inputs, if _all_dims_active is not None
|
||||
Only positive integers are allowed in _all_dims_active!
|
||||
if _all_dims_active is None, slicing is switched off and all X will be passed through as given.
|
||||
Only positive integers are allowed in active_dims!
|
||||
if active_dims is None, slicing is switched off and all X will be passed through as given.
|
||||
|
||||
:param int input_dim: the number of input dimensions to the function
|
||||
:param array-like|None _all_dims_active: list of indices on which dimensions this kernel works on, or none if no slicing
|
||||
:param array-like|None active_dims: list of indices on which dimensions this kernel works on, or none if no slicing
|
||||
|
||||
Do not instantiate.
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -7,9 +7,9 @@ This module provides a meta class for the kernels. The meta class is for
|
|||
slicing the inputs (X, X2) for the kernels, before K (or any other method involving X)
|
||||
gets calls. The `_all_dims_active` of a kernel decide which dimensions the kernel works on.
|
||||
'''
|
||||
from ...core.parameterization.parameterized import ParametersChangedMeta
|
||||
import numpy as np
|
||||
from functools import wraps
|
||||
from paramz.parameterized import ParametersChangedMeta
|
||||
|
||||
def put_clean(dct, name, func):
|
||||
if name in dct:
|
||||
|
|
|
|||
|
|
@ -6,8 +6,8 @@ import numpy as np
|
|||
from .kern import Kern
|
||||
from ...util.linalg import tdot
|
||||
from ...core.parameterization import Param
|
||||
from ...core.parameterization.transformations import Logexp
|
||||
from ...util.caching import Cache_this
|
||||
from paramz.transformations import Logexp
|
||||
from paramz.caching import Cache_this
|
||||
from .psi_comp import PSICOMP_Linear
|
||||
|
||||
class Linear(Kern):
|
||||
|
|
|
|||
|
|
@ -3,9 +3,9 @@
|
|||
|
||||
from .kern import Kern
|
||||
from ...core.parameterization import Param
|
||||
from ...core.parameterization.transformations import Logexp
|
||||
from paramz.transformations import Logexp
|
||||
import numpy as np
|
||||
from ...util.caching import Cache_this
|
||||
from paramz.caching import Cache_this
|
||||
four_over_tau = 2./np.pi
|
||||
|
||||
class MLP(Kern):
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ from .kern import Kern
|
|||
from ...util.linalg import mdot
|
||||
from ...util.decorators import silence_errors
|
||||
from ...core.parameterization.param import Param
|
||||
from ...core.parameterization.transformations import Logexp
|
||||
from paramz.transformations import Logexp
|
||||
|
||||
class Periodic(Kern):
|
||||
def __init__(self, input_dim, variance, lengthscale, period, n_freq, lower, upper, active_dims, name):
|
||||
|
|
|
|||
|
|
@ -4,7 +4,8 @@
|
|||
import numpy as np
|
||||
from .kern import Kern
|
||||
from ...core.parameterization import Param
|
||||
from ...core.parameterization.transformations import Logexp
|
||||
from paramz.transformations import Logexp
|
||||
|
||||
class Poly(Kern):
|
||||
"""
|
||||
Polynomial kernel
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
import numpy as np
|
||||
from .kern import CombinationKernel
|
||||
from ...util.caching import Cache_this
|
||||
from paramz.caching import Cache_this
|
||||
import itertools
|
||||
from functools import reduce
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
# Copyright (c) 2012, GPy authors (see AUTHORS.txt).
|
||||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
|
||||
from ....core.parameterization.parameter_core import Pickleable
|
||||
from ....util.caching import Cache_this
|
||||
from ....core.parameterization import variational
|
||||
from paramz.core.pickleable import Pickleable
|
||||
from paramz.caching import Cache_this
|
||||
from ....core import variational
|
||||
#from linear_psi_comp import LINEAr
|
||||
|
||||
class PSICOMP(Pickleable):
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ An approximated psi-statistics implementation based on Gauss-Hermite Quadrature
|
|||
import numpy as np
|
||||
|
||||
from ....core.parameterization import Param
|
||||
from ....util.caching import Cache_this
|
||||
from paramz.caching import Cache_this
|
||||
from ....util.linalg import tdot
|
||||
from . import PSICOMP
|
||||
|
||||
|
|
@ -30,7 +30,7 @@ class PSICOMP_GH(PSICOMP):
|
|||
@Cache_this(limit=10, ignore_args=(0,))
|
||||
def comp_K(self, Z, qX):
|
||||
if self.Xs is None or self.Xs.shape != qX.mean.shape:
|
||||
from ....core.parameterization import ObsAr
|
||||
from paramz import ObsAr
|
||||
self.Xs = ObsAr(np.empty((self.degree,)+qX.mean.shape))
|
||||
mu, S = qX.mean.values, qX.variance.values
|
||||
S_sq = np.sqrt(S)
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ The module for psi-statistics for RBF kernel
|
|||
"""
|
||||
|
||||
import numpy as np
|
||||
from GPy.util.caching import Cacher
|
||||
from paramz.caching import Cacher
|
||||
|
||||
def psicomputations(variance, lengthscale, Z, variational_posterior, return_psi2_n=False):
|
||||
# here are the "statistics" for psi0, psi1 and psi2
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ The module for psi-statistics for RBF kernel
|
|||
"""
|
||||
|
||||
import numpy as np
|
||||
from ....util.caching import Cache_this
|
||||
from paramz.caching import Cache_this
|
||||
from . import PSICOMP_RBF
|
||||
from ....util import gpu_init
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ The module for psi-statistics for RBF kernel for Spike-and-Slab GPLVM
|
|||
"""
|
||||
|
||||
import numpy as np
|
||||
from ....util.caching import Cache_this
|
||||
from paramz.caching import Cache_this
|
||||
from . import PSICOMP_RBF
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import numpy as np
|
|||
from .stationary import Stationary
|
||||
from .psi_comp import PSICOMP_RBF, PSICOMP_RBF_GPU
|
||||
from ...core import Param
|
||||
from ...core.parameterization.transformations import Logexp
|
||||
from paramz.transformations import Logexp
|
||||
|
||||
class RBF(Stationary):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
import numpy as np
|
||||
from .kern import Kern
|
||||
from ...core.parameterization import Param
|
||||
from ...core.parameterization.transformations import Logexp
|
||||
from paramz.transformations import Logexp
|
||||
|
||||
class Spline(Kern):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ Neural Networks and Machine Learning, pages 133-165. Springer, 1998.
|
|||
|
||||
from .kern import Kern
|
||||
from ...core.parameterization import Param
|
||||
from ...core.parameterization.transformations import Logexp
|
||||
from paramz.transformations import Logexp
|
||||
|
||||
import numpy as np
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
from .kern import Kern
|
||||
import numpy as np
|
||||
from ...core.parameterization import Param
|
||||
from ...core.parameterization.transformations import Logexp
|
||||
from paramz.transformations import Logexp
|
||||
|
||||
class Static(Kern):
|
||||
def __init__(self, input_dim, variance, active_dims, name):
|
||||
|
|
|
|||
|
|
@ -2,15 +2,15 @@
|
|||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
|
||||
|
||||
from .kern import Kern
|
||||
from ...core.parameterization import Param
|
||||
from ...core.parameterization.transformations import Logexp
|
||||
from ...util.linalg import tdot
|
||||
from ... import util
|
||||
import numpy as np
|
||||
from scipy import integrate
|
||||
from .kern import Kern
|
||||
from ...core.parameterization import Param
|
||||
from ...util.linalg import tdot
|
||||
from ... import util
|
||||
from ...util.config import config # for assesing whether to use cython
|
||||
from ...util.caching import Cache_this
|
||||
from paramz.caching import Cache_this
|
||||
from paramz.transformations import Logexp
|
||||
|
||||
try:
|
||||
from . import stationary_cython
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@
|
|||
import numpy as np
|
||||
from .kern import Kern
|
||||
from ...core.parameterization import Param
|
||||
from ...core.parameterization.transformations import Logexp
|
||||
from ...util.caching import Cache_this
|
||||
from paramz.transformations import Logexp
|
||||
from paramz.caching import Cache_this
|
||||
|
||||
class TruncLinear(Kern):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ from scipy import stats, special
|
|||
from . import link_functions
|
||||
from .likelihood import Likelihood
|
||||
from ..core.parameterization import Param
|
||||
from ..core.parameterization.transformations import Logexp
|
||||
from paramz.transformations import Logexp
|
||||
from scipy import stats
|
||||
|
||||
class Gaussian(Likelihood):
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ from . import link_functions
|
|||
from .likelihood import Likelihood
|
||||
from .gaussian import Gaussian
|
||||
from ..core.parameterization import Param
|
||||
from ..core.parameterization.transformations import Logexp
|
||||
from paramz.transformations import Logexp
|
||||
from ..core.parameterization import Parameterized
|
||||
import itertools
|
||||
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ from scipy import stats, integrate
|
|||
from scipy.special import gammaln, gamma
|
||||
from .likelihood import Likelihood
|
||||
from ..core.parameterization import Param
|
||||
from ..core.parameterization.transformations import Logexp
|
||||
from paramz.transformations import Logexp
|
||||
from scipy.special import psi as digamma
|
||||
|
||||
class StudentT(Likelihood):
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import numpy as np
|
|||
from .. import kern
|
||||
from ..core.sparse_gp_mpi import SparseGP_MPI
|
||||
from ..likelihoods import Gaussian
|
||||
from ..core.parameterization.variational import NormalPosterior, NormalPrior
|
||||
from ..core.variational import NormalPosterior, NormalPrior
|
||||
from ..inference.latent_function_inference.var_dtc_parallel import VarDTC_minibatch
|
||||
import logging
|
||||
|
||||
|
|
|
|||
|
|
@ -2,14 +2,12 @@
|
|||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
|
||||
import numpy as np
|
||||
import logging
|
||||
from .. import kern
|
||||
from ..likelihoods import Gaussian
|
||||
from ..core.parameterization.variational import NormalPosterior, NormalPrior
|
||||
from ..inference.latent_function_inference.var_dtc_parallel import VarDTC_minibatch
|
||||
import logging
|
||||
from GPy.models.sparse_gp_minibatch import SparseGPMiniBatch
|
||||
from GPy.core.parameterization.param import Param
|
||||
from GPy.core.parameterization.observable_array import ObsAr
|
||||
from ..core.variational import NormalPosterior, NormalPrior
|
||||
from .sparse_gp_minibatch import SparseGPMiniBatch
|
||||
from ..core.parameterization.param import Param
|
||||
|
||||
class BayesianGPLVMMiniBatch(SparseGPMiniBatch):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -1,10 +1,7 @@
|
|||
# Copyright (c) 2015 the GPy Austhors (see AUTHORS.txt)
|
||||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
|
||||
import numpy as np
|
||||
from .. import kern
|
||||
from .bayesian_gplvm import BayesianGPLVM
|
||||
from ..core.parameterization.variational import NormalPosterior, NormalPrior
|
||||
|
||||
class DPBayesianGPLVM(BayesianGPLVM):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -2,11 +2,11 @@
|
|||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
|
||||
import numpy as np
|
||||
from GPy.core.probabilistic_model import Model
|
||||
from ..core.parameterization import ObsAr
|
||||
from ..core import ProbabilisticModel
|
||||
from paramz import ObsAr
|
||||
from .. import likelihoods
|
||||
|
||||
class GPKroneckerGaussianRegression(Model):
|
||||
class GPKroneckerGaussianRegression(ProbabilisticModel):
|
||||
"""
|
||||
Kronecker GP regression
|
||||
|
||||
|
|
@ -29,7 +29,7 @@ class GPKroneckerGaussianRegression(Model):
|
|||
|
||||
"""
|
||||
def __init__(self, X1, X2, Y, kern1, kern2, noise_var=1., name='KGPR'):
|
||||
Model.__init__(self, name=name)
|
||||
ProbabilisticModel.__init__(self, name=name)
|
||||
# accept the construction arguments
|
||||
self.X1 = ObsAr(X1)
|
||||
self.X2 = ObsAr(X2)
|
||||
|
|
|
|||
|
|
@ -3,8 +3,6 @@
|
|||
|
||||
import numpy as np
|
||||
from ..core import GP
|
||||
from ..core.parameterization import ObsAr
|
||||
from .. import kern
|
||||
from ..core.parameterization.param import Param
|
||||
from ..inference.latent_function_inference import VarGauss
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
# ## Copyright (c) 2012, GPy authors (see AUTHORS.txt).
|
||||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
|
||||
from GPy.core.probabilistic_model import Model
|
||||
import itertools
|
||||
import numpy
|
||||
from ..core.parameterization import Param
|
||||
np = numpy
|
||||
|
||||
from ..core.parameterization import Param
|
||||
from ..core.probabilistic_model import ProbabilisticModel
|
||||
from ..util.block_matrices import get_blocks, get_block_shapes, unblock, get_blocks_3d, get_block_shapes_3d
|
||||
|
||||
def get_shape(x):
|
||||
|
|
@ -21,7 +21,7 @@ def at_least_one_element(x):
|
|||
def flatten_if_needed(x):
|
||||
return numpy.atleast_1d(x).flatten()
|
||||
|
||||
class GradientChecker(Model):
|
||||
class GradientChecker(ProbabilisticModel):
|
||||
|
||||
def __init__(self, f, df, x0, names=None, *args, **kwargs):
|
||||
"""
|
||||
|
|
@ -62,7 +62,7 @@ class GradientChecker(Model):
|
|||
grad.randomize()
|
||||
grad.checkgrad(verbose=1)
|
||||
"""
|
||||
Model.__init__(self, 'GradientChecker')
|
||||
super(GradientChecker, self).__init__(name='GradientChecker')
|
||||
if isinstance(x0, (list, tuple)) and names is None:
|
||||
self.shapes = [get_shape(xi) for xi in x0]
|
||||
self.names = ['X{i}'.format(i=i) for i in range(len(x0))]
|
||||
|
|
|
|||
|
|
@ -5,18 +5,14 @@ import numpy as np
|
|||
import itertools, logging
|
||||
|
||||
from ..kern import Kern
|
||||
from ..core.parameterization.variational import NormalPosterior, NormalPrior
|
||||
from ..core.parameterization import Param, Parameterized
|
||||
from ..core.parameterization.observable_array import ObsAr
|
||||
from ..core.variational import NormalPrior
|
||||
from ..core.parameterization import Param
|
||||
from paramz import ObsAr
|
||||
from ..inference.latent_function_inference.var_dtc import VarDTC
|
||||
from ..inference.latent_function_inference import InferenceMethodList
|
||||
from ..likelihoods import Gaussian
|
||||
from ..util.initialization import initialize_latent
|
||||
from ..core.sparse_gp import SparseGP, GP
|
||||
from GPy.core.parameterization.variational import VariationalPosterior
|
||||
from GPy.models.bayesian_gplvm_minibatch import BayesianGPLVMMiniBatch
|
||||
from GPy.models.bayesian_gplvm import BayesianGPLVM
|
||||
from GPy.models.sparse_gp_minibatch import SparseGPMiniBatch
|
||||
|
||||
class MRD(BayesianGPLVMMiniBatch):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
# Copyright (c) 2013, the GPy Authors (see AUTHORS.txt)
|
||||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
|
||||
from ..core import GP
|
||||
from . import SparseGPClassification
|
||||
from .. import likelihoods
|
||||
from .. import kern
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@ class SparseGPClassificationUncertainInput(SparseGP):
|
|||
.. Note:: Multiple independent outputs are allowed using columns of Y
|
||||
"""
|
||||
def __init__(self, X, X_variance, Y, kernel=None, Z=None, num_inducing=10, Y_metadata=None, normalizer=None):
|
||||
from ..core.parameterization.variational import NormalPosterior
|
||||
from ..core.variational import NormalPosterior
|
||||
if kernel is None:
|
||||
kernel = kern.RBF(X.shape[1])
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
import numpy as np
|
||||
from ..core import SparseGP
|
||||
from ..inference.latent_function_inference import VarDTC
|
||||
from .. import likelihoods
|
||||
from .. import kern
|
||||
from .. import util
|
||||
|
||||
|
|
|
|||
|
|
@ -4,18 +4,15 @@
|
|||
from __future__ import print_function
|
||||
import numpy as np
|
||||
from ..core.parameterization.param import Param
|
||||
from ..core.variational import VariationalPosterior
|
||||
from ..core.sparse_gp import SparseGP
|
||||
from ..core.gp import GP
|
||||
from ..inference.latent_function_inference import var_dtc
|
||||
from .. import likelihoods
|
||||
from ..core.parameterization.variational import VariationalPosterior
|
||||
|
||||
import logging
|
||||
from GPy.inference.latent_function_inference.posterior import Posterior
|
||||
from GPy.inference.optimization.stochastics import SparseGPStochastics,\
|
||||
SparseGPMissing
|
||||
#no stochastics.py file added! from GPy.inference.optimization.stochastics import SparseGPStochastics,\
|
||||
#SparseGPMissing
|
||||
from ..inference.latent_function_inference.posterior import Posterior
|
||||
from ..inference.optimization.stochastics import SparseGPStochastics, SparseGPMissing
|
||||
logger = logging.getLogger("sparse gp")
|
||||
|
||||
class SparseGPMiniBatch(SparseGP):
|
||||
|
|
|
|||
|
|
@ -3,12 +3,11 @@
|
|||
|
||||
|
||||
import numpy as np
|
||||
from ..core import SparseGP
|
||||
from ..core.sparse_gp_mpi import SparseGP_MPI
|
||||
from .. import likelihoods
|
||||
from .. import kern
|
||||
from ..inference.latent_function_inference import VarDTC
|
||||
from ..core.parameterization.variational import NormalPosterior
|
||||
from ..core.variational import NormalPosterior
|
||||
|
||||
class SparseGPRegression(SparseGP_MPI):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -2,9 +2,8 @@
|
|||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
|
||||
|
||||
import numpy as np
|
||||
import sys
|
||||
from GPy.models.sparse_gp_regression import SparseGPRegression
|
||||
from .sparse_gp_regression import SparseGPRegression
|
||||
|
||||
class SparseGPLVM(SparseGPRegression):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ from ..core.sparse_gp_mpi import SparseGP_MPI
|
|||
from .. import kern
|
||||
from ..core.parameterization import Param
|
||||
from ..likelihoods import Gaussian
|
||||
from ..core.parameterization.variational import SpikeAndSlabPrior, SpikeAndSlabPosterior,VariationalPrior
|
||||
from ..core.variational import SpikeAndSlabPrior, SpikeAndSlabPosterior,VariationalPrior
|
||||
from ..inference.latent_function_inference.var_dtc_parallel import update_gradients, VarDTC_minibatch
|
||||
from ..kern.src.psi_comp.ssrbf_psi_gpucomp import PSICOMP_SSRBF_GPU
|
||||
|
||||
|
|
@ -19,7 +19,7 @@ class IBPPosterior(SpikeAndSlabPosterior):
|
|||
"""
|
||||
binary_prob : the probability of the distribution on the slab part.
|
||||
"""
|
||||
from ..core.parameterization.transformations import Logexp
|
||||
from paramz.transformations import Logexp
|
||||
super(IBPPosterior, self).__init__(means, variances, binary_prob, group_spike=True, name=name)
|
||||
self.sharedX = sharedX
|
||||
if sharedX:
|
||||
|
|
@ -60,7 +60,7 @@ class IBPPosterior(SpikeAndSlabPosterior):
|
|||
class IBPPrior(VariationalPrior):
|
||||
def __init__(self, input_dim, alpha =2., name='IBPPrior', **kw):
|
||||
super(IBPPrior, self).__init__(name=name, **kw)
|
||||
from ..core.parameterization.transformations import Logexp, __fixed__
|
||||
from paramz.transformations import Logexp, __fixed__
|
||||
self.input_dim = input_dim
|
||||
self.variance = 1.
|
||||
self.alpha = Param('alpha', alpha, __fixed__)
|
||||
|
|
|
|||
|
|
@ -3,15 +3,15 @@ The Maniforld Relevance Determination model with the spike-and-slab prior
|
|||
"""
|
||||
|
||||
import numpy as np
|
||||
from ..core import Model
|
||||
from ..core import ProbabilisticModel
|
||||
from .ss_gplvm import SSGPLVM
|
||||
from ..core.parameterization.variational import SpikeAndSlabPrior,NormalPosterior,VariationalPrior
|
||||
from ..core.variational import SpikeAndSlabPrior,NormalPosterior,VariationalPrior
|
||||
from ..util.misc import param_to_array
|
||||
from ..kern import RBF
|
||||
from ..core import Param
|
||||
from numpy.linalg.linalg import LinAlgError
|
||||
|
||||
class SSMRD(Model):
|
||||
class SSMRD(ProbabilisticModel):
|
||||
|
||||
def __init__(self, Ylist, input_dim, X=None, X_variance=None, Gammas=None, initx = 'PCA_concat', initz = 'permute',
|
||||
num_inducing=10, Zs=None, kernels=None, inference_methods=None, likelihoods=None, group_spike=True,
|
||||
|
|
@ -117,13 +117,13 @@ class SSMRD(Model):
|
|||
Gammas.append(gamma)
|
||||
return X, X_variance, Gammas, fracs
|
||||
|
||||
@Model.optimizer_array.setter
|
||||
@ProbabilisticModel.optimizer_array.setter
|
||||
def optimizer_array(self, p):
|
||||
if self.mpi_comm != None:
|
||||
if self._IN_OPTIMIZATION_ and self.mpi_comm.rank==0:
|
||||
self.mpi_comm.Bcast(np.int32(1),root=0)
|
||||
self.mpi_comm.Bcast(p, root=0)
|
||||
Model.optimizer_array.fset(self,p)
|
||||
ProbabilisticModel.optimizer_array.fset(self,p)
|
||||
|
||||
def optimize(self, optimizer=None, start=None, **kwargs):
|
||||
self._IN_OPTIMIZATION_ = True
|
||||
|
|
@ -214,7 +214,7 @@ class SpikeAndSlabPrior_SSMRD(SpikeAndSlabPrior):
|
|||
class IBPPrior_SSMRD(VariationalPrior):
|
||||
def __init__(self, nModels, input_dim, alpha =2., tau=None, name='IBPPrior', **kw):
|
||||
super(IBPPrior_SSMRD, self).__init__(name=name, **kw)
|
||||
from ..core.parameterization.transformations import Logexp, __fixed__
|
||||
from paramz.transformations import Logexp, __fixed__
|
||||
self.nModels = nModels
|
||||
self._b_prob_all = 0.5
|
||||
self.input_dim = input_dim
|
||||
|
|
|
|||
|
|
@ -334,7 +334,7 @@ def x_frame1D(X,plot_limits=None,resolution=None):
|
|||
"""
|
||||
assert X.shape[1] ==1, "x_frame1D is defined for one-dimensional inputs"
|
||||
if plot_limits is None:
|
||||
from ...core.parameterization.variational import VariationalPosterior
|
||||
from ...core.variational import VariationalPosterior
|
||||
if isinstance(X, VariationalPosterior):
|
||||
xmin,xmax = X.mean.min(0),X.mean.max(0)
|
||||
else:
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import numpy as np
|
||||
import time
|
||||
from ...core.parameterization.variational import VariationalPosterior
|
||||
from .*core.variational import VariationalPosterior
|
||||
try:
|
||||
import matplotlib.pyplot as plt
|
||||
import matplotlib as mpl
|
||||
|
|
|
|||
|
|
@ -1,37 +0,0 @@
|
|||
'''
|
||||
Created on 4 Sep 2015
|
||||
|
||||
@author: maxz
|
||||
'''
|
||||
import unittest
|
||||
from GPy.util.caching import Cacher
|
||||
from pickle import PickleError
|
||||
|
||||
|
||||
class Test(unittest.TestCase):
|
||||
def setUp(self):
|
||||
def op(x):
|
||||
return x
|
||||
self.cache = Cacher(op, 1)
|
||||
|
||||
def test_pickling(self):
|
||||
self.assertRaises(PickleError, self.cache.__getstate__)
|
||||
self.assertRaises(PickleError, self.cache.__setstate__)
|
||||
|
||||
def test_copy(self):
|
||||
tmp = self.cache.__deepcopy__()
|
||||
assert(tmp.operation is self.cache.operation)
|
||||
self.assertEqual(tmp.limit, self.cache.limit)
|
||||
|
||||
def test_reset(self):
|
||||
self.cache.reset()
|
||||
self.assertDictEqual(self.cache.cached_input_ids, {}, )
|
||||
self.assertDictEqual(self.cache.cached_outputs, {}, )
|
||||
self.assertDictEqual(self.cache.inputs_changed, {}, )
|
||||
|
||||
def test_name(self):
|
||||
assert(self.cache.__name__ == self.cache.operation.__name__)
|
||||
|
||||
if __name__ == "__main__":
|
||||
#import sys;sys.argv = ['', 'Test.testName']
|
||||
unittest.main()
|
||||
|
|
@ -5,7 +5,7 @@ Created on 4 Sep 2015
|
|||
'''
|
||||
import unittest
|
||||
import numpy as np, GPy
|
||||
from GPy.core.parameterization.variational import NormalPosterior
|
||||
from ..core.variational import NormalPosterior
|
||||
|
||||
class Test(unittest.TestCase):
|
||||
|
||||
|
|
|
|||
|
|
@ -1,137 +0,0 @@
|
|||
# Copyright (c) 2014, Max Zwiessele
|
||||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
|
||||
import unittest
|
||||
import numpy as np
|
||||
from GPy.core.parameterization.index_operations import ParameterIndexOperations,\
|
||||
ParameterIndexOperationsView
|
||||
|
||||
one, two, three = 'one', 'two', 'three'
|
||||
|
||||
class Test(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.param_index = ParameterIndexOperations()
|
||||
self.param_index.add(one, [3,9])
|
||||
self.param_index.add(two, [0,5])
|
||||
self.param_index.add(three, [2,4,7,10])
|
||||
self.view = ParameterIndexOperationsView(self.param_index, 2, 6)
|
||||
|
||||
def test_clear(self):
|
||||
self.param_index.clear()
|
||||
self.assertDictEqual(self.param_index._properties, {})
|
||||
|
||||
def test_remove(self):
|
||||
removed = self.param_index.remove(three, np.r_[3:13])
|
||||
self.assertListEqual(removed.tolist(), [4,7,10])
|
||||
self.assertListEqual(self.param_index[three].tolist(), [2])
|
||||
removed = self.param_index.remove(one, [1])
|
||||
self.assertListEqual(removed.tolist(), [])
|
||||
self.assertListEqual(self.param_index[one].tolist(), [3,9])
|
||||
self.assertListEqual(self.param_index.remove('not in there', []).tolist(), [])
|
||||
removed = self.param_index.remove(one, [9])
|
||||
self.assertListEqual(removed.tolist(), [9])
|
||||
self.assertListEqual(self.param_index[one].tolist(), [3])
|
||||
self.assertListEqual(self.param_index.remove('not in there', [2,3,4]).tolist(), [])
|
||||
self.assertListEqual(self.view.remove('not in there', [2,3,4]).tolist(), [])
|
||||
|
||||
def test_shift_left(self):
|
||||
self.view.shift_left(0, 2)
|
||||
self.assertListEqual(self.param_index[three].tolist(), [2,5,8])
|
||||
self.assertListEqual(self.param_index[two].tolist(), [0,3])
|
||||
self.assertListEqual(self.param_index[one].tolist(), [7])
|
||||
#=======================================================================
|
||||
# 0 1 2 3 4 5 6 7 8 9 10
|
||||
# one
|
||||
# two two
|
||||
# three three three
|
||||
# view: [0 1 2 3 4 5 ]
|
||||
#=======================================================================
|
||||
self.assertListEqual(self.view[three].tolist(), [0,3])
|
||||
self.assertListEqual(self.view[two].tolist(), [1])
|
||||
self.assertListEqual(self.view[one].tolist(), [5])
|
||||
self.param_index.shift_left(7, 1)
|
||||
#=======================================================================
|
||||
# 0 1 2 3 4 5 6 7 8 9 10
|
||||
#
|
||||
# two two
|
||||
# three three three
|
||||
# view: [0 1 2 3 4 5 ]
|
||||
#=======================================================================
|
||||
self.assertListEqual(self.param_index[three].tolist(), [2,5,7])
|
||||
self.assertListEqual(self.param_index[two].tolist(), [0,3])
|
||||
self.assertListEqual(self.param_index[one].tolist(), [])
|
||||
self.assertListEqual(self.view[three].tolist(), [0,3,5])
|
||||
self.assertListEqual(self.view[two].tolist(), [1])
|
||||
self.assertListEqual(self.view[one].tolist(), [])
|
||||
|
||||
def test_shift_right(self):
|
||||
self.view.shift_right(3, 2)
|
||||
self.assertListEqual(self.param_index[three].tolist(), [2,4,9,12])
|
||||
self.assertListEqual(self.param_index[two].tolist(), [0,7])
|
||||
self.assertListEqual(self.param_index[one].tolist(), [3,11])
|
||||
|
||||
def test_index_view(self):
|
||||
#=======================================================================
|
||||
# 0 1 2 3 4 5 6 7 8 9 10
|
||||
# one one
|
||||
# two two
|
||||
# three three three three
|
||||
# view: [0 1 2 3 4 5 ]
|
||||
#=======================================================================
|
||||
self.view = ParameterIndexOperationsView(self.param_index, 2, 6)
|
||||
self.assertSetEqual(set(self.view.properties()), set([one, two, three]))
|
||||
for v,p in zip(self.view.properties_for(np.r_[:6]), self.param_index.properties_for(np.r_[2:2+6])):
|
||||
self.assertEqual(v, p)
|
||||
self.assertSetEqual(set(self.view[two]), set([3]))
|
||||
self.assertSetEqual(set(self.param_index[two]), set([0, 5]))
|
||||
self.view.add(two, np.array([0]))
|
||||
self.assertSetEqual(set(self.view[two]), set([0,3]))
|
||||
self.assertSetEqual(set(self.param_index[two]), set([0, 2, 5]))
|
||||
self.view.clear()
|
||||
for v,p in zip(self.view.properties_for(np.r_[:6]), self.param_index.properties_for(np.r_[2:2+6])):
|
||||
self.assertEqual(v, p)
|
||||
self.assertEqual(v, [])
|
||||
param_index = ParameterIndexOperations()
|
||||
param_index.add(one, [3,9])
|
||||
param_index.add(two, [0,5])
|
||||
param_index.add(three, [2,4,7,10])
|
||||
view2 = ParameterIndexOperationsView(param_index, 2, 8)
|
||||
self.view.update(view2)
|
||||
for [i,v],[i2,v2] in zip(sorted(param_index.items()), sorted(self.param_index.items())):
|
||||
self.assertEqual(i, i2)
|
||||
np.testing.assert_equal(v, v2)
|
||||
|
||||
def test_view_of_view(self):
|
||||
#=======================================================================
|
||||
# 0 1 2 3 4 5 6 7 8 9 10
|
||||
# one one
|
||||
# two two
|
||||
# three three three three
|
||||
# view: [0 1 2 3 4 5 ]
|
||||
# view2: [0 1 2 3 4 5 ]
|
||||
#=======================================================================
|
||||
view2 = ParameterIndexOperationsView(self.view, 2, 6)
|
||||
view2.shift_right(0, 2)
|
||||
|
||||
def test_indexview_remove(self):
|
||||
removed = self.view.remove(two, [3])
|
||||
self.assertListEqual(removed.tolist(), [3])
|
||||
removed = self.view.remove(three, np.r_[:5])
|
||||
self.assertListEqual(removed.tolist(), [0, 2])
|
||||
|
||||
def test_misc(self):
|
||||
#py3 fix
|
||||
#for k,v in self.param_index.copy()._properties.iteritems():
|
||||
for k,v in self.param_index.copy()._properties.items():
|
||||
self.assertListEqual(self.param_index[k].tolist(), v.tolist())
|
||||
self.assertEqual(self.param_index.size, 8)
|
||||
self.assertEqual(self.view.size, 5)
|
||||
|
||||
def test_print(self):
|
||||
print(self.param_index)
|
||||
print(self.view)
|
||||
|
||||
if __name__ == "__main__":
|
||||
#import sys;sys.argv = ['', 'Test.test_index_view']
|
||||
unittest.main()
|
||||
|
|
@ -5,7 +5,7 @@
|
|||
The test cases for various inference algorithms
|
||||
"""
|
||||
|
||||
import unittest, itertools
|
||||
import unittest
|
||||
import numpy as np
|
||||
import GPy
|
||||
#np.seterr(invalid='raise')
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
import unittest
|
||||
import numpy as np
|
||||
import GPy
|
||||
import sys
|
||||
from GPy.core.parameterization.param import Param
|
||||
from ..util.config import config
|
||||
|
||||
|
|
@ -17,14 +16,14 @@ except ImportError:
|
|||
config.set('cython', 'working', 'False')
|
||||
|
||||
|
||||
class Kern_check_model(GPy.core.Model):
|
||||
class Kern_check_model(GPy.core.ProbabilisticModel):
|
||||
"""
|
||||
This is a dummy model class used as a base class for checking that the
|
||||
gradients of a given kernel are implemented correctly. It enables
|
||||
checkgrad() to be called independently on a kernel.
|
||||
"""
|
||||
def __init__(self, kernel=None, dL_dK=None, X=None, X2=None):
|
||||
GPy.core.Model.__init__(self, 'kernel_test_model')
|
||||
super(Kern_check_model, self).__init__('kernel_test_model')
|
||||
if kernel==None:
|
||||
kernel = GPy.kern.RBF(1)
|
||||
kernel.randomize(loc=1, scale=0.1)
|
||||
|
|
@ -457,7 +456,7 @@ class KernelTestsProductWithZeroValues(unittest.TestCase):
|
|||
class Kernel_Psi_statistics_GradientTests(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
from GPy.core.parameterization.variational import NormalPosterior
|
||||
from GPy.core.variational import NormalPosterior
|
||||
N,M,Q = 100,20,3
|
||||
|
||||
X = np.random.randn(N,Q)
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import unittest
|
|||
import numpy as np
|
||||
import GPy
|
||||
|
||||
class MappingGradChecker(GPy.core.Model):
|
||||
class MappingGradChecker(GPy.core.ProbabilisticModel):
|
||||
"""
|
||||
This class has everything we need to check the gradient of a mapping. It
|
||||
implement a simple likelihood which is a weighted sum of the outputs of the
|
||||
|
|
|
|||
|
|
@ -1,132 +0,0 @@
|
|||
# Copyright (c) 2014, Max Zwiessele
|
||||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
import unittest
|
||||
from GPy.core.parameterization.parameterized import Parameterized
|
||||
from GPy.core.parameterization.param import Param
|
||||
import numpy
|
||||
|
||||
# One trigger in init
|
||||
_trigger_start = -1
|
||||
|
||||
class ParamTestParent(Parameterized):
|
||||
parent_changed_count = _trigger_start
|
||||
def parameters_changed(self):
|
||||
self.parent_changed_count += 1
|
||||
|
||||
class ParameterizedTest(Parameterized):
|
||||
# One trigger after initialization
|
||||
params_changed_count = _trigger_start
|
||||
def parameters_changed(self):
|
||||
self.params_changed_count += 1
|
||||
|
||||
class Test(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.parent = ParamTestParent('test parent')
|
||||
self.par = ParameterizedTest('test model')
|
||||
self.par2 = ParameterizedTest('test model 2')
|
||||
self.p = Param('test parameter', numpy.random.normal(1,2,(10,3)))
|
||||
|
||||
self.par.link_parameter(self.p)
|
||||
self.par.link_parameter(Param('test1', numpy.random.normal(0,1,(1,))))
|
||||
self.par.link_parameter(Param('test2', numpy.random.normal(0,1,(1,))))
|
||||
|
||||
self.par2.link_parameter(Param('par2 test1', numpy.random.normal(0,1,(1,))))
|
||||
self.par2.link_parameter(Param('par2 test2', numpy.random.normal(0,1,(1,))))
|
||||
|
||||
self.parent.link_parameter(self.par)
|
||||
self.parent.link_parameter(self.par2)
|
||||
|
||||
self._observer_triggered = None
|
||||
self._trigger_count = 0
|
||||
self._first = None
|
||||
self._second = None
|
||||
|
||||
def _trigger(self, me, which):
|
||||
self._observer_triggered = which
|
||||
self._trigger_count += 1
|
||||
if self._first is not None:
|
||||
self._second = self._trigger
|
||||
else:
|
||||
self._first = self._trigger
|
||||
|
||||
def _trigger_priority(self, me, which):
|
||||
if self._first is not None:
|
||||
self._second = self._trigger_priority
|
||||
else:
|
||||
self._first = self._trigger_priority
|
||||
|
||||
def test_observable(self):
|
||||
self.par.add_observer(self, self._trigger, -1)
|
||||
self.assertEqual(self.par.params_changed_count, 0, 'no params changed yet')
|
||||
self.assertEqual(self.par.params_changed_count, self.parent.parent_changed_count, 'parent should be triggered as often as param')
|
||||
|
||||
self.p[0,1] = 3 # trigger observers
|
||||
self.assertIs(self._observer_triggered, self.p, 'observer should have triggered')
|
||||
self.assertEqual(self._trigger_count, 1, 'observer should have triggered once')
|
||||
self.assertEqual(self.par.params_changed_count, 1, 'params changed once')
|
||||
self.assertEqual(self.par.params_changed_count, self.parent.parent_changed_count, 'parent should be triggered as often as param')
|
||||
|
||||
self.par.remove_observer(self)
|
||||
self.p[0,1] = 4
|
||||
self.assertIs(self._observer_triggered, self.p, 'observer should not have triggered')
|
||||
self.assertEqual(self._trigger_count, 1, 'observer should have triggered once')
|
||||
self.assertEqual(self.par.params_changed_count, 2, 'params changed second')
|
||||
self.assertEqual(self.par.params_changed_count, self.parent.parent_changed_count, 'parent should be triggered as often as param')
|
||||
|
||||
self.par.add_observer(self, self._trigger, -1)
|
||||
self.p[0,1] = 4
|
||||
self.assertIs(self._observer_triggered, self.p, 'observer should have triggered')
|
||||
self.assertEqual(self._trigger_count, 2, 'observer should have triggered once')
|
||||
self.assertEqual(self.par.params_changed_count, 3, 'params changed second')
|
||||
self.assertEqual(self.par.params_changed_count, self.parent.parent_changed_count, 'parent should be triggered as often as param')
|
||||
|
||||
self.par.remove_observer(self, self._trigger)
|
||||
self.p[0,1] = 3
|
||||
self.assertIs(self._observer_triggered, self.p, 'observer should not have triggered')
|
||||
self.assertEqual(self._trigger_count, 2, 'observer should have triggered once')
|
||||
self.assertEqual(self.par.params_changed_count, 4, 'params changed second')
|
||||
self.assertEqual(self.par.params_changed_count, self.parent.parent_changed_count, 'parent should be triggered as often as param')
|
||||
|
||||
def test_set_params(self):
|
||||
self.assertEqual(self.par.params_changed_count, 0, 'no params changed yet')
|
||||
self.par.param_array[:] = 1
|
||||
self.par._trigger_params_changed()
|
||||
self.assertEqual(self.par.params_changed_count, 1, 'now params changed')
|
||||
self.assertEqual(self.parent.parent_changed_count, self.par.params_changed_count)
|
||||
|
||||
self.par.param_array[:] = 2
|
||||
self.par._trigger_params_changed()
|
||||
self.assertEqual(self.par.params_changed_count, 2, 'now params changed')
|
||||
self.assertEqual(self.parent.parent_changed_count, self.par.params_changed_count)
|
||||
|
||||
|
||||
def test_priority_notify(self):
|
||||
self.assertEqual(self.par.params_changed_count, 0)
|
||||
self.par.notify_observers(0, None)
|
||||
self.assertEqual(self.par.params_changed_count, 1)
|
||||
self.assertEqual(self.parent.parent_changed_count, self.par.params_changed_count)
|
||||
|
||||
self.par.notify_observers(0, -numpy.inf)
|
||||
self.assertEqual(self.par.params_changed_count, 2)
|
||||
self.assertEqual(self.parent.parent_changed_count, 1)
|
||||
|
||||
def test_priority(self):
|
||||
self.par.add_observer(self, self._trigger, -1)
|
||||
self.par.add_observer(self, self._trigger_priority, 0)
|
||||
self.par.notify_observers(0)
|
||||
self.assertEqual(self._first, self._trigger_priority, 'priority should be first')
|
||||
self.assertEqual(self._second, self._trigger, 'priority should be first')
|
||||
|
||||
self.par.remove_observer(self)
|
||||
self._first = self._second = None
|
||||
|
||||
self.par.add_observer(self, self._trigger, 1)
|
||||
self.par.add_observer(self, self._trigger_priority, 0)
|
||||
self.par.notify_observers(0)
|
||||
self.assertEqual(self._first, self._trigger, 'priority should be second')
|
||||
self.assertEqual(self._second, self._trigger_priority, 'priority should be second')
|
||||
|
||||
if __name__ == "__main__":
|
||||
#import sys;sys.argv = ['', 'Test.testName']
|
||||
unittest.main()
|
||||
|
|
@ -1,264 +0,0 @@
|
|||
'''
|
||||
Created on Feb 13, 2014
|
||||
|
||||
@author: maxzwiessele
|
||||
'''
|
||||
import unittest
|
||||
import GPy
|
||||
import numpy as np
|
||||
from GPy.core.parameterization.parameter_core import HierarchyError
|
||||
from GPy.core.parameterization.observable_array import ObsAr
|
||||
from GPy.core.parameterization.transformations import NegativeLogexp, Logistic
|
||||
from GPy.core.parameterization.parameterized import Parameterized
|
||||
from GPy.core.parameterization.param import Param
|
||||
from GPy.core.parameterization.index_operations import ParameterIndexOperations
|
||||
from functools import reduce
|
||||
|
||||
class ArrayCoreTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.X = np.random.normal(1,1, size=(100,10))
|
||||
self.obsX = ObsAr(self.X)
|
||||
|
||||
def test_init(self):
|
||||
X = ObsAr(self.X)
|
||||
X2 = ObsAr(X)
|
||||
self.assertIs(X, X2, "no new Observable array, when Observable is given")
|
||||
|
||||
def test_slice(self):
|
||||
t1 = self.X[2:78]
|
||||
t2 = self.obsX[2:78]
|
||||
self.assertListEqual(t1.tolist(), t2.tolist(), "Slicing should be the exact same, as in ndarray")
|
||||
|
||||
class ParameterizedTest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.rbf = GPy.kern.RBF(20)
|
||||
self.white = GPy.kern.White(1)
|
||||
from GPy.core.parameterization import Param
|
||||
from GPy.core.parameterization.transformations import Logistic
|
||||
self.param = Param('param', np.random.uniform(0,1,(10,5)), Logistic(0, 1))
|
||||
|
||||
self.test1 = GPy.core.Parameterized("test model")
|
||||
self.test1.param = self.param
|
||||
self.test1.kern = self.rbf+self.white
|
||||
self.test1.link_parameter(self.test1.kern)
|
||||
self.test1.link_parameter(self.param, 0)
|
||||
|
||||
# print self.test1:
|
||||
#=============================================================================
|
||||
# test_model. | Value | Constraint | Prior | Tied to
|
||||
# param | (25L, 2L) | {0.0,1.0} | |
|
||||
# add.rbf.variance | 1.0 | 0.0,1.0 +ve | |
|
||||
# add.rbf.lengthscale | 1.0 | 0.0,1.0 +ve | |
|
||||
# add.white.variance | 1.0 | 0.0,1.0 +ve | |
|
||||
#=============================================================================
|
||||
|
||||
x = np.linspace(-2,6,4)[:,None]
|
||||
y = np.sin(x)
|
||||
self.testmodel = GPy.models.GPRegression(x,y)
|
||||
# print self.testmodel:
|
||||
#=============================================================================
|
||||
# GP_regression. | Value | Constraint | Prior | Tied to
|
||||
# rbf.variance | 1.0 | +ve | |
|
||||
# rbf.lengthscale | 1.0 | +ve | |
|
||||
# Gaussian_noise.variance | 1.0 | +ve | |
|
||||
#=============================================================================
|
||||
|
||||
def test_add_parameter(self):
|
||||
self.assertEquals(self.rbf._parent_index_, 0)
|
||||
self.assertEquals(self.white._parent_index_, 1)
|
||||
self.assertEquals(self.param._parent_index_, 0)
|
||||
pass
|
||||
|
||||
def test_fixes(self):
|
||||
self.white.fix(warning=False)
|
||||
self.test1.unlink_parameter(self.param)
|
||||
self.assertTrue(self.test1._has_fixes())
|
||||
from GPy.core.parameterization.transformations import FIXED, UNFIXED
|
||||
self.assertListEqual(self.test1._fixes_.tolist(),[UNFIXED,UNFIXED,FIXED])
|
||||
self.test1.kern.link_parameter(self.white, 0)
|
||||
self.assertListEqual(self.test1._fixes_.tolist(),[FIXED,UNFIXED,UNFIXED])
|
||||
self.test1.kern.rbf.fix()
|
||||
self.assertListEqual(self.test1._fixes_.tolist(),[FIXED]*3)
|
||||
self.test1.fix()
|
||||
self.assertTrue(self.test1.is_fixed)
|
||||
self.assertListEqual(self.test1._fixes_.tolist(),[FIXED]*self.test1.size)
|
||||
|
||||
def test_remove_parameter(self):
|
||||
from GPy.core.parameterization.transformations import FIXED, UNFIXED, __fixed__, Logexp
|
||||
self.white.fix()
|
||||
self.test1.kern.unlink_parameter(self.white)
|
||||
self.assertIs(self.test1._fixes_,None)
|
||||
|
||||
self.assertIsInstance(self.white.constraints, ParameterIndexOperations)
|
||||
self.assertListEqual(self.white._fixes_.tolist(), [FIXED])
|
||||
self.assertIs(self.test1.constraints, self.rbf.constraints._param_index_ops)
|
||||
self.assertIs(self.test1.constraints, self.param.constraints._param_index_ops)
|
||||
|
||||
self.test1.link_parameter(self.white, 0)
|
||||
self.assertIs(self.test1.constraints, self.white.constraints._param_index_ops)
|
||||
self.assertIs(self.test1.constraints, self.rbf.constraints._param_index_ops)
|
||||
self.assertIs(self.test1.constraints, self.param.constraints._param_index_ops)
|
||||
self.assertListEqual(self.test1.constraints[__fixed__].tolist(), [0])
|
||||
self.assertIs(self.white._fixes_,None)
|
||||
self.assertListEqual(self.test1._fixes_.tolist(),[FIXED] + [UNFIXED] * 52)
|
||||
|
||||
self.test1.unlink_parameter(self.white)
|
||||
self.assertIs(self.test1._fixes_,None)
|
||||
self.assertListEqual(self.white._fixes_.tolist(), [FIXED])
|
||||
self.assertIs(self.test1.constraints, self.rbf.constraints._param_index_ops)
|
||||
self.assertIs(self.test1.constraints, self.param.constraints._param_index_ops)
|
||||
self.assertListEqual(self.test1.constraints[Logexp()].tolist(), list(range(self.param.size, self.param.size+self.rbf.size)))
|
||||
|
||||
def test_remove_parameter_param_array_grad_array(self):
|
||||
val = self.test1.kern.param_array.copy()
|
||||
self.test1.kern.unlink_parameter(self.white)
|
||||
self.assertListEqual(self.test1.kern.param_array.tolist(), val[:2].tolist())
|
||||
|
||||
def test_add_parameter_already_in_hirarchy(self):
|
||||
self.assertRaises(HierarchyError, self.test1.link_parameter, self.white.parameters[0])
|
||||
|
||||
def test_default_constraints(self):
|
||||
self.assertIs(self.rbf.variance.constraints._param_index_ops, self.rbf.constraints._param_index_ops)
|
||||
self.assertIs(self.test1.constraints, self.rbf.constraints._param_index_ops)
|
||||
self.assertListEqual(self.rbf.constraints.indices()[0].tolist(), list(range(2)))
|
||||
from GPy.core.parameterization.transformations import Logexp
|
||||
kern = self.test1.kern
|
||||
self.test1.unlink_parameter(kern)
|
||||
self.assertListEqual(kern.constraints[Logexp()].tolist(), list(range(3)))
|
||||
|
||||
def test_constraints(self):
|
||||
self.rbf.constrain(GPy.transformations.Square(), False)
|
||||
self.assertListEqual(self.test1.constraints[GPy.transformations.Square()].tolist(), list(range(self.param.size, self.param.size+self.rbf.size)))
|
||||
self.assertListEqual(self.test1.constraints[GPy.transformations.Logexp()].tolist(), [self.param.size+self.rbf.size])
|
||||
|
||||
self.test1.kern.unlink_parameter(self.rbf)
|
||||
self.assertListEqual(self.test1.constraints[GPy.transformations.Square()].tolist(), [])
|
||||
|
||||
def test_constraints_link_unlink(self):
|
||||
self.test1.unlink_parameter(self.test1.kern)
|
||||
self.test1.kern.rbf.unlink_parameter(self.test1.kern.rbf.lengthscale)
|
||||
self.test1.kern.rbf.link_parameter(self.test1.kern.rbf.lengthscale)
|
||||
self.test1.kern.rbf.unlink_parameter(self.test1.kern.rbf.lengthscale)
|
||||
self.test1.link_parameter(self.test1.kern)
|
||||
|
||||
def test_constraints_views(self):
|
||||
self.assertEqual(self.white.constraints._offset, self.param.size+self.rbf.size)
|
||||
self.assertEqual(self.rbf.constraints._offset, self.param.size)
|
||||
self.assertEqual(self.param.constraints._offset, 0)
|
||||
|
||||
def test_fixing_randomize(self):
|
||||
self.white.fix(warning=True)
|
||||
val = float(self.white.variance)
|
||||
self.test1.randomize()
|
||||
self.assertEqual(val, self.white.variance)
|
||||
|
||||
def test_randomize(self):
|
||||
ps = self.test1.param.view(np.ndarray).copy()
|
||||
self.test1.param[2:5].fix()
|
||||
self.test1.param.randomize()
|
||||
self.assertFalse(np.all(ps==self.test1.param),str(ps)+str(self.test1.param))
|
||||
|
||||
def test_fixing_randomize_parameter_handling(self):
|
||||
self.rbf.fix(warning=True)
|
||||
val = float(self.rbf.variance)
|
||||
self.test1.kern.randomize()
|
||||
self.assertEqual(val, self.rbf.variance)
|
||||
|
||||
def test_updates(self):
|
||||
val = float(self.testmodel.log_likelihood())
|
||||
self.testmodel.update_model(False)
|
||||
self.testmodel.kern.randomize()
|
||||
self.testmodel.likelihood.randomize()
|
||||
self.assertEqual(val, self.testmodel.log_likelihood())
|
||||
self.testmodel.update_model(True)
|
||||
self.assertNotEqual(val, self.testmodel.log_likelihood())
|
||||
|
||||
def test_fixing_optimize(self):
|
||||
self.testmodel.kern.lengthscale.fix()
|
||||
val = float(self.testmodel.kern.lengthscale)
|
||||
self.testmodel.randomize()
|
||||
self.assertEqual(val, self.testmodel.kern.lengthscale)
|
||||
|
||||
def test_add_parameter_in_hierarchy(self):
|
||||
self.test1.kern.rbf.link_parameter(Param("NEW", np.random.rand(2), NegativeLogexp()), 1)
|
||||
self.assertListEqual(self.test1.constraints[NegativeLogexp()].tolist(), list(range(self.param.size+1, self.param.size+1 + 2)))
|
||||
self.assertListEqual(self.test1.constraints[GPy.transformations.Logistic(0,1)].tolist(), list(range(self.param.size)))
|
||||
self.assertListEqual(self.test1.constraints[GPy.transformations.Logexp(0,1)].tolist(), np.r_[50, 53:55].tolist())
|
||||
|
||||
def test_regular_expression_misc(self):
|
||||
self.testmodel.kern.lengthscale.fix()
|
||||
val = float(self.testmodel.kern.lengthscale)
|
||||
self.testmodel.randomize()
|
||||
self.assertEqual(val, self.testmodel.kern.lengthscale)
|
||||
|
||||
variances = self.testmodel['.*var'].values()
|
||||
self.testmodel['.*var'].fix()
|
||||
self.testmodel.randomize()
|
||||
np.testing.assert_equal(variances, self.testmodel['.*var'].values())
|
||||
|
||||
def test_fix_unfix(self):
|
||||
fixed = self.testmodel.kern.lengthscale.fix()
|
||||
self.assertListEqual(fixed.tolist(), [0])
|
||||
unfixed = self.testmodel.kern.lengthscale.unfix()
|
||||
self.testmodel.kern.lengthscale.constrain_positive()
|
||||
self.assertListEqual(unfixed.tolist(), [0])
|
||||
|
||||
fixed = self.testmodel.kern.fix()
|
||||
self.assertListEqual(fixed.tolist(), [0,1])
|
||||
unfixed = self.testmodel.kern.unfix()
|
||||
self.assertListEqual(unfixed.tolist(), [0,1])
|
||||
|
||||
def test_constraints_in_init(self):
|
||||
class Test(Parameterized):
|
||||
def __init__(self, name=None, parameters=[], *a, **kw):
|
||||
super(Test, self).__init__(name=name)
|
||||
self.x = Param('x', np.random.uniform(0,1,(3,4)))
|
||||
self.x[0].constrain_bounded(0,1)
|
||||
self.link_parameter(self.x)
|
||||
self.x[1].fix()
|
||||
t = Test()
|
||||
c = {Logistic(0,1): np.array([0, 1, 2, 3]), 'fixed': np.array([4, 5, 6, 7])}
|
||||
np.testing.assert_equal(t.x.constraints[Logistic(0,1)], c[Logistic(0,1)])
|
||||
np.testing.assert_equal(t.x.constraints['fixed'], c['fixed'])
|
||||
|
||||
def test_parameter_modify_in_init(self):
|
||||
class TestLikelihood(Parameterized):
|
||||
def __init__(self, param1 = 2., param2 = 3.):
|
||||
super(TestLikelihood, self).__init__("TestLike")
|
||||
self.p1 = Param('param1', param1)
|
||||
self.p2 = Param('param2', param2)
|
||||
|
||||
self.link_parameter(self.p1)
|
||||
self.link_parameter(self.p2)
|
||||
|
||||
self.p1.fix()
|
||||
self.p1.unfix()
|
||||
self.p2.constrain_negative()
|
||||
self.p1.fix()
|
||||
self.p2.constrain_positive()
|
||||
self.p2.fix()
|
||||
self.p2.constrain_positive()
|
||||
|
||||
m = TestLikelihood()
|
||||
print(m)
|
||||
val = m.p1.values.copy()
|
||||
self.assert_(m.p1.is_fixed)
|
||||
self.assert_(m.constraints[GPy.constraints.Logexp()].tolist(), [1])
|
||||
m.randomize()
|
||||
self.assertEqual(m.p1, val)
|
||||
|
||||
def test_checkgrad(self):
|
||||
assert(self.testmodel.kern.checkgrad())
|
||||
assert(self.testmodel.kern.lengthscale.checkgrad())
|
||||
assert(self.testmodel.likelihood.checkgrad())
|
||||
|
||||
def test_printing(self):
|
||||
print(self.test1)
|
||||
print(self.param)
|
||||
print(self.test1[''])
|
||||
print(self.testmodel.hierarchy_name(False))
|
||||
|
||||
if __name__ == "__main__":
|
||||
#import sys;sys.argv = ['', 'Test.test_add_parameter']
|
||||
unittest.main()
|
||||
|
|
@ -7,23 +7,11 @@ import unittest, itertools
|
|||
#import cPickle as pickle
|
||||
import pickle
|
||||
import numpy as np
|
||||
from GPy.core.parameterization.index_operations import ParameterIndexOperations,\
|
||||
ParameterIndexOperationsView
|
||||
import tempfile
|
||||
from GPy.core.parameterization.param import Param
|
||||
from GPy.core.parameterization.observable_array import ObsAr
|
||||
from GPy.core.parameterization.priors import Gaussian
|
||||
from GPy.kern.src.rbf import RBF
|
||||
from GPy.kern.src.linear import Linear
|
||||
from GPy.kern.src.static import Bias, White
|
||||
from GPy.examples.dimensionality_reduction import mrd_simulation
|
||||
from GPy.core.parameterization.variational import NormalPosterior
|
||||
from GPy.core.variational import NormalPosterior
|
||||
from GPy.models.gp_regression import GPRegression
|
||||
from functools import reduce
|
||||
from GPy.util.caching import Cacher
|
||||
import GPy
|
||||
from pickle import PicklingError
|
||||
import GPy
|
||||
|
||||
def toy_model():
|
||||
X = np.linspace(0,1,50)[:, None]
|
||||
|
|
@ -42,89 +30,6 @@ class ListDictTestCase(unittest.TestCase):
|
|||
np.testing.assert_array_equal(a1, a2)
|
||||
|
||||
class Test(ListDictTestCase):
|
||||
def test_parameter_index_operations(self):
|
||||
pio = ParameterIndexOperations(dict(test1=np.array([4,3,1,6,4]), test2=np.r_[2:130]))
|
||||
piov = ParameterIndexOperationsView(pio, 20, 250)
|
||||
#py3 fix
|
||||
#self.assertListDictEquals(dict(piov.items()), dict(piov.copy().iteritems()))
|
||||
self.assertListDictEquals(dict(piov.items()), dict(piov.copy().items()))
|
||||
|
||||
#py3 fix
|
||||
#self.assertListDictEquals(dict(pio.iteritems()), dict(pio.copy().items()))
|
||||
self.assertListDictEquals(dict(pio.items()), dict(pio.copy().items()))
|
||||
|
||||
self.assertArrayListEquals(pio.copy().indices(), pio.indices())
|
||||
self.assertArrayListEquals(piov.copy().indices(), piov.indices())
|
||||
|
||||
with tempfile.TemporaryFile('w+b') as f:
|
||||
pickle.dump(pio, f)
|
||||
f.seek(0)
|
||||
pio2 = pickle.load(f)
|
||||
self.assertListDictEquals(pio._properties, pio2._properties)
|
||||
|
||||
f = tempfile.TemporaryFile('w+b')
|
||||
pickle.dump(piov, f)
|
||||
f.seek(0)
|
||||
pio2 = GPy.load(f)
|
||||
f.close()
|
||||
|
||||
#py3 fix
|
||||
#self.assertListDictEquals(dict(piov.items()), dict(pio2.iteritems()))
|
||||
self.assertListDictEquals(dict(piov.items()), dict(pio2.items()))
|
||||
|
||||
def test_param(self):
|
||||
param = Param('test', np.arange(4*2).reshape(4,2))
|
||||
param[0].constrain_positive()
|
||||
param[1].fix()
|
||||
param[2].set_prior(Gaussian(0,1))
|
||||
pcopy = param.copy()
|
||||
self.assertListEqual(param.tolist(), pcopy.tolist())
|
||||
self.assertListEqual(str(param).split('\n'), str(pcopy).split('\n'))
|
||||
self.assertIsNot(param, pcopy)
|
||||
with tempfile.TemporaryFile('w+b') as f:
|
||||
pickle.dump(param, f)
|
||||
f.seek(0)
|
||||
pcopy = pickle.load(f)
|
||||
self.assertListEqual(param.tolist(), pcopy.tolist())
|
||||
self.assertSequenceEqual(str(param), str(pcopy))
|
||||
|
||||
def test_observable_array(self):
|
||||
obs = ObsAr(np.arange(4*2).reshape(4,2))
|
||||
pcopy = obs.copy()
|
||||
self.assertListEqual(obs.tolist(), pcopy.tolist())
|
||||
with tempfile.TemporaryFile('w+b') as f:
|
||||
pickle.dump(obs, f)
|
||||
f.seek(0)
|
||||
pcopy = pickle.load(f)
|
||||
self.assertListEqual(obs.tolist(), pcopy.tolist())
|
||||
self.assertSequenceEqual(str(obs), str(pcopy))
|
||||
|
||||
def test_parameterized(self):
|
||||
par = RBF(1, active_dims=[1]) + Linear(2, active_dims=[0,2]) + Bias(3) + White(3)
|
||||
par.gradient = 10
|
||||
par.randomize()
|
||||
pcopy = par.copy()
|
||||
self.assertIsInstance(pcopy.constraints, ParameterIndexOperations)
|
||||
self.assertIsInstance(pcopy.rbf.constraints, ParameterIndexOperationsView)
|
||||
self.assertIs(pcopy.constraints, pcopy.rbf.constraints._param_index_ops)
|
||||
self.assertIs(pcopy.constraints, pcopy.rbf.lengthscale.constraints._param_index_ops)
|
||||
self.assertIs(pcopy.constraints, pcopy.linear.constraints._param_index_ops)
|
||||
self.assertListEqual(par.param_array.tolist(), pcopy.param_array.tolist())
|
||||
pcopy.gradient = 10 # gradient does not get copied anymore
|
||||
self.assertListEqual(par.gradient_full.tolist(), pcopy.gradient_full.tolist())
|
||||
self.assertSequenceEqual(str(par), str(pcopy))
|
||||
self.assertIsNot(par.param_array, pcopy.param_array)
|
||||
self.assertIsNot(par.gradient_full, pcopy.gradient_full)
|
||||
with tempfile.TemporaryFile('w+b') as f:
|
||||
par.pickle(f)
|
||||
f.seek(0)
|
||||
pcopy = pickle.load(f)
|
||||
self.assertListEqual(par.param_array.tolist(), pcopy.param_array.tolist())
|
||||
pcopy.gradient = 10
|
||||
np.testing.assert_allclose(par.linear.gradient_full, pcopy.linear.gradient_full)
|
||||
np.testing.assert_allclose(pcopy.linear.gradient_full, 10)
|
||||
self.assertSequenceEqual(str(par), str(pcopy))
|
||||
|
||||
def test_model(self):
|
||||
par = toy_model()
|
||||
pcopy = par.copy()
|
||||
|
|
|
|||
|
|
@ -10,12 +10,12 @@ import scipy.stats as st
|
|||
import GPy
|
||||
|
||||
|
||||
class TestModel(GPy.core.Model):
|
||||
class TestModel(GPy.core.ProbabilisticModel):
|
||||
"""
|
||||
A simple GPy model with one parameter.
|
||||
"""
|
||||
def __init__(self, theta=1.):
|
||||
GPy.core.Model.__init__(self, 'test_model')
|
||||
GPy.core.ProbabilisticModel.__init__(self, 'test_model')
|
||||
theta = GPy.core.Param('theta', theta)
|
||||
self.link_parameter(theta)
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,6 @@ from . import mocap
|
|||
from . import decorators
|
||||
from . import classification
|
||||
from . import subarray_and_sorting
|
||||
from . import caching
|
||||
from . import diag
|
||||
from . import initialization
|
||||
from . import multioutput
|
||||
|
|
|
|||
|
|
@ -1,197 +0,0 @@
|
|||
# Copyright (c) 2012, GPy authors (see AUTHORS.txt).
|
||||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
from ..core.parameterization.observable import Observable
|
||||
import collections, weakref
|
||||
from functools import reduce
|
||||
from pickle import PickleError
|
||||
|
||||
class Cacher(object):
|
||||
def __init__(self, operation, limit=5, ignore_args=(), force_kwargs=()):
|
||||
"""
|
||||
Parameters:
|
||||
***********
|
||||
:param callable operation: function to cache
|
||||
:param int limit: depth of cacher
|
||||
:param [int] ignore_args: list of indices, pointing at arguments to ignore in *args of operation(*args). This includes self!
|
||||
:param [str] force_kwargs: list of kwarg names (strings). If a kwarg with that name is given, the cacher will force recompute and wont cache anything.
|
||||
:param int verbose: verbosity level. 0: no print outs, 1: casual print outs, 2: debug level print outs
|
||||
"""
|
||||
self.limit = int(limit)
|
||||
self.ignore_args = ignore_args
|
||||
self.force_kwargs = force_kwargs
|
||||
self.operation = operation
|
||||
self.order = collections.deque()
|
||||
self.cached_inputs = {} # point from cache_ids to a list of [ind_ids], which where used in cache cache_id
|
||||
|
||||
#=======================================================================
|
||||
# point from each ind_id to [ref(obj), cache_ids]
|
||||
# 0: a weak reference to the object itself
|
||||
# 1: the cache_ids in which this ind_id is used (len will be how many times we have seen this ind_id)
|
||||
self.cached_input_ids = {}
|
||||
#=======================================================================
|
||||
|
||||
self.cached_outputs = {} # point from cache_ids to outputs
|
||||
self.inputs_changed = {} # point from cache_ids to bools
|
||||
|
||||
def id(self, obj):
|
||||
"""returns the self.id of an object, to be used in caching individual self.ids"""
|
||||
return hex(id(obj))
|
||||
|
||||
def combine_inputs(self, args, kw, ignore_args):
|
||||
"Combines the args and kw in a unique way, such that ordering of kwargs does not lead to recompute"
|
||||
inputs= args + tuple(c[1] for c in sorted(kw.items(), key=lambda x: x[0]))
|
||||
# REMOVE the ignored arguments from input and PREVENT it from being checked!!!
|
||||
return [a for i,a in enumerate(inputs) if i not in ignore_args]
|
||||
|
||||
def prepare_cache_id(self, combined_args_kw):
|
||||
"get the cacheid (conc. string of argument self.ids in order)"
|
||||
cache_id = "".join(self.id(a) for a in combined_args_kw)
|
||||
return cache_id
|
||||
|
||||
def ensure_cache_length(self, cache_id):
|
||||
"Ensures the cache is within its limits and has one place free"
|
||||
if len(self.order) == self.limit:
|
||||
# we have reached the limit, so lets release one element
|
||||
cache_id = self.order.popleft()
|
||||
combined_args_kw = self.cached_inputs[cache_id]
|
||||
for ind in combined_args_kw:
|
||||
if ind is not None:
|
||||
ind_id = self.id(ind)
|
||||
tmp = self.cached_input_ids.get(ind_id, None)
|
||||
if tmp is not None:
|
||||
ref, cache_ids = tmp
|
||||
if len(cache_ids) == 1 and ref() is not None:
|
||||
ref().remove_observer(self, self.on_cache_changed)
|
||||
del self.cached_input_ids[ind_id]
|
||||
else:
|
||||
cache_ids.remove(cache_id)
|
||||
self.cached_input_ids[ind_id] = [ref, cache_ids]
|
||||
del self.cached_outputs[cache_id]
|
||||
del self.inputs_changed[cache_id]
|
||||
del self.cached_inputs[cache_id]
|
||||
|
||||
def add_to_cache(self, cache_id, inputs, output):
|
||||
"""This adds cache_id to the cache, with inputs and output"""
|
||||
self.inputs_changed[cache_id] = False
|
||||
self.cached_outputs[cache_id] = output
|
||||
self.order.append(cache_id)
|
||||
self.cached_inputs[cache_id] = inputs
|
||||
for a in inputs:
|
||||
if a is not None and not isinstance(a, int):
|
||||
ind_id = self.id(a)
|
||||
v = self.cached_input_ids.get(ind_id, [weakref.ref(a), []])
|
||||
v[1].append(cache_id)
|
||||
if len(v[1]) == 1:
|
||||
a.add_observer(self, self.on_cache_changed)
|
||||
self.cached_input_ids[ind_id] = v
|
||||
|
||||
def __call__(self, *args, **kw):
|
||||
"""
|
||||
A wrapper function for self.operation,
|
||||
"""
|
||||
#=======================================================================
|
||||
# !WARNING CACHE OFFSWITCH!
|
||||
# return self.operation(*args, **kw)
|
||||
#=======================================================================
|
||||
|
||||
# 1: Check whether we have forced recompute arguments:
|
||||
if len(self.force_kwargs) != 0:
|
||||
for k in self.force_kwargs:
|
||||
if k in kw and kw[k] is not None:
|
||||
return self.operation(*args, **kw)
|
||||
|
||||
# 2: prepare_cache_id and get the unique self.id string for this call
|
||||
inputs = self.combine_inputs(args, kw, self.ignore_args)
|
||||
cache_id = self.prepare_cache_id(inputs)
|
||||
# 2: if anything is not cachable, we will just return the operation, without caching
|
||||
if reduce(lambda a, b: a or (not (isinstance(b, Observable) or b is None or isinstance(b,int))), inputs, False):
|
||||
# print 'WARNING: '+self.operation.__name__ + ' not cacheable!'
|
||||
# print [not (isinstance(b, Observable)) for b in inputs]
|
||||
return self.operation(*args, **kw)
|
||||
# 3&4: check whether this cache_id has been cached, then has it changed?
|
||||
try:
|
||||
if(self.inputs_changed[cache_id]):
|
||||
# 4: This happens, when elements have changed for this cache self.id
|
||||
self.inputs_changed[cache_id] = False
|
||||
self.cached_outputs[cache_id] = self.operation(*args, **kw)
|
||||
except KeyError:
|
||||
# 3: This is when we never saw this chache_id:
|
||||
self.ensure_cache_length(cache_id)
|
||||
self.add_to_cache(cache_id, inputs, self.operation(*args, **kw))
|
||||
except:
|
||||
self.reset()
|
||||
raise
|
||||
# 5: We have seen this cache_id and it is cached:
|
||||
return self.cached_outputs[cache_id]
|
||||
|
||||
def on_cache_changed(self, direct, which=None):
|
||||
"""
|
||||
A callback funtion, which sets local flags when the elements of some cached inputs change
|
||||
|
||||
this function gets 'hooked up' to the inputs when we cache them, and upon their elements being changed we update here.
|
||||
"""
|
||||
for what in [direct, which]:
|
||||
if what is not None:
|
||||
ind_id = self.id(what)
|
||||
_, cache_ids = self.cached_input_ids.get(ind_id, [None, []])
|
||||
for cache_id in cache_ids:
|
||||
self.inputs_changed[cache_id] = True
|
||||
|
||||
def reset(self):
|
||||
"""
|
||||
Totally reset the cache
|
||||
"""
|
||||
[a().remove_observer(self, self.on_cache_changed) if (a() is not None) else None for [a, _] in self.cached_input_ids.values()]
|
||||
self.cached_input_ids = {}
|
||||
self.cached_outputs = {}
|
||||
self.inputs_changed = {}
|
||||
|
||||
def __deepcopy__(self, memo=None):
|
||||
return Cacher(self.operation, self.limit, self.ignore_args, self.force_kwargs)
|
||||
|
||||
def __getstate__(self, memo=None):
|
||||
raise PickleError("Trying to pickle Cacher object with function {}, pickling functions not possible.".format(str(self.operation)))
|
||||
|
||||
def __setstate__(self, memo=None):
|
||||
raise PickleError("Trying to pickle Cacher object with function {}, pickling functions not possible.".format(str(self.operation)))
|
||||
|
||||
@property
|
||||
def __name__(self):
|
||||
return self.operation.__name__
|
||||
|
||||
from functools import partial, update_wrapper
|
||||
|
||||
class Cacher_wrap(object):
|
||||
def __init__(self, f, limit, ignore_args, force_kwargs):
|
||||
self.limit = limit
|
||||
self.ignore_args = ignore_args
|
||||
self.force_kwargs = force_kwargs
|
||||
self.f = f
|
||||
update_wrapper(self, self.f)
|
||||
def __get__(self, obj, objtype=None):
|
||||
return partial(self, obj)
|
||||
def __call__(self, *args, **kwargs):
|
||||
obj = args[0]
|
||||
# import ipdb;ipdb.set_trace()
|
||||
try:
|
||||
caches = obj.__cachers
|
||||
except AttributeError:
|
||||
caches = obj.__cachers = {}
|
||||
try:
|
||||
cacher = caches[self.f]
|
||||
except KeyError:
|
||||
cacher = caches[self.f] = Cacher(self.f, self.limit, self.ignore_args, self.force_kwargs)
|
||||
return cacher(*args, **kwargs)
|
||||
|
||||
class Cache_this(object):
|
||||
"""
|
||||
A decorator which can be applied to bound methods in order to cache them
|
||||
"""
|
||||
def __init__(self, limit=5, ignore_args=(), force_kwargs=()):
|
||||
self.limit = limit
|
||||
self.ignore_args = ignore_args
|
||||
self.force_args = force_kwargs
|
||||
def __call__(self, f):
|
||||
newf = Cacher_wrap(f, self.limit, self.ignore_args, self.force_args)
|
||||
update_wrapper(newf, f)
|
||||
return newf
|
||||
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
import numpy as np
|
||||
from ..core.parameterization import Parameterized, Param
|
||||
from ..core.parameterization.transformations import Logexp
|
||||
from paramz.transformations import Logexp
|
||||
|
||||
|
||||
class WarpingFunction(Parameterized):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue