PyTorch Models

Overview

pth模型保存时是按照“整个模型保存”和“只保存模型参数”会影响模型的加载和访问方式

torch.save(vgg16, "vgg16.pt") 
torch.save(vgg16,"vgg16.ckpt") 
torch.save(vgg16,"vgg16.pth") 
torch.save(vgg16,"vgg16.pkl")

Save & Load Models

保存整个模型

torch.save(model, PATH)
import torch
if __name__ == '__main__':
    model_pth = r'D:\${modelPath}\${modelName}.pth'
    net = torch.load(model_pth, map_location=torch.device('cpu'))
    for key, value in net["state_dict"].items():
        print(key,value.size(),sep="  ")

只保存模型参数

torch.save(net.state_dict(),path2)
model.load_state_dict(torch.load(path2))

你可能感兴趣的:(MachineLearning,pytorch,人工智能,python)