# Prediction after logsoftmax & nllloss

**URL:** <https://discuss.pytorch.org/t/prediction-after-logsoftmax-nllloss/75104>\
**Category:** Uncategorized\
**Created:** [April 2, 2020, 12:56pm UTC](https://discuss.pytorch.org/t/prediction-after-logsoftmax-nllloss/75104 "2020-04-02T12:56:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![snip3r77](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/65b543/32.png) [@snip3r77](https://discuss.pytorch.org/u/snip3r77)\
**Post date:** [April 2, 2020, 12:56pm UTC](https://discuss.pytorch.org/t/prediction-after-logsoftmax-nllloss/75104/1 "2020-04-02T12:56:52Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![b02202050](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/b02202050/32/20566_2.png) [@b02202050](https://discuss.pytorch.org/u/b02202050)\
**Post date:** [April 2, 2020, 1:09pm UTC](https://discuss.pytorch.org/t/prediction-after-logsoftmax-nllloss/75104/2 "2020-04-02T13:09:01Z")

</div>

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.
