Could I use that nn.Criterion module compute combined tensor?

(Isaac.russell) #1

For example, I have 6 tensors with different dimension:

tensor11 size: (b, 64, 224, 224)
tensor21 size: (b, 128, 112, 112)
tensor31 size: (b, 256, 56, 56)

tensor12 size: (b, 64, 224, 224)
tensor22 size: (b, 128, 112, 112)
tensor32 size: (b, 256, 56, 56)

Could I get the tensors which tensor1 has the size (tensor11 size, tensor12 size, tensor13 size), tensor2 has the size(tensor12 size, tensor22 size, tensor 32 size).
If possible, how could I use nn.MSELoss() to computer the loss between elements between these 2 tensor on same positions and get these 3 losses between (tensor11, tensor12), (tensor21, tensor22) and (tensor31, tensor32) by one step while these losses will be utilised in BP?
Thanks.

(Olof Harrysson) #2

Hi, maybe you could flatten the tensors and concat them? Use

flatten1 = tensor11.view(1, -1) and torch.cat(flatten1, flatten2, flatten3)

Would it work if you’d just calculate the 3 losses separately and then add them?

mse = nn.MSELoss()
loss1 = mse(tensor1l, tensor12)
loss2 = mse(tensor2l, tensor22)
loss3 = mse(tensor3l, tensor32)
total_loss = loss1 + loss2 + loss3
# backpropagate
(Isaac.russell) #3

Thanks very much. I calculate 3 losses respectively and concatenate them by () now. It’s weird that the loss can be used in back propagation in this way. I mean python always seems the element in () as A tuple in my computer :joy:

1 Like
(Olof Harrysson) #4

The wonders of Pytorch :smiley: