How to save my trace in the system?

Hi community,

My goal is to save my trace after pm.sample in order to (after inference, and after closing the jupyter notebook where I’m working with my pymc3 model) not to return to run it again and save it and keep it on my computer and then load it again. But how?

I am trying to infer models that take too long and I don’t want every time I log into my jupyter I have to run them again.

In the PyMC3 documentation I can’t find anything. Thanks.

1 Like

You can save your results as a netCDF file using ArviZ:

with model:
    idata = pm.sample(return_inferencedata=True)
idata.to_netcdf("filename.nc")

then load the results with:

idata = az.from_netcdf("filename.nc")

It is also possible to use zarr instead, which is still a very new library and may fail in some cases (haven’t tried it much myself yet) but has some advantages over netCDF

Thankyou @OriolAbril I will try this solution :grin:

1 Like

Hi Oriol,
I dowloaded Netcdf from conda to use in my jupyter from https://anaconda.org/anaconda/netcdf4 but I still got this error:

What am I doing wrong? Maybe it is because I have not included return_inferencedata=True in the pm.sample ?

Thankyou mate.

1 Like

Yes, the current backend is pm.MultiTrace instead of being az.InferenceData, so for now you need to include this. Our goal is to make return_inferencedata=True the default with pymc3 4.0.

Useful links on the topic:

Thankyou so much @OriolAbril
Sorry for being so annoying, but this is my last doubt. I never worked with Arviz, how can I change what I used before (trace["blablabla"]) to the new version in Arviz? (the problem is attached)

1 Like

I generally recommend using

    ...
    idata = pm.sample(...
trace = idata.posterior  # optionally add .stack(sample=("chain", "draw"))

trace is then an xarray.Dataset which has a lot of features, you can select variables with ["var-name"], compute means, quantiles, plot the variables…

Here are some docs that showcase InferenceData capabilities:

2 Likes