Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Load and Retrain PyTorch model…

Hi all! Can you help me? I have a question: "How can I retrain a PyTorch model with just a .pt file ?”

I’ve looked at many guides but haven’t found the answer. Everything there has a model class.

I tried to import using torch.load() and others. But it doesn’t work. When I load the model with torch.load() I can't get the parameters.enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

MyModel class I write myself? Doesn’t it have to match the class in .pt?

>Solution :

To retrain a PyTorch model with just a .pt file, you will need to use the PyTorch API to create a new model instance and load the .pt file as the initial weights for the model. This can be done using the torch.load() function to load the .pt file, and then using the model.load_state_dict() method to load the weights into the model.

Here is an example of how this might be done:

import torch

# Create a new model instance
model = MyModel()

# Load the .pt file as the initial weights for the model
weights = torch.load('model.pt')
model.load_state_dict(weights)

# Retrain the model using the loaded weights as the starting point
model.fit(...)

In this example, MyModel is the class for your model, and model.pt is the .pt file containing the initial weights for the model. The model.fit() method is used to retrain the model using the loaded weights as the starting point.

It is important to note that this approach assumes that the .pt file was created using the same model class (MyModel in this example) and that the model architecture has not changed since the .pt file was created. If the model architecture has changed, you will need to make sure that the new model architecture is compatible with the weights in the .pt file, or you may need to use a different approach to retrain the model.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading