Shortcuts

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

Parameters:
  • n_gram – 计算BLEU分数时使用的最大n-gram。可以是1、2、3或4。

  • weights – 可选的n-grams权重分布。要求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])

初始化一个度量对象及其内部状态。

compute()

返回正在运行的BLEUScore。

load_state_dict(state_dict[, strict])

从state_dict加载度量状态变量。

merge_state(metrics)

将度量状态与其他度量实例的对应部分合并。

reset()

将度量状态变量重置为其默认值。

state_dict()

将度量状态变量保存在state_dict中。

to(device, *args, **kwargs)

将度量状态变量中的张量移动到设备。

update(input, target)

使用新的输入更新度量状态。

属性

device

Metric.to()的最后一个输入设备。