Unable to save Pytorch model to Google Drive in Google Colab?
I am trying to save my model to my drive on google colab. I have used the following code to mount my Google Drive-

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


After all the preprocessing, model definition and training, I want to save my model to the drive because training it will take a long time. So, I will save it to drive at regular intervals and reload from that point to continue.
The code to save my model is:

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)


I am getting the following error:

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

&lt;ipython-input-27-d2510611a9d4&gt; 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...')
----&gt; 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         &gt;&gt;&gt; torch.save(x, buffer)
    218     """
--&gt; 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
--&gt; 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'


I have manually created the folder in my drive and only the file needs to be created. Still, the error persists. Though, I am sure that manually creating the folder was not required. The problem is something else.
Where am I going wrong?

You likely need a leading / in your path.

Try changing this line:

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


to:

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

