【问题标题】:Unable to save Pytorch model to Google Drive in Google Colab?无法在 Google Colab 中将 Pytorch 模型保存到 Google Drive?
【发布时间】:2019-08-30 22:58:40
【问题描述】:

我正在尝试将我的模型保存到我在 google colab 上的驱动器中。我已使用以下代码挂载我的 Google Drive-

from google.colab import drive
drive.mount('/content/gdrive')

在完成所有预处理、模型定义和训练之后,我想将我的模型保存到驱动器中,因为训练需要很长时间。所以,我会保存它以定期驱动并从那时起重新加载以继续。 保存我的模型的代码是:

def save_model(model, model_name, iter):
  path = f'content/gdrive/My Drive/Machine Learning Models/kaggle_jigsaw_{model_name}_iter_{iter}.pth'
  print(f'Saving {model_name} model...')
  torch.save(model.state_dict(), path)
  print(f'{model_name} saved successfully.')

EMBEDDING_DIMS = 128
HIDDEN_SIZE = 256

gru = GRU(vocab.n_words, EMBEDDING_DIMS, HIDDEN_SIZE, 2).to(device)
save_model(gru, 'gru', 0)

我收到以下错误:

Saving gru model...
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-27-d2510611a9d4> in <module>()
      9 
     10 gru = GRU(vocab.n_words, EMBEDDING_DIMS, HIDDEN_SIZE, 2).to(device)
---> 11 save_model(gru, 'gru', 0)

<ipython-input-27-d2510611a9d4> in save_model(model, model_name, iter)
      2   path = f'content/gdrive/My Drive/Machine Learning Models/kaggle_jigsaw_{model_name}_iter_{iter}.pth'
      3   print(f'Saving {model_name} model...')
----> 4   torch.save(model.state_dict(), path)
      5   print(f'{model_name} saved successfully.')
      6 

/usr/local/lib/python3.6/dist-packages/torch/serialization.py in save(obj, f, pickle_module, pickle_protocol)
    217         >>> torch.save(x, buffer)
    218     """
--> 219     return _with_file_like(f, "wb", lambda f: _save(obj, f, pickle_module, pickle_protocol))
    220 
    221 

/usr/local/lib/python3.6/dist-packages/torch/serialization.py in _with_file_like(f, mode, body)
    140             (sys.version_info[0] == 3 and isinstance(f, pathlib.Path)):
    141         new_fd = True
--> 142         f = open(f, mode)
    143     try:
    144         return body(f)

FileNotFoundError: [Errno 2] No such file or directory: 'content/gdrive/My Drive/Machine Learning Models/kaggle_jigsaw_gru_iter_0.pth'

我在我的驱动器中手动创建了文件夹,只需要创建文件。尽管如此,错误仍然存​​在。不过,我确信不需要手动创建文件夹。问题是别的。 我哪里错了?

【问题讨论】:

    标签: python-3.x google-drive-api pytorch google-colaboratory


    【解决方案1】:

    您无法将文件直接保存到已安装的云端硬盘。它不像常规文件系统那样工作。尝试使用名为PyDriveCoUtils 的工具,它基于PyDrive,专为Google Colab 设计:Working with Google Drive

    【讨论】:

    • 这不太正确——当在原始问题中使用 drive.mount 时,驱动器文件作为 FUSE 文件系统挂载,在 Colab 后端作为常规文件系统出现。
    • @BobSmith 这很有趣。但仍然不支持文件复制或移动。只能读写文件,对吗?
    • 支持复制和移动。
    【解决方案2】:

    您的路径中可能需要一个前导 /

    尝试更改此行:

      path = f'content/gdrive/My Drive/Machine Learning Models/kaggle_jigsaw_{model_name}_iter_{iter}.pth'
    

    到:

      path = f'/content/gdrive/My Drive/Machine Learning Models/kaggle_jigsaw_{model_name}_iter_{iter}.pth'
    

    【讨论】:

      【解决方案3】:

      我不知道为什么,但现在它可以正常工作了。我仍然想知道为什么首先会出现这个问题。

      【讨论】:

        猜你喜欢
        • 2020-06-19
        • 2021-01-30
        • 2021-02-24
        • 1970-01-01
        • 2018-09-27
        • 2022-01-27
        • 2022-08-20
        • 2022-07-20
        • 1970-01-01
        相关资源
        最近更新 更多