lancopku / SGM

Sequence Generation Model for Multi-label Classification (COLING 2018)
432 stars 113 forks source link

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

Closed mochp closed 4 years ago

ypengc7512 commented 5 years ago

Can you provide more detailed information?

peterjtan commented 5 years ago

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 5 years ago

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
peterjtan commented 5 years ago

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

mochp commented 5 years ago

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

peterjtan commented 5 years ago

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 5 years ago

sorry, misoperation, I have the same problem.

mochp commented 5 years ago

Can you provide more detailed information?

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

mochp commented 5 years ago

Can you provide more detailed information?

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

and python==3.6

ypengc7512 commented 5 years ago

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 5 years ago

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

HaimianYu commented 5 years ago

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

haoyz commented 5 years ago

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

fzy0728 commented 5 years ago

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