wil.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 _word_info_lost_update(
  18. preds: Union[str, list[str]],
  19. target: Union[str, list[str]],
  20. ) -> tuple[Tensor, Tensor, Tensor]:
  21. """Update the WIL 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 predictions
  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 _word_info_lost_compute(errors: Tensor, target_total: Tensor, preds_total: Tensor) -> Tensor:
  47. """Compute the Word Information Lost.
  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 Lost score
  54. """
  55. return 1 - ((errors / target_total) * (errors / preds_total))
  56. def word_information_lost(preds: Union[str, list[str]], target: Union[str, list[str]]) -> Tensor:
  57. """Word Information Lost 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 better
  59. the performance of the ASR system with a Word Information Lost 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 Lost rate
  65. Examples:
  66. >>> from torchmetrics.functional.text import word_information_lost
  67. >>> preds = ["this is the prediction", "there is an other sample"]
  68. >>> target = ["this is the reference", "there is another one"]
  69. >>> word_information_lost(preds, target)
  70. tensor(0.6528)
  71. """
  72. errors, target_total, preds_total = _word_info_lost_update(preds, target)
  73. return _word_info_lost_compute(errors, target_total, preds_total)