wjko2 / Domain-Agnostic-Sentence-Specificity-Prediction

13 stars 10 forks source link

trying to access .item() on a tensor of size 1. Should it be data[0]? #1

Closed lucas0 closed 5 years ago

lucas0 commented 5 years ago

when calling:

python3 train.py --gpu_id 0 --test_data twitter

I got:

Traceback (most recent call last):
  File "train.py", line 488, in <module>
    train_acc = trainepoch(epoch)
  File "train.py", line 428, in trainepoch
    all_costs.append(loss.item())
  File "/home/lucas/.local/lib/python3.5/site-packages/torch/autograd/variable.py", line 67, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'Variable' object has no attribute 'item'

since print(type(loss),loss) gives:

<class 'torch.autograd.variable.Variable'> Variable containing:
 59.4868
[torch.cuda.FloatTensor of size 1 (GPU 0)]

Shouldn't it be loss.data[0]?

wjko2 commented 5 years ago

This seems to be a pytorch version issue. The code was tested on pytorch 1.0.0.

lucas0 commented 5 years ago

Thank you for the answer. It is definitely a pytorch version issue.