I have made an attempt, with the following method:


utilize itertools.permutations to index one of the tensors;


repeat the other tensor along a new axis;


calculate sum of square difference along last axes


find minimum along the new, permutation axis


take average over batch.


def Loss (target, input):
        #calculate indices
        idx = torch.from_numpy(np.array(list(itertools.permutations(range(output.shape[-2])))))
        #index tensor with indices
        input_perms = input[:, idx, :]

        #repeat other tensor to same length
        target_perms = target.unsqueeze(1).repeat(1,len(idx),1,1)

        #calculate sum of squares
        losses = (input_perms - target_perms)**2
        losses = losses.flatten(start_dim = -2)
        loss_len = losses.shape[-1]
        losses = torch.sum(losses, dim=-1)
        
        #calculate minimum along permutation axis and then mean along batch axis. Remember, we need still to divide through by the number of entries in each of the parts onf the tensor we summed!
        min_loss = torch.mean(losses.min(dim=-1, keepdim=True)[0])/loss_len
        return min_loss