EOFError: Ran out of input, using torch.load()

EOFError: Ran out of input, using torch.load()

我看到这个错误被发布了很多,通常是由于文件在打开后没有正确关闭。但是由于我使用的是集成的 torch.load() 函数,所以我不确定我可以做些什么不同的事情。

首先是节省部分:

    torch.save({
            'model_state_dict': agent.dqn.state_dict(),
             ...
            'loss_history': agent.losshistory
            }, modelpath)

这里是加载部分,我也收到了错误消息:

if os.path.exists(modelpath):
    checkpoint = torch.load(modelpath)
    agent.dqn.load_state_dict(checkpoint['model_state_dict'])
    ...
    agent.losshistory = checkpoint['loss_history']

这里是错误:

Traceback (most recent call last):
  File "c:/Users/levin/Desktop/programming/main.py", line 33, in <module>
    checkpoint = torch.load(modelpath)
  File "C:\Users\levin\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\serialization.py", line 529, in load
    return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
  File "C:\Users\levin\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\serialization.py", line 702, in _legacy_load
    result = unpickler.load()
EOFError: Ran out of input

我还想提到的一件事是,我多次使用这个确切的代码没有问题。我不记得更改过任何可能导致错误的内容。

据此thread 好像读取空文件时会抛出异常,所以请在读取前检查文档的大小,post 如果没有解决请回复。