_pickle.PicklingError:无法 pickle <class '__main__.X'>:在 __main__ 上查找 X 属性失败

Fra*_*ale 5 pickle python-3.x jupyter-notebook

pickle 在 Jupyter 笔记本中完美成功,但当我在 .py 文件中执行时,pickle 失败了。

我不明白为什么?

这只是一个简单的类

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]
Run Code Online (Sandbox Code Playgroud)

Python 版本和包看起来是同一版本