赞
踩
model.parameters()与model.state_dict()都是Pytorch中用于查看网络参数的方法
一般来说,前者多见于优化器的初始化,例如:
后者多见于模型的保存,如:
当我们对网络调参或者查看网络的参数是否具有可复现性时,可能会查看网络的参数
- pretrained_dict = torch.load(yolov4conv137weight)
-
- model_dict = _model.state_dict() #查看模型的权重和biass系数
-
- pretrained_dict = {k1: v for (k, v), k1 in zip(pretrained_dict.items(), model_dict)}
-
- model_dict.update(pretrained_dict) #更新model网络模型的参数的权值和biass,这相当于是一个浅拷贝,对这个更新改变会更改模型的权重和biass
model.state_dict()
其实返回的是一个OrderDict
,存储了网络结构的名字和对应的参数。
例子:
- #encoding:utf-8
-
- import torch
- import torch.nn as nn
- import torch.optim as optim
- import torchvision
- import numpy as mp
- import matplotlib.pyplot as plt
- 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
-
- def main():
- # Initialize model
- model = TheModelClass()
-
- #Initialize optimizer
- optimizer=optim.SGD(model.parameters(),lr=0.001,momentum=0.9)
-
- #print model's state_dict
- print('Model.state_dict:')
- for param_tensor in model.state_dict():
- #打印 key value字典
- 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])
-
-
-
- if __name__=='__main__':
- main()
具体的输出结果如下:可以很清晰的观测到state_dict中存放的key和value的值
- Model.state_dict:
- conv1.weight torch.Size([6, 3, 5, 5])
- conv1.bias torch.Size([6])
- conv2.weight torch.Size([16, 6, 5, 5])
- conv2.bias torch.Size([16])
- fc1.weight torch.Size([120, 400])
- fc1.bias torch.Size([120])
- fc2.weight torch.Size([84, 120])
- fc2.bias torch.Size([84])
- fc3.weight torch.Size([10, 84])
- fc3.bias torch.Size([10])
- Optimizer,s state_dict:
- state {}
- param_groups [{'lr': 0.001, 'momentum': 0.9, 'dampening': 0, 'weight_decay': 0, 'nesterov': False, 'params': [367949288, 367949432, 376459056, 381121808, 381121952, 381122024, 381121880, 381122168, 381122096, 381122312]}]
load_state_dict(state_dict, strict=True)
从 state_dict 中复制参数和缓冲区到 Module 及其子类中
state_dict:包含参数和缓冲区的 Module 状态字典
strict:默认 True,是否严格匹配 state_dict 的键值和 Module.state_dict()的键值
- model = nn.Sequential(self.down1, self.down2, self.down3, self.down4, self.down5, self.neek)
-
- pretrained_dict = torch.load(yolov4conv137weight) #加载已经训练好的模型参数
-
- model_dict = model.state_dict() #查看权重和偏重
-
- # 1. filter out unnecessary keys
- pretrained_dict = {k1: v for (k, v), k1 in zip(pretrained_dict.items(), model_dict)}
-
- # 2. overwrite entries in the existing state dict
- model_dict.update(pretrained_dict) #更新已有的模型的权重和偏重
-
- model.load_state_dict(model_dict) #将更新后的参数重新加载至网络模型中
官方推荐的方法,只保存和恢复模型中的参数
- # save
- torch.save(model.state_dict(), PATH)
-
- # load
- model = MyModel(*args, **kwargs)
- model.load_state_dict(torch.load(PATH))
- model.eval()
torch.load("path路径")表示加载已经训练好的模型
而model.load_state_dict(torch.load(PATH))表示将训练好的模型参数重新加载至网络模型中
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。