0

I've been trying to save the state_dict of a Pytorch model with
torch.save(agent.qnetwork_local.state_dict(), filename) where
filename = datetime.now().strftime('%d-%m-%y-%H:%M_dqnweights.pth')

type(filename) returns str which shouldn't be a problem with torch.save() and it should output a non-empty file. Instead I get an empty file with just the date and time and nothing after that. Putting the date and in the middle of the filename results in an empty file with everything after the date and time cut off.

torch.save(agent.qnetwork_local.state_dict(), 'checkpoint1.pth') and any time I hardcode the string works and gives me the expected non-empty file.

What is going on and how do I fix this?

I am running this code in a Python v3.6.8 virtualenv with Pytorch v1.8.1+cpu on Windows 10.

EdisonMaxwell
  • 121
  • 1
  • 6
  • What is your OS? Windows? – Berriel Jun 13 '21 at 14:01
  • Yes WIndows 10. Editing the question now. – EdisonMaxwell Jun 13 '21 at 17:05
  • I think colon is not allowed in Windows filenames. Try without it. – Berriel Jun 13 '21 at 18:57
  • It worked. I had presumed that any filename problems would just throw an error instead of saving an empty file. Is there a exception or some other mechanism that I can use to prevent writing the file if it is incompatible with the conventions of the operating system the program is running on? – EdisonMaxwell Jun 16 '21 at 15:09
  • Nothing that is built in, AFAIK. Take a look at: https://stackoverflow.com/questions/295135/turn-a-string-into-a-valid-filename – Berriel Jun 16 '21 at 15:49

1 Answers1

0

The colon was the problem in filename = datetime.now().strftime('%d-%m-%y-%H:%m_dqnweights.pth') since it was running on windows.

Changing it to filename = datetime.now().strftime('%d-%m-%y-%H_%M_dqnweights.pth') works as expected.

EdisonMaxwell
  • 121
  • 1
  • 6