torch-light icon indicating copy to clipboard operation
torch-light copied to clipboard

invalid index of a 0-dim tensor

Open DoliteMatheo opened this issue 5 years ago • 0 comments

Hi ne7ermore, Thank you a lot for your code sharing. I was running your retrieval chatbot code train.py and encountered the following error:

Traceback (most recent call last): File "train.py", line 141, in loss, corrects, acc, size = evaluate() File "train.py", line 104, in evaluate eval_loss += loss.data[0] IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number

I searched for the reason and solution, and found that it was a Pytorch version issue, in version > 0.5 you have to change loss.data[0] to loss.item() as indicated in the error message. I just post this issue in order to remind others. P.S. It seemed that loss.data.item() also worked.

DoliteMatheo avatar Jul 17 '19 03:07 DoliteMatheo