AttributeError: module 'pymc' has no attribute 'save_trace' when trying to save trace

Hi All,

I am trying to save a model with:
pm.save_trace(trace, "path", overwrite=True)
but get the error in the title.

Is this not the way to do it?

Many thanks for your help!

For storing the mcmc samples, you can interface through Arviz. See InferenceData — ArviZ dev documentation

2 Likes

Thanks! What is save_trace doing then that I’ve been reading and finding about in all the old threads?

Also: sorry for the thousand questions, is the best practice then to save to dataframe and then load and convert a dataframe again? Or what is the best way to save a trace and load it for use again later?

There isn’t much written here :sweat_smile: arviz.InferenceData.load — ArviZ dev documentation

I just throw everything into a cloudpickle:

import cloudpickle
model_fpath = '/Your/Path/Here.pkl'
with open(model_fpath, 'wb') as buff:
    cloudpickle.dump({'model': model, 'trace': idata,'y_obs':y_obs}, buff)

and then to read:

with open(model_fpath, 'rb') as buff:
      saved_model = cloudpickle.load(buff)
idata = saved_model['trace'] 
1 Like

Many thanks! It was my initial thought too, I read here in discussions and on github where everyone says not to do that but instead use save_trace. Thanks so much, I find that much easier and straightforward too.

Usually I do something like:

import arviz as az
model_fpath = '/Your/Path/Here.nc'
idata.to_netcdf(filepath)
# then to read:
idata = az.from_netcdf(filepath)