python pickle not consistent

765 views Asked by At

I'm using python3.5 on Ubuntu. I trained a model by xgboost sklearn wrapper and save it by pickle.

Here is a link of the pickle file: https://pan.baidu.com/s/1eSoPWxs

The question is each time I load it, the result is different.

For example:

a = pickle.load(open('mymodel', 'rb'))

b = pickle.load(open('mymodel', 'rb'))

print(a == b)

I got result False, but I think it should be True.

Can anybody explain this? Is there any way to solve this?

Thanks a lot!

2

There are 2 answers

0
Ben On

Finally I found the problem, which is nothing to do with pickle. There is something else which causes some randomization in each running.

Thanks for help! Sorry for bothering!

Ben

1
Moses Koledoye On

When sklearn is not installed, the base class of the XGBoostModel is the same as object:

XGBModelBase = object

And you probably already know that two instances of object are not equal:

>>> import pickle
>>> pickle.dump(object(), open('test.txt', 'wb'))
>>> a = pickle.load(open('test.txt', 'rb'))
>>> b = pickle.load(open('test.txt', 'rb'))
>>> a == b
False

>>> object() == object()
False

I'll expect the behavior of __eq__ on the base class XGBModelBase to be consistent in the case sklearn is installed.

Also note that __eq__ was not overriden in the model class, so the behavior you have is as expected.

You may try comparing the dictionaries of the unpickled models and see if that works for you: a.__dict__ == b.__dict__