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.

  • 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)
    >>> 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)


Access comprehensive developer documentation for PyTorch

View Docs


Get in-depth tutorials for beginners and advanced developers

View Tutorials


Find development resources and get your questions answered

View Resources