参考链接: 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 parameters
that are direct members of this module.
recurse (bool类型) - 如果rescue是True,那么yield迭代返回出这个模块
以及该模块的所有子模块的参数. 否则,如果是False,那么只yield迭代返回
出这个模块的直接成员.
Yields 迭代返回
Parameter – module parameter
Parameter类型 - 模块的参数
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 x
print('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 gradients
self.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):
pass
if __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 = attribute4cjhT
model.attribute4cjhP = attribute4cjhP
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,'-'))
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>