Saving and Loading GP model in PYMC3

You can actually avoid re-running the same model definition code twice. This is what I do:

In my “train” script, after I sampled the model, I put

with open(model_fpath, 'wb') as buff:
    pickle.dump({'model': model, 'trace': trace, 'X_shared': X_shared}, buff)

Here X_shared is the Theano shared tensor of the predictor variables.
Then in my separate “production predict” script I start with

with open(model_fpath, 'rb') as buff:
    data = pickle.load(buff)
model = data['model']
trace = data['trace']
X_shared = data['X_shared']

X_shared.set_value(np.asarray(X, theano.config.floatX))

Here X are my predictor variables in production.
Now I can sample the posterior predictive:

with model:
    post_pred = pm.sample_ppc(trace)
5 Likes