cosine_similarity.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright The Lightning team.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Optional
  15. import torch
  16. from torch import Tensor
  17. from torchmetrics.utilities.checks import _check_same_shape
  18. def _cosine_similarity_update(
  19. preds: Tensor,
  20. target: Tensor,
  21. ) -> tuple[Tensor, Tensor]:
  22. """Update and returns variables required to compute Cosine Similarity. Checks for same shape of input tensors.
  23. Args:
  24. preds: Predicted tensor
  25. target: Ground truth tensor
  26. """
  27. _check_same_shape(preds, target)
  28. if preds.ndim != 2:
  29. raise ValueError(
  30. "Expected input to cosine similarity to be 2D tensors of shape `[N,D]` where `N` is the number of samples"
  31. f" and `D` is the number of dimensions, but got tensor of shape {preds.shape}"
  32. )
  33. preds = preds.float()
  34. target = target.float()
  35. return preds, target
  36. def _cosine_similarity_compute(preds: Tensor, target: Tensor, reduction: Optional[str] = "sum") -> Tensor:
  37. """Compute Cosine Similarity.
  38. Args:
  39. preds: Predicted tensor
  40. target: Ground truth tensor
  41. reduction:
  42. The method of reducing along the batch dimension using sum, mean or taking the individual scores
  43. Example:
  44. >>> target = torch.tensor([[1, 2, 3, 4], [1, 2, 3, 4]])
  45. >>> preds = torch.tensor([[1, 2, 3, 4], [-1, -2, -3, -4]])
  46. >>> preds, target = _cosine_similarity_update(preds, target)
  47. >>> _cosine_similarity_compute(preds, target, 'none')
  48. tensor([ 1.0000, -1.0000])
  49. """
  50. dot_product = (preds * target).sum(dim=-1)
  51. preds_norm = preds.norm(dim=-1)
  52. target_norm = target.norm(dim=-1)
  53. similarity = dot_product / (preds_norm * target_norm)
  54. reduction_mapping = {
  55. "sum": torch.sum,
  56. "mean": torch.mean,
  57. "none": lambda x: x,
  58. None: lambda x: x,
  59. }
  60. return reduction_mapping[reduction](similarity) # type: ignore[operator]
  61. def cosine_similarity(preds: Tensor, target: Tensor, reduction: Optional[str] = "sum") -> Tensor:
  62. r"""Compute the `Cosine Similarity`_.
  63. .. math::
  64. cos_{sim}(x,y) = \frac{x \cdot y}{||x|| \cdot ||y||} =
  65. \frac{\sum_{i=1}^n x_i y_i}{\sqrt{\sum_{i=1}^n x_i^2}\sqrt{\sum_{i=1}^n y_i^2}}
  66. where :math:`y` is a tensor of target values, and :math:`x` is a tensor of predictions.
  67. Args:
  68. preds: Predicted tensor with shape ``(N,d)``
  69. target: Ground truth tensor with shape ``(N,d)``
  70. reduction:
  71. The method of reducing along the batch dimension using sum, mean or taking the individual scores
  72. Example:
  73. >>> from torchmetrics.functional.regression import cosine_similarity
  74. >>> target = torch.tensor([[1, 2, 3, 4],
  75. ... [1, 2, 3, 4]])
  76. >>> preds = torch.tensor([[1, 2, 3, 4],
  77. ... [-1, -2, -3, -4]])
  78. >>> cosine_similarity(preds, target, 'none')
  79. tensor([ 1.0000, -1.0000])
  80. """
  81. preds, target = _cosine_similarity_update(preds, target)
  82. return _cosine_similarity_compute(preds, target, reduction)