bayesnf
bayesnf copied to clipboard
Unable to pickle BayesianNeuralFieldMLE
with open('/content/drive/MyDrive/Colab Notebooks/models.pkl', 'wb') as f:
pickle.dump(models["states"]["birth_rate"], f)
Results the error message.
AttributeError Traceback (most recent call last)
[<ipython-input-87-db6752b3d600>](https://localhost:8080/#) in <cell line: 1>()
1 with open('/content/drive/MyDrive/Colab Notebooks/models.pkl', 'wb') as f:
----> 2 pickle.dump(models["states"]["birth_rate"], f)
AttributeError: Can't pickle local object 'structtuple.<locals>.StructTuple'
Thanks for the report, this issue is being tracked here: https://github.com/google/bayesnf/issues/36
Specifically, this is coming from using a JointDistributionCoroutine, which dynamically generates a StructTuple to represent the state, which means pickle can't represent it.
Depending on what kind of object models['states']['birth_rate'] is, you may be able to use arviz
import arviz as az
idata = az.from_pytree(models['states']['birth_rate'])
idata.to_netcdf("models.nc")
models = az.from_netcdf("models.nc") # To reload