首页 > 解决方案 > 如何查看卷积层的第一个过滤器

问题描述

从这篇文中,据说如果您想直接查看内核过滤器矩阵,我们可以获取权重张量并相应地对其进行索引。现在我想看看我层的第一个过滤器的第一个内核。

从博客中,有人提到我可以使用这个conv1.weight[1,1,:,:]How do use this for the following model architecture。

class Cifar10CnnModel(ImageClassificationBase):
    def __init__(self):
        super().__init__()
        self.network = nn.Sequential(
            nn.Conv2d(3, 32, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.MaxPool2d(2, 2), # output: 64 x 16 x 16

            nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.MaxPool2d(2, 2), # output: 128 x 8 x 8

            nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.MaxPool2d(2, 2), # output: 256 x 4 x 4

            nn.Flatten(), 
            nn.Linear(256*4*4, 1024),
            nn.ReLU(),
            nn.Linear(1024, 512),
            nn.ReLU(),
            nn.Linear(512, 10))
        
    def forward(self, xb):
        return self.network(xb)

标签: pythonneural-networkpytorchconv-neural-network

解决方案


如果您通过以下方式创建模型:

model = Cifar10CnnModel()

那么您的权重将存储在顺序模型中,并且可以通过选择顺序模型列表的第一个元素来访问:

model.network[0].weights

推荐阅读