From ebad34a118f5e85b92d42f3b5e72399f9741d43b Mon Sep 17 00:00:00 2001 From: gehbiszumeis <16896724+gehbiszumeis@users.noreply.github.com> Date: Thu, 23 Dec 2021 09:18:26 +0100 Subject: [PATCH] to and & from_dict method for periodic kernel --- GPy/kern/src/periodic.py | 25 ++++++++++++++++++++++ GPy/models/gp_coregionalized_regression.py | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/GPy/kern/src/periodic.py b/GPy/kern/src/periodic.py index 3a7a515e..d97c9234 100644 --- a/GPy/kern/src/periodic.py +++ b/GPy/kern/src/periodic.py @@ -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)) diff --git a/GPy/models/gp_coregionalized_regression.py b/GPy/models/gp_coregionalized_regression.py index 6604f634..4662ab41 100644 --- a/GPy/models/gp_coregionalized_regression.py +++ b/GPy/models/gp_coregionalized_regression.py @@ -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