# Saving and Loading GP model in PYMC3

**URL:** <https://discourse.pymc.io/t/saving-and-loading-gp-model-in-pymc3/1801>\
**Category:** Questions\
**Created:** [August 24, 2018, 7:07pm UTC](https://discourse.pymc.io/t/saving-and-loading-gp-model-in-pymc3/1801 "2018-08-24T19:07:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sudipta](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@Sudipta](https://discourse.pymc.io/u/Sudipta)\
**Post date:** [August 24, 2018, 7:07pm UTC](https://discourse.pymc.io/t/saving-and-loading-gp-model-in-pymc3/1801/1 "2018-08-24T19:07:04Z")

</div>

Is there a way we can save and load GP model in PYMC3? Per [https://media.readthedocs.org/pdf/pymcmc/jss-gp/pymcmc.pdf](https://media.readthedocs.org/pdf/pymcmc/jss-gp/pymcmc.pdf), it’s almost impossible to save and load models in PYMC3…

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [August 25, 2018, 6:41am UTC](https://discourse.pymc.io/t/saving-and-loading-gp-model-in-pymc3/1801/2 "2018-08-25T06:41:11Z")

</div>

You can try pickle the model, but the recommended way is to save the trace using `pm.save_trace` and `pm.load_trace`: [https://github.com/pymc-devs/pymc3/pull/2975](https://github.com/pymc-devs/pymc3/pull/2975). And you re-initial the model everytime (ie, executing the `with pm.Model()...` block).

---

<div class="post-metadata">

**Author:** ![Sudipta](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@Sudipta](https://discourse.pymc.io/u/Sudipta)\
**Post date:** [August 25, 2018, 3:50pm UTC](https://discourse.pymc.io/t/saving-and-loading-gp-model-in-pymc3/1801/3 "2018-08-25T15:50:40Z")

</div>

Thanks very much…I’m still unclear about one thing  
Do you happen have a sample code to predict with this trace load functionality? I have a Gaussian model :  
y= f(x) + e…  
f(x) ~ Gaussian(a, b),  
e ~ N(0, sigma^2)  
Trace saves the posterior of a,b and sigma…

My objective is to predict f(x) for a new x in a new python session without running the model training piece. So if I use the “trace load” do I have to load the training sample separately? I sample code will be useful.

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [August 25, 2018, 7:39pm UTC](https://discourse.pymc.io/t/saving-and-loading-gp-model-in-pymc3/1801/4 "2018-08-25T19:39:00Z")

</div>

You need to rerun the model block, so you would need the training data again. But you dont need to do inference again (ie., not need to call `trace = pm.sample(...)` but `trace = pm.load_trace(...)` instead). And the prediction part should be the same in both cases.

---

<div class="post-metadata">

**Author:** ![gaddamanil16](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/gaddamanil16/32/927_2.png) [@gaddamanil16](https://discourse.pymc.io/u/gaddamanil16)\
**Post date:** [September 15, 2018, 8:37pm UTC](https://discourse.pymc.io/t/saving-and-loading-gp-model-in-pymc3/1801/5 "2018-09-15T20:37:42Z")

</div>

Hi @junpenglao,

So I can simply use `pm.save_trace` and save in a pickle file. Or how do I do it?  
I am running my code on a remote server. I am not able to understand how I can get back trace results so that i can analyse on my computer. And even if I do it, will `pm.load_trace` work without the context.

Help much needed.

Thanks.

---

<div class="post-metadata">

**Author:** ![dsvolk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/dsvolk/32/1672_2.png) [@dsvolk](https://discourse.pymc.io/u/dsvolk)\
**Post date:** [March 27, 2019, 12:07pm UTC](https://discourse.pymc.io/t/saving-and-loading-gp-model-in-pymc3/1801/6 "2019-03-27T12:07:50Z")

</div>

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)
```

---

<div class="post-metadata">

**Author:** ![Krishna\_Kumar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/krishna_kumar/32/4691_2.png) [@Krishna\_Kumar](https://discourse.pymc.io/u/Krishna_Kumar)\
**Post date:** [January 6, 2022, 12:21pm UTC](https://discourse.pymc.io/t/saving-and-loading-gp-model-in-pymc3/1801/7 "2022-01-06T12:21:02Z")

</div>

> [@dsvolk](#):
>
> `post_pred = pm.sample_ppc(trace)`

This works like a charm…🙂 Thank you !!

One small change to prediction api. sample\_ppc is deprecated. Instead we should usesample\_posterior\_predictive

`post_pred = pm.sample_posterior_predictive(trace_reload, model=model_reload, samples=100)`
