How to speedup the following code

I’m trying to compute a matrix with pairwise distances. Following is my code, how to optimize the code to run faster.

          for (i,a),(j,b) in itertools.combinations(enumerate(X_meaned),2):
                cov[i,j],cov[j,i]=self.compute(a,b)

Hi @Shivani_Sri_Varshini,

You’ll need to include a more complete minimal reproducible example before people can help debug your code.

@ AlphaBetaGamma96

X_meaned is torch tensor with dimensions B * M * N where B is batch_size M * N is 2D data. I would like to compute the distances of 2D samples within the batch. Code that i mentioned is giving an output of dimension B*B which is expected but for that i’m considering each sample in batch and computing the distance with respect to all other samples in batch.
self.compute can be euclidean distance or cosine distance function