4

I am using Python and Keras on top of Tensorflow to train my neural networks. When I switched from Ubuntu 16.04 to Windows 10, my model could not be saved anymore when I run the following:

filepath = "checkpoint-"+str(f)+model_type+"-"+optimizer_name+"-{epoch:02d}-{loss:.3f}.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='min')
callbacks_list = [checkpoint]

and later on:

model.fit(X, y,
      batch_size=128,
      epochs=1,
      shuffle=False,
      callbacks=callbacks_list)

I get this Error:

OSError: Unable to create file (Unable to open file: name = 'checkpoint-<_io.textiowrapper name='data/swing-projects100-raw/many-chunks/log-gamma-f3.txt' mode='a' encoding='cp1252'>2l128-adam-0.001-{epoch:02d}-{loss:.3f}.h5', errno = 22, error message = 'invalid argument', flags = 13, o_flags = 302)

I have Keras 2.0.8 and h5py 2.7.0 installed via conda.

I tried

filepath = "checkpoint-"+str(f)+model_type+"-"+optimizer_name+"-{epoch:02d}-{loss:.3f}.hdf5"

with open(filepath, "w") as f:
  f.write("Test.")

and got a similar error:

OSError: [Errno 22] Invalid argument: "checkpoint-<_io.TextIOWrapper name='data/swing-projects100-raw/many-chunks/log-gamma-f3.txt' mode='a' encoding='cp1252'>2L128-Adam-0.001-{epoch:02d}-{loss:.3f}.hdf5"

AIpeter
  • 868
  • 1
  • 7
  • 17
  • 1
    i'd suggest trying to save any simple text file with that filepath: f=open(filepath,'w') – maz Sep 10 '17 at 13:35
  • @maz Please have a look at my edited question. – AIpeter Sep 10 '17 at 13:57
  • https://stackoverflow.com/questions/25584124/oserror-errno-22-invalid-argument-when-use-open-in-python suggests that `filepath` is an invalid filename. If you'd like help determining how to make `filepath` into a valid filename, please add the value of `filepath` that is resulting in the error to the question. – Seth Difley Sep 10 '17 at 14:04
  • @SethDifley You can see the value of `filepath` in my question before the first and the second error message. My path is a relative one and I don't know what's wrong with it. – AIpeter Sep 10 '17 at 14:27
  • 1
    @AIpeter don't you want to be calling `.format` on that string to put some values in? Braces and colons etc... are valid characters for common linux file systems, so you'll literally getting a file containing "{epoch:02d}" in its name - on Windows - those are not valid characters for a filename.. – Jon Clements Sep 10 '17 at 14:49
  • @JonClements This wasn't the reason for my error. You can do it this way in Keras. Have a look at my answer. – AIpeter Sep 10 '17 at 14:53

3 Answers3

4

When I removed str(f) from the filepath, it worked. f is an Integer and I don't know why it caused the error, but removing it from the string solved my problem.

Let me know if you know exactly why.

AIpeter
  • 868
  • 1
  • 7
  • 17
1

I have the similar problem when using tensorflow on a distant machine.

the reason of my maybe 'have no permission to modify the file'.

I solve this problem by use the save path like "../model.h5"———the folder where you have permission.

may this helps someone.

Song
  • 351
  • 3
  • 7
0

I had a similar problem with this code:

agent.save("./saved_models/weights_episode_{}.h5".format(e))

I solved it by manually creating the folder saved_models

e being an integer did not cause any problems in my case.

charelf
  • 3,103
  • 4
  • 29
  • 51