Prediction after logsoftmax & nllloss

Hi,

at the last 2 lines how does it work? as in why do we need to exp?
i thought we can just model(img)

and we will obtain the result?
because when we model(img).size() the output is torch.Size([1, 10]) which is the 10 categories and the highest is the predicted result?

Thanks

model = nn.Sequential(nn.Linear(784, 128),
nn.ReLU(),
nn.Linear(128, 64),
nn.ReLU(),
nn.Linear(64, 10),
nn.LogSoftmax(dim=1))
criterion = nn.NLLLoss()
optimizer = optim.SGD(model.parameters(), lr=0.003)

  • snip some codes -

images, labels = next(iter(trainloader))

img = images[0].view(1, 784)

Turn off gradients to speed up this part

with torch.no_grad():

logps = model(img) #input an image after re-shaped
Output of the network are log-probabilities, need to take exponential for probabilities

ps = torch.exp(logps) # ← same effect
ps = F.softmax(logps,dim=0) <–same effect
view_classify(img.view(1, 28, 28), ps)

Yes, you can just result_class = logps.argmax(dim=1) to see the prediction class.
But in real-world application, you have to set a confidence threshold to balance your FPR and TPR.
Therefore, confidence(probability) is always needed when you make prediction.