torcheval.metrics.functional.bleu_score¶
- torcheval.metrics.functional.bleu_score(input: str | Sequence[str], target: Sequence[str | Sequence[str]], n_gram: int = 4, weights: Tensor | None = None, device: device | None = None) Tensor ¶
Compute BLEU score given translations and references for each translation. Its class version is
torcheval.metrics.texBLEUScore
.- Parameters:
input – Translations to score.
target – List of references for each translation. Requires len(input) = len(target)
n_gram – Maximum n-gram to use when computing BLEU score. Can be 1, 2, 3, or 4.
weights – Optional weight distribution of n-grams. Requires len(weights) = n_gram. If unspecified, will use uniform weights.
Examples –
>>> import torch >>> from torcheval.metrics.functional.text import bleu >>> candidates = ["the squirrel is eating the nut"] >>> references = [["a squirrel is eating a nut", "the squirrel is eating a tasty nut"]] >>> bleu_score(candidates, references, n_gram=4) tensor(0.53728497) >>> candidates = ["the squirrel is eating the nut", "the cat is on the mat"] >>> references = [["a squirrel is eating a nut", "the squirrel is eating a tasty nut"], ["there is a cat on the mat", "a cat is on the mat"]] >>> bleu_score(candidates, references, n_gram=4) tensor(0.65341892)