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