参考链接: torch.nn.Module.named_parameters(prefix=’’, recurse=True)
原文及翻译:
named_parameters(prefix='', recurse=True)
方法:named_parameters(prefix='', recurse=True)
Returns an iterator over module parameters, yielding both
the name of the parameter as well as the parameter itself.
返回一个迭代器,该迭代器能够遍历模块的参数,迭代返回的是参数的名字和参数本身.
Parameters 参数
prefix (str) – prefix to prepend to all parameter names.
prefix (字符串类型) – 添加到所有参数名字上的前缀.
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 (布尔类型) – 如果是True,那么迭代返回这个模块以及这
个模块的所有子模块的参数.否则如果是False,迭代返回此模块的直
接属性成员的参数.
Yields 迭代返回
(string, Parameter) – Tuple containing the name and parameter
(字符串, 参数) – 包含参数名和参数自身的元组
Example: 例子:
>>> for name, param in self.named_parameters():
>>> if name in ['bias']:
>>> print(param.size())
代码实验展示:
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("测试模型named_parameters(prefix='', recurse=True)方法".center(100,"-"))
for name, param in model.named_parameters(prefix='', recurse=True):
print('参数名字是:', name, '参数形状是:', param.shape)
print('-'*100)
print("测试模型named_parameters(prefix='铁凡', recurse=True)方法".center(100,"-"))
for name, param in model.named_parameters(prefix='铁凡', recurse=True):
print('参数名字是:', name, '参数形状是:', param.shape)
print('-'*100)
print("测试模型named_parameters(prefix='昊昊', recurse=False)方法".center(100,"-"))
for name, param in model.named_parameters(prefix='昊昊', recurse=False):
print('参数名字是:', name, '参数形状是:', param.shape)
print('-'*100)
print("测试模型named_parameters(prefix='', recurse=False)方法".center(100,"-"))
for name, param in model.named_parameters(prefix='', recurse=False):
print('参数名字是:', name, '参数形状是:', param.shape)
print('-'*100)
控制台输出结果展示:
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6
加载个人及系统配置文件用了 1097 毫秒。
(base) 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' '53529' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\test4cxq\test31.py'
cuda(GPU)是否可用: True
torch的版本: 1.2.0+cu92
---------------------------------------------测试模型(CPU)----------------------------------------------
--------------------------测试模型named_parameters(prefix='', recurse=True)方法---------------------------
参数名字是: attribute4cxq 参数形状是: torch.Size([])
参数名字是: attribute4lzq 参数形状是: torch.Size([4])
参数名字是: attribute4hh 参数形状是: torch.Size([3, 4, 5, 6])
参数名字是: attribute4wyf 参数形状是: torch.Size([7, 8, 9, 10])
参数名字是: conv1.0.weight 参数形状是: torch.Size([64, 1, 3, 3])
参数名字是: conv1.0.bias 参数形状是: torch.Size([64])
参数名字是: conv1.2.weight 参数形状是: torch.Size([128, 64, 3, 3])
参数名字是: conv1.2.bias 参数形状是: torch.Size([128])
参数名字是: dense.0.weight 参数形状是: torch.Size([1024, 25088])
参数名字是: dense.0.bias 参数形状是: torch.Size([1024])
参数名字是: dense.3.weight 参数形状是: torch.Size([10, 1024])
参数名字是: dense.3.bias 参数形状是: torch.Size([10])
参数名字是: layer4cxq1.weight 参数形状是: torch.Size([33, 2, 4, 4])
参数名字是: layer4cxq1.bias 参数形状是: torch.Size([33])
参数名字是: layer4cxq4.weight 参数形状是: torch.Size([1024, 25088])
参数名字是: layer4cxq4.bias 参数形状是: torch.Size([1024])
----------------------------------------------------------------------------------------------------
-------------------------测试模型named_parameters(prefix='铁凡', recurse=True)方法--------------------------
参数名字是: 铁凡.attribute4cxq 参数形状是: torch.Size([])
参数名字是: 铁凡.attribute4lzq 参数形状是: torch.Size([4])
参数名字是: 铁凡.attribute4wyf 参数形状是: torch.Size([7, 8, 9, 10])
参数名字是: 铁凡.conv1.0.weight 参数形状是: torch.Size([64, 1, 3, 3])
参数名字是: 铁凡.conv1.0.bias 参数形状是: torch.Size([64])
参数名字是: 铁凡.conv1.2.weight 参数形状是: torch.Size([128, 64, 3, 3])
参数名字是: 铁凡.conv1.2.bias 参数形状是: torch.Size([128])
参数名字是: 铁凡.dense.0.weight 参数形状是: torch.Size([1024, 25088])
参数名字是: 铁凡.dense.0.bias 参数形状是: torch.Size([1024])
参数名字是: 铁凡.dense.3.weight 参数形状是: torch.Size([10, 1024])
参数名字是: 铁凡.dense.3.bias 参数形状是: torch.Size([10])
参数名字是: 铁凡.layer4cxq1.weight 参数形状是: torch.Size([33, 2, 4, 4])
参数名字是: 铁凡.layer4cxq1.bias 参数形状是: torch.Size([33])
参数名字是: 铁凡.layer4cxq4.weight 参数形状是: torch.Size([1024, 25088])
参数名字是: 铁凡.layer4cxq4.bias 参数形状是: torch.Size([1024])
----------------------------------------------------------------------------------------------------
-------------------------测试模型named_parameters(prefix='昊昊', recurse=False)方法-------------------------
参数名字是: 昊昊.attribute4cxq 参数形状是: torch.Size([])
参数名字是: 昊昊.attribute4lzq 参数形状是: torch.Size([4])
参数名字是: 昊昊.attribute4hh 参数形状是: torch.Size([3, 4, 5, 6])
参数名字是: 昊昊.attribute4wyf 参数形状是: torch.Size([7, 8, 9, 10])
----------------------------------------------------------------------------------------------------
--------------------------测试模型named_parameters(prefix='', recurse=False)方法--------------------------
参数名字是: attribute4cxq 参数形状是: torch.Size([])
参数名字是: attribute4lzq 参数形状是: torch.Size([4])
参数名字是: attribute4hh 参数形状是: torch.Size([3, 4, 5, 6])
参数名字是: attribute4wyf 参数形状是: torch.Size([7, 8, 9, 10])
----------------------------------------------------------------------------------------------------
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq> conda activate ssd4pytorch1_2_0
(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>