Get probability from detected class

username: rizal_alfarizi
cooked: <p>I have a network like this for image classification, for now i have 2 classes:</p>
<pre><code class="lang-auto">class ActionNet(Module):

    def __init__(self, num_class=4):

        super(ActionNet, self).__init__()

        

        self.cnn_layer = Sequential(

            #conv1

            Conv2d(in_channels=1, out_channels=32, kernel_size=1, bias=False),

            BatchNorm2d(32),

            PReLU(num_parameters=32),

            MaxPool2d(kernel_size=3),

            #conv2

            Conv2d(in_channels=32, out_channels=64, kernel_size=1, bias=False),

            BatchNorm2d(64),

            PReLU(num_parameters=64),

            MaxPool2d(kernel_size=3),

            #flatten

            Flatten(),

            Linear(576, 128),

            BatchNorm1d(128),

            ReLU(inplace=True),

            Dropout(0.5),

            Linear(128, num_class)

        )

    

    def forward(self, x):

        x = self.cnn_layer(x)

        return x
</code></pre>
<p>then after i training my network, i predict the image using this code:</p>
<pre><code class="lang-auto">def predict_image(image):

    

    input = torch.from_numpy(image)

    input = input.unsqueeze(1)

    input = input.to(device)

    output = model(input)

    index = output.data.cpu().numpy().argmax()

    return index
</code></pre>
<p>how do i get all class probability of prediction image?</p>

username: Nikronic
cooked: <p>Hi,</p>
<p>This can be achieved by simply using <code>nn.Softmax()</code>. You can add it in your model definition to get index and probs at the same time. Right after this line:</p>
<aside class="quote no-group quote-modified" data-username="rizal_alfarizi" data-post="1" data-topic="85358">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/rizal_alfarizi/48/22628_2.png" class="avatar"> rizal_alfarizi:</div>
<blockquote>
<p><code>Linear(128, num_class)</code><br>
<code>Softmax()</code></p>
</blockquote>
</aside>
<p>Or let’s say you have outputs (logits) and you are looking for probs here in your code:</p>
<aside class="quote no-group" data-username="rizal_alfarizi" data-post="1" data-topic="85358">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/rizal_alfarizi/48/22628_2.png" class="avatar"> rizal_alfarizi:</div>
<blockquote>
<p><code>output = model(input)</code></p>
</blockquote>
</aside>
<p>You can achieve it by same trick:</p>
<pre><code class="lang-auto">softmax = nn.Softmax()
output = model(input)
probs = softmax(output)
</code></pre>
<p>Bests</p>

username: rizal_alfarizi
cooked: <p>is it ok to use softmax like that if im using CrossEntropy loss function</p>

username: Nikronic
cooked: <p>No, actually, softmax normalize logits between [0, 1] in the way that sum of all values adds up to 1, So, it can be interpreted as probability. And cross entropy does this internally.<br>
For instance, a output of nn.Linear can be like this for 2 classes:</p>
<pre><code class="lang-auto">output = [[-2.3, 8.46],
          [-.56, 3.24], 
                   ...]
</code></pre>
<p>And after applying softmax:</p>
<pre><code class="lang-auto">output = [[0.1, 0.9],
          [0.3, 0.7],
                 ...]
</code></pre>
<p>edit: explanation was based on another library and partly wrong.<br>
Thanks <a class="mention" href="/u/timesler">@timesler</a></p>

username: timesler
cooked: <p>No, you should avoid using <code>CrossEntropyLoss</code> on softmax-transformed values. <code>CrossEntropyLoss</code> expects raw logit values and calculates <code>LogSoftmax</code> followed by <code>NLLLoss</code> internally.</p>
<p>This is described in the <a href="https://pytorch.org/docs/master/generated/torch.nn.CrossEntropyLoss.html" rel="nofollow noopener">documentation here</a>.</p>
<p>Typically, it’s better practice to do the softmax calculation outside your model forward method so that you can pass the logits directly to the loss function.</p>