3venthoriz0n
3venthoriz0n

Reputation: 117

Pytorch BCELoss not accepting lists

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?

Upvotes: 0

Views: 1151

Answers (2)

3venthoriz0n
3venthoriz0n

Reputation: 117

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.

Upvotes: 0

Reuben
Reuben

Reputation: 477

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

Upvotes: 1

Related Questions