pytorch-获取模型中间结果,*list的含义

Posted shuangcao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pytorch-获取模型中间结果,*list的含义相关的知识,希望对你有一定的参考价值。

获取模型的中间结果的简单方法

以alexnet为例

1 import torchvision.models as models
2 import torch.nn as nn
3 
4 if __name__ == __main__:
5     alexnet = models.alexnet(pretrained=True)
6     print(alexnet)
7     alexnet.classifier = nn.Sequential(*list(alexnet.classifier.children())[::2])  # 返回直接子模块上的迭代器,[::2],针对所有,取步长为2
8     print(alexnet.classifier) #改变后的 alexnet.classifier模块
9     print(alexnet) # 改变后的

结果:

print(alexnet):

AlexNet(
(features): Sequential(
(0): Conv2d(3, 64, kernel_size=(11, 11), stride=(4, 4), padding=(2, 2))
(1): ReLU(inplace=True)
(2): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
(3): Conv2d(64, 192, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(4): ReLU(inplace=True)
(5): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
(6): Conv2d(192, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(7): ReLU(inplace=True)
(8): Conv2d(384, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(9): ReLU(inplace=True)
(10): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(11): ReLU(inplace=True)
(12): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
)
(avgpool): AdaptiveAvgPool2d(output_size=(6, 6))
(classifier): Sequential(
(0): Dropout(p=0.5, inplace=False)
(1): Linear(in_features=9216, out_features=4096, bias=True)
(2): ReLU(inplace=True)
(3): Dropout(p=0.5, inplace=False)
(4): Linear(in_features=4096, out_features=4096, bias=True)
(5): ReLU(inplace=True)
(6): Linear(in_features=4096, out_features=1000, bias=True)
)
)

 

print(alexnet.classifier):


Sequential(
(0): Dropout(p=0.5, inplace=False)
(1): ReLU(inplace=True)
(2): Linear(in_features=4096, out_features=4096, bias=True)
(3): Linear(in_features=4096, out_features=1000, bias=True)
)

 

print(alexnet):


AlexNet(
(features): Sequential(
(0): Conv2d(3, 64, kernel_size=(11, 11), stride=(4, 4), padding=(2, 2))
(1): ReLU(inplace=True)
(2): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
(3): Conv2d(64, 192, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(4): ReLU(inplace=True)
(5): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
(6): Conv2d(192, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(7): ReLU(inplace=True)
(8): Conv2d(384, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(9): ReLU(inplace=True)
(10): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(11): ReLU(inplace=True)
(12): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
)
(avgpool): AdaptiveAvgPool2d(output_size=(6, 6))
(classifier): Sequential(
(0): Dropout(p=0.5, inplace=False)
(1): ReLU(inplace=True)
(2): Linear(in_features=4096, out_features=4096, bias=True)
(3): Linear(in_features=4096, out_features=1000, bias=True)
)
)

补基础知识:*list:提取列表里面的元素

1 lst =[1,2,3]
2 print(*lst[:-1])# 1 2,提取列表里面的元素
3 
4 def add(a, b):
5     return a + b
6 data = [4, 3]
7 print(add(*data)) # 7 # equals to print add(4, 3)
8 data = {a: 5, b: 7}
9 print(add(**data)) # 12 # equals to print add(5, 7)

 

以上是关于pytorch-获取模型中间结果,*list的含义的主要内容,如果未能解决你的问题,请参考以下文章

PyTorch 迁移学习 (Transfer Learning) 代码详解

PyTorch 迁移学习 (Transfer Learning) 代码详解

PyTorch输出网络中间层结果

PyTorch输出网络中间层结果

Pytorch:实战指南

取出预训练模型中间层的输出(pytorch)