I built and optimized a Sparse Gaussian Process Regression model using the GPy library. The documentation recommends to save the model as follow:
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.
I am able to save the parameters of the model and recreate the model from them. However, I need to know in advance the kernel architecture that was used to build the model (defined in the function create_kernel
below). To create and save the model, I do the following:
def create_kernel():
# This function could change
return GPy.kern.RBF(4,ARD=True) + GPy.kern.White(4)
gp = GPy.models.SparseGPRegression(X, y,
num_inducing=100,
kernel=create_kernel())
# optimization steps
# ...
# Save the model parameters
np.save('gp_params.npy',gp.param_array)
np.save('gp_y.npy',y)
np.save('gp_X.npy',X_gpr)
To load the model, I am doing the following at the moment. The problem is that I might not have access to the create_kernel
function.
# Load model
y_load = np.load('gp_y.npy')
X_load = np.load('gp_X.npy')
gp_load = GPy.models.SparseGPRegression(X_load, y_load,
initialize=False,
num_inducing=100,
kernel=create_kernel()) # Kernel is problematic here
gp_load.update_model(False)
gp_load.initialize_parameter()
gp_load[:] = np.load('gp_params.npy')
gp_load.update_model(True)
What is the best way to store the kernel for later use? The parameters of the kernel and the inducing inputs are stored in the gp_params.npy
file but not the structure of the kernel. At the moment, I have to know which function was used to create the model which will not always be the case.
Thanks a lot for your help!