torch.nn.Module.parameters(recurse=True)
参考链接: torch.nn.Module.parameters(recurse=True)
原文及翻译:
parameters(recurse=True)Returns an iterator over module parameters.返回一个迭代器,该迭代器可以遍历模块的参数.This is typically passed to an optimizer.通常用该方法将参数传递给优化器.Parameters 参数recurse (bool) – if True, then yields parameters of this module and all submodules. Otherwise, yields only parametersthat are direct members of this module.recurse (bool类型) - 如果rescue是True,那么yield迭代返回出这个模块以及该模块的所有子模块的参数. 否则,如果是False,那么只yield迭代返回出这个模块的直接成员.Yields 迭代返回Parameter – module parameterParameter类型 - 模块的参数Example: 例子:>>> for param in model.parameters():>>> print(type(param.data), param.size())<class 'torch.FloatTensor'> (20L,)<class 'torch.FloatTensor'> (20L, 1L, 5L, 5L)
代码实验展示:
import torch
import torch.nn as nn
torch.manual_seed(seed=20200910)
class Model(torch.nn.Module):def __init__(self):super(Model,self).__init__()self.conv1=torch.nn.Sequential( # 输入torch.Size([64, 1, 28, 28])torch.nn.Conv2d(1,64,kernel_size=3,stride=1,padding=1),torch.nn.ReLU(), # 输出torch.Size([64, 64, 28, 28])torch.nn.Conv2d(64,128,kernel_size=3,stride=1,padding=1), # 输出torch.Size([64, 128, 28, 28])torch.nn.ReLU(),torch.nn.MaxPool2d(stride=2,kernel_size=2) # 输出torch.Size([64, 128, 14, 14]))self.dense=torch.nn.Sequential( # 输入torch.Size([64, 14*14*128])torch.nn.Linear(14*14*128,1024), # 输出torch.Size([64, 1024])torch.nn.ReLU(),torch.nn.Dropout(p=0.5),torch.nn.Linear(1024,10) # 输出torch.Size([64, 10]) )self.layer4cxq1 = torch.nn.Conv2d(2,33,4,4)self.layer4cxq2 = torch.nn.ReLU()self.layer4cxq3 = torch.nn.MaxPool2d(stride=2,kernel_size=2)self.layer4cxq4 = torch.nn.Linear(14*14*128,1024)self.layer4cxq5 = torch.nn.Dropout(p=0.8)self.attribute4cxq = nn.Parameter(torch.tensor(20200910.0))self.attribute4lzq = nn.Parameter(torch.tensor([2.0,3.0,4.0,5.0])) self.attribute4hh = nn.Parameter(torch.randn(3,4,5,6))self.attribute4wyf = nn.Parameter(torch.randn(7,8,9,10))def forward(self,x): # torch.Size([64, 1, 28, 28])x = self.conv1(x) # 输出torch.Size([64, 128, 14, 14])x = x.view(-1,14*14*128) # torch.Size([64, 14*14*128])x = self.dense(x) # 输出torch.Size([64, 10])return xprint('cuda(GPU)是否可用:',torch.cuda.is_available())
print('torch的版本:',torch.__version__)model = Model() #.cuda()print("测试模型(CPU)".center(100,"-"))
print(type(model))print("测试模型parameters(recurse=True)方法".center(100,"-"))
for param in model.parameters(recurse=True):print(param.shape)print('-'*100)
print("测试模型parameters(recurse=False)方法".center(100,"-"))
for param in model.parameters(recurse=False):print(param.shape)
控制台输出结果:
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。尝试新的跨平台 PowerShell https://aka.ms/pscore6加载个人及系统配置文件用了 1147 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq> conda activate ssd4pytorch1_2_0
(ssd4pytorch1_2_0) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq> & 'D:\Anaconda3\envs\ssd4pytorch1_2_0\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\lib\python\debugpy\launcher' '53281' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\test4cxq\test31.py'
cuda(GPU)是否可用: True
torch的版本: 1.2.0+cu92
---------------------------------------------测试模型(CPU)----------------------------------------------
<class '__main__.Model'>
-----------------------------------测试模型parameters(recurse=True)方法-----------------------------------
torch.Size([])
torch.Size([4])
torch.Size([3, 4, 5, 6])
torch.Size([7, 8, 9, 10])
torch.Size([64, 1, 3, 3])
torch.Size([64])
torch.Size([128, 64, 3, 3])
torch.Size([128])
torch.Size([1024, 25088])
torch.Size([1024])
torch.Size([10, 1024])
torch.Size([10])
torch.Size([33, 2, 4, 4])
torch.Size([33])
torch.Size([1024, 25088])
torch.Size([1024])
----------------------------------------------------------------------------------------------------
----------------------------------测试模型parameters(recurse=False)方法-----------------------------------
torch.Size([])
torch.Size([4])
torch.Size([3, 4, 5, 6])
torch.Size([7, 8, 9, 10])
(ssd4pytorch1_2_0) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq>
代码实验展示:
import torch
import torch.nn as nn
torch.manual_seed(seed=20200910)class Model4CXQ(nn.Module):def __init__(self):super(Model4CXQ, self).__init__()# super().__init__()self.attribute4cxq = nn.Parameter(torch.tensor(20200910.0))self.attribute4lzq = nn.Parameter(torch.tensor(20200.0))# self.attribute4scc = nn.Parameter(torch.Tensor(2.0)) # TypeError: new(): data must be a sequence (got float)# self.attribute4pq = nn.Parameter(torch.tensor(2)) # RuntimeError: Only Tensors of floating point dtype can require gradientsself.attribute4zh = nn.Parameter(torch.Tensor(2))# self.attribute4yzb = nn.Parameter(torch.tensor(912.0))self.attribute4yzb = (torch.tensor(912.0))self.attribute4gcx = (torch.tensor(3))self.attribute4ymw = (torch.Tensor(3))def forward(self, x):passif __name__ == "__main__":model = Model4CXQ()print()print("打印参数".center(50,'-'))for param in model.parameters():print(param)print('调用named_parameters()'.center(100,"-"))for name, param in model.named_parameters():print(name,'-->',param)print("打印字典".center(50,'-'))for k, v in model.state_dict().items():print(k, v)print()print("增加属性".center(50,'-'))attribute4cjhT = torch.Tensor(3)attribute4cjhP = nn.Parameter(torch.Tensor(2))model.attribute4cjhT = attribute4cjhTmodel.attribute4cjhP = attribute4cjhPprint("打印参数".center(50,'-'))for param in model.parameters():print(param)print('调用named_parameters()'.center(100,"-"))for name, param in model.named_parameters():print(name,'-->',param)print("打印字典".center(50,'-'))for k, v in model.state_dict().items():print(k, v)print()print("注册属性".center(50,'-'))attribute4cjhRT = torch.Tensor(3)attribute4cjhRP = nn.Parameter(torch.Tensor(2))# model.register_parameter('attribute4cjhRT', attribute4cjhRT) # 上一行代码报错,注册的应该是Parameter类型,而不是FloatTensor类型# 报错信息:# TypeError: cannot assign 'torch.FloatTensor' object to parameter 'attribute4cjhRT' (torch.nn.Parameter or None required)model.register_parameter('登记注册attribute4cjhRP属性', attribute4cjhRP)print("打印参数".center(50,'-'))for param in model.parameters():print(param)print('调用named_parameters()'.center(100,"-"))for name, param in model.named_parameters():print(name,'-->',param)print("打印字典".center(50,'-'))for k, v in model.state_dict().items():print(k, v)
控制台输出结果:
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。尝试新的跨平台 PowerShell https://aka.ms/pscore6加载个人及系统配置文件用了 979 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq> conda activate ssd4pytorch1_2_0
(ssd4pytorch1_2_0) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq> & 'D:\Anaconda3\envs\ssd4pytorch1_2_0\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\lib\python\debugpy\launcher' '63993' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\test4cxq\test2.py'-----------------------打印参数-----------------------
Parameter containing:
tensor(20200910., requires_grad=True)
Parameter containing:
tensor(20200., requires_grad=True)
Parameter containing:
tensor([0., 0.], requires_grad=True)
----------------------------------------调用named_parameters()----------------------------------------
attribute4cxq --> Parameter containing:
tensor(20200910., requires_grad=True)
attribute4lzq --> Parameter containing:
tensor(20200., requires_grad=True)
attribute4zh --> Parameter containing:
tensor([0., 0.], requires_grad=True)
-----------------------打印字典-----------------------
attribute4cxq tensor(20200910.)
attribute4lzq tensor(20200.)
attribute4zh tensor([0., 0.])-----------------------增加属性-----------------------
-----------------------打印参数-----------------------
Parameter containing:
tensor(20200910., requires_grad=True)
Parameter containing:
tensor(20200., requires_grad=True)
Parameter containing:
tensor([0., 0.], requires_grad=True)
Parameter containing:
tensor([6.5713e+05, 1.5286e-19], requires_grad=True)
----------------------------------------调用named_parameters()----------------------------------------
attribute4cxq --> Parameter containing:
tensor(20200910., requires_grad=True)
attribute4lzq --> Parameter containing:
tensor(20200., requires_grad=True)
attribute4zh --> Parameter containing:
tensor([0., 0.], requires_grad=True)
attribute4cjhP --> Parameter containing:
tensor([6.5713e+05, 1.5286e-19], requires_grad=True)
-----------------------打印字典-----------------------
attribute4cxq tensor(20200910.)
attribute4lzq tensor(20200.)
attribute4zh tensor([0., 0.])
attribute4cjhP tensor([6.5713e+05, 1.5286e-19])-----------------------注册属性-----------------------
-----------------------打印参数-----------------------
Parameter containing:
tensor(20200910., requires_grad=True)
Parameter containing:
tensor(20200., requires_grad=True)
Parameter containing:
tensor([0., 0.], requires_grad=True)
Parameter containing:
tensor([6.5713e+05, 1.5286e-19], requires_grad=True)
Parameter containing:
tensor([0., 0.], requires_grad=True)
----------------------------------------调用named_parameters()----------------------------------------
attribute4cxq --> Parameter containing:
tensor(20200910., requires_grad=True)
attribute4lzq --> Parameter containing:
tensor(20200., requires_grad=True)
attribute4zh --> Parameter containing:
tensor([0., 0.], requires_grad=True)
attribute4cjhP --> Parameter containing:
tensor([6.5713e+05, 1.5286e-19], requires_grad=True)
登记注册attribute4cjhRP属性 --> Parameter containing:
tensor([0., 0.], requires_grad=True)
-----------------------打印字典-----------------------
attribute4cxq tensor(20200910.)
attribute4lzq tensor(20200.)
attribute4zh tensor([0., 0.])
attribute4cjhP tensor([6.5713e+05, 1.5286e-19])
登记注册attribute4cjhRP属性 tensor([0., 0.])
(ssd4pytorch1_2_0) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq>
torch.nn.Module.parameters(recurse=True)相关推荐
- pytorch nn.Module.parameters
返回模型的parameters的迭代对象. 1.应用 >>> for param in model.parameters(): >>> print(type(par ...
- pytorch中的神经网络模块基础类——torch.nn.Module
1.torch.nn.Module概要 pytorch官网对torch.nn.Module的描述如下. torch.nn.Module是所有的神经网络模块的基类,且所有的神经网络模块都可以包含其他的子 ...
- torch.nn.Module()
torch.nn.Module() 如果自己想研究,官方文档 它是所有的神经网络的根父类! 你的神经网络必然要继承! 模块也可以包含其他模块,允许将它们嵌套在树结构中.所以呢,你可以将子模块指定为常规 ...
- pytorch torch.nn.Module
应用 >>> for name, param in self.named_parameters(): >>> if name in ['bias']: >&g ...
- pytorch torch.nn.Module.register_buffer
API register_buffer(name: str, tensor: Optional[torch.Tensor], persistent: bool = True) → None 注册buf ...
- 深度之眼 PyTorch 训练营第 4 期(5):构建模型 torch.nn.Module
本文中,我们看一看如何构建模型. 创造一个模型分两步:构建模型和权值初始化.而构建模型又有"定义单独的网络层"和"把它们拼在一起"两步. 1. torch.nn ...
- Pytorch的自定义拓展:torch.nn.Module和torch.autograd.Function
参考链接:pytorch的自定义拓展之(一)--torch.nn.Module和torch.autograd.Function_LoveMIss-Y的博客-CSDN博客_pytorch自定义backw ...
- Pytorch:模型的保存与加载 torch.save()、torch.load()、torch.nn.Module.load_state_dict()
Pytorch 保存和加载模型后缀:.pt 和.pth 1 torch.save() [source] 保存一个序列化(serialized)的目标到磁盘.函数使用了Python的pickle程序用于 ...
- torch.nn.Module.named_parameters ()
参考 torch.nn.Module.named_parameters () - 云+社区 - 腾讯云 named_parameters(prefix='', recurse=True)[source ...
- torch.nn.Module.half()
链接: torch.nn.Module.half() 原文及翻译: half() 方法: half()Casts all floating point parameters and buffers t ...
最新文章
- quartz集群调度机制调研及源码分析---转载
- 史上最全的机器学习资料(上)
- Spring MVC 启动
- UEditor编辑器第一次赋值失败的解决方法
- 【Java】Socket多客户端Client-Server聊天程序
- 机器学习均方误差_机器学习:均方误差和回归线简介
- 使用缓冲字符流BufferedReader和文件字符流FileReader读取文本文件
- 高端大气星级酒店展示网站静态模板
- socket 服务器端和客户端通信,面向TCP的
- 每次离开总是 装做轻松的样子 微笑着说回去吧 转身泪湿眼底
- lol服务器维护是拳头,问拳头-英雄联盟官方网站-腾讯游戏
- 收藏的一些有意思的网站
- sticky你了解多少
- 18 | 需求管理:太多人给你安排任务,怎么办?
- jmeter性能测试之分布式
- 计算机考研和就业pk,考研PK就业:提高自身竞争力比文凭更重要
- 练习:罗马数字转整数
- android sqlite #039;,问题详情_百度云推送_免费专业最精准的移动推送服务平台
- 自然数学-对数性质证明
- oracle等待事件4——buffer busy wait 特别介绍
热门文章
- 微信小程序tabbar 小程序自定义 tabbar怎么做
- 传统蓝牙协议栈 串口协议SPP(Serial Port Profile)概念介绍
- C/C++实现关闭命令行快速编辑模式(Windows系统)
- android 来电滑动接听电话,安卓手机来电时不能滑动接听现象的原因及解决方法介绍...
- 【Spark学习】Spark思维导图(超详细!)
- oracle 定时备份数据库脚本
- 最新微信养号、使用、解封必看的注意事项
- adb指令执行电脑关机
- 安卓手机无线投屏电脑 三种方法轻松搞
- 软件架构设计说明书该怎么写?