Runtime Error for Crossentropy Loss

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.

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

Thank you for reaching out. I tried to run that segment separately and then it did not throw an error.

For the above snippet, there was no error.
Screenshot from 2024-01-17 09-59-14

However, in the model pipeline, I get the error. I have printed out the predictions, targets and loss from the model.
Screenshot from 2024-01-17 10-00-06