This example will make it easier to understand. The following fails:
A = tensor.torch([[1, 2, 3], [4, 5, 6]]) # shape : (2, 3)
B = tensor.torch([[1, 2], [3, 4], [5, 6]]) # shape : (3, 2)
print((A - B).shape)
# RuntimeError: The size of tensor A (3) must match the size of tensor B (2) at non-singleton dimension 1
# ==================================================================
A = tensor.torch([[1, 2], [3, 4], [5, 6]]) # shape : (3, 2)
B = tensor.torch([[1, 2], [3, 4],]) # shape : (2, 2)
print((A - B).shape)
# RuntimeError: The size of tensor A (3) must match the size of tensor B (2) at non-singleton dimension 0
But the following works well:
a = torch.ones(8).unsqueeze(0).unsqueeze(-1).expand(4, 8, 7)
a_temp = a.unsqueeze(2) # shape : ( 4, 8, 1, 7 )
b_temp = torch.transpose(a_temp, 1, 2) # shape : ( 4, 1, 8, 7 )
print(a_temp-b_temp) # shape : ( 4, 8, 8, 7 )
Why does the latter work, but not the former?
How/why has the result shape been expanded?
question from:
https://stackoverflow.com/questions/66059474/why-torch-tensor-subtract-works-well-when-tensor-size-is-different 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…