wil.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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, Optional, Union
  16. from torch import Tensor, tensor
  17. from torchmetrics.functional.text.wil import _word_info_lost_compute, _word_info_lost_update
  18. from torchmetrics.metric import Metric
  19. from torchmetrics.utilities.imports import _MATPLOTLIB_AVAILABLE
  20. from torchmetrics.utilities.plot import _AX_TYPE, _PLOT_OUT_TYPE
  21. if not _MATPLOTLIB_AVAILABLE:
  22. __doctest_skip__ = ["WordInfoLost.plot"]
  23. class WordInfoLost(Metric):
  24. r"""Word Information Lost (`WIL`_) is a metric of the performance of an automatic speech recognition system.
  25. This value indicates the percentage of words that were incorrectly predicted between a set of ground-truth
  26. sentences and a set of hypothesis sentences. The lower the value, the better the performance of the ASR system
  27. with a WordInfoLost of 0 being a perfect score. Word Information Lost rate can then be computed as:
  28. .. math::
  29. wil = 1 - \frac{C}{N} + \frac{C}{P}
  30. where:
  31. - :math:`C` is the number of correct words,
  32. - :math:`N` is the number of words in the reference
  33. - :math:`P` is the number of words in the prediction
  34. As input to ``forward`` and ``update`` the metric accepts the following input:
  35. - ``preds`` (:class:`~List`): Transcription(s) to score as a string or list of strings
  36. - ``target`` (:class:`~List`): Reference(s) for each speech input as a string or list of strings
  37. As output of ``forward`` and ``compute`` the metric returns the following output:
  38. - ``wil`` (:class:`~torch.Tensor`): A tensor with the Word Information Lost score
  39. Args:
  40. kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.
  41. Examples:
  42. >>> from torchmetrics.text import WordInfoLost
  43. >>> preds = ["this is the prediction", "there is an other sample"]
  44. >>> target = ["this is the reference", "there is another one"]
  45. >>> wil = WordInfoLost()
  46. >>> wil(preds, target)
  47. tensor(0.6528)
  48. """
  49. is_differentiable: bool = False
  50. higher_is_better: bool = False
  51. full_state_update: bool = False
  52. plot_lower_bound: float = 0.0
  53. plot_upper_bound: float = 1.0
  54. errors: Tensor
  55. target_total: Tensor
  56. preds_total: Tensor
  57. def __init__(
  58. self,
  59. **kwargs: Any,
  60. ) -> None:
  61. super().__init__(**kwargs)
  62. self.add_state("errors", tensor(0.0), dist_reduce_fx="sum")
  63. self.add_state("target_total", tensor(0.0), dist_reduce_fx="sum")
  64. self.add_state("preds_total", tensor(0.0), dist_reduce_fx="sum")
  65. def update(self, preds: Union[str, list[str]], target: Union[str, list[str]]) -> None:
  66. """Update state with predictions and targets."""
  67. errors, target_total, preds_total = _word_info_lost_update(preds, target)
  68. self.errors += errors
  69. self.target_total += target_total
  70. self.preds_total += preds_total
  71. def compute(self) -> Tensor:
  72. """Calculate the Word Information Lost."""
  73. return _word_info_lost_compute(self.errors, self.target_total, self.preds_total)
  74. def plot(
  75. self, val: Optional[Union[Tensor, Sequence[Tensor]]] = None, ax: Optional[_AX_TYPE] = None
  76. ) -> _PLOT_OUT_TYPE:
  77. """Plot a single or multiple values from the metric.
  78. Args:
  79. val: Either a single result from calling `metric.forward` or `metric.compute` or a list of these results.
  80. If no value is provided, will automatically call `metric.compute` and plot that result.
  81. ax: An matplotlib axis object. If provided will add plot to that axis
  82. Returns:
  83. Figure and Axes object
  84. Raises:
  85. ModuleNotFoundError:
  86. If `matplotlib` is not installed
  87. .. plot::
  88. :scale: 75
  89. >>> # Example plotting a single value
  90. >>> from torchmetrics.text import WordInfoLost
  91. >>> metric = WordInfoLost()
  92. >>> preds = ["this is the prediction", "there is an other sample"]
  93. >>> target = ["this is the reference", "there is another one"]
  94. >>> metric.update(preds, target)
  95. >>> fig_, ax_ = metric.plot()
  96. .. plot::
  97. :scale: 75
  98. >>> # Example plotting multiple values
  99. >>> from torchmetrics.text import WordInfoLost
  100. >>> metric = WordInfoLost()
  101. >>> preds = ["this is the prediction", "there is an other sample"]
  102. >>> target = ["this is the reference", "there is another one"]
  103. >>> values = [ ]
  104. >>> for _ in range(10):
  105. ... values.append(metric(preds, target))
  106. >>> fig_, ax_ = metric.plot(values)
  107. """
  108. return self._plot(val, ax)