3

I saw this error being posted a lot and often it was due to the file not being closed properly after opening. But since I'm using the integrated torch.load() function, I'm not sure what I could do different.

First the saving part:

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

and here the loading part, where I also get the error message:

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

and here the error:

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

One more thing I want to mention is that I used this exact code several times without a problem. I can't remember changing anything that could have caused the error.

1 Answers1

7

According to this thread it seems to raise an exception when reading an empty file, so please check the size of the document before reading it and post a response if it is not solved.

Leonardo Emili
  • 391
  • 3
  • 7
  • Unless a size of 137 Bytes counts as empty it isn't. –  Apr 14 '20 at 21:09
  • I really think you are missing something when saving the model because yes 137 means there is no data there. In fact, the average size for these documents is ~200MB but can reach even GBs. I suggest you to double-check the save part. – Leonardo Emili Apr 14 '20 at 21:18
  • Ok but since it has worked fine so far and I didnt change a thing. I can only asume that when i force-closed the script with crtl+c it was just executing the torch.save() and somehow it broke (didn't close the file properly). If that is even possible... –  Apr 14 '20 at 21:31
  • Yes, that or probably an error occurred when writing the file. – Leonardo Emili Apr 14 '20 at 21:40