bayesnf icon indicating copy to clipboard operation
bayesnf copied to clipboard

Unable to pickle BayesianNeuralFieldMLE

Open f0lie opened this issue 1 year ago • 2 comments

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'

f0lie avatar Jun 27 '24 21:06 f0lie

Thanks for the report, this issue is being tracked here: https://github.com/google/bayesnf/issues/36

fsaad avatar Jul 09 '24 14:07 fsaad

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

ColCarroll avatar Jul 09 '24 15:07 ColCarroll