我正在关注有关 Deep Learning for Self-Driving Cars 的在线教程用 torch 。但是当 python 尝试 pickle lambda 时我遇到了一些问题。
我尝试定义自己的函数,但结果仍然相同
下面是我定义训练数据集的地方。
params = {'batch_size': 32, 'shuffle': True, 'num_workers':4}
training_set = Dataset(train_samples, transform=transdorms.Compose([transforms.Lambda(lambda x: (x/255)-0.5)])
training_generator = DataLoader(training_set, **params)
那么问题出在下面的代码上。
for local_batch, (centers, lefts, rights) in enumerate(training generator):
... Some code
显示以下错误
_pickle.PicklingError: Can't pickle
<function <lambda> at 0x000001B915587948>
: attribute lookup on main failed
最佳答案
Python 文档说 lambda
函数无法被 pickle。
https://docs.python.org/3/library/pickle.html#id2 :
Note that functions (built-in and user-defined) are pickled by “fully qualified” name reference, not by value. 2 This means that only the function name is pickled, along with the name of the module the function is defined in. Neither the function’s code, nor any of its function attributes are pickled. Thus the defining module must be importable in the unpickling environment, and the module must contain the named object, otherwise an exception will be raised.
https://docs.python.org/3/library/pickle.html#id7 :
This is why lambda functions cannot be pickled: all lambda functions share the same name: .
关于Python 3 无法pickle lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57953100/