❖PyTorch SAVING AND LOADING MODELS

和保存模型相关的三个APi是:

torch.save

torch.load

torch.nn.Module.load_state_dict

================================

WHAT IS A STATE_DICT?

在pytorch中,torch.nn.Module model 中可学习的参数(权重和偏置)可以通过model.parameters()来获得。

state_dict是一个Python字典对象用来装每一层参数tensor

注意到,在state_dict中只有可学习参数的层(卷积层和线性变换层)有这些个元素

Optimizer objects (torch.optim) 也有一个state_dict用来保存优化器的状态信息,包括超参数

import torch.nn as nn
from torch import optim
import torch.nn.functional as F
# Define model
class TheModelClass(nn.Module):
    def __init__(self):
        super(TheModelClass, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

# Initialize model
model = TheModelClass()

# Initialize optimizer
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)

# Print model's state_dict
print("Model's state_dict:")
for param_tensor in model.state_dict():
    print(param_tensor, "\t", model.state_dict()[param_tensor].size())

# Print optimizer's state_dict
print("Optimizer's state_dict:")
for var_name in optimizer.state_dict():
    print(var_name, "\t", optimizer.state_dict()[var_name])

    原文作者:胡今朝
    原文地址: https://zhuanlan.zhihu.com/p/53419702
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞