Retrain pth model without model structure

According to PyTorch documents, it seems we have to define the model structure before we model.load_state_dict(model_state_dict) .
If we save entire model to pth file and load pth by model = torch.load(PATH), the directory structure should not be changed.

Actually, it seems we have to know model structure if we want to retrain model.

Does it exist any method or any weight file type that we can retrain the PyTorch model without knowing the model structure?

Scripting a model via torch.jit.script and storing it via torch.jit.save should create a model file which you could directly load via torch.jit.load and execute the model.

1 Like

@ptrblck Thanks for replying.

After I load scripted model via model = torch.jit.load('model.pth'), could I directly resume training by model.train()? or I need to save optimizer and loss, and load them while resuming training as well?

If you want to continue the training, you should save the optimizer's state_dict as well (potentially also the learning rate scheduler’s state_dict). However, if you want to re-train or fine-tune the model, you could create a new optimizer and treat the model as a pre-trained one.

1 Like