3

How can I save and load Gaussian process models created using the GPy package?

Here's how I define the model and optimize for its parameters:

# define kernel
ker =   GPy.kern.Matern52(rescaled_x_train.shape[1],ARD=True) + GPy.kern.White(rescaled_x_train.shape[1]) + \
        GPy.kern.Linear(rescaled_x_train.shape[1]) 

y_gp = y_train.values.reshape(y_train.shape[0],1)        
x_gp = rescaled_x_train
# create a GP model
m = GPy.models.GPRegression(x_gp, y_gp , ker) 

m.optimize(messages=True,max_f_eval = 3000)

But I don't know how I can save m in such a way that I can load it to be used for prediction.

I tried the following:

# Save the model parameters
np.save('gp_params.npy',m.param_array)
np.save('gp_y.npy',y_gp)
np.save('gp_X.npy',x_gp)

# Load model
y_load = np.load('gp_y.npy')
X_load = np.load('gp_X.npy')
gp_load = GPy.models.GPRegression(X_load, y_load, kernel= ker,
                                   initialize=True)  

However, this loads a model that is different than the one I created originally. Can any one help please?

Amir
  • 10,600
  • 9
  • 48
  • 75
owise
  • 1,055
  • 16
  • 28
  • 1
    Have you tried serializing the model with [`pickle`](https://docs.python.org/3/library/pickle.html)? Did that work? I know pickle doesn't always work with objects... – Engineero Mar 27 '19 at 20:56
  • Yes I tried, but the problem is in Gaussian processes, the model consists of: the kernel, the optimised parameters, and the training data. All of these have to be packed together to make a reusable model. I failed to pickle the kernel –  owise Mar 27 '19 at 21:30

1 Answers1

2

Actually it turned out to be much easier than I thought, we can just pickle as @ Engineero suggested like any other model, interestingly, all needed elements will be automatically pickled including: Kernel, the optimised Hyperparameters, and the training data:

with open(models_path + "model_name.dump" , "wb") as f:
     pickle.dump(m, f)  

and then you can simply load it:

model = pickle.load(open(models_path + "model_name.dump","rb"))
owise
  • 1,055
  • 16
  • 28