Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception pickling kernels #605

Open
mbway opened this issue Feb 25, 2018 · 10 comments
Open

Exception pickling kernels #605

mbway opened this issue Feb 25, 2018 · 10 comments

Comments

@mbway
Copy link

mbway commented Feb 25, 2018

I get this error when pickling a kernel once it has been used by a GP. Since the exception doesn't occur before this, I am assuming that the kernels are supposed to be picklable? The error is the same for other kernels I tried. It is also triggered by using copy.deepcopy.
My intention was to save the unused kernel anyway, and so passing a copy to the GP solved my issue, but this might still be a bug that needs fixing?

#!/usr/bin/env python3
import GPy
import pickle
import numpy as np
import sys

print('python version = ' + sys.version)
print('GPy.__version__ = ' + GPy.__version__)

k = GPy.kern.Matern52(1)

print('before using')
pickle.loads(pickle.dumps(k))
print('k._name = ' + k._name)

gp = GPy.models.GPRegression(np.array([[1]]), np.array([[1]]), kernel=k)

print('after using')
print('k._name = ' + k._name)
print('saving')
saved = pickle.dumps(k)
print('loading')
pickle.loads(saved)
python version = 3.6.3 (default, Oct  3 2017, 21:45:48) 
[GCC 7.2.0]
GPy.__version__ = 1.9.2
before using
k._name = Mat52
after using
k._name = Mat52
saving
loading
Traceback (most recent call last):
  File "pickle_problem.py", line 20, in <module>
    pickle.loads(saved)
  File "/usr/local/lib/python3.6/dist-packages/paramz/parameterized.py", line 340, in __setstate__
    super(Parameterized, self).__setstate__(state)
  File "/usr/local/lib/python3.6/dist-packages/GPy/core/parameterization/priorizable.py", line 18, in __setstate__
    super(Priorizable, self).__setstate__(state)
  File "/usr/local/lib/python3.6/dist-packages/paramz/core/parameter_core.py", line 495, in __setstate__
    super(Parameterizable, self).__setstate__(state)
  File "/usr/local/lib/python3.6/dist-packages/paramz/core/indexable.py", line 56, in __setstate__
    super(Indexable, self).__setstate__(state)
  File "/usr/local/lib/python3.6/dist-packages/paramz/core/pickleable.py", line 125, in __setstate__
    self.observers = ObserverList()
  File "/usr/local/lib/python3.6/dist-packages/paramz/parameterized.py", line 330, in __setattr__
    pnames = self.parameter_names(False, adjust_for_printing=True, recursive=False)
  File "/usr/local/lib/python3.6/dist-packages/paramz/core/parameter_core.py", line 191, in parameter_names
    names.extend([adjust(x.name) for x in self.parameters])
  File "/usr/local/lib/python3.6/dist-packages/paramz/core/parameter_core.py", line 191, in <listcomp>
    names.extend([adjust(x.name) for x in self.parameters])
  File "/usr/local/lib/python3.6/dist-packages/paramz/core/nameable.py", line 63, in name
    return self._name
AttributeError: 'Matern52' object has no attribute '_name'

@cwlgadd
Copy link

cwlgadd commented Apr 23, 2018

I have run into the exact same problem. Unfortunately I can't use the same work around though.

@yenicelik
Copy link

yenicelik commented May 9, 2018

Exact same problem over here! Did anyone find a work-around or a fix?

@cwlgadd
Copy link

cwlgadd commented May 9, 2018

My work-around was to not store the GP/kernel and to re-instantiate it every time.. which isn't efficient, but worked for me because I'm using a mixture model. I didn't have the time to look into a fix.

@cwlgadd
Copy link

cwlgadd commented May 18, 2018

Has anyone been able to look into a fix?

@maxwab
Copy link

maxwab commented Jun 17, 2018

Same here. This issue makes it impossible to use GPy with multiprocessing for me.

@gregorgebhardt
Copy link

Following up on @cwlgadd a possible work-around is to implement the methods _to_dict / to_dict (see here and here) and _from_dict (see here) and use them for serializing the kernel objects. This allows to use GPy with multiprocessing.

@yenicelik
Copy link

Another work-around for multiprocessing is to use the https://github.com/uqfoundation/multiprocess which has the exact same interface as the standard python multiprocessing, but uses dill instead of pickle as the backend.

@mbway
Copy link
Author

mbway commented Jun 20, 2018

in my testing dill also triggers the exception

@Amir-Arsalan
Copy link

@mbway Were you able to use the the to_dict method? It seems pretty easy to use but I have issues using as shown here. Would appreciate if you can help me figure out where I'm making a mistake.

@mbway
Copy link
Author

mbway commented Jan 15, 2020

for my use case I didn't actually require pickling kernels that have been used, so I restructured my code to copy the kernel and pass the copy to the GP instead, allowing the object holding the unused kernel to be pickled. This probably doesn't help most cases though, sorry

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants