How to calculate cosine similarity of two multi-demensional vectors?

How to calculate cosine similarity of two multi-demensional vectors through torch.cosine_similarity?

The docs give you an example:

input1 = torch.randn(100, 128)
input2 = torch.randn(100, 128)
output = F.cosine_similarity(input1, input2)
print(output)

If you want to use more dimensions, refer to the docs for the shape explanation.
E.g. for a 4-dim tensor, where you would like to compute the distance along dim2, this code should work:

input1 = torch.randn(100, 128, 32, 32)
input2 = torch.randn(100, 128, 32, 32)
output = F.cosine_similarity(input1, input2, dim=2)
print(output)
1 Like

Thanks your reply!
But if I want to calculate cosine similarity between one dimension and the fourth dimension, How should I do?

You could permute one tensor, so that the corresponding axes are in the same dimension.

I give you my current example.
I obtain two tensor which are [1,3,512,512] by neural network.
I mean that I want to calculate cosine similarity of four-dim tensor.
Can I use torch.cosine_similarity(input1,input2,dim=3)?
Or I only compute the sum of each dimension first, and then average each dimension.

Yes, you should be able to use dim=3, if both tensors have the printed shape.
Are you seeing any errors using it?

That might depend on your use case and what you would like to achieve, so I can’t be really helpful here.

Thanks your reply!
I mean that the two four-dim tensors are two feature vectors, I want to calculate the similarity of the two feature vectors.