is anyone know how I can save and load a trained model in colab?
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:
[Errno 2] No such file or directory: ‘/content/gdrive/My Drive/autoencoder 18 Dec 2019.pt’
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'))
Thank you for your quick response, actually I was wondered more to know about loading a model?
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 ?
Unfortunately you can’t do that. You need to reinitialize the model with any weights and load the weights.
Because for loading the weights you need to have Network with architecture defined
@jmandivarapu1 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?