mbrs.decoders.base module#

class mbrs.decoders.base.DecoderBase(cfg: ~mbrs.decoders.base.DecoderBase.Config, metric: ~mbrs.metrics.base.MetricBase, selector: ~mbrs.selectors.base.Selector = <mbrs.selectors.nbest.SelectorNbest object>)[source]#

Bases: ABC

Decoder base class.

class Config[source]#

Bases: object

Configuration for the decoder.

class Output(idx: list[int], sentence: list[str], score: list[float])[source]#

Bases: object

  • idx (list[int]): Index numbers of the n-best hypotheses.

  • sentence (list[str]): Sentences of the n-best hypotheses.

  • score (list[float]): Scores of the n-best hypotheses.

idx: list[int]#
score: list[float]#
sentence: list[str]#
argbest(x: Tensor) Tensor[source]#

Return the index of the best element.

Parameters:

x (Tensor) – Input 1-D array.

Returns:

A scalar tensor of the best index.

Return type:

Tensor

property maximize: bool#

Return True when maximizing the objective score.

select(hypotheses: list[str], expected_scores: Tensor, nbest: int = 1, source: str | None = None, **kwargs) Output[source]#

Select the final output list.

Parameters:
  • hypotheses (list[str]) – Hypotheses.

  • expected_scores (Tensor) – The expected scores for each hypothesis.

  • nbest (int) – Return the n-best hypotheses based on the selection rule.

  • source (str, optional) – A source.

  • maximize (bool) – Whether maximize the scores or not.

Returns:

Outputs.

Return type:

Selector.Output

superior(a: float, b: float) bool[source]#

Return whether the score a is superior to the score b.

Parameters:
Returns:

Return True when a is superior to b.

Return type:

bool

topk(x: Tensor, k: int = 1) tuple[list[float], list[int]][source]#

Return the top-k best elements and corresponding indices.

Parameters:
  • x (Tensor) – Input 1-D array.

  • k (int) – Return the top-k values and indices.

Returns:

tuple[list[float], list[int]]
  • list[float]: The top-k values.

  • list[int]: The top-k indices.

class mbrs.decoders.base.DecoderReferenceBased(cfg: ~mbrs.decoders.base.DecoderBase.Config, metric: ~mbrs.metrics.base.MetricBase, selector: ~mbrs.selectors.base.Selector = <mbrs.selectors.nbest.SelectorNbest object>)[source]#

Bases: DecoderBase

Decoder base class for strategies that use references like MBR decoding.

abstract decode(hypotheses: list[str], references: list[str], source: str | None = None, nbest: int = 1, reference_lprobs: Tensor | None = None) Output[source]#

Select the n-best hypotheses based on the strategy.

Parameters:
  • hypotheses (list[str]) – Hypotheses.

  • references (list[str]) – References.

  • source (str, optional) – A source.

  • nbest (int) – Return the n-best hypotheses.

  • reference_lprobs (Tensor, optional) – Log-probabilities for each reference sample. The shape must be (len(references),). See https://arxiv.org/abs/2311.05263.

Returns:

The n-best hypotheses.

Return type:

Decoder.Output

metric: Metric#
class mbrs.decoders.base.DecoderReferenceless(cfg: ~mbrs.decoders.base.DecoderBase.Config, metric: ~mbrs.metrics.base.MetricBase, selector: ~mbrs.selectors.base.Selector = <mbrs.selectors.nbest.SelectorNbest object>)[source]#

Bases: DecoderBase

Decoder base class for reference-free strategies.

abstract decode(hypotheses: list[str], source: str, nbest: int = 1) Output[source]#

Select the n-best hypotheses based on the strategy.

Parameters:
  • hypotheses (list[str]) – Hypotheses.

  • source (str) – A source.

  • nbest (int) – Return the n-best hypotheses.

Returns:

The n-best hypotheses.

Return type:

Decoder.Output

metric: MetricReferenceless#