Extract a layer as a vecor

Hi,
I have a trained model and I want to extract one of the layers as a vector of values.
For example, I want conv3 of the following model:

class Model(torch.nn.Module):
    def __init__(self, num_inputs):
        super(Model, self).__init__()
        self.conv1 = nn.Conv2d(num_inputs, 32, 3, stride=2, padding=1)
        self.conv2 = nn.Conv2d(32, 32, 3, stride=2, padding=1)
        self.conv3 = nn.Conv2d(32, 32, 3, stride=2, padding=1)
        self.conv4 = nn.Conv2d(32, 32, 3, stride=2, padding=1)


    def forward(self, inputs):
        x = F.elu(self.conv1(inputs))
        x = F.elu(self.conv2(x))
        x = F.elu(self.conv3(x))
        x = F.elu(self.conv4(x))
        return x

Any suggestions of how to do it?

Change the forward function like this:

my_conv3 = None

class Model(torch.nn.Module):
    def __init__(self, num_inputs):
        super(Model, self).__init__()
        self.conv1 = nn.Conv2d(num_inputs, 32, 3, stride=2, padding=1)
        self.conv2 = nn.Conv2d(32, 32, 3, stride=2, padding=1)
        self.conv3 = nn.Conv2d(32, 32, 3, stride=2, padding=1)
        self.conv4 = nn.Conv2d(32, 32, 3, stride=2, padding=1)

def forward(self, inputs):
        x = F.elu(self.conv1(inputs))
        x = F.elu(self.conv2(x))
        conv3 = F.elu(self.conv3(x))
        my_conv3 = conv3
        x = F.elu(self.conv4(conv3))
        return x
1 Like