Shortcuts

torcheval.metrics.BLEUScore

class torcheval.metrics.BLEUScore(*, n_gram: int, weights: Tensor | None = None, device: device | None = None)

Compute BLEU score (https://en.wikipedia.org/wiki/BLEU) given translations and references. Its functional version is torcheval.metrics.functional.text.bleu.

Parameters:
  • 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 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

Initialize a metric object and its internal states.

Use self._add_state() to initialize state variables of your metric class. The state variables should be either torch.Tensor, a list of torch.Tensor, a dictionary with torch.Tensor as values, or a deque of torch.Tensor.

Methods

__init__(*, n_gram[, weights, device])

Initialize a metric object and its internal states.

compute()

Returns the running BLEUScore.

load_state_dict(state_dict[, strict])

Loads metric state variables from state_dict.

merge_state(metrics)

Merge the metric state with its counterparts from other metric instances.

reset()

Reset the metric state variables to their default value.

state_dict()

Save metric state variables in state_dict.

to(device, *args, **kwargs)

Move tensors in metric state variables to device.

update(input, target)

Update the metric state with new inputs.

Attributes

device

The last input device of Metric.to().

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources