bleu_score
BleuScore
¶
Bases: Trace
Calculate the Bleu score for a nlp task and report it back to the logger.
Calculate BLEU score (Bilingual Evaluation Understudy) from Papineni, Kishore, Salim Roukos, Todd Ward, and Wei-Jing Zhu. 2002."BLEU: a method for automatic evaluation of machine translation."In Proceedings of ACL. https://www.aclweb.org/anthology/P02-1040.pdf
The BLEU metric scores a translation on a scale of 0 to 1, in an attempt to measure the adequacy and fluency of the Machine Translation output. The closer to 1 the test sentences score, the more overlap there is with their human reference translations and thus, the better the system is deemed to be. The MT output would score 1 only if it is identical to the reference human translation. But even two competent human translations of the exact same material may only score in the 0.6 or 0.7 range as they are likely to use different vocabulary and phrasing. We should be wary of very high BLEU scores (in excess of 0.7) as it is probably measuring improperly or overfitting.
The default BLEU calculates a score for up to 4-grams using uniform weights (this is called BLEU-4). To evaluate your translations with lower order ngrams, use customized "n_gram". E.g. when accounting for up to 2-grams with uniform weights (this is called BLEU-2) use n_gram=2.
If there is no ngrams overlap for any order of n-grams, BLEU returns the value 0. This is because the precision for the order of n-grams withoutoverlap is 0, and the geometric mean in the final BLEU score computation multiplies the 0 with the precision of other n-grams. This results in 0. Shorter translations may have inflated precision values due to having smaller denominators; therefore, we give them proportionally smaller smoothed counts. Instead of scaling to 1/(2^k), Chen and Cherry suggests dividing by 1/ln(len(T)), where T is the length of the translation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
true_key |
str
|
Name of the key that corresponds to ground truth in the batch dictionary. |
required |
pred_key |
str
|
Name of the key that corresponds to predicted score in the batch dictionary. |
required |
mode |
Union[None, str, Iterable[str]]
|
What mode(s) to execute this Trace in. For example, "train", "eval", "test", or "infer". To execute regardless of mode, pass None. To execute in all modes except for a particular one, you can pass an argument like "!infer" or "!train". |
('eval', 'test')
|
ds_id |
Union[None, str, Iterable[str]]
|
What dataset id(s) to execute this Trace in. To execute regardless of ds_id, pass None. To execute in all ds_ids except for a particular one, you can pass an argument like "!ds1". |
None
|
output_name |
str
|
Name of the key to store back to the state. |
'bleu_score'
|
n_gram |
int
|
Number of grams used to calculate bleu score. |
4
|
per_ds |
bool
|
Whether to automatically compute this metric individually for every ds_id it runs on, in addition to
computing an aggregate across all ds_ids on which it runs. This is automatically False if |
True
|
Source code in fastestimator/fastestimator/trace/metric/bleu_score.py
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 |
|
batch_precision_parameters
¶
Calculate modified precision per n_gram for input references and hypotheses combinations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
references |
List[ndarray]
|
Ground truth sentences. |
required |
hypotheses |
List[ndarray]
|
Predicted sentences. |
required |
Returns:
Type | Description |
---|---|
List[float]
|
List of sentence level bleu scores |
Source code in fastestimator/fastestimator/trace/metric/bleu_score.py
get_brevity_penalty
¶
Calculate the brevity penalty of the corpus.
Returns:
Type | Description |
---|---|
float
|
Brevity penalty for corpus. |
Source code in fastestimator/fastestimator/trace/metric/bleu_score.py
get_corpus_bleu_score
¶
Calculate the bleu score using corpus level brevity penalty and geometric average precision.
Returns:
Type | Description |
---|---|
float
|
Corpus level bleu score. |
Source code in fastestimator/fastestimator/trace/metric/bleu_score.py
get_output_weights
¶
Generate weights tuple based on n_gram.
Returns:
Type | Description |
---|---|
Tuple[float, ...]
|
Tuple of n_gram weights |
Raises:
Type | Description |
---|---|
ValueError
|
When n_gram provided is less than or equal to 0.. |
Source code in fastestimator/fastestimator/trace/metric/bleu_score.py
get_smoothened_modified_precision
¶
Calculate the smoothened modified precision.
Returns:
Type | Description |
---|---|
List[float]
|
List of smoothened modified precision of n_grams. |
Source code in fastestimator/fastestimator/trace/metric/bleu_score.py
get_formated_list
¶
Filter the padding(elements with 0 value) and typecast the elements of list to str.
Returns:
Type | Description |
---|---|
List[str]
|
Formated list. |
Source code in fastestimator/fastestimator/trace/metric/bleu_score.py
get_formated_reference
¶
Encapsulate formated list in another list.
Returns:
Type | Description |
---|---|
List[List[str]]
|
List encapsulated formated list. |