Encounter the problem "IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number".

I encountered the problem as shown in the following pic:
image
When I tried to change loss.data[0] to loss.item(), but encountered another problem, error message is shown as follows:
image
How can I solve this, thank you very much.

I believe loss is a tensor, thus to detach data from tensor you should use loss[0].data