Save PyTorch model architecture with `torch.save()`

How can I save the architecture of a model in PyTorch so I can apply different tweaks to it?

Rather than copying the whole class definition each time, is there a better way to save the architecture of a model in PyTorch?

torch.save(model.state_dict(), FILE)
or
torch.save(model, FILE)

These methods do not save the architecture of a model, unlike with Tensorflow’s .pb file. Is there a better way than copying the whole class definition each time to save the architecture of a model in PyTorch?

Yes, you can save the architecture of a model in PyTorch by using the torch.save() function on the model’s state_dict(). This will save the parameters of the model, including the architecture. Here is an example:

torch.save(model.state_dict(), 'model.pth')

To load the saved model architecture, you can create a new instance of the model and load the saved state dictionary using the torch.load() function. Here is an example:

model = MyModelClass()
model.load_state_dict(torch.load('model.pth'))

This way, you can apply different tweaks to the model without having to redefine the architecture each time.