lancopku/SGM

The size of tensor a (576) must match the size of tensor b (64) at non-singleton dimension 1

mochp opened this issue · 14 comments

mochp commented
The size of tensor a (576) must match the size of tensor b (64) at non-singleton dimension 1

Can you provide more detailed information?

I encountered a similar problem and here is the traceback:

Traceback (most recent call last):
  File "train.py", line 265, in <module>
    main()
  File "train.py", line 257, in main
    train(i)
  File "train.py", line 174, in train
    loss, num_total, _, _, _ = model.compute_loss(outputs, targets, opt.memory)
  File "/Users/peter/Documents/repo/SGM/models/seq2seq.py", line 36, in compute_loss
    return models.cross_entropy_loss(hidden_outputs, self.decoder, targets, self.criterion, self.config)
  File "/Users/peter/Documents/repo/SGM/models/loss.py", line 46, in cross_entropy_loss
    num_correct = pred.data.eq(targets.data).masked_select(targets.ne(dict.PAD).data).sum()
RuntimeError: The size of tensor a (576) must match the size of tensor b (64) at non-singleton dimension 1
mochp commented

I encountered a similar problem and here is the traceback:

Traceback (most recent call last):
  File "train.py", line 265, in <module>
    main()
  File "train.py", line 257, in main
    train(i)
  File "train.py", line 174, in train
    loss, num_total, _, _, _ = model.compute_loss(outputs, targets, opt.memory)
  File "/Users/peter/Documents/repo/SGM/models/seq2seq.py", line 36, in compute_loss
    return models.cross_entropy_loss(hidden_outputs, self.decoder, targets, self.criterion, self.config)
  File "/Users/peter/Documents/repo/SGM/models/loss.py", line 46, in cross_entropy_loss
    num_correct = pred.data.eq(targets.data).masked_select(targets.ne(dict.PAD).data).sum()
RuntimeError: The size of tensor a (576) must match the size of tensor b (64) at non-singleton dimension 1

@mochp I see that you closed the issue. Could you comment on how you fixed this error? Many thanks.

mochp commented

I encountered a similar problem and here is the traceback:

Traceback (most recent call last):
  File "train.py", line 265, in <module>
    main()
  File "train.py", line 257, in main
    train(i)
  File "train.py", line 174, in train
    loss, num_total, _, _, _ = model.compute_loss(outputs, targets, opt.memory)
  File "/Users/peter/Documents/repo/SGM/models/seq2seq.py", line 36, in compute_loss
    return models.cross_entropy_loss(hidden_outputs, self.decoder, targets, self.criterion, self.config)
  File "/Users/peter/Documents/repo/SGM/models/loss.py", line 46, in cross_entropy_loss
    num_correct = pred.data.eq(targets.data).masked_select(targets.ne(dict.PAD).data).sum()
RuntimeError: The size of tensor a (576) must match the size of tensor b (64) at non-singleton dimension 1

the same to you

I encountered a similar problem and here is the traceback:

Traceback (most recent call last):
  File "train.py", line 265, in <module>
    main()
  File "train.py", line 257, in main
    train(i)
  File "train.py", line 174, in train
    loss, num_total, _, _, _ = model.compute_loss(outputs, targets, opt.memory)
  File "/Users/peter/Documents/repo/SGM/models/seq2seq.py", line 36, in compute_loss
    return models.cross_entropy_loss(hidden_outputs, self.decoder, targets, self.criterion, self.config)
  File "/Users/peter/Documents/repo/SGM/models/loss.py", line 46, in cross_entropy_loss
    num_correct = pred.data.eq(targets.data).masked_select(targets.ne(dict.PAD).data).sum()
RuntimeError: The size of tensor a (576) must match the size of tensor b (64) at non-singleton dimension 1

the same to you

So you still have the problem, right? If it is the case, could you re-open the issue? (It is marked as closed now. )

mochp commented

sorry, misoperation, I have the same problem.

mochp commented

Can you provide more detailed information?

my pytorch==0.4.1 ,and the mistake is just like peterjtan.

mochp commented

Can you provide more detailed information?

my pytorch==0.4.1 ,and the mistake is just like peterjtan.

and python==3.6

The provided code needs to be run under Pytorch 0.3. Running under Pytorch 0.4 may cause some errors. I will migrate the code to the Pytorch 0.4 environment some time later.

mochp commented

The provided code needs to be run under Pytorch 0.3. Running under Pytorch 0.4 may cause some errors. I will migrate the code to the Pytorch 0.4 environment some time later.

thank you

I have the same problem. When would you like to update the code?

haoyz commented

num_correct = pred.data.eq(targets.data.reshape(-1)).masked_select(targets.ne(dict.PAD).data.reshape(-1)).sum()
works for me

models/loss.py line 46
num_correct = pred.data.eq(targets.data.reshape(-1)).masked_select(targets.ne(dict.PAD).data.reshape(-1)).sum()
and line 49
loss = loss.item()
thanks for @haoyz