Python 3 无法pickle lambda

标签 python lambda pytorch

我正在关注有关 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/

相关文章:

pytorch - 如何在多次运行中重现 RNN 结果?

python - 将 CSV 解析为 Pytorch 张量

python - 值错误 : Too many values to unpack

python - 图例中的多行(具有不同的标记)

C# LINQ "inner join"集合

lambda - (反射(reflection))隐式和显式 lambda 声明

python - 在Python中比较两个大文件

python - 如何找到发出 POST 请求所需的参数和 header ?

c# - 捕获异步回调中抛出的异常

unity3d - 如何在 Unity 中为生产代码部署 pytorch 神经网络