_deprecated.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. from collections.abc import Sequence
  2. from typing import Any, Literal, Optional
  3. from torchmetrics.text.bleu import BLEUScore
  4. from torchmetrics.text.cer import CharErrorRate
  5. from torchmetrics.text.chrf import CHRFScore
  6. from torchmetrics.text.eed import ExtendedEditDistance
  7. from torchmetrics.text.mer import MatchErrorRate
  8. from torchmetrics.text.perplexity import Perplexity
  9. from torchmetrics.text.sacre_bleu import SacreBLEUScore
  10. from torchmetrics.text.squad import SQuAD
  11. from torchmetrics.text.ter import TranslationEditRate
  12. from torchmetrics.text.wer import WordErrorRate
  13. from torchmetrics.text.wil import WordInfoLost
  14. from torchmetrics.text.wip import WordInfoPreserved
  15. from torchmetrics.utilities.prints import _deprecated_root_import_class
  16. class _BLEUScore(BLEUScore):
  17. """Wrapper for deprecated import.
  18. >>> preds = ['the cat is on the mat']
  19. >>> target = [['there is a cat on the mat', 'a cat is on the mat']]
  20. >>> bleu = _BLEUScore()
  21. >>> bleu(preds, target)
  22. tensor(0.7598)
  23. """
  24. def __init__(
  25. self,
  26. n_gram: int = 4,
  27. smooth: bool = False,
  28. weights: Optional[Sequence[float]] = None,
  29. **kwargs: Any,
  30. ) -> None:
  31. _deprecated_root_import_class("BLEUScore", "text")
  32. super().__init__(n_gram=n_gram, smooth=smooth, weights=weights, **kwargs)
  33. class _CharErrorRate(CharErrorRate):
  34. """Wrapper for deprecated import.
  35. >>> preds = ["this is the prediction", "there is an other sample"]
  36. >>> target = ["this is the reference", "there is another one"]
  37. >>> cer = _CharErrorRate()
  38. >>> cer(preds, target)
  39. tensor(0.3415)
  40. """
  41. def __init__(
  42. self,
  43. **kwargs: Any,
  44. ) -> None:
  45. _deprecated_root_import_class("CharErrorRate", "text")
  46. super().__init__(**kwargs)
  47. class _CHRFScore(CHRFScore):
  48. """Wrapper for deprecated import.
  49. >>> preds = ['the cat is on the mat']
  50. >>> target = [['there is a cat on the mat', 'a cat is on the mat']]
  51. >>> chrf = _CHRFScore()
  52. >>> chrf(preds, target)
  53. tensor(0.8640)
  54. """
  55. def __init__(
  56. self,
  57. n_char_order: int = 6,
  58. n_word_order: int = 2,
  59. beta: float = 2.0,
  60. lowercase: bool = False,
  61. whitespace: bool = False,
  62. return_sentence_level_score: bool = False,
  63. **kwargs: Any,
  64. ) -> None:
  65. _deprecated_root_import_class("CHRFScore", "text")
  66. super().__init__(
  67. n_char_order=n_char_order,
  68. n_word_order=n_word_order,
  69. beta=beta,
  70. lowercase=lowercase,
  71. whitespace=whitespace,
  72. return_sentence_level_score=return_sentence_level_score,
  73. **kwargs,
  74. )
  75. class _ExtendedEditDistance(ExtendedEditDistance):
  76. """Wrapper for deprecated import.
  77. >>> preds = ["this is the prediction", "here is an other sample"]
  78. >>> target = ["this is the reference", "here is another one"]
  79. >>> eed = _ExtendedEditDistance()
  80. >>> eed(preds=preds, target=target)
  81. tensor(0.3078)
  82. """
  83. def __init__(
  84. self,
  85. language: Literal["en", "ja"] = "en",
  86. return_sentence_level_score: bool = False,
  87. alpha: float = 2.0,
  88. rho: float = 0.3,
  89. deletion: float = 0.2,
  90. insertion: float = 1.0,
  91. **kwargs: Any,
  92. ) -> None:
  93. _deprecated_root_import_class("ExtendedEditDistance", "text")
  94. super().__init__(
  95. language=language,
  96. return_sentence_level_score=return_sentence_level_score,
  97. alpha=alpha,
  98. rho=rho,
  99. deletion=deletion,
  100. insertion=insertion,
  101. **kwargs,
  102. )
  103. class _MatchErrorRate(MatchErrorRate):
  104. """Wrapper for deprecated import.
  105. >>> preds = ["this is the prediction", "there is an other sample"]
  106. >>> target = ["this is the reference", "there is another one"]
  107. >>> mer = _MatchErrorRate()
  108. >>> mer(preds, target)
  109. tensor(0.4444)
  110. """
  111. def __init__(
  112. self,
  113. **kwargs: Any,
  114. ) -> None:
  115. _deprecated_root_import_class("MatchErrorRate", "text")
  116. super().__init__(**kwargs)
  117. class _Perplexity(Perplexity):
  118. """Wrapper for deprecated import.
  119. >>> from torch import rand, randint
  120. >>> preds = rand(2, 8, 5)
  121. >>> target = randint(5, (2, 8))
  122. >>> target[0, 6:] = -100
  123. >>> perp = _Perplexity(ignore_index=-100)
  124. >>> perp(preds, target)
  125. tensor(5.8540)
  126. """
  127. def __init__(
  128. self,
  129. ignore_index: Optional[int] = None,
  130. **kwargs: Any,
  131. ) -> None:
  132. _deprecated_root_import_class("Perplexity", "text")
  133. super().__init__(ignore_index=ignore_index, **kwargs)
  134. class _SacreBLEUScore(SacreBLEUScore):
  135. """Wrapper for deprecated import.
  136. >>> preds = ['the cat is on the mat']
  137. >>> target = [['there is a cat on the mat', 'a cat is on the mat']]
  138. >>> sacre_bleu = _SacreBLEUScore()
  139. >>> sacre_bleu(preds, target)
  140. tensor(0.7598)
  141. """
  142. def __init__(
  143. self,
  144. n_gram: int = 4,
  145. smooth: bool = False,
  146. tokenize: Literal["none", "13a", "zh", "intl", "char"] = "13a",
  147. lowercase: bool = False,
  148. weights: Optional[Sequence[float]] = None,
  149. **kwargs: Any,
  150. ) -> None:
  151. _deprecated_root_import_class("SacreBLEUScore", "text")
  152. super().__init__(
  153. n_gram=n_gram, smooth=smooth, tokenize=tokenize, lowercase=lowercase, weights=weights, **kwargs
  154. )
  155. class _SQuAD(SQuAD):
  156. """Wrapper for deprecated import.
  157. >>> preds = [{"prediction_text": "1976", "id": "56e10a3be3433e1400422b22"}]
  158. >>> target = [{"answers": {"answer_start": [97], "text": ["1976"]}, "id": "56e10a3be3433e1400422b22"}]
  159. >>> squad = _SQuAD()
  160. >>> squad(preds, target)
  161. {'exact_match': tensor(100.), 'f1': tensor(100.)}
  162. """
  163. def __init__(self, **kwargs: Any) -> None:
  164. _deprecated_root_import_class("SQuAD", "text")
  165. super().__init__(**kwargs)
  166. class _TranslationEditRate(TranslationEditRate):
  167. """Wrapper for deprecated import.
  168. >>> preds = ['the cat is on the mat']
  169. >>> target = [['there is a cat on the mat', 'a cat is on the mat']]
  170. >>> ter = _TranslationEditRate()
  171. >>> ter(preds, target)
  172. tensor(0.1538)
  173. """
  174. def __init__(
  175. self,
  176. normalize: bool = False,
  177. no_punctuation: bool = False,
  178. lowercase: bool = True,
  179. asian_support: bool = False,
  180. return_sentence_level_score: bool = False,
  181. **kwargs: Any,
  182. ) -> None:
  183. _deprecated_root_import_class("TranslationEditRate", "text")
  184. super().__init__(
  185. normalize=normalize,
  186. no_punctuation=no_punctuation,
  187. lowercase=lowercase,
  188. asian_support=asian_support,
  189. return_sentence_level_score=return_sentence_level_score,
  190. **kwargs,
  191. )
  192. class _WordErrorRate(WordErrorRate):
  193. """Wrapper for deprecated import.
  194. >>> preds = ["this is the prediction", "there is an other sample"]
  195. >>> target = ["this is the reference", "there is another one"]
  196. >>> wer = _WordErrorRate()
  197. >>> wer(preds, target)
  198. tensor(0.5000)
  199. """
  200. def __init__(self, **kwargs: Any) -> None:
  201. _deprecated_root_import_class("WordErrorRate", "text")
  202. super().__init__(**kwargs)
  203. class _WordInfoLost(WordInfoLost):
  204. """Wrapper for deprecated import.
  205. >>> preds = ["this is the prediction", "there is an other sample"]
  206. >>> target = ["this is the reference", "there is another one"]
  207. >>> wil = _WordInfoLost()
  208. >>> wil(preds, target)
  209. tensor(0.6528)
  210. """
  211. def __init__(self, **kwargs: Any) -> None:
  212. _deprecated_root_import_class("WordInfoLost", "text")
  213. super().__init__(**kwargs)
  214. class _WordInfoPreserved(WordInfoPreserved):
  215. """Wrapper for deprecated import.
  216. >>> preds = ["this is the prediction", "there is an other sample"]
  217. >>> target = ["this is the reference", "there is another one"]
  218. >>> wip = WordInfoPreserved()
  219. >>> wip(preds, target)
  220. tensor(0.3472)
  221. """
  222. def __init__(self, **kwargs: Any) -> None:
  223. _deprecated_root_import_class("WordInfoPreserved", "text")
  224. super().__init__(**kwargs)