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)

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.

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