Unable to pickle gaussian process

Hi,

in Pymc3 I happily saved the trace and the gaussian process object as a pickle file and used it later for predictive checks, predictions, charts etc, e.g.:

with pm.model() as model: 

   gp = pm.gp.Marginal(cov_func=cov_tot)
   y_ = gp.marginal_likelihood('y_', X=X_a, y=y_a, noise=sigma_n)
   trace = pm.sample(**bayes_kws)

pickle.dump(obj={'gp': gp, 'trace': trace}, file=out_file)

When I try this now I get the following error:

AttributeError: Can't pickle local object '_make_nice_attr_error.<locals>.fn'

Is this a bug? Or do I have to make the model fresh each time I want to use it? I know I can just load the trace and avoid sampling - that’s not a problem.

I’m using pymc ‘4.0.0b6’, python 3.9 on a mac OS 12.1, in a jupyter notebook.

Thanks,

Rob

You can try to use cloudpickle. It’s a drop-in replacement for pickle that’s much more capable. It’s what we use internally in PyMC for multiprocessing at the moment.

Hi, thanks. I tried that and it still didn’t work:

Is there a specific reason to pickle only the gp component of the model? If not you could try to pickle the entire model instead.

1 Like

Hi, no, this was just to demonstrate the problem quickly. pickling the model raises the same problem:

1 Like

Sounds like it could be a library issue. Do you mind opening an issue on our GitHub repository with a minimal fully reproducible example for developers to investigate more easily?

1 Like

Certainly can! Thanks for your help.

I am having the same issue with a different model (not a Gaussian Process). I couldn’t find an issue on Github, so I decided to go ahead and create the ticket myself: pymc 4.0: AttributeError: Can’t pickle local object ‘_make_nice_attr_error..fn’ · Issue #5886 · pymc-devs/pymc (github.com)

Update here: In the issue, another issue kindly provided the advice to not use pickle from cloudpickle, but rather cloudpickle's own dump` function. This solves the problem for me.

See pymc 4.0: AttributeError: Can't pickle local object '_make_nice_attr_error.<locals>.fn' · Issue #5886 · pymc-devs/pymc · GitHub