Vgg and softmax + features

Hello everyone, can anyone suggest me the right approach to get from data features and output from softmax using VGG19?
I watched the class of VGG, but that return only the features. Is it possible to obtain the value from the softmax? I need to this for the cross-entropy and to the testing phase.
Thanks in advance.

Hi Ric,

for multi-class classification use cases, you could use nn.CrossEntropyLoss as the loss function. This criterion expects logits as the model output, which explains the missing softmax (or log_softmax) after the final layer in the torchvision models.

If you really want to apply a softmax on the output, you could use the following code:

import torch.nn.functional as F

output = model(data)
prob = F.softmax(output, 1)

However, as described, you should not pass the softmax to a loss function.

Alternatively to the raw logits, you could instead apply F.log_softmax(output, 1) and pass it to nn.NLLLoss, which is exactly, was nn.CrossEntropyLoss is doing internally.

Let me know, if that clarifies the question or if you need more information.

1 Like

Thanks for reply @ptrblck. I applied as you said, but probably i’ve a problem into test phase. For a better understanding a put the code:

with torch.no_grad():
    for data, labels in testloader:
        if use_gpu:
            data, labels = data.cuda(), labels.cuda()

        features = model(data)
        sm = torch.nn.Softmax()
        probabilities = sm(features)

        predictions =[1]
        total += labels.size(0)
        correct += (predictions ==

Is it correct to pass the layer of Softmax result to get accuracy? This is my first time I use VGG19. Thanks for your help.

Yes, your code should be alright.
However, the .max(1) call will also return the same indices, if you apply it on features directly, so you don’t really need the softmax here.

Also, I would recommend to cast the equality check to float before summing, as it’ll return a torch.uint8 tensor, which might overflow, if you are using large patches or e.g. pixel-wise accuracy:

correct += (predictions ==
1 Like

ok, i think i solved insering as follow into forward function of VGG:

x = F.log_softmax(out)
Do you think is a good approach? In this way i return features and the result of softmax.

F.log_softmax is fine if you are using nn.NLLLoss.

1 Like