mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-04-27 05:46:24 +02:00
Serialization: Add docstrings
This commit is contained in:
parent
7b2af57aee
commit
11aa6ea27b
24 changed files with 393 additions and 69 deletions
|
|
@ -110,7 +110,14 @@ class GP(Model):
|
||||||
self.posterior = None
|
self.posterior = None
|
||||||
|
|
||||||
def to_dict(self, save_data=True):
|
def to_dict(self, save_data=True):
|
||||||
input_dict = super(GP, self)._to_dict()
|
"""
|
||||||
|
Convert the object into a json serializable dictionary.
|
||||||
|
Note: It uses the private method _save_to_input_dict of the parent.
|
||||||
|
|
||||||
|
:param boolean save_data: if true, it adds the training data self.X and self.Y to the dictionary
|
||||||
|
:return dict: json serializable dictionary containing the needed information to instantiate the object
|
||||||
|
"""
|
||||||
|
input_dict = super(GP, self)._save_to_input_dict()
|
||||||
input_dict["class"] = "GPy.core.GP"
|
input_dict["class"] = "GPy.core.GP"
|
||||||
if not save_data:
|
if not save_data:
|
||||||
input_dict["X"] = None
|
input_dict["X"] = None
|
||||||
|
|
@ -137,7 +144,7 @@ class GP(Model):
|
||||||
return input_dict
|
return input_dict
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _from_dict(input_dict, data=None):
|
def _build_from_input_dict(input_dict, data=None):
|
||||||
import GPy
|
import GPy
|
||||||
import numpy as np
|
import numpy as np
|
||||||
if (input_dict['X'] is None) or (input_dict['Y'] is None):
|
if (input_dict['X'] is None) or (input_dict['Y'] is None):
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ class Mapping(Parameterized):
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def _to_dict(self):
|
def _save_to_input_dict(self):
|
||||||
input_dict = {}
|
input_dict = {}
|
||||||
input_dict["input_dim"] = self.input_dim
|
input_dict["input_dim"] = self.input_dim
|
||||||
input_dict["output_dim"] = self.output_dim
|
input_dict["output_dim"] = self.output_dim
|
||||||
|
|
@ -37,16 +37,27 @@ class Mapping(Parameterized):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_dict(input_dict):
|
def from_dict(input_dict):
|
||||||
|
"""
|
||||||
|
Instantiate an object of a derived class using the information
|
||||||
|
in input_dict (built by the to_dict method of the derived class).
|
||||||
|
More specifically, after reading the derived class from input_dict,
|
||||||
|
it calls the method _build_from_input_dict of the derived class.
|
||||||
|
Note: This method should not be overrided in the derived class. In case
|
||||||
|
it is needed, please override _build_from_input_dict instate.
|
||||||
|
|
||||||
|
:param dict input_dict: Dictionary with all the information needed to
|
||||||
|
instantiate the object.
|
||||||
|
"""
|
||||||
import copy
|
import copy
|
||||||
input_dict = copy.deepcopy(input_dict)
|
input_dict = copy.deepcopy(input_dict)
|
||||||
mapping_class = input_dict.pop('class')
|
mapping_class = input_dict.pop('class')
|
||||||
input_dict["name"] = str(input_dict["name"])
|
input_dict["name"] = str(input_dict["name"])
|
||||||
import GPy
|
import GPy
|
||||||
mapping_class = eval(mapping_class)
|
mapping_class = eval(mapping_class)
|
||||||
return mapping_class._from_dict(mapping_class, input_dict)
|
return mapping_class._build_from_input_dict(mapping_class, input_dict)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _from_dict(mapping_class, input_dict):
|
def _build_from_input_dict(mapping_class, input_dict):
|
||||||
return mapping_class(**input_dict)
|
return mapping_class(**input_dict)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,10 @@ class Model(ParamzModel, Priorizable):
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
super(Model, self).__init__(name) # Parameterized.__init__(self)
|
super(Model, self).__init__(name) # Parameterized.__init__(self)
|
||||||
|
|
||||||
def _to_dict(self):
|
def _save_to_input_dict(self):
|
||||||
|
"""
|
||||||
|
It is used by the public method to_dict to create json serializable dictionary.
|
||||||
|
"""
|
||||||
input_dict = {}
|
input_dict = {}
|
||||||
input_dict["name"] = self.name
|
input_dict["name"] = self.name
|
||||||
return input_dict
|
return input_dict
|
||||||
|
|
@ -18,16 +21,37 @@ class Model(ParamzModel, Priorizable):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_dict(input_dict, data=None):
|
def from_dict(input_dict, data=None):
|
||||||
|
"""
|
||||||
|
Instantiate an object of a derived class using the information
|
||||||
|
in input_dict (built by the to_dict method of the derived class).
|
||||||
|
More specifically, after reading the derived class from input_dict,
|
||||||
|
it calls the method _build_from_input_dict of the derived class.
|
||||||
|
Note: This method should not be overrided in the derived class. In case
|
||||||
|
it is needed, please override _build_from_input_dict instate.
|
||||||
|
|
||||||
|
:param dict input_dict: Dictionary with all the information needed to
|
||||||
|
instantiate the object.
|
||||||
|
"""
|
||||||
import copy
|
import copy
|
||||||
input_dict = copy.deepcopy(input_dict)
|
input_dict = copy.deepcopy(input_dict)
|
||||||
model_class = input_dict.pop('class')
|
model_class = input_dict.pop('class')
|
||||||
input_dict["name"] = str(input_dict["name"])
|
input_dict["name"] = str(input_dict["name"])
|
||||||
import GPy
|
import GPy
|
||||||
model_class = eval(model_class)
|
model_class = eval(model_class)
|
||||||
return model_class._from_dict(input_dict, data)
|
return model_class._build_from_input_dict(input_dict, data)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _from_dict(model_class, input_dict, data=None):
|
def _build_from_input_dict(model_class, input_dict, data=None):
|
||||||
|
"""
|
||||||
|
This method is used by the public method from_dict to build an object
|
||||||
|
of class model_class using the information contained in input_dict.
|
||||||
|
Note: This method is often overrided in the derived class to deal with
|
||||||
|
any pre-processing of the parameters in input_dict before calling the
|
||||||
|
constructor of the object.
|
||||||
|
|
||||||
|
:param str model_class: Class of the object to build.
|
||||||
|
:param dict input_dict: Extra information needed by the constructor of model_class.
|
||||||
|
"""
|
||||||
return model_class(**input_dict)
|
return model_class(**input_dict)
|
||||||
|
|
||||||
def save_model(self, output_filename, compress=True, save_data=True):
|
def save_model(self, output_filename, compress=True, save_data=True):
|
||||||
|
|
|
||||||
|
|
@ -119,16 +119,24 @@ class SparseGP(GP):
|
||||||
self._Zgrad = self.Z.gradient.copy()
|
self._Zgrad = self.Z.gradient.copy()
|
||||||
|
|
||||||
def to_dict(self, save_data=True):
|
def to_dict(self, save_data=True):
|
||||||
|
"""
|
||||||
|
Convert the object into a json serializable dictionary.
|
||||||
|
|
||||||
|
:param boolean save_data: if true, it adds the training data self.X and self.Y to the dictionary
|
||||||
|
:return dict: json serializable dictionary containing the needed information to instantiate the object
|
||||||
|
"""
|
||||||
input_dict = super(SparseGP, self).to_dict(save_data)
|
input_dict = super(SparseGP, self).to_dict(save_data)
|
||||||
input_dict["class"] = "GPy.core.SparseGP"
|
input_dict["class"] = "GPy.core.SparseGP"
|
||||||
input_dict["Z"] = self.Z.tolist()
|
input_dict["Z"] = self.Z.tolist()
|
||||||
return input_dict
|
return input_dict
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _from_dict(input_dict, data=None):
|
def _build_from_input_dict(input_dict, data=None):
|
||||||
|
# Called from the from_dict method.
|
||||||
import GPy
|
import GPy
|
||||||
if (input_dict['X'] is None) or (input_dict['Y'] is None):
|
if (input_dict['X'] is None) or (input_dict['Y'] is None):
|
||||||
assert(data is not None)
|
if data is None:
|
||||||
|
raise ValueError("The model was serialized whithout the training data. 'data' must be not None!")
|
||||||
input_dict["X"], input_dict["Y"] = np.array(data[0]), np.array(data[1])
|
input_dict["X"], input_dict["Y"] = np.array(data[0]), np.array(data[1])
|
||||||
elif data is not None:
|
elif data is not None:
|
||||||
print("WARNING: The model has been saved with X,Y! The original values are being overriden!")
|
print("WARNING: The model has been saved with X,Y! The original values are being overriden!")
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,7 @@ class LatentFunctionInference(object):
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def _to_dict(self):
|
def _save_to_input_dict(self):
|
||||||
input_dict = {}
|
input_dict = {}
|
||||||
return input_dict
|
return input_dict
|
||||||
|
|
||||||
|
|
@ -50,15 +50,27 @@ class LatentFunctionInference(object):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_dict(input_dict):
|
def from_dict(input_dict):
|
||||||
|
"""
|
||||||
|
Instantiate an object of a derived class using the information
|
||||||
|
in input_dict (built by the to_dict method of the derived class).
|
||||||
|
More specifically, after reading the derived class from input_dict,
|
||||||
|
it calls the method _build_from_input_dict of the derived class.
|
||||||
|
Note: This method should not be overrided in the derived class. In case
|
||||||
|
it is needed, please override _build_from_input_dict instate.
|
||||||
|
|
||||||
|
:param dict input_dict: Dictionary with all the information needed to
|
||||||
|
instantiate the object.
|
||||||
|
"""
|
||||||
|
|
||||||
import copy
|
import copy
|
||||||
input_dict = copy.deepcopy(input_dict)
|
input_dict = copy.deepcopy(input_dict)
|
||||||
inference_class = input_dict.pop('class')
|
inference_class = input_dict.pop('class')
|
||||||
import GPy
|
import GPy
|
||||||
inference_class = eval(inference_class)
|
inference_class = eval(inference_class)
|
||||||
return inference_class._from_dict(inference_class, input_dict)
|
return inference_class._build_from_input_dict(inference_class, input_dict)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _from_dict(inference_class, input_dict):
|
def _build_from_input_dict(inference_class, input_dict):
|
||||||
return inference_class(**input_dict)
|
return inference_class(**input_dict)
|
||||||
|
|
||||||
class InferenceMethodList(LatentFunctionInference, list):
|
class InferenceMethodList(LatentFunctionInference, list):
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,15 @@ class ExactGaussianInference(LatentFunctionInference):
|
||||||
pass#self._YYTfactor_cache = caching.cache()
|
pass#self._YYTfactor_cache = caching.cache()
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
input_dict = super(ExactGaussianInference, self)._to_dict()
|
"""
|
||||||
|
Convert the object into a json serializable dictionary.
|
||||||
|
|
||||||
|
Note: It uses the private method _save_to_input_dict of the parent.
|
||||||
|
|
||||||
|
:return dict: json serializable dictionary containing the needed information to instantiate the object
|
||||||
|
"""
|
||||||
|
|
||||||
|
input_dict = super(ExactGaussianInference, self)._save_to_input_dict()
|
||||||
input_dict["class"] = "GPy.inference.latent_function_inference.exact_gaussian_inference.ExactGaussianInference"
|
input_dict["class"] = "GPy.inference.latent_function_inference.exact_gaussian_inference.ExactGaussianInference"
|
||||||
return input_dict
|
return input_dict
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,14 @@ class cavityParams(object):
|
||||||
self.tau[i] = 1./post_params.Sigma_diag[i] - eta*ga_approx.tau[i]
|
self.tau[i] = 1./post_params.Sigma_diag[i] - eta*ga_approx.tau[i]
|
||||||
self.v[i] = post_params.mu[i]/post_params.Sigma_diag[i] - eta*ga_approx.v[i]
|
self.v[i] = post_params.mu[i]/post_params.Sigma_diag[i] - eta*ga_approx.v[i]
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
|
"""
|
||||||
|
Convert the object into a json serializable dictionary.
|
||||||
|
|
||||||
|
Note: It uses the private method _save_to_input_dict of the parent.
|
||||||
|
|
||||||
|
:return dict: json serializable dictionary containing the needed information to instantiate the object
|
||||||
|
"""
|
||||||
|
|
||||||
return {"tau": self.tau.tolist(), "v": self.v.tolist()}
|
return {"tau": self.tau.tolist(), "v": self.v.tolist()}
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_dict(input_dict):
|
def from_dict(input_dict):
|
||||||
|
|
@ -59,6 +67,14 @@ class gaussianApproximation(object):
|
||||||
|
|
||||||
return (delta_tau, delta_v)
|
return (delta_tau, delta_v)
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
|
"""
|
||||||
|
Convert the object into a json serializable dictionary.
|
||||||
|
|
||||||
|
Note: It uses the private method _save_to_input_dict of the parent.
|
||||||
|
|
||||||
|
:return dict: json serializable dictionary containing the needed information to instantiate the object
|
||||||
|
"""
|
||||||
|
|
||||||
return {"tau": self.tau.tolist(), "v": self.v.tolist()}
|
return {"tau": self.tau.tolist(), "v": self.v.tolist()}
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_dict(input_dict):
|
def from_dict(input_dict):
|
||||||
|
|
@ -89,6 +105,14 @@ class posteriorParams(posteriorParamsBase):
|
||||||
DSYR(self.Sigma, si, -ci)
|
DSYR(self.Sigma, si, -ci)
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
|
"""
|
||||||
|
Convert the object into a json serializable dictionary.
|
||||||
|
|
||||||
|
Note: It uses the private method _save_to_input_dict of the parent.
|
||||||
|
|
||||||
|
:return dict: json serializable dictionary containing the needed information to instantiate the object
|
||||||
|
"""
|
||||||
|
|
||||||
#TODO: Implement a more memory efficient variant
|
#TODO: Implement a more memory efficient variant
|
||||||
if self.L is None:
|
if self.L is None:
|
||||||
return { "mu": self.mu.tolist(), "Sigma": self.Sigma.tolist()}
|
return { "mu": self.mu.tolist(), "Sigma": self.Sigma.tolist()}
|
||||||
|
|
@ -133,6 +157,14 @@ class posteriorParamsDTC(posteriorParamsBase):
|
||||||
#mu = np.dot(Sigma, v_tilde)
|
#mu = np.dot(Sigma, v_tilde)
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
|
"""
|
||||||
|
Convert the object into a json serializable dictionary.
|
||||||
|
|
||||||
|
Note: It uses the private method _save_to_input_dict of the parent.
|
||||||
|
|
||||||
|
:return dict: json serializable dictionary containing the needed information to instantiate the object
|
||||||
|
"""
|
||||||
|
|
||||||
return { "mu": self.mu.tolist(), "Sigma_diag": self.Sigma_diag.tolist()}
|
return { "mu": self.mu.tolist(), "Sigma_diag": self.Sigma_diag.tolist()}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
@ -205,8 +237,8 @@ class EPBase(object):
|
||||||
def __getstate__(self):
|
def __getstate__(self):
|
||||||
return [super(EPBase, self).__getstate__() , [self.epsilon, self.eta, self.delta]]
|
return [super(EPBase, self).__getstate__() , [self.epsilon, self.eta, self.delta]]
|
||||||
|
|
||||||
def _to_dict(self):
|
def _save_to_input_dict(self):
|
||||||
input_dict = super(EPBase, self)._to_dict()
|
input_dict = super(EPBase, self)._save_to_input_dict()
|
||||||
input_dict["epsilon"]=self.epsilon
|
input_dict["epsilon"]=self.epsilon
|
||||||
input_dict["eta"]=self.eta
|
input_dict["eta"]=self.eta
|
||||||
input_dict["delta"]=self.delta
|
input_dict["delta"]=self.delta
|
||||||
|
|
@ -370,7 +402,15 @@ class EP(EPBase, ExactGaussianInference):
|
||||||
return Posterior(woodbury_inv=Wi, woodbury_vector=alpha, K=K), log_marginal, {'dL_dK':dL_dK, 'dL_dthetaL':dL_dthetaL, 'dL_dm':alpha}
|
return Posterior(woodbury_inv=Wi, woodbury_vector=alpha, K=K), log_marginal, {'dL_dK':dL_dK, 'dL_dthetaL':dL_dthetaL, 'dL_dm':alpha}
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
input_dict = super(EP, self)._to_dict()
|
"""
|
||||||
|
Convert the object into a json serializable dictionary.
|
||||||
|
|
||||||
|
Note: It uses the private method _save_to_input_dict of the parent.
|
||||||
|
|
||||||
|
:return dict: json serializable dictionary containing the needed information to instantiate the object
|
||||||
|
"""
|
||||||
|
|
||||||
|
input_dict = super(EP, self)._save_to_input_dict()
|
||||||
input_dict["class"] = "GPy.inference.latent_function_inference.expectation_propagation.EP"
|
input_dict["class"] = "GPy.inference.latent_function_inference.expectation_propagation.EP"
|
||||||
if self.ga_approx_old is not None:
|
if self.ga_approx_old is not None:
|
||||||
input_dict["ga_approx_old"] = self.ga_approx_old.to_dict()
|
input_dict["ga_approx_old"] = self.ga_approx_old.to_dict()
|
||||||
|
|
@ -384,7 +424,7 @@ class EP(EPBase, ExactGaussianInference):
|
||||||
return input_dict
|
return input_dict
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _from_dict(inference_class, input_dict):
|
def _build_from_input_dict(inference_class, input_dict):
|
||||||
ga_approx_old = input_dict.pop('ga_approx_old', None)
|
ga_approx_old = input_dict.pop('ga_approx_old', None)
|
||||||
if ga_approx_old is not None:
|
if ga_approx_old is not None:
|
||||||
ga_approx_old = gaussianApproximation.from_dict(ga_approx_old)
|
ga_approx_old = gaussianApproximation.from_dict(ga_approx_old)
|
||||||
|
|
@ -545,7 +585,15 @@ class EPDTC(EPBase, VarDTC):
|
||||||
|
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
input_dict = super(EPDTC, self)._to_dict()
|
"""
|
||||||
|
Convert the object into a json serializable dictionary.
|
||||||
|
|
||||||
|
Note: It uses the private method _save_to_input_dict of the parent.
|
||||||
|
|
||||||
|
:return dict: json serializable dictionary containing the needed information to instantiate the object
|
||||||
|
"""
|
||||||
|
|
||||||
|
input_dict = super(EPDTC, self)._save_to_input_dict()
|
||||||
input_dict["class"] = "GPy.inference.latent_function_inference.expectation_propagation.EPDTC"
|
input_dict["class"] = "GPy.inference.latent_function_inference.expectation_propagation.EPDTC"
|
||||||
if self.ga_approx_old is not None:
|
if self.ga_approx_old is not None:
|
||||||
input_dict["ga_approx_old"] = self.ga_approx_old.to_dict()
|
input_dict["ga_approx_old"] = self.ga_approx_old.to_dict()
|
||||||
|
|
@ -558,7 +606,7 @@ class EPDTC(EPBase, VarDTC):
|
||||||
return input_dict
|
return input_dict
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _from_dict(inference_class, input_dict):
|
def _build_from_input_dict(inference_class, input_dict):
|
||||||
ga_approx_old = input_dict.pop('ga_approx_old', None)
|
ga_approx_old = input_dict.pop('ga_approx_old', None)
|
||||||
if ga_approx_old is not None:
|
if ga_approx_old is not None:
|
||||||
ga_approx_old = gaussianApproximation.from_dict(ga_approx_old)
|
ga_approx_old = gaussianApproximation.from_dict(ga_approx_old)
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,15 @@ class Add(CombinationKernel):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
input_dict = super(Add, self)._to_dict()
|
"""
|
||||||
|
Convert the object into a json serializable dictionary.
|
||||||
|
|
||||||
|
Note: It uses the private method _save_to_input_dict of the parent.
|
||||||
|
|
||||||
|
:return dict: json serializable dictionary containing the needed information to instantiate the object
|
||||||
|
"""
|
||||||
|
|
||||||
|
input_dict = super(Add, self)._save_to_input_dict()
|
||||||
input_dict["class"] = str("GPy.kern.Add")
|
input_dict["class"] = str("GPy.kern.Add")
|
||||||
return input_dict
|
return input_dict
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -60,7 +60,7 @@ class Kern(Parameterized):
|
||||||
from .psi_comp import PSICOMP_GH
|
from .psi_comp import PSICOMP_GH
|
||||||
self.psicomp = PSICOMP_GH()
|
self.psicomp = PSICOMP_GH()
|
||||||
|
|
||||||
def _to_dict(self):
|
def _save_to_input_dict(self):
|
||||||
input_dict = {}
|
input_dict = {}
|
||||||
input_dict["input_dim"] = self.input_dim
|
input_dict["input_dim"] = self.input_dim
|
||||||
if isinstance(self.active_dims, np.ndarray):
|
if isinstance(self.active_dims, np.ndarray):
|
||||||
|
|
@ -76,16 +76,28 @@ class Kern(Parameterized):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_dict(input_dict):
|
def from_dict(input_dict):
|
||||||
|
"""
|
||||||
|
Instantiate an object of a derived class using the information
|
||||||
|
in input_dict (built by the to_dict method of the derived class).
|
||||||
|
More specifically, after reading the derived class from input_dict,
|
||||||
|
it calls the method _build_from_input_dict of the derived class.
|
||||||
|
Note: This method should not be overrided in the derived class. In case
|
||||||
|
it is needed, please override _build_from_input_dict instate.
|
||||||
|
|
||||||
|
:param dict input_dict: Dictionary with all the information needed to
|
||||||
|
instantiate the object.
|
||||||
|
"""
|
||||||
|
|
||||||
import copy
|
import copy
|
||||||
input_dict = copy.deepcopy(input_dict)
|
input_dict = copy.deepcopy(input_dict)
|
||||||
kernel_class = input_dict.pop('class')
|
kernel_class = input_dict.pop('class')
|
||||||
input_dict["name"] = str(input_dict["name"])
|
input_dict["name"] = str(input_dict["name"])
|
||||||
import GPy
|
import GPy
|
||||||
kernel_class = eval(kernel_class)
|
kernel_class = eval(kernel_class)
|
||||||
return kernel_class._from_dict(kernel_class, input_dict)
|
return kernel_class._build_from_input_dict(kernel_class, input_dict)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _from_dict(kernel_class, input_dict):
|
def _build_from_input_dict(kernel_class, input_dict):
|
||||||
return kernel_class(**input_dict)
|
return kernel_class(**input_dict)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -375,15 +387,15 @@ class CombinationKernel(Kern):
|
||||||
if link_parameters:
|
if link_parameters:
|
||||||
self.link_parameters(*kernels)
|
self.link_parameters(*kernels)
|
||||||
|
|
||||||
def _to_dict(self):
|
def _save_to_input_dict(self):
|
||||||
input_dict = super(CombinationKernel, self)._to_dict()
|
input_dict = super(CombinationKernel, self)._save_to_input_dict()
|
||||||
input_dict["parts"] = {}
|
input_dict["parts"] = {}
|
||||||
for ii in range(len(self.parts)):
|
for ii in range(len(self.parts)):
|
||||||
input_dict["parts"][ii] = self.parts[ii].to_dict()
|
input_dict["parts"][ii] = self.parts[ii].to_dict()
|
||||||
return input_dict
|
return input_dict
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _from_dict(kernel_class, input_dict):
|
def _build_from_input_dict(kernel_class, input_dict):
|
||||||
parts = input_dict.pop('parts', None)
|
parts = input_dict.pop('parts', None)
|
||||||
subkerns = []
|
subkerns = []
|
||||||
for pp in parts:
|
for pp in parts:
|
||||||
|
|
|
||||||
|
|
@ -52,14 +52,14 @@ class Linear(Kern):
|
||||||
self.psicomp = PSICOMP_Linear()
|
self.psicomp = PSICOMP_Linear()
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
input_dict = super(Linear, self)._to_dict()
|
input_dict = super(Linear, self)._save_to_input_dict()
|
||||||
input_dict["class"] = "GPy.kern.Linear"
|
input_dict["class"] = "GPy.kern.Linear"
|
||||||
input_dict["variances"] = self.variances.values.tolist()
|
input_dict["variances"] = self.variances.values.tolist()
|
||||||
input_dict["ARD"] = self.ARD
|
input_dict["ARD"] = self.ARD
|
||||||
return input_dict
|
return input_dict
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _from_dict(kernel_class, input_dict):
|
def _build_from_input_dict(kernel_class, input_dict):
|
||||||
useGPU = input_dict.pop('useGPU', None)
|
useGPU = input_dict.pop('useGPU', None)
|
||||||
return Linear(**input_dict)
|
return Linear(**input_dict)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,15 @@ class Prod(CombinationKernel):
|
||||||
super(Prod, self).__init__(_newkerns, name)
|
super(Prod, self).__init__(_newkerns, name)
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
input_dict = super(Prod, self)._to_dict()
|
"""
|
||||||
|
Convert the object into a json serializable dictionary.
|
||||||
|
|
||||||
|
Note: It uses the private method _save_to_input_dict of the parent.
|
||||||
|
|
||||||
|
:return dict: json serializable dictionary containing the needed information to instantiate the object
|
||||||
|
"""
|
||||||
|
|
||||||
|
input_dict = super(Prod, self)._save_to_input_dict()
|
||||||
input_dict["class"] = str("GPy.kern.Prod")
|
input_dict["class"] = str("GPy.kern.Prod")
|
||||||
return input_dict
|
return input_dict
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,15 @@ class RBF(Stationary):
|
||||||
self.link_parameter(self.inv_l)
|
self.link_parameter(self.inv_l)
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
input_dict = super(RBF, self)._to_dict()
|
"""
|
||||||
|
Convert the object into a json serializable dictionary.
|
||||||
|
|
||||||
|
Note: It uses the private method _save_to_input_dict of the parent.
|
||||||
|
|
||||||
|
:return dict: json serializable dictionary containing the needed information to instantiate the object
|
||||||
|
"""
|
||||||
|
|
||||||
|
input_dict = super(RBF, self)._save_to_input_dict()
|
||||||
input_dict["class"] = "GPy.kern.RBF"
|
input_dict["class"] = "GPy.kern.RBF"
|
||||||
input_dict["inv_l"] = self.use_invLengthscale
|
input_dict["inv_l"] = self.use_invLengthscale
|
||||||
if input_dict["inv_l"] == True:
|
if input_dict["inv_l"] == True:
|
||||||
|
|
|
||||||
|
|
@ -94,7 +94,15 @@ class StdPeriodic(Kern):
|
||||||
self.link_parameters(self.variance, self.period, self.lengthscale)
|
self.link_parameters(self.variance, self.period, self.lengthscale)
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
input_dict = super(StdPeriodic, self)._to_dict()
|
"""
|
||||||
|
Convert the object into a json serializable dictionary.
|
||||||
|
|
||||||
|
Note: It uses the private method _save_to_input_dict of the parent.
|
||||||
|
|
||||||
|
:return dict: json serializable dictionary containing the needed information to instantiate the object
|
||||||
|
"""
|
||||||
|
|
||||||
|
input_dict = super(StdPeriodic, self)._save_to_input_dict()
|
||||||
input_dict["class"] = "GPy.kern.StdPeriodic"
|
input_dict["class"] = "GPy.kern.StdPeriodic"
|
||||||
input_dict["variance"] = self.variance.values.tolist()
|
input_dict["variance"] = self.variance.values.tolist()
|
||||||
input_dict["period"] = self.period.values.tolist()
|
input_dict["period"] = self.period.values.tolist()
|
||||||
|
|
|
||||||
|
|
@ -14,8 +14,8 @@ class Static(Kern):
|
||||||
self.variance = Param('variance', variance, Logexp())
|
self.variance = Param('variance', variance, Logexp())
|
||||||
self.link_parameters(self.variance)
|
self.link_parameters(self.variance)
|
||||||
|
|
||||||
def _to_dict(self):
|
def _save_to_input_dict(self):
|
||||||
input_dict = super(Static, self)._to_dict()
|
input_dict = super(Static, self)._save_to_input_dict()
|
||||||
input_dict["variance"] = self.variance.values.tolist()
|
input_dict["variance"] = self.variance.values.tolist()
|
||||||
return input_dict
|
return input_dict
|
||||||
|
|
||||||
|
|
@ -139,12 +139,12 @@ class Bias(Static):
|
||||||
super(Bias, self).__init__(input_dim, variance, active_dims, name)
|
super(Bias, self).__init__(input_dim, variance, active_dims, name)
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
input_dict = super(Bias, self)._to_dict()
|
input_dict = super(Bias, self)._save_to_input_dict()
|
||||||
input_dict["class"] = "GPy.kern.Bias"
|
input_dict["class"] = "GPy.kern.Bias"
|
||||||
return input_dict
|
return input_dict
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _from_dict(kernel_class, input_dict):
|
def _build_from_input_dict(kernel_class, input_dict):
|
||||||
useGPU = input_dict.pop('useGPU', None)
|
useGPU = input_dict.pop('useGPU', None)
|
||||||
return Bias(**input_dict)
|
return Bias(**input_dict)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -79,8 +79,8 @@ class Stationary(Kern):
|
||||||
assert self.variance.size==1
|
assert self.variance.size==1
|
||||||
self.link_parameters(self.variance, self.lengthscale)
|
self.link_parameters(self.variance, self.lengthscale)
|
||||||
|
|
||||||
def _to_dict(self):
|
def _save_to_input_dict(self):
|
||||||
input_dict = super(Stationary, self)._to_dict()
|
input_dict = super(Stationary, self)._save_to_input_dict()
|
||||||
input_dict["variance"] = self.variance.values.tolist()
|
input_dict["variance"] = self.variance.values.tolist()
|
||||||
input_dict["lengthscale"] = self.lengthscale.values.tolist()
|
input_dict["lengthscale"] = self.lengthscale.values.tolist()
|
||||||
input_dict["ARD"] = self.ARD
|
input_dict["ARD"] = self.ARD
|
||||||
|
|
@ -366,12 +366,20 @@ class Exponential(Stationary):
|
||||||
return -self.K_of_r(r)
|
return -self.K_of_r(r)
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
input_dict = super(Exponential, self)._to_dict()
|
"""
|
||||||
|
Convert the object into a json serializable dictionary.
|
||||||
|
|
||||||
|
Note: It uses the private method _save_to_input_dict of the parent.
|
||||||
|
|
||||||
|
:return dict: json serializable dictionary containing the needed information to instantiate the object
|
||||||
|
"""
|
||||||
|
|
||||||
|
input_dict = super(Exponential, self)._save_to_input_dict()
|
||||||
input_dict["class"] = "GPy.kern.Exponential"
|
input_dict["class"] = "GPy.kern.Exponential"
|
||||||
return input_dict
|
return input_dict
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _from_dict(kernel_class, input_dict):
|
def _build_from_input_dict(kernel_class, input_dict):
|
||||||
useGPU = input_dict.pop('useGPU', None)
|
useGPU = input_dict.pop('useGPU', None)
|
||||||
return Exponential(**input_dict)
|
return Exponential(**input_dict)
|
||||||
|
|
||||||
|
|
@ -424,12 +432,20 @@ class Matern32(Stationary):
|
||||||
super(Matern32, self).__init__(input_dim, variance, lengthscale, ARD, active_dims, name)
|
super(Matern32, self).__init__(input_dim, variance, lengthscale, ARD, active_dims, name)
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
input_dict = super(Matern32, self)._to_dict()
|
"""
|
||||||
|
Convert the object into a json serializable dictionary.
|
||||||
|
|
||||||
|
Note: It uses the private method _save_to_input_dict of the parent.
|
||||||
|
|
||||||
|
:return dict: json serializable dictionary containing the needed information to instantiate the object
|
||||||
|
"""
|
||||||
|
|
||||||
|
input_dict = super(Matern32, self)._save_to_input_dict()
|
||||||
input_dict["class"] = "GPy.kern.Matern32"
|
input_dict["class"] = "GPy.kern.Matern32"
|
||||||
return input_dict
|
return input_dict
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _from_dict(kernel_class, input_dict):
|
def _build_from_input_dict(kernel_class, input_dict):
|
||||||
useGPU = input_dict.pop('useGPU', None)
|
useGPU = input_dict.pop('useGPU', None)
|
||||||
return Matern32(**input_dict)
|
return Matern32(**input_dict)
|
||||||
|
|
||||||
|
|
@ -513,12 +529,20 @@ class Matern52(Stationary):
|
||||||
super(Matern52, self).__init__(input_dim, variance, lengthscale, ARD, active_dims, name)
|
super(Matern52, self).__init__(input_dim, variance, lengthscale, ARD, active_dims, name)
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
input_dict = super(Matern52, self)._to_dict()
|
"""
|
||||||
|
Convert the object into a json serializable dictionary.
|
||||||
|
|
||||||
|
Note: It uses the private method _save_to_input_dict of the parent.
|
||||||
|
|
||||||
|
:return dict: json serializable dictionary containing the needed information to instantiate the object
|
||||||
|
"""
|
||||||
|
|
||||||
|
input_dict = super(Matern52, self)._save_to_input_dict()
|
||||||
input_dict["class"] = "GPy.kern.Matern52"
|
input_dict["class"] = "GPy.kern.Matern52"
|
||||||
return input_dict
|
return input_dict
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _from_dict(kernel_class, input_dict):
|
def _build_from_input_dict(kernel_class, input_dict):
|
||||||
useGPU = input_dict.pop('useGPU', None)
|
useGPU = input_dict.pop('useGPU', None)
|
||||||
return Matern52(**input_dict)
|
return Matern52(**input_dict)
|
||||||
|
|
||||||
|
|
@ -578,12 +602,20 @@ class ExpQuad(Stationary):
|
||||||
super(ExpQuad, self).__init__(input_dim, variance, lengthscale, ARD, active_dims, name)
|
super(ExpQuad, self).__init__(input_dim, variance, lengthscale, ARD, active_dims, name)
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
input_dict = super(ExpQuad, self)._to_dict()
|
"""
|
||||||
|
Convert the object into a json serializable dictionary.
|
||||||
|
|
||||||
|
Note: It uses the private method _save_to_input_dict of the parent.
|
||||||
|
|
||||||
|
:return dict: json serializable dictionary containing the needed information to instantiate the object
|
||||||
|
"""
|
||||||
|
|
||||||
|
input_dict = super(ExpQuad, self)._save_to_input_dict()
|
||||||
input_dict["class"] = "GPy.kern.ExpQuad"
|
input_dict["class"] = "GPy.kern.ExpQuad"
|
||||||
return input_dict
|
return input_dict
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _from_dict(kernel_class, input_dict):
|
def _build_from_input_dict(kernel_class, input_dict):
|
||||||
useGPU = input_dict.pop('useGPU', None)
|
useGPU = input_dict.pop('useGPU', None)
|
||||||
return ExpQuad(**input_dict)
|
return ExpQuad(**input_dict)
|
||||||
|
|
||||||
|
|
@ -621,13 +653,21 @@ class RatQuad(Stationary):
|
||||||
self.link_parameters(self.power)
|
self.link_parameters(self.power)
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
input_dict = super(RatQuad, self)._to_dict()
|
"""
|
||||||
|
Convert the object into a json serializable dictionary.
|
||||||
|
|
||||||
|
Note: It uses the private method _save_to_input_dict of the parent.
|
||||||
|
|
||||||
|
:return dict: json serializable dictionary containing the needed information to instantiate the object
|
||||||
|
"""
|
||||||
|
|
||||||
|
input_dict = super(RatQuad, self)._save_to_input_dict()
|
||||||
input_dict["class"] = "GPy.kern.RatQuad"
|
input_dict["class"] = "GPy.kern.RatQuad"
|
||||||
input_dict["power"] = self.power.values.tolist()
|
input_dict["power"] = self.power.values.tolist()
|
||||||
return input_dict
|
return input_dict
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _from_dict(kernel_class, input_dict):
|
def _build_from_input_dict(kernel_class, input_dict):
|
||||||
useGPU = input_dict.pop('useGPU', None)
|
useGPU = input_dict.pop('useGPU', None)
|
||||||
return RatQuad(**input_dict)
|
return RatQuad(**input_dict)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,15 @@ class Bernoulli(Likelihood):
|
||||||
self.log_concave = True
|
self.log_concave = True
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
input_dict = super(Bernoulli, self)._to_dict()
|
"""
|
||||||
|
Convert the object into a json serializable dictionary.
|
||||||
|
|
||||||
|
Note: It uses the private method _save_to_input_dict of the parent.
|
||||||
|
|
||||||
|
:return dict: json serializable dictionary containing the needed information to instantiate the object
|
||||||
|
"""
|
||||||
|
|
||||||
|
input_dict = super(Bernoulli, self)._save_to_input_dict()
|
||||||
input_dict["class"] = "GPy.likelihoods.Bernoulli"
|
input_dict["class"] = "GPy.likelihoods.Bernoulli"
|
||||||
return input_dict
|
return input_dict
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,15 @@ class Gaussian(Likelihood):
|
||||||
self.log_concave = True
|
self.log_concave = True
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
input_dict = super(Gaussian, self)._to_dict()
|
"""
|
||||||
|
Convert the object into a json serializable dictionary.
|
||||||
|
|
||||||
|
Note: It uses the private method _save_to_input_dict of the parent.
|
||||||
|
|
||||||
|
:return dict: json serializable dictionary containing the needed information to instantiate the object
|
||||||
|
"""
|
||||||
|
|
||||||
|
input_dict = super(Gaussian, self)._save_to_input_dict()
|
||||||
input_dict["class"] = "GPy.likelihoods.Gaussian"
|
input_dict["class"] = "GPy.likelihoods.Gaussian"
|
||||||
input_dict["variance"] = self.variance.values.tolist()
|
input_dict["variance"] = self.variance.values.tolist()
|
||||||
return input_dict
|
return input_dict
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ class Likelihood(Parameterized):
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def _to_dict(self):
|
def _save_to_input_dict(self):
|
||||||
input_dict = {}
|
input_dict = {}
|
||||||
input_dict["name"] = self.name
|
input_dict["name"] = self.name
|
||||||
input_dict["gp_link_dict"] = self.gp_link.to_dict()
|
input_dict["gp_link_dict"] = self.gp_link.to_dict()
|
||||||
|
|
@ -57,6 +57,18 @@ class Likelihood(Parameterized):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_dict(input_dict):
|
def from_dict(input_dict):
|
||||||
|
"""
|
||||||
|
Instantiate an object of a derived class using the information
|
||||||
|
in input_dict (built by the to_dict method of the derived class).
|
||||||
|
More specifically, after reading the derived class from input_dict,
|
||||||
|
it calls the method _build_from_input_dict of the derived class.
|
||||||
|
Note: This method should not be overrided in the derived class. In case
|
||||||
|
it is needed, please override _build_from_input_dict instate.
|
||||||
|
|
||||||
|
:param dict input_dict: Dictionary with all the information needed to
|
||||||
|
instantiate the object.
|
||||||
|
"""
|
||||||
|
|
||||||
import copy
|
import copy
|
||||||
input_dict = copy.deepcopy(input_dict)
|
input_dict = copy.deepcopy(input_dict)
|
||||||
likelihood_class = input_dict.pop('class')
|
likelihood_class = input_dict.pop('class')
|
||||||
|
|
@ -64,10 +76,10 @@ class Likelihood(Parameterized):
|
||||||
name = input_dict.pop('name')
|
name = input_dict.pop('name')
|
||||||
import GPy
|
import GPy
|
||||||
likelihood_class = eval(likelihood_class)
|
likelihood_class = eval(likelihood_class)
|
||||||
return likelihood_class._from_dict(likelihood_class, input_dict)
|
return likelihood_class._build_from_input_dict(likelihood_class, input_dict)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _from_dict(likelihood_class, input_dict):
|
def _build_from_input_dict(likelihood_class, input_dict):
|
||||||
import copy
|
import copy
|
||||||
input_dict = copy.deepcopy(input_dict)
|
input_dict = copy.deepcopy(input_dict)
|
||||||
gp_link_dict = input_dict.pop('gp_link_dict')
|
gp_link_dict = input_dict.pop('gp_link_dict')
|
||||||
|
|
|
||||||
|
|
@ -46,20 +46,32 @@ class GPTransformation(object):
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def _to_dict(self):
|
def _save_to_input_dict(self):
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_dict(input_dict):
|
def from_dict(input_dict):
|
||||||
|
"""
|
||||||
|
Instantiate an object of a derived class using the information
|
||||||
|
in input_dict (built by the to_dict method of the derived class).
|
||||||
|
More specifically, after reading the derived class from input_dict,
|
||||||
|
it calls the method _build_from_input_dict of the derived class.
|
||||||
|
Note: This method should not be overrided in the derived class. In case
|
||||||
|
it is needed, please override _build_from_input_dict instate.
|
||||||
|
|
||||||
|
:param dict input_dict: Dictionary with all the information needed to
|
||||||
|
instantiate the object.
|
||||||
|
"""
|
||||||
|
|
||||||
import copy
|
import copy
|
||||||
input_dict = copy.deepcopy(input_dict)
|
input_dict = copy.deepcopy(input_dict)
|
||||||
link_class = input_dict.pop('class')
|
link_class = input_dict.pop('class')
|
||||||
import GPy
|
import GPy
|
||||||
link_class = eval(link_class)
|
link_class = eval(link_class)
|
||||||
return link_class._from_dict(link_class, input_dict)
|
return link_class._build_from_input_dict(link_class, input_dict)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _from_dict(link_class, input_dict):
|
def _build_from_input_dict(link_class, input_dict):
|
||||||
return link_class(**input_dict)
|
return link_class(**input_dict)
|
||||||
|
|
||||||
class Identity(GPTransformation):
|
class Identity(GPTransformation):
|
||||||
|
|
@ -82,7 +94,15 @@ class Identity(GPTransformation):
|
||||||
return np.zeros_like(f)
|
return np.zeros_like(f)
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
input_dict = super(Identity, self)._to_dict()
|
"""
|
||||||
|
Convert the object into a json serializable dictionary.
|
||||||
|
|
||||||
|
Note: It uses the private method _save_to_input_dict of the parent.
|
||||||
|
|
||||||
|
:return dict: json serializable dictionary containing the needed information to instantiate the object
|
||||||
|
"""
|
||||||
|
|
||||||
|
input_dict = super(Identity, self)._save_to_input_dict()
|
||||||
input_dict["class"] = "GPy.likelihoods.link_functions.Identity"
|
input_dict["class"] = "GPy.likelihoods.link_functions.Identity"
|
||||||
return input_dict
|
return input_dict
|
||||||
|
|
||||||
|
|
@ -106,7 +126,15 @@ class Probit(GPTransformation):
|
||||||
return (safe_square(f)-1.)*std_norm_pdf(f)
|
return (safe_square(f)-1.)*std_norm_pdf(f)
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
input_dict = super(Probit, self)._to_dict()
|
"""
|
||||||
|
Convert the object into a json serializable dictionary.
|
||||||
|
|
||||||
|
Note: It uses the private method _save_to_input_dict of the parent.
|
||||||
|
|
||||||
|
:return dict: json serializable dictionary containing the needed information to instantiate the object
|
||||||
|
"""
|
||||||
|
|
||||||
|
input_dict = super(Probit, self)._save_to_input_dict()
|
||||||
input_dict["class"] = "GPy.likelihoods.link_functions.Probit"
|
input_dict["class"] = "GPy.likelihoods.link_functions.Probit"
|
||||||
return input_dict
|
return input_dict
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,14 @@ class Constant(Mapping):
|
||||||
return np.zeros_like(X)
|
return np.zeros_like(X)
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
input_dict = super(Constant, self)._to_dict()
|
"""
|
||||||
|
Convert the object into a json serializable dictionary.
|
||||||
|
|
||||||
|
Note: It uses the private method _save_to_input_dict of the parent.
|
||||||
|
|
||||||
|
:return dict: json serializable dictionary containing the needed information to instantiate the object
|
||||||
|
"""
|
||||||
|
input_dict = super(Constant, self)._save_to_input_dict()
|
||||||
input_dict["class"] = "GPy.mappings.Constant"
|
input_dict["class"] = "GPy.mappings.Constant"
|
||||||
input_dict["value"] = self.C.values[0]
|
input_dict["value"] = self.C.values[0]
|
||||||
return input_dict
|
return input_dict
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,13 @@ class Identity(Mapping):
|
||||||
return dL_dF
|
return dL_dF
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
input_dict = super(Identity, self)._to_dict()
|
"""
|
||||||
|
Convert the object into a json serializable dictionary.
|
||||||
|
|
||||||
|
Note: It uses the private method _save_to_input_dict of the parent.
|
||||||
|
|
||||||
|
:return dict: json serializable dictionary containing the needed information to instantiate the object
|
||||||
|
"""
|
||||||
|
input_dict = super(Identity, self)._save_to_input_dict()
|
||||||
input_dict["class"] = "GPy.mappings.Identity"
|
input_dict["class"] = "GPy.mappings.Identity"
|
||||||
return input_dict
|
return input_dict
|
||||||
|
|
|
||||||
|
|
@ -39,13 +39,21 @@ class Linear(Mapping):
|
||||||
return np.dot(dL_dF, self.A.T)
|
return np.dot(dL_dF, self.A.T)
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
input_dict = super(Linear, self)._to_dict()
|
"""
|
||||||
|
Convert the object into a json serializable dictionary.
|
||||||
|
|
||||||
|
Note: It uses the private method _save_to_input_dict of the parent.
|
||||||
|
|
||||||
|
:return dict: json serializable dictionary containing the needed information to instantiate the object
|
||||||
|
"""
|
||||||
|
|
||||||
|
input_dict = super(Linear, self)._save_to_input_dict()
|
||||||
input_dict["class"] = "GPy.mappings.Linear"
|
input_dict["class"] = "GPy.mappings.Linear"
|
||||||
input_dict["A"] = self.A.values.tolist()
|
input_dict["A"] = self.A.values.tolist()
|
||||||
return input_dict
|
return input_dict
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _from_dict(mapping_class, input_dict):
|
def _build_from_input_dict(mapping_class, input_dict):
|
||||||
import copy
|
import copy
|
||||||
input_dict = copy.deepcopy(input_dict)
|
input_dict = copy.deepcopy(input_dict)
|
||||||
A = np.array(input_dict.pop('A'))
|
A = np.array(input_dict.pop('A'))
|
||||||
|
|
|
||||||
|
|
@ -48,17 +48,41 @@ class SparseGPClassification(SparseGP):
|
||||||
SparseGPClassification(sparse_gp.X, sparse_gp.Y, sparse_gp.Z, sparse_gp.kern, sparse_gp.likelihood, sparse_gp.inference_method, sparse_gp.mean_function, name='sparse_gp_classification')
|
SparseGPClassification(sparse_gp.X, sparse_gp.Y, sparse_gp.Z, sparse_gp.kern, sparse_gp.likelihood, sparse_gp.inference_method, sparse_gp.mean_function, name='sparse_gp_classification')
|
||||||
|
|
||||||
def to_dict(self, save_data=True):
|
def to_dict(self, save_data=True):
|
||||||
|
"""
|
||||||
|
Store the object into a json serializable dictionary
|
||||||
|
|
||||||
|
:param boolean save_data: if true, it adds the data self.X and self.Y to the dictionary
|
||||||
|
:return dict: json serializable dictionary containing the needed information to instantiate the object
|
||||||
|
"""
|
||||||
model_dict = super(SparseGPClassification,self).to_dict(save_data)
|
model_dict = super(SparseGPClassification,self).to_dict(save_data)
|
||||||
model_dict["class"] = "GPy.models.SparseGPClassification"
|
model_dict["class"] = "GPy.models.SparseGPClassification"
|
||||||
return model_dict
|
return model_dict
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_dict(input_dict, data=None):
|
def from_dict(input_dict, data=None):
|
||||||
|
"""
|
||||||
|
Instantiate an SparseGPClassification object using the information
|
||||||
|
in input_dict (built by the to_dict method).
|
||||||
|
|
||||||
|
:param data: It is used to provide X and Y for the case when the model
|
||||||
|
was saved using save_data=False in to_dict method.
|
||||||
|
:type data: tuple(:class:`np.ndarray`, :class:`np.ndarray`)
|
||||||
|
"""
|
||||||
import GPy
|
import GPy
|
||||||
m = GPy.core.model.Model.from_dict(input_dict, data)
|
m = GPy.core.model.Model.from_dict(input_dict, data)
|
||||||
return GPClassification.from_sparse_gp(m)
|
from copy import deepcopy
|
||||||
|
sparse_gp = deepcopy(m)
|
||||||
|
return SparseGPClassification(sparse_gp.X, sparse_gp.Y, sparse_gp.Z, sparse_gp.kern, sparse_gp.likelihood, sparse_gp.inference_method, sparse_gp.mean_function, name='sparse_gp_classification')
|
||||||
|
|
||||||
def save_model(self, output_filename, compress=True, save_data=True):
|
def save_model(self, output_filename, compress=True, save_data=True):
|
||||||
|
"""
|
||||||
|
Method to serialize the model.
|
||||||
|
|
||||||
|
:param string output_filename: Output file
|
||||||
|
:param boolean compress: If true compress the file using zip
|
||||||
|
:param boolean save_data: if true, it serializes the training data
|
||||||
|
(self.X and self.Y)
|
||||||
|
"""
|
||||||
self._save_model(output_filename, compress=True, save_data=True)
|
self._save_model(output_filename, compress=True, save_data=True)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -52,21 +52,33 @@ class _Norm(object):
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def _to_dict(self):
|
def _save_to_input_dict(self):
|
||||||
input_dict = {}
|
input_dict = {}
|
||||||
return input_dict
|
return input_dict
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_dict(input_dict):
|
def from_dict(input_dict):
|
||||||
|
"""
|
||||||
|
Instantiate an object of a derived class using the information
|
||||||
|
in input_dict (built by the to_dict method of the derived class).
|
||||||
|
More specifically, after reading the derived class from input_dict,
|
||||||
|
it calls the method _build_from_input_dict of the derived class.
|
||||||
|
Note: This method should not be overrided in the derived class. In case
|
||||||
|
it is needed, please override _build_from_input_dict instate.
|
||||||
|
|
||||||
|
:param dict input_dict: Dictionary with all the information needed to
|
||||||
|
instantiate the object.
|
||||||
|
"""
|
||||||
|
|
||||||
import copy
|
import copy
|
||||||
input_dict = copy.deepcopy(input_dict)
|
input_dict = copy.deepcopy(input_dict)
|
||||||
normalizer_class = input_dict.pop('class')
|
normalizer_class = input_dict.pop('class')
|
||||||
import GPy
|
import GPy
|
||||||
normalizer_class = eval(normalizer_class)
|
normalizer_class = eval(normalizer_class)
|
||||||
return normalizer_class._from_dict(normalizer_class, input_dict)
|
return normalizer_class._build_from_input_dict(normalizer_class, input_dict)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _from_dict(normalizer_class, input_dict):
|
def _build_from_input_dict(normalizer_class, input_dict):
|
||||||
return normalizer_class(**input_dict)
|
return normalizer_class(**input_dict)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -96,7 +108,15 @@ class Standardize(_Norm):
|
||||||
return self.mean is not None
|
return self.mean is not None
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
input_dict = super(Standardize, self)._to_dict()
|
"""
|
||||||
|
Convert the object into a json serializable dictionary.
|
||||||
|
|
||||||
|
Note: It uses the private method _save_to_input_dict of the parent.
|
||||||
|
|
||||||
|
:return dict: json serializable dictionary containing the needed information to instantiate the object
|
||||||
|
"""
|
||||||
|
|
||||||
|
input_dict = super(Standardize, self)._save_to_input_dict()
|
||||||
input_dict["class"] = "GPy.util.normalizer.Standardize"
|
input_dict["class"] = "GPy.util.normalizer.Standardize"
|
||||||
if self.mean is not None:
|
if self.mean is not None:
|
||||||
input_dict["mean"] = self.mean.tolist()
|
input_dict["mean"] = self.mean.tolist()
|
||||||
|
|
@ -104,7 +124,7 @@ class Standardize(_Norm):
|
||||||
return input_dict
|
return input_dict
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _from_dict(kernel_class, input_dict):
|
def _build_from_input_dict(kernel_class, input_dict):
|
||||||
s = Standardize()
|
s = Standardize()
|
||||||
if "mean" in input_dict:
|
if "mean" in input_dict:
|
||||||
s.mean = np.array(input_dict["mean"])
|
s.mean = np.array(input_dict["mean"])
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue