栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【pytorch】pytorch读模型打印参数

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【pytorch】pytorch读模型打印参数

下面的代码包含用途有:

1.训练时多GPU,推理时所有层多出一个module时替换;

2.训练模型出现层的定义不一致时替换;

3.打印训练过程中学习的参数,可视化对应参数的值。

import torch
from collections import OrderedDict
from your_model import Net

# your net architecture
net = Net()
model_path = "your_model_path"

# load model parameters
state_dict = torch.load(model_path, map_location="cpu")
# define a new dict
new_state_dict = OrderedDict()
for k,v in state_dict.items():
    # if you train model with mutil gpu
    if "module" in k:
        name = k[7:]
    else:
        name = k
    # if some layer's name need to change
    if "fc" in name:
        name = name.replace("fc", "classifier")
    # print your layer params
    print("layer : {}, value : {}nn".format(name, v))
    # store the value to new_state_dict
    new_state_dict[name] = v

# load model param to model architecture
net.load_state_dict(new_state_dict)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/325865.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号