Python IndexError: invalid index of a 0-dim tensor. Use `tensor.item()` in Python or `tensor.item()` in C++ to convert a 0-dim tensor to a number Leave a reply When the error was reported, it was because of loss.data[0] After modification, it becomes loss.item()