# 保存模型
torch.save(model.state_dict(), "GCN_NET3.pth")
# 加载模型
model.load_state_dict(torch.load("GCN_NET3.pth")) # model在之前已经实例化了
Can‘t pickle typing.Union[torch.Tensor, NoneType]: it‘s not the same object as typing.Union
在使用torch.save()时出现了下面的错误:_pickle.PicklingError: Can't pickle typing.Union[torch.Tensor, NoneType]: it's not the same object as typing.Union
在一个使用 fastapi 框架的 web 项目调试过程中,遇到了一个有关多进程参数序列化的问题。session对象作为参数传子进程时时报”_pickle.PicklingError: Can’t pickle : it’s not the same object as sqlalchemy.orm.session.Session”,网上基本查不到直接的解决方式,我查了很多资料,终于得以解决。
非常感谢这篇介绍 mod_wsgi 的文档帮助我解决了问题,这里我给出文档链接。
文档主要讲的
PyTorch中torch.Tensor是主要的Tensor类,而torch.tenso则是返回一个Tensor的函数。
通常情况下,torch.tensor(data, dtype=torch.float64, device=None, requires_grad=False) 和Tensor等价,其中torch.Tensor又可以写作torch.FloatTensor。
此外Tensor能够返回空的tensor,但是torch.tensor不行,如下:
tensor_without_data = tor
最近在用pytorch做SAE,想要利用网格搜索的方法找到最优解,使用torch.save()保存模型时总是报出:PicklingError: Can't pickle <class 'SAE.AutoEncoder'>: it's not the same object as SAE.AutoEncoder 的错误,查了半天发现可能是我利用for循环运行程序时,多次保存model.pkl文件造成的。查了半天资料终于找到了解决办法,把它记录下来以供大家参考。解决方案如下:
①首先安装dill模
今天在使用
pytorch保存模型时,突然出现
Can’t
pickle typing.
Union[
torch.
Tensor,
NoneType]: it’s not the same
object
的错误,找了好多文章也没找到解决办法,后来把保存代码由
save(model,path)换成save(model.state_dict(), path)终于不报错了
1 出现该异常的原因是pickle模块不能序列化lambda function.查看下面的链接可知pickle模块可序列那些类型
https://docs.python.org/3/library/pickle.html#what-can-be-pickled-and-unpickled
2 解决方案
https://github.com/uqfoundation/dill
解决方法参考:https://stackoverflow.com/questions/10045363/pickling-cv2-keypoint-causes-picklingerror
在使用SIFT将提取到的特征保存到pkl文件时,会出现如下错误:
_pickle.PicklingError: Can't pickle <class 'cv2.KeyPoint'>: it's...
成功解决ForkingPickler(file, protocol).dump(obj) TypeError: can't pickle Environment objects
以及self = reduction.pickle.load(from_parent) EOFError: Ran out of input
本人是在调试lsun数据集&&神经网络代码时出现,问题显示如下:
因为windows操作系统的原因,在Windows中,多进程multiprocessing使用的是序
Traceback (most recent call last):
File "", line 1, in
File "D:\opt\Python27\lib\multiprocessing\forking.py", line 373, in main
prepa...
pickle.PicklingError: Can’t pickle <function at 0x0000016ACED65160>: attribute lookup on main failed
multiprocessing模块不但支持多进程,
其中managers子模块还支持把多进程分布到多台机器上。
task_master.py
出现一下报错:
pickle.PicklingError: Can’t pickle <function at 0x0000016ACED651
当您尝试将使用 Flask 框架编写的 Python 应用程序序列化(即将其转换为 pickle 格式)时,可能会遇到 Flask Attribute Error:“无法 pickel 本地对象'run.<locals>. server_forever'”。这是因为 Flask 的本地对象无法被序列化。
更具体地说,这个错误是在使用 Flask 内置发行程序时出现的。发行程序使用 multiprocessing 库将应用程序作为独立进程运行,但 multiprocessing 库依赖于 pickle 库来序列化和传递进程之间的数据。由于 Flask 中的本地对象无法被 pickle 序列化,因此会引发 Attribute Error。
解决此错误的方法是使用不同的服务器,例如 Gunicorn 或 uWSGI。这些服务器不依赖于 multiprocessing 库或 pickle 序列化,并且可以与 Flask 应用程序兼容。
总之,当您遇到 Flask Attribute Error:“无法 pickel 本地对象'run.<locals>. server_forever'”时,您可以使用不同的服务器来解决此问题。这么做将避免对 Flask 的本地对象进行序列化,并使您能够顺利地运行您的 Python 应用程序。
Can‘t pickle typing.Union[torch.Tensor, NoneType]: it‘s not the same object as typing.Union