mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-05-07 02:52:40 +02:00
Merge pull request #321 from SheffieldML/limit=3
[chaching] changing all chacher limits to 3
This commit is contained in:
commit
462e23ab2b
48 changed files with 72 additions and 72 deletions
|
|
@ -28,4 +28,4 @@ from .src.trunclinear import TruncLinear,TruncLinear_inf
|
|||
from .src.splitKern import SplitKern,DEtime
|
||||
from .src.splitKern import DEtime as DiffGenomeKern
|
||||
from .src.spline import Spline
|
||||
from .src.basis_funcs import LogisticBasisFuncKernel, LinearSlopeBasisFuncKernel, BasisFuncKernel, ChangePointBasisFuncKernel, DomainKernel
|
||||
from .src.basis_funcs import LogisticBasisFuncKernel, LinearSlopeBasisFuncKernel, BasisFuncKernel, ChangePointBasisFuncKernel, DomainKernel
|
||||
|
|
|
|||
|
|
@ -162,4 +162,4 @@ class ODE_t(Kern):
|
|||
|
||||
self.lengthscale_Yt.gradient = np.sum(dkYdlent*(-0.5*self.lengthscale_Yt**(-2)) * dL_dK)
|
||||
|
||||
self.ubias.gradient = np.sum(dkdubias * dL_dK)
|
||||
self.ubias.gradient = np.sum(dkdubias * dL_dK)
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
from . import psi_comp
|
||||
from . import psi_comp
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ class Add(CombinationKernel):
|
|||
else:
|
||||
return False
|
||||
|
||||
@Cache_this(limit=2, force_kwargs=['which_parts'])
|
||||
@Cache_this(limit=3, force_kwargs=['which_parts'])
|
||||
def K(self, X, X2=None, which_parts=None):
|
||||
"""
|
||||
Add all kernels together.
|
||||
|
|
@ -51,7 +51,7 @@ class Add(CombinationKernel):
|
|||
which_parts = [which_parts]
|
||||
return reduce(np.add, (p.K(X, X2) for p in which_parts))
|
||||
|
||||
@Cache_this(limit=2, force_kwargs=['which_parts'])
|
||||
@Cache_this(limit=3, force_kwargs=['which_parts'])
|
||||
def Kdiag(self, X, which_parts=None):
|
||||
if which_parts is None:
|
||||
which_parts = self.parts
|
||||
|
|
@ -98,17 +98,17 @@ class Add(CombinationKernel):
|
|||
[target.__iadd__(p.gradients_XX_diag(dL_dKdiag, X)) for p in self.parts]
|
||||
return target
|
||||
|
||||
@Cache_this(limit=1, force_kwargs=['which_parts'])
|
||||
@Cache_this(limit=3, force_kwargs=['which_parts'])
|
||||
def psi0(self, Z, variational_posterior):
|
||||
if not self._exact_psicomp: return Kern.psi0(self,Z,variational_posterior)
|
||||
return reduce(np.add, (p.psi0(Z, variational_posterior) for p in self.parts))
|
||||
|
||||
@Cache_this(limit=1, force_kwargs=['which_parts'])
|
||||
@Cache_this(limit=3, force_kwargs=['which_parts'])
|
||||
def psi1(self, Z, variational_posterior):
|
||||
if not self._exact_psicomp: return Kern.psi1(self,Z,variational_posterior)
|
||||
return reduce(np.add, (p.psi1(Z, variational_posterior) for p in self.parts))
|
||||
|
||||
@Cache_this(limit=1, force_kwargs=['which_parts'])
|
||||
@Cache_this(limit=3, force_kwargs=['which_parts'])
|
||||
def psi2(self, Z, variational_posterior):
|
||||
if not self._exact_psicomp: return Kern.psi2(self,Z,variational_posterior)
|
||||
psi2 = reduce(np.add, (p.psi2(Z, variational_posterior) for p in self.parts))
|
||||
|
|
@ -144,7 +144,7 @@ class Add(CombinationKernel):
|
|||
raise NotImplementedError("psi2 cannot be computed for this kernel")
|
||||
return psi2
|
||||
|
||||
@Cache_this(limit=1, force_kwargs=['which_parts'])
|
||||
@Cache_this(limit=3, force_kwargs=['which_parts'])
|
||||
def psi2n(self, Z, variational_posterior):
|
||||
if not self._exact_psicomp: return Kern.psi2n(self, Z, variational_posterior)
|
||||
psi2 = reduce(np.add, (p.psi2n(Z, variational_posterior) for p in self.parts))
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ class EQ_ODE2(Kern):
|
|||
self.W = Param('W', W)
|
||||
self.link_parameters(self.lengthscale, self.C, self.B, self.W)
|
||||
|
||||
@Cache_this(limit=2)
|
||||
@Cache_this(limit=3)
|
||||
def K(self, X, X2=None):
|
||||
#This way is not working, indexes are lost after using k._slice_X
|
||||
#index = np.asarray(X, dtype=np.int)
|
||||
|
|
|
|||
|
|
@ -68,7 +68,7 @@ class Kern(Parameterized):
|
|||
def _effective_input_dim(self):
|
||||
return np.size(self._all_dims_active)
|
||||
|
||||
@Cache_this(limit=20)
|
||||
@Cache_this(limit=3)
|
||||
def _slice_X(self, X):
|
||||
try:
|
||||
return X[:, self._all_dims_active].astype('float')
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ class Linear(Kern):
|
|||
self.link_parameter(self.variances)
|
||||
self.psicomp = PSICOMP_Linear()
|
||||
|
||||
@Cache_this(limit=2)
|
||||
@Cache_this(limit=3)
|
||||
def K(self, X, X2=None):
|
||||
if self.ARD:
|
||||
if X2 is None:
|
||||
|
|
@ -62,7 +62,7 @@ class Linear(Kern):
|
|||
else:
|
||||
return self._dot_product(X, X2) * self.variances
|
||||
|
||||
@Cache_this(limit=1, ignore_args=(0,))
|
||||
@Cache_this(limit=3, ignore_args=(0,))
|
||||
def _dot_product(self, X, X2=None):
|
||||
if X2 is None:
|
||||
return tdot(X)
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ class MLP(Kern):
|
|||
self.link_parameters(self.variance, self.weight_variance, self.bias_variance)
|
||||
|
||||
|
||||
@Cache_this(limit=20, ignore_args=())
|
||||
@Cache_this(limit=3, ignore_args=())
|
||||
def K(self, X, X2=None):
|
||||
if X2 is None:
|
||||
X_denom = np.sqrt(self._comp_prod(X)+1.)
|
||||
|
|
@ -57,7 +57,7 @@ class MLP(Kern):
|
|||
XTX = self._comp_prod(X,X2)/X_denom[:,None]/X2_denom[None,:]
|
||||
return self.variance*four_over_tau*np.arcsin(XTX)
|
||||
|
||||
@Cache_this(limit=20, ignore_args=())
|
||||
@Cache_this(limit=3, ignore_args=())
|
||||
def Kdiag(self, X):
|
||||
"""Compute the diagonal of the covariance matrix for X."""
|
||||
X_prod = self._comp_prod(X)
|
||||
|
|
@ -88,14 +88,14 @@ class MLP(Kern):
|
|||
"""Gradient of diagonal of covariance with respect to X"""
|
||||
return self._comp_grads_diag(dL_dKdiag, X)[3]
|
||||
|
||||
@Cache_this(limit=50, ignore_args=())
|
||||
@Cache_this(limit=3, ignore_args=())
|
||||
def _comp_prod(self, X, X2=None):
|
||||
if X2 is None:
|
||||
return (np.square(X)*self.weight_variance).sum(axis=1)+self.bias_variance
|
||||
else:
|
||||
return (X*self.weight_variance).dot(X2.T)+self.bias_variance
|
||||
|
||||
@Cache_this(limit=20, ignore_args=(1,))
|
||||
@Cache_this(limit=3, ignore_args=(1,))
|
||||
def _comp_grads(self, dL_dK, X, X2=None):
|
||||
var,w,b = self.variance, self.weight_variance, self.bias_variance
|
||||
K = self.K(X, X2)
|
||||
|
|
@ -130,7 +130,7 @@ class MLP(Kern):
|
|||
dX2 = common.T.dot(X)*w-((common*XTX).sum(axis=0)/(X2_prod+1.))[:,None]*X2*w
|
||||
return dvar, dw, db, dX, dX2
|
||||
|
||||
@Cache_this(limit=20, ignore_args=(1,))
|
||||
@Cache_this(limit=3, ignore_args=(1,))
|
||||
def _comp_grads_diag(self, dL_dKdiag, X):
|
||||
var,w,b = self.variance, self.weight_variance, self.bias_variance
|
||||
K = self.Kdiag(X)
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ class Poly(Kern):
|
|||
_, _, B = self._AB(X, X2)
|
||||
return B * self.variance
|
||||
|
||||
@Cache_this(limit=2)
|
||||
@Cache_this(limit=3)
|
||||
def _AB(self, X, X2=None):
|
||||
if X2 is None:
|
||||
dot_prod = np.dot(X, X.T)
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ class Prod(CombinationKernel):
|
|||
kernels.insert(i, part)
|
||||
super(Prod, self).__init__(kernels, name)
|
||||
|
||||
@Cache_this(limit=2, force_kwargs=['which_parts'])
|
||||
@Cache_this(limit=3, force_kwargs=['which_parts'])
|
||||
def K(self, X, X2=None, which_parts=None):
|
||||
if which_parts is None:
|
||||
which_parts = self.parts
|
||||
|
|
@ -48,7 +48,7 @@ class Prod(CombinationKernel):
|
|||
which_parts = [which_parts]
|
||||
return reduce(np.multiply, (p.K(X, X2) for p in which_parts))
|
||||
|
||||
@Cache_this(limit=2, force_kwargs=['which_parts'])
|
||||
@Cache_this(limit=3, force_kwargs=['which_parts'])
|
||||
def Kdiag(self, X, which_parts=None):
|
||||
if which_parts is None:
|
||||
which_parts = self.parts
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ from .gaussherm import PSICOMP_GH
|
|||
from . import rbf_psi_comp, linear_psi_comp, ssrbf_psi_comp, sslinear_psi_comp
|
||||
|
||||
class PSICOMP_RBF(PSICOMP):
|
||||
@Cache_this(limit=10, ignore_args=(0,))
|
||||
@Cache_this(limit=3, ignore_args=(0,))
|
||||
def psicomputations(self, kern, Z, variational_posterior, return_psi2_n=False):
|
||||
variance, lengthscale = kern.variance, kern.lengthscale
|
||||
if isinstance(variational_posterior, variational.NormalPosterior):
|
||||
|
|
@ -31,7 +31,7 @@ class PSICOMP_RBF(PSICOMP):
|
|||
else:
|
||||
raise ValueError("unknown distriubtion received for psi-statistics")
|
||||
|
||||
@Cache_this(limit=10, ignore_args=(0,2,3,4))
|
||||
@Cache_this(limit=3, ignore_args=(0,2,3,4))
|
||||
def psiDerivativecomputations(self, kern, dL_dpsi0, dL_dpsi1, dL_dpsi2, Z, variational_posterior):
|
||||
variance, lengthscale = kern.variance, kern.lengthscale
|
||||
if isinstance(variational_posterior, variational.NormalPosterior):
|
||||
|
|
@ -43,7 +43,7 @@ class PSICOMP_RBF(PSICOMP):
|
|||
|
||||
class PSICOMP_Linear(PSICOMP):
|
||||
|
||||
@Cache_this(limit=10, ignore_args=(0,))
|
||||
@Cache_this(limit=3, ignore_args=(0,))
|
||||
def psicomputations(self, kern, Z, variational_posterior, return_psi2_n=False):
|
||||
variances = kern.variances
|
||||
if isinstance(variational_posterior, variational.NormalPosterior):
|
||||
|
|
@ -53,7 +53,7 @@ class PSICOMP_Linear(PSICOMP):
|
|||
else:
|
||||
raise ValueError("unknown distriubtion received for psi-statistics")
|
||||
|
||||
@Cache_this(limit=10, ignore_args=(0,2,3,4))
|
||||
@Cache_this(limit=3, ignore_args=(0,2,3,4))
|
||||
def psiDerivativecomputations(self, kern, dL_dpsi0, dL_dpsi1, dL_dpsi2, Z, variational_posterior):
|
||||
variances = kern.variances
|
||||
if isinstance(variational_posterior, variational.NormalPosterior):
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ class PSICOMP_GH(PSICOMP):
|
|||
def _setup_observers(self):
|
||||
pass
|
||||
|
||||
@Cache_this(limit=10, ignore_args=(0,))
|
||||
@Cache_this(limit=3, ignore_args=(0,))
|
||||
def comp_K(self, Z, qX):
|
||||
if self.Xs is None or self.Xs.shape != qX.mean.shape:
|
||||
from paramz import ObsAr
|
||||
|
|
@ -38,7 +38,7 @@ class PSICOMP_GH(PSICOMP):
|
|||
self.Xs[i] = self.locs[i]*S_sq+mu
|
||||
return self.Xs
|
||||
|
||||
@Cache_this(limit=10, ignore_args=(0,))
|
||||
@Cache_this(limit=3, ignore_args=(0,))
|
||||
def psicomputations(self, kern, Z, qX, return_psi2_n=False):
|
||||
mu, S = qX.mean.values, qX.variance.values
|
||||
N,M,Q = mu.shape[0],Z.shape[0],mu.shape[1]
|
||||
|
|
@ -62,7 +62,7 @@ class PSICOMP_GH(PSICOMP):
|
|||
psi2 += self.weights[i]* tdot(Kfu.T)
|
||||
return psi0, psi1, psi2
|
||||
|
||||
@Cache_this(limit=10, ignore_args=(0, 2,3,4))
|
||||
@Cache_this(limit=3, ignore_args=(0, 2,3,4))
|
||||
def psiDerivativecomputations(self, kern, dL_dpsi0, dL_dpsi1, dL_dpsi2, Z, qX):
|
||||
mu, S = qX.mean.values, qX.variance.values
|
||||
if self.cache_K: Xs = self.comp_K(Z, qX)
|
||||
|
|
|
|||
|
|
@ -132,5 +132,5 @@ def _psi2compDer(dL_dpsi2, variance, lengthscale, Z, mu, S):
|
|||
|
||||
return _dL_dvar, _dL_dl, _dL_dZ, _dL_dmu, _dL_dS
|
||||
|
||||
_psi1computations = Cacher(__psi1computations, limit=5)
|
||||
_psi2computations = Cacher(__psi2computations, limit=5)
|
||||
_psi1computations = Cacher(__psi1computations, limit=3)
|
||||
_psi2computations = Cacher(__psi2computations, limit=3)
|
||||
|
|
|
|||
|
|
@ -326,7 +326,7 @@ class PSICOMP_RBF_GPU(PSICOMP_RBF):
|
|||
except:
|
||||
return self.fall_back.psicomputations(kern, Z, variational_posterior, return_psi2_n)
|
||||
|
||||
@Cache_this(limit=10, ignore_args=(0,))
|
||||
@Cache_this(limit=3, ignore_args=(0,))
|
||||
def _psicomputations(self, kern, Z, variational_posterior, return_psi2_n=False):
|
||||
"""
|
||||
Z - MxQ
|
||||
|
|
@ -371,7 +371,7 @@ class PSICOMP_RBF_GPU(PSICOMP_RBF):
|
|||
except:
|
||||
return self.fall_back.psiDerivativecomputations(kern, dL_dpsi0, dL_dpsi1, dL_dpsi2, Z, variational_posterior)
|
||||
|
||||
@Cache_this(limit=10, ignore_args=(0,2,3,4))
|
||||
@Cache_this(limit=3, ignore_args=(0,2,3,4))
|
||||
def _psiDerivativecomputations(self, kern, dL_dpsi0, dL_dpsi1, dL_dpsi2, Z, variational_posterior):
|
||||
# resolve the requirement of dL_dpsi2 to be symmetric
|
||||
if len(dL_dpsi2.shape)==2: dL_dpsi2 = (dL_dpsi2+dL_dpsi2.T)/2
|
||||
|
|
|
|||
|
|
@ -88,7 +88,7 @@ try:
|
|||
return psi0,psi1,psi2,psi2n
|
||||
|
||||
from GPy.util.caching import Cacher
|
||||
psicomputations = Cacher(_psicomputations, limit=1)
|
||||
psicomputations = Cacher(_psicomputations, limit=3)
|
||||
|
||||
def psiDerivativecomputations(dL_dpsi0, dL_dpsi1, dL_dpsi2, variance, lengthscale, Z, variational_posterior):
|
||||
ARD = (len(lengthscale)!=1)
|
||||
|
|
|
|||
|
|
@ -375,7 +375,7 @@ class PSICOMP_SSRBF_GPU(PSICOMP_RBF):
|
|||
def get_dimensions(self, Z, variational_posterior):
|
||||
return variational_posterior.mean.shape[0], Z.shape[0], Z.shape[1]
|
||||
|
||||
@Cache_this(limit=1, ignore_args=(0,))
|
||||
@Cache_this(limit=3, ignore_args=(0,))
|
||||
def psicomputations(self, kern, Z, variational_posterior, return_psi2_n=False):
|
||||
"""
|
||||
Z - MxQ
|
||||
|
|
@ -409,7 +409,7 @@ class PSICOMP_SSRBF_GPU(PSICOMP_RBF):
|
|||
else:
|
||||
return psi0, psi1_gpu.get(), psi2_gpu.get()
|
||||
|
||||
@Cache_this(limit=1, ignore_args=(0,2,3,4))
|
||||
@Cache_this(limit=3, ignore_args=(0,2,3,4))
|
||||
def psiDerivativecomputations(self, kern, dL_dpsi0, dL_dpsi1, dL_dpsi2, Z, variational_posterior):
|
||||
variance, lengthscale = kern.variance, kern.lengthscale
|
||||
from ....util.linalg_gpu import sum_axis
|
||||
|
|
|
|||
|
|
@ -81,11 +81,11 @@ class Stationary(Kern):
|
|||
def dK_dr(self, r):
|
||||
raise NotImplementedError("implement derivative of the covariance function wrt r to use this class")
|
||||
|
||||
@Cache_this(limit=20, ignore_args=())
|
||||
@Cache_this(limit=3, ignore_args=())
|
||||
def dK2_drdr(self, r):
|
||||
raise NotImplementedError("implement second derivative of covariance wrt r to use this method")
|
||||
|
||||
@Cache_this(limit=5, ignore_args=())
|
||||
@Cache_this(limit=3, ignore_args=())
|
||||
def K(self, X, X2=None):
|
||||
"""
|
||||
Kernel function applied on inputs X and X2.
|
||||
|
|
|
|||
|
|
@ -54,12 +54,12 @@ class TruncLinear(Kern):
|
|||
self.add_parameter(self.variances)
|
||||
self.add_parameter(self.delta)
|
||||
|
||||
@Cache_this(limit=2)
|
||||
@Cache_this(limit=3)
|
||||
def K(self, X, X2=None):
|
||||
XX = self.variances*self._product(X, X2)
|
||||
return XX.sum(axis=-1)
|
||||
|
||||
@Cache_this(limit=2)
|
||||
@Cache_this(limit=3)
|
||||
def _product(self, X, X2=None):
|
||||
if X2 is None:
|
||||
X2 = X
|
||||
|
|
@ -149,12 +149,12 @@ class TruncLinear_inf(Kern):
|
|||
self.add_parameter(self.variances)
|
||||
|
||||
|
||||
# @Cache_this(limit=2)
|
||||
# @Cache_this(limit=3)
|
||||
def K(self, X, X2=None):
|
||||
tmp = self._product(X, X2)
|
||||
return (self.variances*tmp).sum(axis=-1)
|
||||
|
||||
# @Cache_this(limit=2)
|
||||
# @Cache_this(limit=3)
|
||||
def _product(self, X, X2=None):
|
||||
if X2 is None:
|
||||
X2 = X
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue