当我尝试调用“torch.save”将模型保存在“tmp_file”中时,它会出现
FileNotFoundError
。回溯如下:
Traceback (most recent call last):
File "C:/.../python/simulations/hdr.py", line 234, in
test_hdr_continuous()
File "C:/.../python/simulations/hdr.py", line 195, in test_hdr_continuous
model = fit_mdn(X[:split], y[:split], nepochs=20)
File "C:\...\python\simulations\continuous.py", line 192, in fit_mdn
torch.save(model, tmp_file)
File "C:\...\python\venv\lib\site-packages\torch\serialization.py", line 161, in save
return _with_file_like(f, “wb”, lambda f: _save(obj, f, pickle_module, pickle_protocol))
File “C:\Users\Haoran\Documents\GitHub\dose-response\python\venv\lib\site-packages\torch\serialization.py”, line 116, in _with_file_like
f = open(f, mode)
FileNotFoundError: [Errno 2] No such file or directory: ‘/tmp/tmp_file_4358f298-a1d9-4c81-9e44-db4d8f1b4319’
奇怪的是,在我的 Mac 上一切都运行得很好,但我在 Windows 桌面上却遇到了这个错误。
正如 shmee 观察到的,您正在尝试在
Windows机器上写入
/tmp/[...]
。因此你得到FileNotFoundError
。tempfile
包很有用,尤其是 NamedTemporaryFile
:此函数创建一个临时文件并返回其名称,以便您可以在程序中访问/使用它。