Saving and loading model in colab (google drive)

pedram1: <p>Hi there</p>
<p>is anyone know how I can save and load a trained model in colab?<br>
actually when I used suggested procedure for saving the model, it worked and I see my model in google drive, however when I am going to load it I faced with the below problem:</p>
<p>[Errno 2] No such file or directory: ‘/content/gdrive/My Drive/autoencoder 18 Dec 2019.pt’</p>

jmandivarapu1: <p>try checking the files exist in the directory</p>
<pre><code class="lang-auto">import torch
import torch.nn.functional as F
import torch.optim as optim
import torch.backends.cudnn as cudnn
import torch.backends.cudnn as cudnn
import numpy as np
import torch.nn as nn
import os

class LeNet_5_Caffe(nn.Module):
    """LeNet-5 without padding in the first layer.
    This is based on Caffe's implementation of Lenet-5 and is slightly different
    from the vanilla LeNet-5. Note that the first layer does NOT have padding
    and therefore intermediate shapes do not match the official LeNet-5.

    Based on https://github.com/mi-lad/snip/blob/master/train.py
    by Milad Alizadeh.
    """

    def __init__(self, save_features=None, bench_model=False):
        super().__init__()
        self.conv1 = nn.Conv2d(1, 10, 5, padding=0, bias=True)
        self.conv2 = nn.Conv2d(10, 20, 5, bias=True)
        self.fc1 = nn.Linear(320, 50)
        self.fc2 = nn.Linear(50, 10)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        x = F.max_pool2d(x, 2)
        x = F.relu(self.conv2(x))
        x = F.max_pool2d(x, 2)
        x = F.relu(self.fc1(x.view(-1, 320)))
        x = F.log_softmax(self.fc2(x), dim=1)


        return x

model_10=LeNet_5_Caffe()

torch.save(model_10.state_dict(), 'abc.pt')
print(os.getcwd())
print(os.listdir())
model_10.load_state_dict(torch.load('abc.pt'))
</code></pre>
<p>output of print statement is</p>
<pre><code class="lang-auto">/content ['.config', 'abc.pt', 'sample_data']

&lt;All keys matched successfully&gt;
``</code></pre>

pedram1: <p>Hi</p>
<p>Thank you for your quick response, actually I was wondered more to know about loading a model?<br>
I would like to know, when I trained a model in Google Colab, by just saving the model, cn I again reload that model in another python file (colab also) and just feed it with some data and see the results, without again defining the model ?</p>

jmandivarapu1: <aside class="quote no-group" data-username="pedram1" data-post="3" data-topic="64463">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pedram1/48/21032_2.png" class="avatar"> pedram1:</div>
<blockquote>
<p>ctually I was wondered more to know about loading a model?<br>
I would like to know, when I trained a model in Google Colab, by just saving the model, cn I again reload that model in another python file (colab also) and just feed it with some data and see the results, without again defining the model ?</p>
</blockquote>
</aside>
<p>Unfortunately you can’t do that. You need to reinitialize the model with any weights and load the weights.<br>
Because for loading the weights you need to have Network with architecture defined</p>

DeepTorch: <p><a class="mention" href="/u/jmandivarapu1">@jmandivarapu1</a> I had the model trained and saved on Google Colab but when I try to load the model the next day, it outputs an error stating that there is no such file. Is there something else I missed?</p>