Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird behavior of CosineSimilarityMetric when used with tensors of shape [d] #2240

Closed
ValerianRey opened this issue Nov 26, 2023 · 0 comments · Fixed by #2241
Closed

Weird behavior of CosineSimilarityMetric when used with tensors of shape [d] #2240

ValerianRey opened this issue Nov 26, 2023 · 0 comments · Fixed by #2241
Labels
bug / fix Something isn't working help wanted Extra attention is needed v1.2.x

Comments

@ValerianRey
Copy link
Contributor

🐛 Bug?

According to the documentation, CosineSimilarityMetric requires tensors of shape [N, d], with N the batch size and d the dimension of the vectors.

Using it with vectors of shape [d] does not raise any error, and the call to compute behaves weirdly and gives confusing results. I'm not sure whether this is the expected behavior, a bug, or if this usage is simply unintended (and then maybe an error could have been raised).

To Reproduce

>>> from torchmetrics import CosineSimilarity
>>> from torch import tensor
>>> 
>>> cosine_similarity = CosineSimilarity(reduction="mean")
>>> a = tensor([1., 1., 1.])
>>> b = tensor([100., 100., 100.])
>>> cosine_similarity(a, b)
tensor(1.)  # a and b have the same direction, so this is normal.
>>> cosine_similarity(b, a)  
tensor(1.)  # same for b and a.
>>> cosine_similarity.compute()
tensor(0.0200)  # I would expect this to be 1 too (the average of the 2 previous calls).

The obtained result (0.02) is actually the cosine similarity between [1, 1, 1, 100, 100, 100] and [100, 100, 100, 1, 1, 1]. I would have expected to get instead the average between the cosine similarity of [1, 1, 1] and [100, 100, 100] and the cosine similarity of [100, 100, 100] and [1, 1, 1], which is 1.

If instead we use it as the documentation says, with tensors of shape [N, d], we get different results:

>>> from torchmetrics import CosineSimilarity
>>> from torch import tensor
>>> 
>>> cosine_similarity = CosineSimilarity(reduction="mean")
>>> a = tensor([[1., 1., 1.]])  # tensor of shape [1, 3] instead of [3]
>>> b = tensor([[100., 100., 100.]])  # tensor of shape [1, 3] instead of [3]
>>> cosine_similarity(a, b)
tensor(1.)
>>> cosine_similarity(b, a)
tensor(1.)
>>> cosine_similarity.compute()
tensor(1.)  # 1 instead of 0.02

Environment:

  • TorchMetrics 1.2.0
  • Python 3.10.10
  • torch 2.1.1
  • Ubuntu 20.04.6 LTS

Additional context

@ValerianRey ValerianRey added bug / fix Something isn't working help wanted Extra attention is needed labels Nov 26, 2023
@Borda Borda added the v1.2.x label Nov 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug / fix Something isn't working help wanted Extra attention is needed v1.2.x
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants