MLBazaar / BTB

A simple, extensible library for developing AutoML systems
https://mlbazaar.github.io/BTB/
MIT License
172 stars 41 forks source link

save and load using pickle #96

Closed alireza-akhondi-asl closed 5 years ago

alireza-akhondi-asl commented 5 years ago

Description

I would like to save and load the tuner objects using pickle. However, when I try to load the saved object I get an error message:

~\Anaconda3\lib\site-packages\btb\hyper_parameter.py in __new__(cls, param_type, param_range)
     49                 param_type = ParamTypes[param_type.upper()]
     50             else:
---> 51                 raise ValueError('Invalid param type {}'.format(param_type))
     52 
     53         for subclass in cls.subclasses():

ValueError: Invalid param type None

What I Did

import pickle
from btb.tuning import GP
from btb import HyperParameter, ParamTypes

tunables = [('hidden_dim', HyperParameter(ParamTypes.INT_CAT, [64,128,256,384,512,640,768])),]
tuner = GP(tunables)

parameters = tuner.propose()
score=2.7085395288
tuner.add(parameters, score)
with open("file.pkl", "wb") as f:
            pickle.dump(tuner, f, pickle.HIGHEST_PROTOCOL)

with open('file.pkl', 'rb') as input:
            pickle.load(input)
micahjsmith commented 5 years ago

This originates because unpickling class instances does something similar to

def load(cls, attributes):
    obj = cls.__new__(cls)
    obj.__dict__.update(attributes)
    return obj

where here, cls is IntCatHyperParameter. This uses the default parameter values in HyperParameter.__new__ of param_type=None which is invalid.

micahjsmith commented 5 years ago

Smaller test case:

import pickle
from btb import HyperParameter
param = HyperParameter('int', [0, 1])
with open('/tmp/param.pkl', 'wb') as f:
    pickle.dump(param, f, pickle.HIGHEST_PROTOCOL)
with open('/tmp/param.pkl', 'rb') as f:
    pickle.load(f)

Note that the error does not occur when using the default pickling protocol (0) rather than pickle.HIGHEST_PROTOCOL.

alireza-akhondi-asl commented 5 years ago

Thank you!