0

My convLSTM model returns a list of hidden states (17 total, size (1,3,128,128)) and my target is a list of 17 images( all tensors size: (3,128,128) When the loss function is called, I get the following error:

File "/Users/xyz/opt/anaconda3/envs/matrix/lib/python3.7/site->packages/torch/nn/modules/loss.py", line 498, in forward return F.binary_cross_entropy(input, target, weight=self.weight, >reduction=self.reduction) File "/Users/xyz/opt/anaconda3/envs/matrix/lib/python3.7/site->packages/torch/nn/functional.py", line 2052, in binary_cross_entropy if target.size() != input.size(): AttributeError: 'list' object has no attribute 'size'

Part of the training loop:

    hc = model.init_hidden(batch_size=1)
    for batch_idx, (data, target) in enumerate(train_loader):
        optimizer.zero_grad()
        # Set target, images 2 to 18
        target = data[1:]
        if gpu:
            data = data.cuda()
            target = target.cuda()
            hc.cuda()
        # Get outputs of LSTM
        output = model(data, hc)
        # Calculate loss
        loss = criterion(output, target)
        loss.backward()
        optimizer.step()

I was expecting a size mismatch error but got this instead. How can I fix this?

3venthoriz0n
  • 117
  • 2
  • 12

2 Answers2

1

target needs to be a tensor, not a list of tensors.

Example

    >>> m = nn.Sigmoid()
    >>> loss = nn.BCELoss()
    >>> input = torch.randn(3, requires_grad=True)
    >>> target = torch.empty(3).random_(2) #This is a tensor, not a list
    >>> output = loss(m(input), target)
    >>> output.backward()

Take a look at BCELoss in torch.nn.modules.loss or torch.nn

Reuben
  • 467
  • 3
  • 9
  • I tried doing: `target = torch.tensor(data[1:]` I got the same error. Then I tried doing `output = torch.tensor(output)` now I get the following error: ValueError: only one element tensors can be converted to Python scalars – 3venthoriz0n Nov 26 '19 at 16:33
  • You'll have to concatenate the list of tensors to create a tensor. Please refer [torch.cat](https://pytorch.org/docs/stable/torch.html#torch.cat) – Reuben Nov 27 '19 at 04:53
0

Hi I solved it by using torch.stack. Could have used torch.cat but wanted a tensor with a list of tensors to pass to the loss function to match the target format so used torch.stack.

3venthoriz0n
  • 117
  • 2
  • 12