named_parameters
是 PyTorch 中一个非常有用的函数,用于访问模型中所有定义的参数及其对应的名称。- 它是
torch.nn.Module
类的方法之一,返回一个生成器,生成(name, parameter)
对,name
是参数的名称,parameter
是对应的参数张量。
1 举例
1.0 创建模型
python
import torch
import torch.nn as nn
# 定义一个简单的模型
class SimpleModel(nn.Module):
def __init__(self):
super(SimpleModel, self).__init__()
self.conv1 = nn.Conv2d(1, 20, 5)
self.conv2 = nn.Conv2d(20, 64, 5)
self.fc1 = nn.Linear(64 * 4 * 4, 500)
self.fc2 = nn.Linear(500, 10)
def forward(self, x):
x = torch.relu(self.conv1(x))
x = torch.relu(self.conv2(x))
x = x.view(-1, 64 * 4 * 4)
x = torch.relu(self.fc1(x))
x = self.fc2(x)
return x
# 实例化模型
model_tst = SimpleModel()
1.1 应用1:打印模型的所有参数及其名称
python
for name, param in model_tst.named_parameters():
print(name, param.shape)
'''
conv1.weight torch.Size([20, 1, 5, 5])
conv1.bias torch.Size([20])
conv2.weight torch.Size([64, 20, 5, 5])
conv2.bias torch.Size([64])
fc1.weight torch.Size([500, 1024])
fc1.bias torch.Size([500])
fc2.weight torch.Size([10, 500])
fc2.bias torch.Size([10])
conv1.weight torch.Size([20, 1, 5, 5])
conv1.bias torch.Size([20])
conv2.weight torch.Size([64, 20, 5, 5])
conv2.bias torch.Size([64])
fc1.weight torch.Size([500, 1024])
fc1.bias torch.Size([500])
fc2.weight torch.Size([10, 500])
fc2.bias torch.Size([10])
'''
1.2 应用2:冻结特定层的参数
假设我们只想训练全连接层,而冻结卷积层的参数:
python
for name, param in model_tst.named_parameters():
if 'conv' in name:
param.requires_grad = False
1.3 应用3:自定义优化器参数
可以使用 named_parameters
创建自定义的参数组,以便对不同的参数组应用不同的学习率:
python
optimizer = torch.optim.SGD([
{'params': [param for name, param in model_tst.named_parameters() if 'conv' in name], 'lr': 0.01},
{'params': [param for name, param in model_tst.named_parameters() if 'fc' in name], 'lr': 0.1}
], momentum=0.9)