a-PyTorch-Tutorial-to-Image-Captioning icon indicating copy to clipboard operation
a-PyTorch-Tutorial-to-Image-Captioning copied to clipboard

AttributeError: 'PackedSequence' object has no attribute 'size'

Open gzzyyxh opened this issue 3 years ago • 3 comments

Hello, when python train.py was executed I faced an odd error:

Traceback (most recent call last):
  File "D:\a-PyTorch-Tutorial-to-Image-Captioning\train.py", line 332, in <module>
    main()
  File "D:\a-PyTorch-Tutorial-to-Image-Captioning\train.py", line 111, in main     
    train(train_loader=train_loader,
  File "D:\a-PyTorch-Tutorial-to-Image-Captioning\train.py", line 207, in train    
    top5 = accuracy(scores, targets, 5)
  File "D:\a-PyTorch-Tutorial-to-Image-Captioning\utils.py", line 283, in accuracy 
    batch_size = targets.size(0)
AttributeError: 'PackedSequence' object has no attribute 'size'

So, what can I do? Thank you.

gzzyyxh avatar Jun 07 '22 19:06 gzzyyxh

@gzzyyxh Hi, that's absolutely an easy question. Just replace size with batch_sizes. Is it odd?

OrangeSodahub avatar Jul 24 '22 06:07 OrangeSodahub

Traceback (most recent call last): File "train.py", line 328, in main() File "train.py", line 116, in main epoch=epoch) File "train.py", line 204, in train top5 = accuracy(scores, targets, 5) File "/workspace/data/a-PyTorch-Tutorial-to-Image-Captioning/utils.py", line 286, in accuracy batch_size = targets.batch_sizes(0) TypeError: 'Tensor' object is not callable

Getting this error with the suggested change, can u pls tell me how to make it work

MCA-eng avatar Oct 15 '22 13:10 MCA-eng

@MCA-eng According to the TypeError message you can know targets.batch_sizes is of Tensor type, it cannot be called through () like functions, you should try [] to index.

OrangeSodahub avatar Jan 02 '23 06:01 OrangeSodahub