torcheval.metrics.BLEUScore¶
- class torcheval.metrics.BLEUScore(*, n_gram: int, weights: Tensor | None = None, device: device | None = None)¶
计算 BLEU 分数(https://en.wikipedia.org/wiki/BLEU)给定翻译和参考。其函数版本为
torcheval.metrics.functional.text.bleu
。- 参数:
n_gram – 计算 BLEU 分数时使用的最大 n-gram。可以是 1、2、3 或 4。
weights – n-gram 的可选权重分布。要求 len(weights) = n_gram。如果未指定,将使用统一权重。
示例
>>> import torch >>> from torcheval.metrics import BLEUScore >>> metric = BLEUScore(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"]] >>> metric.update(candidates, references) >>> metric.compute() tensor(0.65341892) >>> candidates = ["i like ice cream and apple pie"] >>> references = [["i like apple pie with ice cream on top", "i like ice cream with my apple pie", "i enjoy my apple pie with ice cream"]] >>> metric.update(candidates, references) >>> metric.compute() tensor(0.56377503)
- __init__(*, n_gram: int, weights: Tensor | None = None, device: device | None = None) None ¶
初始化指标对象及其内部状态。
使用
self._add_state()
初始化指标类的状态变量。状态变量应该是torch.Tensor
、torch.Tensor
列表、以torch.Tensor
作为值的字典或torch.Tensor
的双端队列。
方法
__init__
(*, n_gram[, weights, device])初始化指标对象及其内部状态。
计算
()返回运行中的 BLEUScore。
load_state_dict
(state_dict[, strict])从 state_dict 加载指标状态变量。
merge_state
(metrics)将指标状态与其来自其他指标实例的对应状态合并。
重置
()将指标状态变量重置为其默认值。
state_dict
()在 state_dict 中保存指标状态变量。
to
(device, *args, **kwargs)将指标状态变量中的张量移动到设备上。
update
(input, target)使用新输入更新指标状态。
属性
设备
Metric.to()
的最后一个输入设备。