【Pytorch】使用torch.load()加载模型时报错,提示加载的模型是zip格式

问题

在pytorch版本1.6.0上保存最优模型,当将该模型下载到其他设备上时发现模型从.pt转成了zip压缩包格式或者直接使用在其他版本的pytorch上加载时报错。

解决方法

torch官网上指出
The 1.6 release of PyTorch switched torch.save to use a new zipfile-based file format. torch.load still retains the ability to load files in the old format. If for any reason you want torch.save to use the old format, pass the kwarg _use_new_zipfile_serialization=False.
在这里插入图片描述

# 保存模型时
torch.save(model.state_dict(), model_cp,_use_new_zipfile_serialization=False)

参考

https://www.freesion.com/article/87611412928/

猜你喜欢

转载自blog.csdn.net/qq_44846512/article/details/118677603
今日推荐