Cyanogenoid / pytorch-vqa

Strong baseline for visual question answering
238 stars 97 forks source link

expanded size of the tensor (2048) must match the existing size (128) #1

Closed pplantinga closed 7 years ago

pplantinga commented 7 years ago

Attempting to run this code without any modifications sometimes results in this error:

will save to logs/2017-09-26_17:30:02.pth
train E000:   0% 0/3396 [00:00<?, ?it/s]Traceback (most recent call last):
  File "train.py", line 128, in <module>
    main()
  File "train.py", line 109, in main
    _ = run(net, train_loader, optimizer, tracker, train=True, prefix='train', epoch=i)
  File "train.py", line 55, in run
    out = net(v, q, q_len)
  File "/usr/lib/python3.6/site-packages/torch/nn/modules/module.py", line 224, in __call__
    result = self.forward(*input, **kwargs)
  File "/usr/lib/python3.6/site-packages/torch/nn/parallel/data_parallel.py", line 58, in forward
    return self.module(*inputs[0], **kwargs[0])
  File "/usr/lib/python3.6/site-packages/torch/nn/modules/module.py", line 224, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/peter/Documents/Repositories/pytorch-vqa/model.py", line 51, in forward
    v = v / (v.norm(p=2, dim=1).expand_as(v) + 1e-8)
  File "/usr/lib/python3.6/site-packages/torch/autograd/variable.py", line 725, in expand_as
    return Expand.apply(self, (tensor.size(),))
  File "/usr/lib/python3.6/site-packages/torch/autograd/_functions/tensor.py", line 111, in forward
    result = i.expand(*new_size)
RuntimeError: The expanded size of the tensor (2048) must match the existing size (128) at non-singleton dimension 1. at /tmp/yaourt-tmp-peter/aur-python-pytorch/src/pytorch-0.2.0/torch/lib/THC/generic/THCTensor.c:323
pplantinga commented 7 years ago

I think this error results from using pytorch 0.2, seems to work fine with 0.1.12

The issue is that reduction functions automatically remove dimensions in 0.2, but not 0.1.12, so adding keepdim=True should fix this issue. I'll submit a PR later.