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?
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
.