fowlkes_mallows_index.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 collections.abc import Sequence
  15. from typing import Any, List, Optional, Union
  16. from torch import Tensor
  17. from torchmetrics.functional.clustering import fowlkes_mallows_index
  18. from torchmetrics.metric import Metric
  19. from torchmetrics.utilities.data import dim_zero_cat
  20. from torchmetrics.utilities.imports import _MATPLOTLIB_AVAILABLE
  21. from torchmetrics.utilities.plot import _AX_TYPE, _PLOT_OUT_TYPE
  22. if not _MATPLOTLIB_AVAILABLE:
  23. __doctest_skip__ = ["FowlkesMallowsIndex.plot"]
  24. class FowlkesMallowsIndex(Metric):
  25. r"""Compute `Fowlkes-Mallows Index`_.
  26. .. math::
  27. FMI(U,V) = \frac{TP}{\sqrt{(TP + FP) * (TP + FN)}}
  28. Where :math:`TP` is the number of true positives, :math:`FP` is the number of false positives, and :math:`FN` is
  29. the number of false negatives.
  30. As input to ``forward`` and ``update`` the metric accepts the following input:
  31. - ``preds`` (:class:`~torch.Tensor`): single integer tensor with shape ``(N,)`` with predicted cluster labels
  32. - ``target`` (:class:`~torch.Tensor`): single integer tensor with shape ``(N,)`` with ground truth cluster labels
  33. As output of ``forward`` and ``compute`` the metric returns the following output:
  34. - ``fmi`` (:class:`~torch.Tensor`): A tensor with the Fowlkes-Mallows index.
  35. Args:
  36. kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.
  37. Example::
  38. >>> import torch
  39. >>> from torchmetrics.clustering import FowlkesMallowsIndex
  40. >>> preds = torch.tensor([2, 2, 0, 1, 0])
  41. >>> target = torch.tensor([2, 2, 1, 1, 0])
  42. >>> fmi = FowlkesMallowsIndex()
  43. >>> fmi(preds, target)
  44. tensor(0.5000)
  45. """
  46. is_differentiable: bool = True
  47. higher_is_better: Optional[bool] = True
  48. full_state_update: bool = False
  49. plot_lower_bound: float = 0.0
  50. plot_upper_bound: float = 1.0
  51. preds: List[Tensor]
  52. target: List[Tensor]
  53. def __init__(self, **kwargs: Any) -> None:
  54. super().__init__(**kwargs)
  55. self.add_state("preds", default=[], dist_reduce_fx="cat")
  56. self.add_state("target", default=[], dist_reduce_fx="cat")
  57. def update(self, preds: Tensor, target: Tensor) -> None:
  58. """Update state with predictions and targets."""
  59. self.preds.append(preds)
  60. self.target.append(target)
  61. def compute(self) -> Tensor:
  62. """Compute Fowlkes-Mallows index over state."""
  63. return fowlkes_mallows_index(dim_zero_cat(self.preds), dim_zero_cat(self.target))
  64. def plot(self, val: Union[Tensor, Sequence[Tensor], None] = None, ax: Optional[_AX_TYPE] = None) -> _PLOT_OUT_TYPE:
  65. """Plot a single or multiple values from the metric.
  66. Args:
  67. val: Either a single result from calling `metric.forward` or `metric.compute` or a list of these results.
  68. If no value is provided, will automatically call `metric.compute` and plot that result.
  69. ax: An matplotlib axis object. If provided will add plot to that axis
  70. Returns:
  71. Figure and Axes object
  72. Raises:
  73. ModuleNotFoundError:
  74. If `matplotlib` is not installed
  75. .. plot::
  76. :scale: 75
  77. >>> # Example plotting a single value
  78. >>> import torch
  79. >>> from torchmetrics.clustering import FowlkesMallowsIndex
  80. >>> metric = FowlkesMallowsIndex()
  81. >>> metric.update(torch.randint(0, 4, (10,)), torch.randint(0, 4, (10,)))
  82. >>> fig_, ax_ = metric.plot(metric.compute())
  83. .. plot::
  84. :scale: 75
  85. >>> # Example plotting multiple values
  86. >>> import torch
  87. >>> from torchmetrics.clustering import FowlkesMallowsIndex
  88. >>> metric = FowlkesMallowsIndex()
  89. >>> values = [ ]
  90. >>> for _ in range(10):
  91. ... values.append(metric(torch.randint(0, 4, (10,)), torch.randint(0, 4, (10,))))
  92. >>> fig_, ax_ = metric.plot(values)
  93. """
  94. return self._plot(val, ax)