to and & from_dict method for periodic kernel

This commit is contained in:
gehbiszumeis 2021-12-23 09:18:26 +01:00
parent 5c587e91b9
commit ebad34a118
2 changed files with 26 additions and 1 deletions

View file

@ -40,6 +40,13 @@ class Periodic(Kern):
return alpha*np.cos(omega*x + phase)
return f
def _save_to_input_dict(self):
input_dict = super(Periodic, self)._save_to_input_dict()
input_dict["variance"] = self.variance.values.tolist()
input_dict["lengthscale"] = self.lengthscale.values.tolist()
input_dict["period"] = self.period.values.tolist()
return input_dict
@silence_errors
def _cos_factorization(self, alpha, omega, phase):
r1 = np.sum(alpha*np.cos(phase),axis=1)[:,None]
@ -200,6 +207,24 @@ class PeriodicMatern32(Periodic):
self.G = self.Gram_matrix()
self.Gi = np.linalg.inv(self.G)
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
"""
input_dict = super(PeriodicMatern32, self)._save_to_input_dict()
input_dict["class"] = "GPy.kern.PeriodicMatern32"
return input_dict
@staticmethod
def _build_from_input_dict(kernel_class, input_dict):
useGPU = input_dict.pop('useGPU', None)
return kernel_class(**input_dict)
def Gram_matrix(self):
La = np.column_stack((self.a[0]*np.ones((self.n_basis,1)),self.a[1]*self.basis_omega,self.a[2]*self.basis_omega**2))
Lo = np.column_stack((self.basis_omega,self.basis_omega,self.basis_omega))

View file

@ -23,7 +23,7 @@ class GPCoregionalizedRegression(GP):
:type likelihoods_list: None | a list GPy.likelihoods
:param name: model name
:type name: string
:param W_rank: number tuples of the corregionalization parameters 'W' (see coregionalize kernel documentation)
:param W_rank: number tuples of the coregionalization parameters 'W' (see coregionalize kernel documentation)
:type W_rank: integer
:param kernel_name: name of the kernel
:type kernel_name: string