Save and Load model

here i save my model:


and now i am stuck here

This code works:

x = torch.randn(1, 3, 64, 64) # (batch size or #of images,channels RGB,width,height)
model = Net()
output = model(x)
torch.save({'state_dict': model.state_dict()}, 'tmp.pt')

model = Net()
state_dict = torch.load('tmp.pt')['state_dict']
model.load_state_dict(state_dict)
print(model)

Could you try to find differences between your code and mine?

you mean first i save my model according to ur code and then load it?

I assume you have already saved your model, so my code is just to show that initializing the model and loading the state_dict does work and the error might still be somewhere else in your code.

If a pretrained model is saved as Dataparallel can it be loaded as nn.Sequential model?

nn.DataParallel stores the actual model in the .module attribute.
If you store the state_dict from the model.module, you’ll be able to directly use it without wrapping the model into nn.DataParallel.

what are the advantages and disadvantages of just using the python pickle

@ptrblck what are the advantages and disadvantages of just using the python pickle module vs the load/save stuff pytorch has built?


Internally pickle (or a custom pickle module you provide) will be used, so you could also manually store the data most likely.
However, the _save method will apply a specific format to load it properly.

I’ll answer in the other threads separately, but I would recommend to keep “similar” questions in one place. :wink:

2 Likes

I’m trying! That’s why I try to reference questions between each other but I don’t have control that other people ask repeated questions :frowning: I will try to not make this issue worse. Appreciate the advice. :slight_smile:

Hi Ptrblck,

I want to save my data which are 2000x11x11x11 tensor. I used
torch.save(TargetGAN,Path, _use_new_zipfile_serialization=False)

but gave me this error :

TypeError: save() got an unexpected keyword argument ‘_use_new_zipfile_serialization’

I need to use _use_new_zipfile_serialization=False, otherwise could not load the data on the GPU and will consider it as a zip file which I don’t want.

Many thanks

This argument as added in PyTorch 1.4.0, so you might want to update your Pytorch version, if you want to use it.

1 Like