2

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__'>


marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Alex Deft
  • 2,531
  • 1
  • 19
  • 34
  • are you importing `HParams` in the file you do `dill.load()`? – afghanimah Apr 26 '20 at 05:30
  • @afghanimah No. I do realize if I import it it fixes everything. But, if that is the case, then `pickle` is just as good. What's the benefit of `dill` then? – Alex Deft Apr 26 '20 at 06:03
  • https://stackoverflow.com/questions/58193119/how-is-dill-different-from-pythons-pickle-module – afghanimah Apr 26 '20 at 06:55
  • @afghanimah I have read that one already. I gathered from it that it only extends the types of objects that can be serialized, that's it. Please correct me if I'm wrong. – Alex Deft Apr 26 '20 at 07:23
  • 1
    That seems to be my understanding as well at a glance – afghanimah Apr 26 '20 at 07:29
  • @AlexDeft: I'm the `dill` author. It's hard to tell what you are doing, can you create a full minimal example that demonstrates how you are producing your error? Should I assume that `hp = HParams()`? Which of the `dill` serialization variants did you try? or diagnostic tools? – Mike McKerns Apr 29 '20 at 13:01
  • @MikeMcKerns Hi. I'm sorry, it has been some time and I'm trying now but I failed to reproduce the same error as you requested, however I got somsthing more familiar. Please see `EDIT 1` – Alex Deft May 03 '20 at 13:04
  • `dill` doesn't do terribly well with `super`. If I remove the unnecessary call to `super`, 'HP' can be dumped and loaded. I tried with `recurse` set to True and also False, and both can load the pickle.. You might want to try `dill.source.dumpsource`. – Mike McKerns May 03 '20 at 14:23

0 Answers0