nn.Module里面有两个特别重要的关于参数的属性,分别是named_parameters()
和parameters()
。named_parameters()是给出网络层的名字和参数的迭代器,parameters()会给出一个网络的全部参数的迭代器。
- 举例说明:
import torch
import torch.nn as nn
from torch.autograd import Variable
class Model(nn.Module):
def __init__(self, c_h, c_out):
super(Model, self).__init__()
self.conv2d = nn.Conv2d(c_h, c_out, kernel_size=(2, 3))
self.conv1d = nn.Conv1d(c_h, c_out, kernel_size=1)
self.linear = nn.Linear(c_h, c_out)
# 设置可学习参数
self.weight_1 = nn.Parameter(torch.FloatTensor(1), requires_grad=True)
# init
self.weight_1.data.fill_(0.25)
self.value = torch.tensor(1)
self.test = Variable(self.value)
model = Model(3, 4)
"""
nn.Module里面有两个特别重要的关于参数的属性,分别是named_parameters()和parameters()。
named_parameters()是给出网络层的名字和参数的迭代器,parameters()会给出一个网络的全部参数的迭代器。
"""
def print_network(model, name):
"""Print out the network information."""
num_params = 0
print(model)
print(name)
for p in model.parameters():
num_params += p.numel() # numel()获取tensor中一共包含多少个元素
print("The number of parameters: {}".format(num_params))
for p in model.named_parameters():
print(len(p))
print(p)
if __name__ == '__main__':
print_network(model, 'm')
- 结果
Model(
(conv2d): Conv2d(3, 4, kernel_size=(2, 3), stride=(1, 1))
(conv1d): Conv1d(3, 4, kernel_size=(1,), stride=(1,))
(linear): Linear(in_features=3, out_features=4, bias=True)
)
m
The number of parameters: 109
2
('weight_1', Parameter containing:
tensor([0.2500], requires_grad=True))
2
('conv2d.weight', Parameter containing:
tensor([[[[ 0.1952, 0.1276, 0.1480],
[ 0.0232, 0.0853, 0.0471]],
[[ 0.2219, -0.1046, 0.0738],
[-0.0163, -0.0100, 0.1927]],
[[ 0.1709, 0.0461, 0.0137],
[ 0.1762, 0.0511, 0.0319]]],
[[[-0.2333, 0.0426, -0.0946],
[ 0.0997, 0.0225, 0.0358]],
[[-0.1744, 0.0281, -0.1157],
[ 0.0461, 0.1030, -0.1360]],
[[ 0.2107, 0.1877, 0.1386],
[-0.0401, -0.0251, -0.0446]]],
[[[ 0.1975, -0.0323, 0.0199],
[ 0.0722, -0.1193, -0.2274]],
[[ 0.2100, 0.1317, 0.2273],
[ 0.0426, -0.0987, -0.2054]],
[[ 0.1995, -0.1346, 0.0621],
[ 0.0949, 0.0104, -0.0909]]],
[[[-0.1033, -0.0297, -0.0506],
[ 0.1811, -0.1832, 0.1673]],
[[ 0.2206, -0.0414, 0.0783],
[ 0.0911, -0.0315, -0.1597]],
[[-0.0766, 0.2039, 0.1840],
[-0.0629, -0.0137, -0.2211]]]], requires_grad=True))
2
('conv2d.bias', Parameter containing:
tensor([0.2280, 0.1339, 0.0501, 0.0747], requires_grad=True))
2
('conv1d.weight', Parameter containing:
tensor([[[ 0.3422],
[-0.3975],
[ 0.3537]],
[[-0.5534],
[ 0.5642],
[-0.1036]],
[[ 0.2595],
[-0.5371],
[ 0.3225]],
[[ 0.0061],
[ 0.3125],
[ 0.2301]]], requires_grad=True))
2
('conv1d.bias', Parameter containing:
tensor([ 0.0465, -0.3245, 0.4590, -0.4186], requires_grad=True))
2
('linear.weight', Parameter containing:
tensor([[-0.4758, 0.1002, -0.3299],
[-0.2612, 0.4921, -0.2546],
[ 0.2926, -0.1463, 0.0831],
[ 0.2100, -0.0217, 0.1397]], requires_grad=True))
2
('linear.bias', Parameter containing:
tensor([-0.1661, 0.5512, -0.0258, 0.3975], requires_grad=True))