Pytorch cosine similarity NxN elements
I have 128 vectors of embeddings
image.shape = torch.Size([128, 512])
text.shape = torch.Size([128, 512])

And I want to calculate the tensor containing the cosine similarity between all elements (i.e:
cosine.shape = torch.Size([128, 128])

Where the first row is the cosine similarity between the 1st image and all text (128), etc.
At the moment I'm only doing this, but the result is a one-dimension array containing only N cosine similarities.
cosine_similarity = torch.nn.CosineSimilarity()

cosine = cosine_similarity(image, text)

How can I do it? I tried to transpose text but didn't work

The way pytorch computes cosine similarity internally is like this:
    def cos_sim(A, B, dim, eps=1e-08):
      numerator = torch.mul(A, B).sum(axis=dim, keepdims=True)
      A_l2 = torch.mul(A, A).sum(axis=dim, keepdims=True)
      B_l2 = torch.mul(B, B).sum(axis=dim, keepdims=True)
      denominator = torch.max(torch.sqrt(torch.mul(A_l2, B_l2)) torch.tensor(eps))
      return torch.div(numerator, denominator).squeeze()

In order to get NxN cosine similarity, you can instead use this function:
    def nxn_cos_sim(A, B, dim=1, eps=1e-8):
      numerator = A @ B.T
      A_l2 = torch.mul(A, A).sum(axis=dim)
      B_l2 = torch.mul(B, B).sum(axis=dim)
      denominator = torch.max(torch.sqrt(torch.outer(A_l2, B_l2)), torch.tensor(eps))
      return torch.div(numerator, denominator)

Ahmed's answer also works, but if the value for N is large you will face memory issues using it.
