Python 无法在Google Colab中将ML模型保存到Google驱动器

Python 无法在Google Colab中将ML模型保存到Google驱动器,python,tensorflow,machine-learning,google-drive-api,google-colaboratory,Python,Tensorflow,Machine Learning,Google Drive Api,Google Colaboratory,下面是我用来保存模型的函数。文件夹“models”不存在。但是它不应该创建一个文件夹“models”并将模型保存为文件名。我在任何地方都找不到解决办法 # Create a function to save a model def save_model(model, suffix=None): """ Saves a given model in a models directory and appends a suffix (str) for clarity and reuse.

下面是我用来保存模型的函数。文件夹“models”不存在。但是它不应该创建一个文件夹“models”并将模型保存为文件名。我在任何地方都找不到解决办法

# Create a function to save a model
def save_model(model, suffix=None):
  """
  Saves a given model in a models directory and appends a suffix (str)
  for clarity and reuse.
  """
  # Create model directory with current time
  modeldir = os.path.join("/content/drive/My Drive/Dog Eyes/models/",
                          datetime.datetime.now().strftime("%Y%m%d-%H%M%s"))
  model_path = modeldir + "-" + suffix + ".h5" # save format of model
  print(f"Saving model to: {model_path}...")
  model.save(model_path)
  return model_path
我调用函数的方式如下

# Save our model trained on 1000 images
save_model(model, suffix="1000-images-mobilenetv2-Adam")
这是我在执行函数时遇到的错误

Unable to create file (unable to open file: name = '/content/drive/My Drive/Dog Eyes/models/20200306-18501583520636-1000-images-mobilenetv2-Adam.h5', errno = 2, error message = 'No such file or directory', flags = 13, o_flags = 242)
完整的错误消息



Saving model to: /content/drive/My Drive/Dog Eyes/models/20200306-18501583520636-1000-images-mobilenetv2-Adam.h5...

---------------------------------------------------------------------------

OSError                                   Traceback (most recent call last)

<ipython-input-62-d56ea448536c> in <module>()
----> 1 save_model(model, suffix="1000-images-mobilenetv2-Adam")

5 frames

/tensorflow-2.1.0/python3.6/h5py/_hl/files.py in make_fid(name, mode, userblock_size, fapl, fcpl, swmr)
    177         fid = h5f.create(name, h5f.ACC_EXCL, fapl=fapl, fcpl=fcpl)
    178     elif mode == 'w':
--> 179         fid = h5f.create(name, h5f.ACC_TRUNC, fapl=fapl, fcpl=fcpl)
    180     elif mode == 'a':
    181         # Open in append mode (read/write).

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

h5py/h5f.pyx in h5py.h5f.create()

OSError: Unable to create file (unable to open file: name = '/content/drive/My Drive/Dog Eyes/models/20200306-18501583520636-1000-images-mobilenetv2-Adam.h5', errno = 2, error message = 'No such file or directory', flags = 13, o_flags = 242)



文件夹“models”未创建,这似乎是问题所在。手动创建“models”文件夹将解决此问题,并且模型将保存在正确的路径中