Advertisement

Pytorch学习(十七)--- 模型加载问题解析

阅读量:

简单的模型load

通常情况下,模型的存储过程涉及将所有参数通过model.cpu().state_dict()进行保存,而在模型加载阶段,则普遍采用model.load_state_dict(torch.load(model_path))的方式完成。需要特别指出的是,torch.load函数所返回的数据结构本质上是一个OrderedDict对象。

复制代码
    import torch
    import torch.nn as nn
    
    class Net_old(nn.Module):
    def __init__(self):
        super(Net_old, self).__init__()
        self.nets = nn.Sequential(
            torch.nn.Conv2d(1, 2, 3),
            torch.nn.ReLU(True),
            torch.nn.Conv2d(2, 1, 3),
            torch.nn.ReLU(True),
            torch.nn.Conv2d(1, 1, 3)
        )
    def forward(self, x):
        return

全部评论 (0)

还没有任何评论哟~