PyTorch: Batch Outer-Addition
I have two PyTorch tensors: A and B, both of shape (b, c, 3). I want to make outer product C of A and B so that the resulting shape is (b, c, 3, 3), and replace the multiplication operation with addition. How should I do it?

You can add a corresponding singleton dimension:

C = A[..., None] + B[..., None, :]


For example, with batch and channel dimensions equal to 1 (b=1, c=1):

import torch
A = torch.tensor([[[1, 2, 3.]]]) 
B = torch.tensor([[[4., 5., 6.]]])
A[..., None] + B[..., None, :]
Out[ ]:
tensor([[[[5., 6., 7.],
          [6., 7., 8.],
          [7., 8., 9.]]]])

