_pickle.PicklingError: Can't pickle <class '__main__.X'>: attribute lookup X on __main__ failed

Viewed 1237

pickle succeed perfectly in a Jupyter notebook but when I do it in a .py file pickle fails.

I don't understand why?

It's just a simple class

class X:
    def __init__(self, ref):
        import torch
        self.ref = torch.tensor(ref)
        self.result = torch.full(ref.shape, True, dtype=torch.uint8)
        self.index = None

    def partial_fit(self, X):
        assert len(X) > 0
        import numpy
        blablabla

    def transform(self, X):
        return X[:, self.index]

Python version and package looks to be at same version

0 Answers
Related