I'm trying to pickle a class. Pickle fails with attribute error because the class constructor is not present at load time. I gathered that dill
module is an extension of pickle
module. However, I still get errors. Namely:
Traceback (most recent call last):
File "C:\Users\s4551072\.conda\envs\gpuenv\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-4-447b415d0f92>", line 1, in <module>
dill.load(open('hp', 'rb'))
File "C:\Users\s4551072\.conda\envs\gpuenv\lib\site-packages\dill\_dill.py", line 270, in load
return Unpickler(file, ignore=ignore, **kwds).load()
File "C:\Users\s4551072\.conda\envs\gpuenv\lib\site-packages\dill\_dill.py", line 472, in load
obj = StockUnpickler.load(self)
File "C:\Users\s4551072\.conda\envs\gpuenv\lib\site-packages\dill\_dill.py", line 577, in _load_type
return _reverse_typemap[name]
KeyError: 'ClassType'
Which I get upon loading.
At saving:
dill.dump(hp, open('hp', 'wb'))
At loading (in a completely different file):
dill.load(open('hp', 'rb'))
Where hp
is an instance of the following class
import tensorflow as tf
import resources.toolbox as tb
class HParams(tb.HyperParam):
def __init__(self):
# =================== Enviroment ========================
super().__init__()
self.exp_name = 'trial' # 'gru_vs_rnn_vs_lstm'
self.save_path = './tmp/lstm/' + self.exp_name
self.save_path = os.path.abspath(self.save_path)
os.makedirs(self.save_path, exist_ok=True)
self.pkg = tf.__name__
self.s = tb.get_diag(16, 44)
I've tried all sorts of tricks to no avail.
ُEDIT1:
This is the main file:
import lib
import dill
class HP(lib.HyperParam):
def __init__(self):
super().__init__()
if __name__ == '__main__':
hp = HP()
dill.dump(hp, open('myfile', 'wb'))
And this is lib
module
import os
import inspect
class HyperParam:
def __init__(self):
"""
It is preferable to pass the packages used, so that later this class can be saved and loaded.
"""
# =================== Enviroment ========================
self.exp_name = 'default'
self.root = 'tmp'
self.pkg = None
# self.device = config_device(HyperParam, Device.gpu0)
# ===================== DATA ============================
self.cal = [False, True][0]
self.freq_select = None
self.norm_factor = None
self.sort_freq = None
self.dmd = False
self.seed = 234
# =================== Model =============================
self.l1 = 0.0005
# ===================== Training ========================
self.split = 0.2
self.lr = 0.0005
self.batch_size = 32
self.epochs = 30
self.milk = [None]
self.nsl = [False, True][0]
self.adv_multiplier = 0.2
self.adv_step_size = 0.01
self.adv_grad_norm = 'infinity'
@staticmethod
def from_saved(path):
path = os.path.join(path, f'metadata/HyperParam')
import pickle
return pickle.load(open(path, "rb"))()
def __repr__(self):
return ''.join(inspect.getsourcelines(self.__class__)[0])
Run the main file, it will save and object. Now, in a new file, try to load that:
import dill
a = dill.load(open('myfile', 'rb'))
Traceback (most recent call last):
File "C:\Users\s4551072\.conda\envs\gpuenv\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-3-390afbf8b35b>", line 1, in <module>
a = dill.load(open('./training/myfile', 'rb'))
File "C:\Users\s4551072\.conda\envs\gpuenv\lib\site-packages\dill\_dill.py", line 270, in load
return Unpickler(file, ignore=ignore, **kwds).load()
File "C:\Users\s4551072\.conda\envs\gpuenv\lib\site-packages\dill\_dill.py", line 472, in load
obj = StockUnpickler.load(self)
File "C:\Users\s4551072\.conda\envs\gpuenv\lib\site-packages\dill\_dill.py", line 462, in find_class
return StockUnpickler.find_class(self, module, name)
AttributeError: Can't get attribute 'HP' on <module '__main__'>