Skip to content

Add to the tutorial how to save and load models #356

@igrahek

Description

@igrahek

Sorry if I missed this in the tutorial, but I can't find anything on how to save and load models. I was looking into arviz, and this flow seems to work, but I'm not sure that it's the best way. Also, it is ok for getting posteriors, plotting the traces, and doing model summary. However, it seems to break for the posterior predictives.

cav_data = hssm.load_data("cavanagh_theta")

model = hssm.HSSM(
    data=cav_data,
    hierarchical=True,
    prior_settings="safe",
    model="ddm",
    loglik_kind="approx_differentiable")

modelObject = model_safe.sample(chains=1, 
    cores=1, 
    draws=50, 
    tune=50)

# Save
modelObject.to_netcdf('model')

# Load
modelObject = az.InferenceData.from_netcdf('model')

# Summarize
az.summary(modelObject, var_names=['~a','~t', '~z'])

# Plot posteriors
az.plot_trace(model)

# Plot PPCs
az.plot_ppc(modelObject)
Error: `data` argument must have the group "posterior_predictive" for ppcplot

It seems to me that when saved and loaded in this way, we have the InferenceData object. Is there a better way to do all of this so that I'm using HSSM functions? For example this code breaks:

modelObject.summary()

AttributeError: 'InferenceData' object has no attribute 'summary'

For getting the HSSM plot_posterior_predictive function to work, do I need to supply the model, the InferenceData, and the raw data? Or is there a way to save modelObject so that all of that is within it?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions