An UserWarning
zgplvyou opened this issue · 5 comments
UserWarning: invalid index of a 0-dim tensor. This will be an error in PyTorch 0.5. Use tensor.item() to convert a 0-dim tensor to a Python number
train_loss += loss.data[0]
so transforming the 'train_loss += loss.data[0] ' into 'train_loss+=loss.item() ' in the 'train.py:200' can fix the warning on the new pytorch version.
I have added the code to automatically select pytorch version.
/home/kerax/usys/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:31: UserWarning: invalid index of a 0-dim tensor. This will be an error in PyTorch 0.5. Use tensor.item() to convert a 0-dim tensor to a Python number
/home/kerax/usys/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:32: UserWarning: invalid index of a 0-dim tensor. This will be an error in PyTorch 0.5. Use tensor.item() to convert a 0-dim tensor to a Python number
Iter: 0, D: 1.351, G:0.6499
Hi @biswajitcsecu
You may change something in your code.
For example,
version = torch.__version__
if int(version[2]) > 3: # for the new version like 0.4.0 and 0.5.0
running_loss += loss.item()
else : # for the old version like 0.3.0 and 0.3.1
running_loss += loss.data[0]
thank you