Skip to content
This repository has been archived by the owner on May 28, 2019. It is now read-only.

Training Error. Epoch 5. #50

Closed
PetrochukM opened this issue Mar 30, 2018 · 3 comments
Closed

Training Error. Epoch 5. #50

PetrochukM opened this issue Mar 30, 2018 · 3 comments

Comments

@PetrochukM
Copy link

This is the stack trace:

Traceback (most recent call last):
  File "train.py", line 211, in <module>
    main()
  File "train.py", line 199, in main
    train(model, criterion, optimizer, epoch, train_losses)
  File "train.py", line 119, in train
    loss = criterion(output, target[0], target[1])
  File "/home/michael/.local/lib/python2.7/site-packages/torch/nn/modules/module.py", line 206, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/michael/Desktop/loop/model.py", line 42, in forward
    mask_ = mask.expand_as(input)
  File "/home/michael/.local/lib/python2.7/site-packages/torch/autograd/variable.py", line 655, in expand_as
    return Expand(tensor.size())(self)
  File "/home/michael/.local/lib/python2.7/site-packages/torch/autograd/_functions/tensor.py", line 115, in forward
    result = i.expand(*self.sizes)
RuntimeError: The expanded size of the tensor (21) must match the existing size (5) at               non-singleton dimension 0. at /b/wheel/pytorch-src/torch/lib/TH/THStorage.c:99

Any clue what is going on?

@PetrochukM
Copy link
Author

Why are the target lengths:
loss = criterion(output, target[0], target[1])

Used to create a mask on the output:

        mask = self._sequence_mask(target[1]).unsqueeze(2)
        mask_ = mask.expand_as(input)

@PetrochukM
Copy link
Author

Fixed this with:

class MaskedMSE(nn.Module):
    def __init__(self):
        super(MaskedMSE, self).__init__()
        self.criterion = nn.MSELoss(size_average=False)

    # Taken from
    # https://github.com/spro/practical-pytorch/blob/master/seq2seq-translation
    @staticmethod
    def _sequence_mask(sequence_length, max_len):
        batch_size = sequence_length.size(0)
        seq_range = torch.arange(0, max_len).long()
        seq_range_expand = seq_range.unsqueeze(0).expand(batch_size, max_len)
        seq_range_expand = Variable(seq_range_expand)
        if sequence_length.is_cuda:
            seq_range_expand = seq_range_expand.cuda()
        seq_length_expand = sequence_length.unsqueeze(1) \
                                           .expand_as(seq_range_expand)
        return (seq_range_expand < seq_length_expand).t().float()

    def forward(self, input, target, lengths):
        max_len = input.size(0)
        mask = self._sequence_mask(lengths, max_len).unsqueeze(2)
        mask_ = mask.expand_as(input)
        self.loss = self.criterion(input*mask_, target*mask_)
        self.loss = self.loss / mask.sum()
        return self.loss

@adampolyak
Copy link
Contributor

Thanks!
Fixed in master.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants