CrossEntropyLoss Expected Target Size error

username: 90210
cooked: <p>Hi! I am trying to use CrossEntropyLoss as the loss function for a Linear model but I am getting the following error:</p>
<pre><code class="lang-auto">Expected target size (98, 1), got torch.Size([98, 20, 1])
</code></pre>
<p><em>I realise there is a number of posts about this error already, but none of the proposed solutions worked for me.</em></p>
<p>The predictions from my Linear model are in the shape <code>[98,20,1]</code> and my ground-truth values are in the shape <code>[98,20,1]</code>.</p>
<p>From this <a href="https://discuss.pytorch.org/t/valueerror-expected-target-size-32-7-got-torch-size-32/42409/2">response</a>, I know that the output should be <code>[batch_size, nb_classes]</code> so <code>[98, 20]</code> in my case and the target should be <code>[batch_size]</code> or <code>[98]</code> in my case.</p>
<p>Now my question is, how do I make it so that I can use CrossEntropyLoss without transforming my ground-truth values to <code>[98]</code>? Is this even possible?</p>
<p>Edit: if I transform the ground-truth values to be <code>[98,1]</code> with <code>y = y[:,1]</code>, I get the following error:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/f/dfd4f62497f9cf405bf788acb53e8bc80adfdda2.png" data-download-href="https://discuss.pytorch.org/uploads/default/dfd4f62497f9cf405bf788acb53e8bc80adfdda2" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/d/f/dfd4f62497f9cf405bf788acb53e8bc80adfdda2.png" alt="image" data-base62-sha1="vW6K7cVNh1FXCSk9a3urUWcnqAW" width="690" height="269" data-dominant-color="F6F6F9"><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">847×331 11.7 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>Thank you!</p>

username: Dwight_Foster
cooked: <p>Can you just print out an example target?</p>

username: 90210
cooked: <p>Sure:</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/c/7/c7877529cf7ab392c77dffefa2c2f49ebc3b7fc3.png" alt="image" data-base62-sha1="st7esWN5XpftLudMzAr27zcqvB1" width="372" height="374"></p>
<p>^ this is a <code>print(y[0])</code>.</p>

username: Dwight_Foster
cooked: <p>Ok now I am confused. What are those labels supposed to represent? Are those the softmax scores? Or something else?</p>

username: 90210
cooked: <p>Softmax…I think. Here’s my model:</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/c/8/c899acc494676cd33fd909cabb70254a0b078749.png" alt="image" data-base62-sha1="sCAJWmGJabNaLRF4e4OCcfuT0oN" width="392" height="338"></p>
<p>I am calling it as such:</p>
<p><code>net = Net(inputs=3, hiddens=5, outputs=1)</code></p>
<p>The input to the model is in the shape <code>[batch_size x 3]</code></p>
<p>Hope that makes it more clear?</p>

username: Dwight_Foster
cooked: <p>Ok if they are supposed to be the softmax score then one thing you could do is try just getting the argmax of them using torch.argmax. That would give it the correct shape and it would provide the loss function with the correct label.</p>

username: 90210
cooked: <p>Sorry I am a little confused. Do you mean to get the argmax of the predictions? Or the actual ground-truth values?</p>

username: Dwight_Foster
cooked: <p>the actual ground truth. I don’t think your targets are softmax however because they do not add up to 1. So I am still a little lost on what they are.</p>