1. 程式人生 > >pytorch報錯:UserWarning: invalid index of a 0-dim tensor. This will be an error in PyTorch 0.5.。。原因

pytorch報錯:UserWarning: invalid index of a 0-dim tensor. This will be an error in PyTorch 0.5.。。原因

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

當報這個錯的一般會有兩個原因,一個是這種寫法:

loss.data[0]   

應該改成loss.item()

還有就是這種寫法:

im = Variable(im.cuda(),volatile=True)

應該改成im = Variable(im.cuda())