From 98fef6d622aba8ef77ccbe1d5bb5689912424fe9 Mon Sep 17 00:00:00 2001 From: gehbiszumeis <16896724+gehbiszumeis@users.noreply.github.com> Date: Mon, 8 Mar 2021 13:52:22 +0100 Subject: [PATCH] new: Added to_dict() method to Ornstein-Uhlenbeck (OU) kernel --- GPy/kern/src/stationary.py | 18 +++++++++++++++++- GPy/testing/serialization_tests.py | 15 ++++++++------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/GPy/kern/src/stationary.py b/GPy/kern/src/stationary.py index 888320a0..527bb9bb 100644 --- a/GPy/kern/src/stationary.py +++ b/GPy/kern/src/stationary.py @@ -412,7 +412,6 @@ class Exponential(Stationary): # return (F, L, Qc, H, Pinf) - class OU(Stationary): """ OU kernel: @@ -426,6 +425,23 @@ class OU(Stationary): def __init__(self, input_dim, variance=1., lengthscale=None, ARD=False, active_dims=None, name='OU'): super(OU, self).__init__(input_dim, variance, lengthscale, ARD, active_dims, name) + 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(OU, self)._save_to_input_dict() + input_dict["class"] = "GPy.kern.OU" + return input_dict + + @staticmethod + def _build_from_input_dict(kernel_class, input_dict): + useGPU = input_dict.pop('useGPU', None) + return OU(**input_dict) + def K_of_r(self, r): return self.variance * np.exp(-r) diff --git a/GPy/testing/serialization_tests.py b/GPy/testing/serialization_tests.py index d147d59c..93ec4b2d 100644 --- a/GPy/testing/serialization_tests.py +++ b/GPy/testing/serialization_tests.py @@ -26,14 +26,15 @@ class Test(unittest.TestCase): k7 = GPy.kern.Matern32(2, variance=1.0, lengthscale=[1.0,3.0], ARD=True, active_dims = [1,1]) k8 = GPy.kern.Matern52(2, variance=2.0, lengthscale=[2.0,1.0], ARD=True, active_dims = [1,0]) k9 = GPy.kern.ExpQuad(2, variance=3.0, lengthscale=[1.0,2.0], ARD=True, active_dims = [0,1]) - k10 = k1 + k1.copy() + k2 + k3 + k4 + k5 + k6 - k11 = k1 * k2 * k2.copy() * k3 * k4 * k5 - k12 = (k1 + k2) * (k3 + k4 + k5) - k13 = ((k1 + k2) * k3) + k4 + k5 * k7 - k14 = ((k1 + k2) * k3) + k4 * k5 + k8 - k15 = ((k1 * k2) * k3) + k4 * k5 + k8 + k9 + k10 = GPy.kern.OU(2, variance=2.0, lengthscale=[2.0, 1.0], ARD=True, active_dims=[1, 0]) + k11 = k1 + k1.copy() + k2 + k3 + k4 + k5 + k6 + k12 = k1 * k2 * k2.copy() * k3 * k4 * k5 + k13 = (k1 + k2) * (k3 + k4 + k5) + k14 = ((k1 + k2) * k3) + k4 + k5 * k7 + k15 = ((k1 + k2) * k3) + k4 * k5 + k8 * k10 + k16 = ((k1 * k2) * k3) + k4 * k5 + k8 + k9 - k_list = [k1,k2,k3,k4,k5,k6,k7,k8,k9,k10,k11,k12,k13,k14,k15] + k_list = [k1,k2,k3,k4,k5,k6,k7,k8,k9,k10,k11,k12,k13,k14,k15,k16] for kk in k_list: kk_dict = kk.to_dict()