The use of torch::sigmoid_

username: Wyatthy
cooked: <p>I am doing a classification job for MNIST , the last Net of my model is <code>torch::log_softmax(x, /*dim=*/1)</code> , now i wanna convert the output tensor to a probability vector ， so i use <code>auto outsig=torch::sigmoid_(output);</code> ， but i got a bad <code>outsig</code> tensor :</p>
<blockquote>
<p>1.6201e-08  5.1166e-06  2.5344e-06  2.8637e-03  2.4327e-02  1.9071e-04 5.6446e-08  1.2600e-04  1.9227e-03  4.9237e-01</p>
</blockquote>
<p>and my <code>ouput</code> tensor is:</p>
<blockquote>
<p>-17.9382 -12.1830 -12.8856  -5.8528  -3.6915  -8.5646 -16.6900  -8.9791</p>
</blockquote>
<p>it’s not very inappropriate，the effect I expect is this:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/c/8ca5e8216776d42be5f8e233c19c81756204a940.png" data-download-href="https://discuss.pytorch.org/uploads/default/8ca5e8216776d42be5f8e233c19c81756204a940" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/8/c/8ca5e8216776d42be5f8e233c19c81756204a940_2_507x375.png" alt="image" data-base62-sha1="k4eiz6NvmrzxF8oBfAt1HFUmIUg" width="507" height="375" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/8/c/8ca5e8216776d42be5f8e233c19c81756204a940_2_507x375.png, https://discuss.pytorch.org/uploads/default/optimized/3X/8/c/8ca5e8216776d42be5f8e233c19c81756204a940_2_760x562.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/8/c/8ca5e8216776d42be5f8e233c19c81756204a940_2_1014x750.png 2x" data-dominant-color="F5F5F6"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1086×801 47.6 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Can someone tell me what to do？THANKSS!!!</p>

username: InnovArul
cooked: <aside class="quote no-group" data-username="Wyatthy" data-post="1" data-topic="151820">
<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/wyatthy/48/48331_2.png" class="avatar"> Wyatthy:</div>
<blockquote>
<p>now i wanna convert the output tensor to a probability vector</p>
</blockquote>
</aside>
<p>If you have used <code>torch::log_softmax</code> in the final layer, I would assume that it’s a multi-class classification problem. The suitable function for getting probability will be <code>torch::softmax()</code> instead of <code>torch::sigmoid()</code>.<br>
Can you try it (<code>torch::softmax()</code>)?</p>

username: Wyatthy
cooked: <p>Thank you very much！You are right, choosing to use sigmoid is stupid. My last layer used logsoftmax , so i should use <code>exp(output)</code> to get the probability vector whose sum is 1 .</p>
<p>Actually , The results of the two outputs <code>sigmoid(output)</code> and <code>exp(output)</code> are the same , I think this is because e^x is too small and approaches 0 .</p>

username: InnovArul
cooked: <p>I think that you should be using <code>torch::softmax()</code> instead of <code>torch::exp()</code>.<br>
Softmax normalizes each <code>exp(output_i)</code>.<br>
i.e.,  <code>exp(output_i) / (exp(output_0) + ... + exp(output_n))</code>.</p>