【问题标题】:Pytorch Torch.save FileNotFoundErrorPytorch Torch.save FileNotFoundError
【发布时间】:2018-07-24 15:56:17
【问题描述】:

当我尝试调用“torch.save”将我的模型保存在“tmp_file”中时,它会出现FileNotFoundError。回溯如下:

Traceback(最近一次调用最后一次): 文件“C:/Users/Haoran/Documents/GitHub/dose-response/python/simulations/hdr.py”,第 234 行,在 test_hdr_continuous() 文件“C:/Users/Haoran/Documents/GitHub/dose-response/python/simulations/hdr.py”,第 195 行,在 test_hdr_continuous 模型 = fit_mdn(X[:split], y[:split], nepochs=20) 文件“C:\Users\Haoran\Documents\GitHub\dose-response\python\simulations\continuous.py”,第 192 行,在 fit_mdn torch.save(模型,tmp_file) 文件“C:\Users\Haoran\Documents\GitHub\dose-response\python\venv\lib\site-packages\torch\serialization.py”,第 161 行,保存 return _with_file_like(f, “wb”, lambda f: _save(obj, f, pickle_module, pickle_protocol)) 文件“C:\Users\Haoran\Documents\GitHub\dose-response\python\venv\lib\site-packages\torch\serialization.py”,第 116 行,在 _with_file_like f = 打开(f,模式) FileNotFoundError:[Errno 2] 没有这样的文件或目录:'/tmp/tmp_file_4358f298-a1d9-4c81-9e44-db4d8f1b4319'

奇怪的是,在我的 mac 上一切正常,但我在 Windows 桌面上遇到了这个错误。

【问题讨论】:

  • /tmp/[...]不是windows下的有效路径
  • @shmee 已修复,谢谢!

标签: python pytorch torch


【解决方案1】:

正如shmee 所观察到的,您正试图在Windows 机器上写信给/tmp/[...]。因此你会得到FileNotFoundError
为了使您的代码与操作系统无关,您可能会发现 python 的 tempfile 包很有用,尤其是 NamedTemporaryFile:此函数创建一个临时文件并返回其名称,因此您可以在程序中访问/使用它。

【讨论】:

    猜你喜欢
    • 2019-10-14
    • 2021-09-14
    • 2022-11-22
    • 2020-12-05
    • 2020-11-16
    • 2021-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多