wip.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 Union
  15. from torch import Tensor, tensor
  16. from torchmetrics.functional.text.helper import _edit_distance
  17. def _wip_update(
  18. preds: Union[str, list[str]],
  19. target: Union[str, list[str]],
  20. ) -> tuple[Tensor, Tensor, Tensor]:
  21. """Update the wip score with the current set of references and predictions.
  22. Args:
  23. preds: Transcription(s) to score as a string or list of strings
  24. target: Reference(s) for each speech input as a string or list of strings
  25. Returns:
  26. Number of edit operations to get from the reference to the prediction, summed over all samples
  27. Number of words overall references
  28. Number of words overall prediction
  29. """
  30. if isinstance(preds, str):
  31. preds = [preds]
  32. if isinstance(target, str):
  33. target = [target]
  34. total = tensor(0.0)
  35. errors = tensor(0.0)
  36. target_total = tensor(0.0)
  37. preds_total = tensor(0.0)
  38. for pred, tgt in zip(preds, target):
  39. pred_tokens = pred.split()
  40. target_tokens = tgt.split()
  41. errors += _edit_distance(pred_tokens, target_tokens)
  42. target_total += len(target_tokens)
  43. preds_total += len(pred_tokens)
  44. total += max(len(target_tokens), len(pred_tokens))
  45. return errors - total, target_total, preds_total
  46. def _wip_compute(errors: Tensor, target_total: Tensor, preds_total: Tensor) -> Tensor:
  47. """Compute the Word Information Preserved.
  48. Args:
  49. errors: Number of edit operations to get from the reference to the prediction, summed over all samples
  50. target_total: Number of words overall references
  51. preds_total: Number of words overall prediction
  52. Returns:
  53. Word Information Preserved score
  54. """
  55. return (errors / target_total) * (errors / preds_total)
  56. def word_information_preserved(preds: Union[str, list[str]], target: Union[str, list[str]]) -> Tensor:
  57. """Word Information Preserved rate is a metric of the performance of an automatic speech recognition system.
  58. This value indicates the percentage of characters that were incorrectly predicted. The lower the value, the
  59. better the performance of the ASR system with a Word Information preserved rate of 0 being a perfect score.
  60. Args:
  61. preds: Transcription(s) to score as a string or list of strings
  62. target: Reference(s) for each speech input as a string or list of strings
  63. Returns:
  64. Word Information preserved rate
  65. Examples:
  66. >>> from torchmetrics.functional.text import word_information_preserved
  67. >>> preds = ["this is the prediction", "there is an other sample"]
  68. >>> target = ["this is the reference", "there is another one"]
  69. >>> word_information_preserved(preds, target)
  70. tensor(0.3472)
  71. """
  72. errors, reference_total, prediction_total = _wip_update(preds, target)
  73. return _wip_compute(errors, reference_total, prediction_total)