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 ¶
给定每个翻译的翻译和参考,计算 BLEU 分数。其类版本为
torcheval.metrics.texBLEUScore
。- 参数:
input – 要评分的翻译。
target – 每个翻译的参考列表。要求 len(input) = len(target)
n_gram – 计算 BLEU 分数时使用的最大 n 元语法。可以是 1、2、3 或 4。
weights – n 元语法的可选权重分布。要求 len(weights) = n_gram。如果未指定,将使用统一权重。
示例 –
>>> 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)