MSELoss always produces 0 - what am I doing wrong?

Sally_Goldin: <p>I’m a pytorch neophyte so please be gentle.  <img src="https://discuss.pytorch.org/images/emoji/apple/grin.png?v=10" title=":grin:" class="emoji" alt=":grin:"></p>
<p>I have trained a model to predict a 5-element output from an input image. The 5 elements must sum to 1.0 so I am using softmax on the output from the last fully connected layer, and using the MSELoss to assess discrepancy between predicted and actual values.</p>
<p>The training process seems to be working well, with the loss gradually declining as expected. However, when I try to validate using new images, the MSELoss is always 0, even though I can see that the predictions are far from perfect. The code is very similar to the training so I am quite perplexed.</p>
<p>Here’s the relevant code:</p>
<pre><code class="lang-auto">
criterion = nn.MSELoss()
with torch.no_grad():
    for i, data in enumerate(valid_generator, 0):
        # get the inputs; data is a dictionary of [inputs, labels]
        inputs = data['inputs']
        labels = data['labels']
        outputs = net(inputs)
        errortensor = criterion(outputs, labels)
        loss = errortensor.item()
        print(outputs)
        print(labels)
        print('loss is %d' % (loss))
        if (loss &lt; minloss):
            minloss = loss
        if (loss &gt; maxloss):
            maxloss = loss
        sumloss = sumloss + loss
        total += labels.size(0)
        imagecount += batch_size
        print('---------')
</code></pre>
<p>And here is a snippet of the output:</p>
<pre><code class="lang-auto">---------
tensor([[0.0718, 0.1101, 0.0555, 0.0925, 0.6701]])
tensor([[0.0000, 0.1000, 0.1000, 0.1000, 0.7000]])
loss is 0
---------
tensor([[0.0823, 0.0229, 0.0689, 0.1235, 0.7024]])
tensor([[0.0000, 0.1000, 0.1000, 0.1000, 0.7000]])
loss is 0
---------
tensor([[0.0836, 0.1115, 0.0604, 0.0858, 0.6588]])
tensor([[0.0000, 0.1000, 0.1000, 0.1000, 0.7000]])
loss is 0
---------
tensor([[0.0707, 0.0771, 0.0481, 0.0745, 0.7296]])
tensor([[0.0000, 0.1000, 0.1000, 0.1000, 0.7000]])
loss is 0
---------
</code></pre>
<p>Why is the loss always zero? (I suspect it’s something simple I am missing.)</p>
<p>Thanks!</p>

KFrank: <p>Hi Sally!</p>
<aside class="quote no-group quote-modified" data-username="Sally_Goldin" data-post="1" data-topic="127931" 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/sally_goldin/48/18108_2.png" class="avatar"> Sally_Goldin:</div>
<blockquote>
<pre><code class="lang-auto">        print('loss is %d' % (loss))
</code></pre>
<p>Why is the loss always zero?</p>
</blockquote>
</aside>
<p>This is a python issue, rather than anything specific to pytorch.</p>
<p><code>%d</code> formats your value as an integer, truncating any fractional part.</p>
<p>By construction, assuming that your <code>labels</code> are between 0 and 1,<br>
your <code>loss</code> will (almost) always be less than one and be truncated<br>
to zero.</p>
<p>That is, <code>MSELoss</code> is not producing 0, but you’re printing it out as 0.</p>
<p>Best.</p>
<p>K. Frank</p>

Sally_Goldin: <p>oh… how embarrassing! Thank you! (I am also new to Python…)</p>