在我使用Pytorch训练网络后,在保存模型的这一步报错了。说明一下,我的系统是Ubuntu20.04,Pytorch版本是cuda11.8。
我保存模型的代码如下:
torch.save(model, "/home/code/lstm-free/checkpoint/a.pth")
运行上述保存的代码块后,报错如下:
Traceback (most recent call last):
File "/home/code/lstm-free/main.py", line 24, in <module>
torch.save(trained_model, "/home/code/lstm-free/checkpoint/a.pth")
File "/home/admin/miniconda3/envs/py_310/lib/python3.10/site-packages/torch/serialization.py", line 618, in save
with _open_zipfile_writer(f) as opened_zipfile:
File "/home/admin/miniconda3/envs/py_310/lib/python3.10/site-packages/torch/serialization.py", line 492, in _open_zipfile_writer
return container(name_or_buffer)
File "/home/admin/miniconda3/envs/py_310/lib/python3.10/site-packages/torch/serialization.py", line 463, in __init__
super().__init__(torch._C.PyTorchFileWriter(self.name))
RuntimeError: File /home/code/lstm-free/checkpoint/a.pth cannot be opened.
经过各种搜索解决方案之后,有换torch版本,有添加checkpoint文件夹的,都不行。最后发现,是因为我的代码文件夹的权限不够,不能够对文件进行读写。因此我就给文件夹赋予了root权限。最后问题迎刃而解。赋予root权限的代码如下:
sudo chmod 777 YourPath
因为是在学习使用云服务器进行Python代码运行,很多地方还不是很懂,找了很久才解决这个问题。记录一下。