Saving torch models

class VAE(nn.Module):
    def __init__(self):
        super(VAE, self).__init__()

        self.fc1 = nn.Linear(784, 400)

model = VAE()

with open('model_save', 'wb') as f: 
torch.save(model, f)

When I open model_save file, I see the following: mode_save is not UTF encoded. Saving Disabled. See console for more details. And, When I try to use torch.load, I see this error: "module" object has no attribute VAE. Would someone please advise me? Thanks.

I have no idea when are you getting the first error and what does it mean. I’ve never seen that.

About the second thing, the problem with saving modules is that they will break if you move the model code into a different file or directory, because they don’t save the class, but a path where pickle will try to look it up at load time. I’d recommend using state_dict() for serialization and load_state_dict() for reloading the weights.

2 Likes

Thanks for your reply. Should I be pass to torch.save the model object like I did above or VAE which is the class itself? When I save the model object add try to load it again, I get “‘str’ object is not callable” as I fail at "result = map_location(storage, location)" and for the exact same call, sometime I get a different error: " Attribute error" saying that it Couldn’t locate/load the VAE class.

net = Net(...).cuda()
torch.save(net.state_dict(), './net.pth')
net.load_state_dict(torch.load('./net.pth'))
5 Likes

Are you using the map_fn argument of torch.load? Do you remap the location of the storages?

Hey,
The first error about Encoding probably appeared when you tried to open a file (ie model_save) with a format not editable with Jupyter Notebook (as Jupyter outputs this error when trying to edit non-text files)

Indeed, the model is saved in binary format (see source), therefore is not editable, so you get the jupyter corresponding error.
Nevertheless, you still are able to open the model in Python with torch.load, even if it has been recommended to save/load parameters.

1 Like

I am implementing these exact same lines of code, but I get an error.

Code : (On a different computer, with GPU)

model = Classifier()
… training…
torch.save(model.state_dict(), ‘./model_Q2.pth’)

Code: (on my laptop)

model = Classifier()
model.load_state_dict(torch.load(‘./model_Q2.pth’))

Error : module ‘torch._C’ has no attribute ‘_cuda_getDevice’

Note : I trained my model with cuda = True on a virtual environment on a network (with GPUs), but I want to evaluate my model on my laptop.

I believe we need to ‘switch off’ the cuda before saving.

Could you help?

Ok, got it!

model = Classifier()
torch.save(model.state_dict(), ‘./model_Q2.pth’)
model.load_state_dict(torch.load(‘./model_Q2.pth’, map_location=lambda storage, loc: storage))

… map_location=lambda storage, loc: storage) : load the model on CPU.

See this: On a cpu device, how to load checkpoint saved on gpu device