查看模型、查看模型参数的方法(主要针对迁移学习)
一:模型查看
我们有以下代码:
import torch
class MyModel(torch.nn.Module):
def __init__(self):
super().__init__()
self.layer1 = torch.nn.Sequential(
torch.nn.Linear(3, 4),
torch.nn.Linear(4, 3),
)
self.layer2 = torch.nn.Linear(3, 6)
self.layer3 = torch.nn.Sequential(
torch.nn.Linear(6, 7),
torch.nn.Linear(7, 5),
)
def forward(self, x):
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
return x
net = MyModel()
print(net)
代码分析:模型有3个大层,每个层中又有一些不定数量的小层。以self.layer1举例:layer1中有2个先行层,分别为Linear(3, 4)和Linear(4, 3)。
1. 使用print(net)方法来查看模型
现在我们来打印出来模型的网络结果如下:
MyModel(
(layer1): Sequential(
(0): Linear(in_features=3, out_features=4, bias=True)
(1): Linear(in_features=4, out_features=3, bias=True)
)
(layer2): Linear(in_features=3, out_features=6, bias=True)
(layer3): Sequential(
(0): Linear(in_features=6, out_features=7, bias=True)
(1): Linear(in_features=7, out_features=5, bias=True)
)
)
使用 print(net) 方法打印的模型结构显示了 MyModel 类的各个层及其参数。以下是对打印结果的总结:
1. 模型名称:
- MyModel:这是模型的名称。
2. 层结构
- layer1:一个顺序容器(Sequential),包含两个线性层。
- layer2:一个线性层。
- layer3:另一个顺序容器(Sequential),包含两个线性层。
2. 使用net.modules()查看模型
for layer in net.modules():
print(type(layer)
遍历网络的每个模块,并打印每个模块的类型:
<class '__main__.MyModel'>
<class 'torch.nn.modules.container.Sequential'>
<class 'torch.nn.modules.linear.Linear'>
<class 'torch.nn.modules.linear.Linear'>
<class 'torch.nn.modules.linear.Linear'>
<class 'torch.nn.modules.container.Sequential'>
<class 'torch.nn.modules.linear.Linear'>
<class 'torch.nn.modules.linear.Linear'>
根据输出的顺序,可以总结出模型的层结构如下:
1. MyModel(模型实例)
2. Sequential(layer1)
- Linear(layer1 的第一个线性层)
- Linear(layer1 的第二个线性层)
- Linear(layer2)
4. Sequential(layer3)
- Linear(layer3 的第一个线性层)
- Linear(layer3 的第二个线性层)
for layer in net.modules():
print(layer)
遍历网络的每个模块,并打印每个模块的内容:
MyModel(
(layer1): Sequential(
(0): Linear(in_features=3, out_features=4, bias=True)
(1): Linear(in_features=4, out_features=3, bias=True)
)
(layer2): Linear(in_features=3, out_features=6, bias=True)
(layer3): Sequential(
(0): Linear(in_features=6, out_features=7, bias=True)
(1): Linear(in_features=7, out_features=5, bias=True)
)
)
Sequential(
(0): Linear(in_features=3, out_features=4, bias=True)
(1): Linear(in_features=4, out_features=3, bias=True)
)
Linear(in_features=3, out_features=4, bias=True)
Linear(in_features=4, out_features=3, bias=True)
Linear(in_features=3, out_features=6, bias=True)
Sequential(
(0): Linear(in_features=6, out_features=7, bias=True)
(1): Linear(in_features=7, out_features=5, bias=True)
)
Linear(in_features=6, out_features=7, bias=True)
Linear(in_features=7, out_features=5, bias=True)
如上图所示,递归的打印出了模型的结构。
综上所示,modules()会递归地将我们的模型层,全部打印出来。
3. 使用net.named_modules()查看模型
for name, layer in net.named_modules():
print(name, type(layer))
打印结果如下:
<class '__main__.MyModel'>
layer1 <class 'torch.nn.modules.container.Sequential'>
layer1.0 <class 'torch.nn.modules.linear.Linear'>
layer1.1 <class 'torch.nn.modules.linear.Linear'>
layer2 <class 'torch.nn.modules.linear.Linear'>
layer3 <class 'torch.nn.modules.container.Sequential'>
layer3.0 <class 'torch.nn.modules.linear.Linear'>
layer3.1 <class 'torch.nn.modules.linear.Linear'>
相较于modules(),named_modules()会将模型层的名字打印出来,同样也是递归打印。
for name, layer in net.named_modules():
print(name, layer)
打印结果如下:
MyModel(
(layer1): Sequential(
(0): Linear(in_features=3, out_features=4, bias=True)
(1): Linear(in_features=4, out_features=3, bias=True)
)
(layer2): Linear(in_features=3, out_features=6, bias=True)
(layer3): Sequential(
(0): Linear(in_features=6, out_features=7, bias=True)
(1): Linear(in_features=7, out_features=5, bias=True)
)
)
layer1 Sequential(
(0): Linear(in_features=3, out_features=4, bias=True)
(1): Linear(in_features=4, out_features=3, bias=True)
)
layer1.0 Linear(in_features=3, out_features=4, bias=True)
layer1.1 Linear(in_features=4, out_features=3, bias=True)
layer2 Linear(in_features=3, out_features=6, bias=True)
layer3 Sequential(
(0): Linear(in_features=6, out_features=7, bias=True)
(1): Linear(in_features=7, out_features=5, bias=True)
)
layer3.0 Linear(in_features=6, out_features=7, bias=True)
layer3.1 Linear(in_features=7, out_features=5, bias=True)
综上所示,modules()和named_modules()类似,都是递归打印模型的结构,只是named_modules()方法会打印出模型层的名字。
4. 使用net.children(),net.named_modules()查看模型
for layer in net.children():
print(layer)
打印结果如下:只打印网络的第一层子网络,而不会递归打印子网络内部的层。
Sequential(
(0): Linear(in_features=3, out_features=4, bias=True)
(1): Linear(in_features=4, out_features=3, bias=True)
)
Linear(in_features=3, out_features=6, bias=True)
Sequential(
(0): Linear(in_features=6, out_features=7, bias=True)
(1): Linear(in_features=7, out_features=5, bias=True)
)
for name, layer in net.named_children():
print(name, layer)
打印结果如下:
layer1 Sequential(
(0): Linear(in_features=3, out_features=4, bias=True)
(1): Linear(in_features=4, out_features=3, bias=True)
)
layer2 Linear(in_features=3, out_features=6, bias=True)
layer3 Sequential(
(0): Linear(in_features=6, out_features=7, bias=True)
(1): Linear(in_features=7, out_features=5, bias=True)
)
二:模型参数查看
1. 使用net.parameters()、net.named_parameters()查看模型参数
使用net.parameters()查看模型参数:
for param in net.parameters():
print(param.shape)
打印结果如下:同样是递归打印
torch.Size([4, 3])
torch.Size([4])
torch.Size([3, 4])
torch.Size([3])
torch.Size([6, 3])
torch.Size([6])
torch.Size([7, 6])
torch.Size([7])
torch.Size([5, 7])
torch.Size([5])
使用net.named_parameters()查看模型参数:
for name, param in net.named_parameters():
print(name, param.shape)
打印结果如下:递归打印,加上参数名。
layer1.0.weight torch.Size([4, 3])
layer1.0.bias torch.Size([4])
layer1.1.weight torch.Size([3, 4])
layer1.1.bias torch.Size([3])
layer2.weight torch.Size([6, 3])
layer2.bias torch.Size([6])
layer3.0.weight torch.Size([7, 6])
layer3.0.bias torch.Size([7])
layer3.1.weight torch.Size([5, 7])
layer3.1.bias torch.Size([5])
2. 使用net.state_dict()查看模型参数
net.state_dict()是将我们模型的每一个的参数名和参数值以字典的形式迭代出来,使用net.state_dict().item()可以获取字典中的键值对。
for key, value in net.state_dict().items():
print(key, value.shape)
打印结果如下:
layer1.0.weight torch.Size([4, 3])
layer1.0.bias torch.Size([4])
layer1.1.weight torch.Size([3, 4])
layer1.1.bias torch.Size([3])
layer2.weight torch.Size([6, 3])
layer2.bias torch.Size([6])
layer3.0.weight torch.Size([7, 6])
layer3.0.bias torch.Size([7])
layer3.1.weight torch.Size([5, 7])
layer3.1.bias torch.Size([5])
从打印结果来看,与net.named_parameters()的打印结果是一样的。
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)