diff --git a/GPy/inference/latent_function_inference/expectation_propagation.py b/GPy/inference/latent_function_inference/expectation_propagation.py index b2a3d4b6..0c575412 100644 --- a/GPy/inference/latent_function_inference/expectation_propagation.py +++ b/GPy/inference/latent_function_inference/expectation_propagation.py @@ -51,7 +51,7 @@ class EP(EPBase, ExactGaussianInference): if K is None: K = kern.K(X) - if self._ep_approximation is None: + if getattr(self, '_ep_approximation', None) is None: #if we don't yet have the results of runnign EP, run EP and store the computed factors in self._ep_approximation mu, Sigma, mu_tilde, tau_tilde, Z_tilde = self._ep_approximation = self.expectation_propagation(K, Y, likelihood, Y_metadata) else: @@ -159,7 +159,7 @@ class EPDTC(EPBase, VarDTC): else: Kmn = psi1.T - if self._ep_approximation is None: + if getattr(self, '_ep_approximation', None) is None: mu, Sigma, mu_tilde, tau_tilde, Z_tilde = self._ep_approximation = self.expectation_propagation(Kmm, Kmn, Y, likelihood, Y_metadata) else: mu, Sigma, mu_tilde, tau_tilde, Z_tilde = self._ep_approximation diff --git a/README.md b/README.md index fceab117..5b556bfd 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,33 @@ If you're having trouble installing GPy via `pip install GPy` here is a probable [![Windows](https://img.shields.io/badge/download-windows-orange.svg)](https://pypi.python.org/pypi/GPy) [![MacOSX](https://img.shields.io/badge/download-macosx-blue.svg)](https://pypi.python.org/pypi/GPy) +# Saving models in a consistent way across versions: + +As pickle is inconsistent across python versions and heavily dependent on class structure, it behaves inconsistent across versions. +Pickling as meant to serialize models within the same environment, and not to store models on disk to be used later on. + +To save a model it is best to save the m.param_array of it to disk (using numpy’s np.save). +Additionally, you save the script, which creates the model. +In this script you can create the model using initialize=False as a keyword argument and with the data loaded as normal. +You then set the model parameters by setting m.param_array[:] = loaded_params as the previously saved parameters. +Then you initialize the model by m.initialize_parameter(), which will make the model usable. +Be aware that up to this point the model is in an inconsistent state and cannot be used to produce any results. + +```python +# let X, Y be data loaded above +# Model creation: +m = GPy.models.GPRegression(X, Y) +m.optimize() +# 1: Saving a model: +np.save('model_save.npy', m.param_array) +# 2: loading a model +# Model creation, without initialization: +m = GPy.models(GPRegression(X,Y,initialize=False) +m[:] = np.load('model_save.npy') +m.initialize_parameter() +print m +``` + ## Running unit tests: Ensure nose is installed via pip: