BCELoss not accepting lists


My convLSTM model returns a list of hidden states and my target is a list of 17 images( all tensors)
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):
    # Set target, images 2 to 18
    target = data[1:]
    if gpu:
        data = data.cuda()
        target = target.cuda()
    # Get outputs of LSTM
    output = model(data, hc)
    # Calculate loss
    loss = criterion(output, target)

How can I fix this?

You need to pass torch.Tensor to criterion instead of list
target = torch.Tensor(data[1:])

tried that. I’m still getting the same error. May be because ‘output’ is also a list of tensors? I tried casting output the same way. but I get a new error now:

ValueError: only one element tensors can be converted to Python scalars

Can you post the full traceback?
From the above traceback, target seems to be the cause for exception.

Here you go:

train.py:129: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
  target = torch.tensor(data[1:])
Traceback (most recent call last):
  File "train.py", line 149, in <module>
  File "train.py", line 137, in train
    output = torch.tensor(output)
ValueError: only one element tensors can be converted to Python scalars

Can you print shape and type of the variables data, target and hc ?