你能帮我个忙吗?我有一个问题:"How can I retrain a PyTorch model with just a .pt file ?”
我看了很多指南,但都没有找到答案.那里的一切都有一个模范班.
我try 使用torch.load()和其他方法进行导入.但这并不管用.当我用Torch.Load()I can't get the parameters.
enter image description here加载模型时
我自己写的模型类?它不一定要与.pt中的类匹配吗?
你能帮我个忙吗?我有一个问题:"How can I retrain a PyTorch model with just a .pt file ?”
我看了很多指南,但都没有找到答案.那里的一切都有一个模范班.
我try 使用torch.load()和其他方法进行导入.但这并不管用.当我用Torch.Load()I can't get the parameters.
enter image description here加载模型时
我自己写的模型类?它不一定要与.pt中的类匹配吗?
要仅使用.pt文件重新训练一个PyTorch模型,您将需要使用PyTorch API创建一个新的模型实例,并加载.pt文件作为模型的初始权重.这可以使用torch.Load()函数加载.pt文件,然后使用Model.Load_State_dict()方法将权重加载到模型中.
以下是一个如何做到这一点的例子:
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(...)
在本例中,MyModel是您的模型的类,Model.pt是包含模型初始权重的.pt文件.Fit()方法用于使用加载的权重作为起点重新训练模型.
需要注意的是,此方法假定.pt文件是使用相同的模型类(在本例中为MyModel)创建的,并且模型体系 struct 自创建.pt文件以来没有更改.如果模型体系 struct 已更改,则需要确保新模型体系 struct 与.pt文件中的权重兼容,否则可能需要使用不同的方法重新训练模型.