Iterate over channels of a convolutional layer

(Nicholas Dwork) #1

I would like to iterate over the values of a convolutional layer. I have done the following:

   for thisMod in net.modules():                                                                               
     if hasattr(thisMod, 'weight'):                                                                            
       weightData =                                                                
       biasData = 
       if isinstance( thisMod, torch.nn.modules.conv.Conv2d ):
         print( weightData.shape )

Rather than weightData being the array of size [30,3,3,3] array (where 30 is the number of out channels), weightData is of size [3,3,3]. I suspect that this is one channel.

How do I iterate over the channels of a convolutional layer?


That’s strange. It’s working in this case:

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 3)
        self.conv2 = nn.Conv2d(6, 12, 3)
    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        return x

net = Net()
# Print the shape