Extract the output of a specific layer of a pre-trained network while a sample passed through the network

S_M: <p>Hello I have a pre-trained vgg11 network with its weights the structure of this network is as follows:</p>
<p>VGG(<br>
(features): Sequential(<br>
(0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(2): ReLU(inplace=True)<br>
(3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
(4): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(5): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(6): ReLU(inplace=True)<br>
(7): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
(8): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(9): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(10): ReLU(inplace=True)<br>
(11): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(12): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(13): ReLU(inplace=True)<br>
(14): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
(15): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(16): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(17): ReLU(inplace=True)<br>
(18): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(19): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(20): ReLU(inplace=True)<br>
(21): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
(22): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(23): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(24): ReLU(inplace=True)<br>
(25): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(26): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(27): ReLU(inplace=True)<br>
(28): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
)<br>
(avgpool): AdaptiveAvgPool2d(output_size=(1, 1))<br>
(classifier): Sequential(<br>
(0): Linear(in_features=512, out_features=4096, bias=True)<br>
(1): ReLU(inplace=True)<br>
(2): Dropout(p=0.5, inplace=False)<br>
(3): Linear(in_features=4096, out_features=4096, bias=True)<br>
(4): ReLU(inplace=True)<br>
(5): Dropout(p=0.5, inplace=False)<br>
(6): Linear(in_features=4096, out_features=10, bias=True)<br>
)<br>
)</p>
<p>the model is loaded by <code>mymodel=torch.save(model.state_dict(), PATH)</code> then I evaluate it to pass a sample through my model</p>
<pre><code class="lang-auto">mymodel.eval()
out=mymodel(img)
</code></pre>
<p>out refers to the output of the last layer. I need the logits I mean the input of the softmax layer how can I access it?</p>

ptrblck: <p>Assuming the output is created by the last linear layer, <code>out</code> would already contain the logits.</p>

S_M: <p>I thought the Linear layer (6) refers to the softmax as it contains some neurons same as the number of classes. So the Linear layer (3) or the Dropout (5) had been supposed to be the logits.</p>

ptrblck: <p>No, the outputs of an <code>nn.Linear</code> layer are not bounded and can contain any values in <code>[-Inf, +Inf]</code> and are thus logits. If you want to get the (softmax) probabilities, you would have to apply another <code>nn.Softmax</code> layer (or its functional form via <code>F.softmax</code>).</p>

S_M: <p>I need the logits of the network so as you said the ‘out’ can help me. Thank you so much</p>