PyTorch 中的 model.named_parameters() 和 model.parameters()
假设有一个model继承nn.model类
在 PyTorch 中,model.named_parameters() 和 model.parameters() 是用于查看模型参数的两个重要方法。学习了怎么查看模型的参数和结构,以下是如何使用这些方法及其差异的详细说明:
①named_parameters()返回的list中,每个元组(与list相似,只是数据不可修改)打包了2个内容,分别是layer-name和②layer-param(网络层的名字和参数的迭代器);
parameters()只有后者layer-param(参数的迭代器)
即一个可以打印参数和名字、一个只能打印参数
1. model.named_parameters()
model.named_parameters() 返回一个生成器,生成每个参数的名称和相应的参数值。这对于查看和修改特定参数的可训练状态非常有用。
示例代码
model= DarkNet([1, 2, 8, 8, 4])
for name, param in model.named_parameters():
print(name, param.requires_grad)
param.requires_grad = False
输出:
conv1.weight True
bn1.weight True
bn1.bias True
layer1.ds_conv.weight True
layer1.ds_bn.weight True
layer1.ds_bn.bias True
layer1.residual_0.conv1.weight True
layer1.residual_0.bn1.weight True
layer1.residual_0.bn1.bias True
layer1.residual_0.conv2.weight True
layer1.residual_0.bn2.weight True
layer1.residual_0.bn2.bias True
layer2.ds_conv.weight True
layer2.ds_bn.weight True
layer2.ds_bn.bias True
layer2.residual_0.conv1.weight True
layer2.residual_0.bn1.weight True
layer2.residual_0.bn1.bias True
....
通过这种方式,你可以查看和更改每个参数的可训练属性。
而parameters()只有后者layer-param(参数的迭代器)
for index, param in enumerate(model.parameters()):
print(param.shape)
输出:
torch.Size([16, 3, 3, 3])
torch.Size([16])
torch.Size([16])
torch.Size([16, 16, 3, 3])
torch.Size([16])
torch.Size([16])
torch.Size([16, 16, 3, 3])
torch.Size([16])
torch.Size([16])
torch.Size([16, 16, 3, 3])
torch.Size([16])
torch.Size([16])
torch.Size([16, 16, 3, 3])
torch.Size([16])
torch.Size([16])
torch.Size([32, 16, 3, 3])
torch.Size([32])
torch.Size([32])
torch.Size([32, 32, 3, 3])
torch.Size([32])
torch.Size([32])
torch.Size([32, 32, 3, 3])
torch.Size([32])
torch.Size([32])
torch.Size([32, 32, 3, 3])
torch.Size([32])
torch.Size([32])
torch.Size([32, 32, 3, 3])
torch.Size([32])
torch.Size([32])
torch.Size([64, 32, 3, 3])
torch.Size([64])
torch.Size([64])
torch.Size([64, 64, 3, 3])
torch.Size([64])
torch.Size([64])
torch.Size([64, 64, 3, 3])
torch.Size([64])
torch.Size([64])
torch.Size([64, 64, 3, 3])
torch.Size([64])
torch.Size([64])
torch.Size([64, 64, 3, 3])
torch.Size([64])
torch.Size([64])
torch.Size([64, 64, 3, 3])
torch.Size([64])
torch.Size([64])
torch.Size([10, 64])
torch.Size([10])
总结
model.named_parameters() 用于获取模型参数的名称和参数本身,可以更改参数的 requires_grad 属性。
model.parameters() 用于获取模型参数的张量,但不包括参数名称。
这两种方法都可以用来检查和修改模型的参数。
可以把两者结合
将两者结合进行迭代,同时具有索引,网络层名字及param
for index, (name, param) in zip(enumerate(model.parameters()), model.named_parameters()):
print(index[0])
print(name, param.shape)
输出结果为
0
conv1.weight torch.Size([32, 3, 3, 3])
1
bn1.weight torch.Size([32])
2
bn1.bias torch.Size([32])
3
layer1.ds_conv.weight torch.Size([64, 32, 3, 3])
4
layer1.ds_bn.weight torch.Size([64])
5
layer1.ds_bn.bias torch.Size([64])
6
layer1.residual_0.conv1.weight torch.Size([32, 64, 1, 1])
7
layer1.residual_0.bn1.weight torch.Size([32])
8
layer1.residual_0.bn1.bias torch.Size([32])
9
layer1.residual_0.conv2.weight torch.Size([64, 32, 3, 3])
1670

被折叠的 条评论
为什么被折叠?



