How to save Pytorch model (graph + weights) in a single file

I have saved the model using the torch.save(_model, PATH1) function and weights in torch.save(‘model_state_dict’: _model.state_dict(), PATH2).

I want to load the model from another system.

When I load the “_model” I get the error saying that the model architecture is not defined (Can’t get attribute ‘Net’) and had to instantiate the class that creates the model and then load the weights from the checkpoint _model.state_dict in path2.

Is there a way to save both the weights and architecture in a file so that I need not depend on the class definition file, to first create the model object and then load the weights from state dict?

I am looking for a solution similar to what Lua+Torch implementation of torch.save(model, path) does.

@albanD @smth @apaszke @colesbury

Hi,

Unfortunately this is a python limitation. You cannot save the code easily and in a portable way.

You can try to save a torchscript model that you get from the jit if your model can be handled by torchscript.

thanks for clarifying