How to use F.softmax

granth_jain: <p>Hi,</p>
<p>I have a tensor and I want to calculate softmax along the rows of the tensor.</p>
<blockquote>
<p>action_values  = t.tensor([[-0.4001, -0.2948,  0.1288]])</p>
</blockquote>
<p>as I understand cutting the tensor row-wise we need to specify dim as 1.<br>
However I got an unexpected result.</p>
<p>can someone please help me in understanding how softmax and dim in softmax works.</p>
<p>Below is what I tried, but none gave me successful results.</p>
<blockquote>
<p>F.softmax(action_values - max(action_values), dim = 0)<br>
Out[15]: tensor([[1., 1., 1.]])</p>
<p>F.softmax(action_values - max(action_values), dim = 1)<br>
Out[16]: tensor([[0.3333, 0.3333, 0.3333]])</p>
<p>F.softmax(action_values - max(action_values), dim = -1)<br>
Out[17]: tensor([[0.3333, 0.3333, 0.3333]])</p>
</blockquote>

KFrank: <p>Hi Granth!</p>
<aside class="quote no-group quote-modified" data-username="granth_jain" data-post="1" data-topic="103897" data-full="true">
<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/granth_jain/48/27523_2.png" class="avatar"> granth_jain:</div>
<blockquote>
<p>I want to calculate softmax along the rows of the tensor.<br>
…<br>
However I got an unexpected result.</p>
</blockquote>
</aside>
<p>The short answer is that you are calling python’s <code>max()</code> function,<br>
rather than pytorch’s <code>torch.max()</code> tensor function.  This is causing<br>
you to calculate <code>softmax()</code> for a tensor that is all zeros.</p>
<p>You have two issues:</p>
<p>First is the use of pytorch’s <code>max()</code>. <code>max()</code> doesn’t understand<br>
tensors, and for reasons that have to do with the details of <code>max()</code>'s<br>
implementation, this simply returns <code>action_values</code> again (with the<br>
singleton dimension removed).</p>
<p>The second is that there is no need to subtract a scalar from your<br>
tensor before calling <code>softmax()</code>.  Any such scalar drops out anyway<br>
in the <code>softmax()</code> calculation.</p>
<p>This script illustrates what is going on:</p>
<pre data-code-wrap="python"><code class="lang-python">import torch
torch.__version__
action_values = torch.tensor([[-0.4001, -0.2948, 0.1288]])
action_values
max (action_values)         # this is python's max, not pytorch's
torch.max (action_values)   # pytorch's tensor-version of max
action_values - max (action_values)
action_values - torch.max (action_values)
tzeros = torch.zeros ((1, 3))
tzeros
torch.nn.functional.softmax (tzeros, dim = 0)
torch.nn.functional.softmax (tzeros, dim = 1)
torch.nn.functional.softmax (action_values, dim = 1)         # what you want
torch.nn.functional.softmax (action_values - 2.3, dim = 1)   # shift drops out
</code></pre>
<p>Here is its output:</p>
<pre><code class="lang-plaintext">&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'1.6.0'
&gt;&gt;&gt; action_values = torch.tensor([[-0.4001, -0.2948, 0.1288]])
&gt;&gt;&gt; action_values
tensor([[-0.4001, -0.2948,  0.1288]])
&gt;&gt;&gt; max (action_values)         # this is python's max, not pytorch's
tensor([-0.4001, -0.2948,  0.1288])
&gt;&gt;&gt; torch.max (action_values)   # pytorch's tensor-version of max
tensor(0.1288)
&gt;&gt;&gt; action_values - max (action_values)
tensor([[0., 0., 0.]])
&gt;&gt;&gt; action_values - torch.max (action_values)
tensor([[-0.5289, -0.4236,  0.0000]])
&gt;&gt;&gt; tzeros = torch.zeros ((1, 3))
&gt;&gt;&gt; tzeros
tensor([[0., 0., 0.]])
&gt;&gt;&gt; torch.nn.functional.softmax (tzeros, dim = 0)                               tensor([[1., 1., 1.]])
&gt;&gt;&gt; torch.nn.functional.softmax (tzeros, dim = 1)                               tensor([[0.3333, 0.3333, 0.3333]])
&gt;&gt;&gt; torch.nn.functional.softmax (action_values, dim = 1)         # what you want
tensor([[0.2626, 0.2918, 0.4456]])
&gt;&gt;&gt; torch.nn.functional.softmax (action_values - 2.3, dim = 1)   # shift drops out
tensor([[0.2626, 0.2918, 0.4456]])
</code></pre>
<p>Best.</p>
<p>K. Frank</p>