I have a tensor A to size (batch_size, n, m). For each training example in batch, I want to calculate L2 norm between all possible two pairs along third dimension.
Explaining clearly: I want to do dot product for two specific training examples of size (n, m) i.e. for all n vectors of dimension m, I want to calculate L2 norm(tensors undergoing L2 norm will have size (m, 1)) and total number of dot products will be nC2. So my final tensor will be of size (batch_size, nC2). Is there an efficient way to do this other than explicitly writing for loops. Also, is there a way to do this by just two for loops?
Currently I’m doing:
final = []
for x in range(batch_size):
    for y in range(n):
        for z in range (y+1, n):
            final.append(torch.dist(A[x,y,:], A[x,z,:], p=2)
final = torch.stack(final) 
final = final.view(batch, -1)