pytorch升级打怪(八)

保存模型和加载已有模型

保存并加载模型

在本节中,我们将研究如何通过保存、加载和运行模型预测来保持模型状态。

import torch
import torchvision.models as models

保存

PyTorch模型将学习的参数存储在内部状态字典中,称为state_dict。这些可以通过thetorchtorch.save方法持久化:

model = models.vgg16(weights='IMAGENET1K_V1')
torch.save(model.state_dict(), 'model_weights.pth')

加载

要加载模型权重,您需要先创建同一模型的实例,然后使用load_state_dict()方法加载参数。

model = models.vgg16() # we do not specify ``weights``, i.e. create untrained model
model.load_state_dict(torch.load('model_weights.pth'))
print(model.eval())

```shell
VGG(
  (features): Sequential(
    (0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (1): ReLU(inplace=True)
    (2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (3): ReLU(inplace=True)
    (4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (5): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (6): ReLU(inplace=True)
    (7): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (8): ReLU(inplace=True)
    (9): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (10): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (11): ReLU(inplace=True)
    (12): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (13): ReLU(inplace=True)
    (14): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (15): ReLU(inplace=True)
    (16): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (17): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (18): ReLU(inplace=True)
    (19): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (20): ReLU(inplace=True)
    (21): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (22): ReLU(inplace=True)
    (23): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (24): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (25): ReLU(inplace=True)
    (26): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (27): ReLU(inplace=True)
    (28): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (29): ReLU(inplace=True)
    (30): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (avgpool): AdaptiveAvgPool2d(output_size=(7, 7))
  (classifier): Sequential(
    (0): Linear(in_features=25088, out_features=4096, bias=True)
    (1): ReLU(inplace=True)
    (2): Dropout(p=0.5, inplace=False)
    (3): Linear(in_features=4096, out_features=4096, bias=True)
    (4): ReLU(inplace=True)
    (5): Dropout(p=0.5, inplace=False)
    (6): Linear(in_features=4096, out_features=1000, bias=True)
  )
)

相关推荐

  1. pytorch升级

    2024-03-19 12:34:05       19 阅读
  2. pytorch升级(一)

    2024-03-19 12:34:05       22 阅读
  3. pytorch升级(二)

    2024-03-19 12:34:05       16 阅读
  4. pytorch升级(四)

    2024-03-19 12:34:05       20 阅读
  5. pytorch升级(七)

    2024-03-19 12:34:05       19 阅读
  6. pytorch升级(五)

    2024-03-19 12:34:05       18 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-19 12:34:05       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-19 12:34:05       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-19 12:34:05       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-19 12:34:05       18 阅读

热门阅读

  1. 机器视觉系统选型-选型&标定&通信

    2024-03-19 12:34:05       19 阅读
  2. 一个经典的wordpress产品列表模板含CSS样式

    2024-03-19 12:34:05       19 阅读
  3. 将Excel转为PDF、PDF添加图片

    2024-03-19 12:34:05       18 阅读
  4. excel是编程语言?

    2024-03-19 12:34:05       18 阅读
  5. C语言经典面试题目(十五)

    2024-03-19 12:34:05       17 阅读
  6. mysql 常见问题

    2024-03-19 12:34:05       15 阅读
  7. 人工智能需要的数学基础有哪些?

    2024-03-19 12:34:05       19 阅读