CosineSimilarity > 1

Hello, I’m trying to understand the CosineSimilarity for tensors with different dimensions. I don’t quite understand the reason why the output is >1. Shouldn’t cosine similarity always be between -1 to 1?

from torch.nn import CosineSimilarity as cs

f = torch.tensor([[3,2]], dtype = torch.float)
g = torch.tensor([[3,2],[1,2]], dtype = torch.float)

cos = cs(dim =0,eps = 0.3)
z = cos(d,e)
print(z)

output: tensor([1.2649, 1.4142])

Hey @Yunchao_Liu
I think here you would need to keep the dims of both the matrices the same. With reference to the docs we need to have same shape matrices and do the cosine similarity on a said dim.

With that in mind, we will have a value from the range of -1 to 1.

1 Like

Hi @ariG23498 Thanks!

Now I have another question since I need to implement calculating cosine similarity between tensors of different dimensions. This is the problem description