Runtime Error for Crossentropy Loss

Dasun_Premathilaka: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/5/f53f140b74c8cde32e2c3b3c2a6b16443ad13d94.png" data-download-href="https://discuss.pytorch.org/uploads/default/f53f140b74c8cde32e2c3b3c2a6b16443ad13d94" title="Screenshot from 2024-01-17 09-01-30"><img src="https://discuss.pytorch.org/uploads/default/original/3X/f/5/f53f140b74c8cde32e2c3b3c2a6b16443ad13d94.png" alt="Screenshot from 2024-01-17 09-01-30" data-base62-sha1="yZy7K5KOUEM3zRopQGgVo7oGxtW" width="690" height="63" data-dominant-color="292B32"><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">Screenshot from 2024-01-17 09-01-30</span><span class="informations">803×74 11.5 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>I am getting a runtime error when the code tries to run loss.backward(). The loss is calculated yet the loss.backward() line does not work. As mentioned in other discussions, I have checked the dtype of the targets (int64) and predictions (float32) as well. Would be grateful if anyone could comment on this. Thank you in advance.</p>

ptrblck: <p>Could you please post a minimal and executable code snippet to reproduce the issue, as I haven’t seen this particular error message?</p>

Dasun_Premathilaka: <p>Thank you for reaching out. I tried to run that segment separately and then it did not throw an error.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/6/56ce21c00957443d6c4b9432757e4639dc37612d.png" data-download-href="https://discuss.pytorch.org/uploads/default/56ce21c00957443d6c4b9432757e4639dc37612d" title="Screenshot from 2024-01-17 09-58-51"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/6/56ce21c00957443d6c4b9432757e4639dc37612d_2_690x239.png" alt="Screenshot from 2024-01-17 09-58-51" data-base62-sha1="cnUHgEHDdbwIAQW1MHc96r8feX3" width="690" height="239" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/6/56ce21c00957443d6c4b9432757e4639dc37612d_2_690x239.png, https://discuss.pytorch.org/uploads/default/original/3X/5/6/56ce21c00957443d6c4b9432757e4639dc37612d.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/5/6/56ce21c00957443d6c4b9432757e4639dc37612d.png 2x" data-dominant-color="222326"><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">Screenshot from 2024-01-17 09-58-51</span><span class="informations">789×274 22.5 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>

Dasun_Premathilaka: <p>For the above snippet, there was no error.<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/e/3/e3d032998f1a6610175592ac7afb593131cd4384.png" alt="Screenshot from 2024-01-17 09-59-14" data-base62-sha1="wvkrJ57fEKjXV7NBTkC7sp9uql6" width="498" height="92"></p>

Dasun_Premathilaka: <p>However, in the model pipeline, I get the error. I have printed out the predictions, targets and loss from the model.<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/6/3/63a49a5ed81b87908df623a0c53015915008f218.png" alt="Screenshot from 2024-01-17 10-00-06" data-base62-sha1="edtVEwkcy9fOR5MjyrFTZbq1ofu" width="610" height="73"></p>