pytorch获取模型某一层参数名及参数值方式
作者:HuiYu-Li 发布时间:2022-07-03 06:10:28
标签:pytorch,模型,参数名,参数值
1、Motivation:
I wanna modify the value of some param;
I wanna check the value of some param.
The needed function:
2、state_dict() #generator type
model.modules()#generator type
named_parameters()#OrderDict type
from torch import nn
import torch
#creat a simple model
model = nn.Sequential(
nn.Conv3d(1,16,kernel_size=1),
nn.Conv3d(16,2,kernel_size=1))#tend to print the W of this layer
input = torch.randn([1,1,16,256,256])
if torch.cuda.is_available():
print('cuda is avaliable')
model.cuda()
input = input.cuda()
#打印某一层的参数名
for name in model.state_dict():
print(name)
#Then I konw that the name of target layer is '1.weight'
#schemem1(recommended)
print(model.state_dict()['1.weight'])
#scheme2
params = list(model.named_parameters())#get the index by debuging
print(params[2][0])#name
print(params[2][1].data)#data
#scheme3
params = {}#change the tpye of 'generator' into dict
for name,param in model.named_parameters():
params[name] = param.detach().cpu().numpy()
print(params['0.weight'])
#scheme4
for layer in model.modules():
if(isinstance(layer,nn.Conv3d)):
print(layer.weight)
#打印每一层的参数名和参数值
#schemem1(recommended)
for name,param in model.named_parameters():
print(name,param)
#scheme2
for name in model.state_dict():
print(name)
print(model.state_dict()[name])
来源:https://blog.csdn.net/weixin_44058333/article/details/92691656
0
投稿
猜你喜欢
- 合并两个没有共同列的dataframe,相当于按行号求笛卡尔积。最终效果如下以下代码是参考别人的代码修改的:def cartesian_df
- 1.在本地建立项目 可使用Eclipse,Idea等开发工具创建项目 打开根目录到所在在工程名的下一级2.使用git 客户端 进
- %有哪几种含义?查找手册翻看《The Python Libary Reference》python库指南中附录index部分(P1899):
- 前言当多线程访问同一个公共资源时,如果涉及到修改该公共资源的操作就可能会出现由于数据不同步导致的线程安全问题。一般情况下我们可以通过给公共资
- pip install指定安装目录pip install一般会有默认的安装目录,可以通过python -m site进行查看,当然也可以对默
- 一、面相对象继承机制 这个实例使用UML很好的解释了继承机制。 &nb
- Online人们搜索、浏览、阅读、跟帖、互动投票,玩游戏,甚至在线买卖交易。这些行为都是“用户体验”,听起来用户体验似乎无所不能,造成了互联
- 前言记得刚入门那个时候,自己处理编码转换问题往往是“百度:url解码、base64加密、hex……”,或者是使用一款叫做“小葵多功能转换工具
- 上一文,介绍了vue.js动态添加、删除绑定的radio选项,本文介绍如何选中radio的某一项绑定的数据和上文的model是一致的,选中r
- 前述VARCHAR和CHAR是两种最主要的字符串类型。不幸的是,很难精确地解释这些值是怎么存储在磁盘和内存中的,因为这跟存储引擎的具体实现有
- 技术在进步,思维在发展,网页上的花样当然也要一天天地赶时髦了。在“滚动字符”、“跑马灯”已成平常的今天,网页上还能变出新花样吗?◆制作鼠标指
- 本文实例为大家分享了linux采用binary方式安装mysql的具体步骤,供大家参考,具体内容如下1、下载binary文件在官网上下载 m
- jquery的ajax请求方法:$.ajax({ &nbs
- 首先从 ueEditor官网 下载最新版本的包,目前官网上提供了ASP、.NET、PHP、JSP版本的,django版本只有一个第三方个人开
- Python “TypeError: unhashable type: ‘dict&rsqu
- 今晚想实现这样一个功能:将输入字符串中的字母 “i” 变成字母 “p”。当时想的很简单,直接用for循环遍历,然后替代,出问题的代码如下:n
- 目录一、前言二、Json.loads与eval 性能对比1. eval2. json.loads一、前言最近发现一些小伙伴使用eval来处理
- 时间序列是指在一段时间内发生的任何可量化的度量或事件。尽管这听起来微不足道,但几乎任何东西都可以被认为是时间序列。一个月里你每小时的平均心率
- 一、如何将列表数据写入文件 ⾸先,我们来看看下⾯这段代码,并思考:这段代码有没有问题,如果有问题的话,要怎么改?li = [
- Python 环境安装 下载 Python 安装包进入 python 官网 ,在Downloads(下载)下面,点击 Window 进入下载