mbrs.selectors.diverse module#

class mbrs.selectors.diverse.SelectorDiverse(cfg: Config)[source]#

Bases: Selector

class Config(diversity_metric: Metrics = Metrics.bleu, diversity_metric_config: Config | None = None, diversity_lambda: float = 0.1, local_search_iterations: int = 20, local_search_neighbors: int = 1, seed: int = 0)[source]#

Bases: Config

Configuration for the selector.

diversity_lambda: float = 0.1#
diversity_metric: Metrics = 'bleu'#
diversity_metric_config: Config | None = None#
local_search_iterations: int = 20#
local_search_neighbors: int = 1#
seed: int = 0#
class Objective(score: float, expected_score: float, diversity_score: float)[source]#

Bases: object

  • score (float): The objective score.

  • expected_score (float): Expected score for the n-best list.

  • diversity_score (float): Diversity score for the n-best list.

diversity_score: float#
expected_score: float#
score: float#
class Output(idx: list[int], sentence: list[str], score: list[float], nbest_objective_score: float, nbest_expected_score: float, nbest_diversity_score: float)[source]#

Bases: Output

  • 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.

  • nbest_objective_score: (float): Objective score for the n-best list.

  • nbest_expected_score (float): Expected score for the n-best list.

  • nbest_diversity_score: (float): Diversity score for the n-best list.

idx: list[int]#
nbest_diversity_score: float#
nbest_expected_score: float#
nbest_objective_score: float#
score: list[float]#
sentence: list[str]#
cfg: Config#
compute_objective(expected_scores: Tensor, hypothesis_dissimilarities: Tensor, mask: Tensor) Objective[source]#

Compute the objective function.

Parameters:
  • expected_scores (Tensor) – The expected scores for each hypothesis. The shape is (H,).

  • hypothesis_dissimilarities (Tensor) – The pairwise dissimilarities for all hypotheses. The shape is (H, H).

  • mask (Tensor) – Boolean tensor of shape (H,). The positions of True elements are calculated in the objective and the others are discarded.

Returns:

The objective scores that contain the expected score, diversity score, and the sum of them.

Return type:

Objective

diversity_metric: Metric#
search_greedy_best_first(expected_scores: Tensor, hypothesis_dissimilarities: Tensor, nbest: int = 1, maximize: bool = True) Tensor[source]#

Search the solution by greedy best first search.

Parameters:
  • expected_scores (Tensor) – The expected scores for each hypothesis. The shape is (H,).

  • hypothesis_dissimilarities (Tensor) – The pairwise dissimilarities for all hypotheses. The shape is (H, H).

  • nbest (int) – The number of final outputs.

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

Returns:

Boolean tensor of shape (H,) where True positions indicate that they are selected.

Return type:

Tensor

search_local(expected_scores: Tensor, hypothesis_dissimilarities: Tensor, initial_selections: Tensor, nbest: int = 1, maximize: bool = True) Tensor[source]#

Search the solution by greedy best first search.

Parameters:
  • expected_scores (Tensor) – The expected scores for each hypothesis. The shape is (H,).

  • hypothesis_dissimilarities (Tensor) – The pairwise dissimilarities for all hypotheses. The shape is (H, H).

  • initial_selections (Tensor) – Boolean tensor of shape (H,) where True positions indicate that they are selected.

  • nbest (int) – The number of final outputs.

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

Returns:

Boolean tensor of shape (H,) where True positions indicate that they are selected.

Return type:

Tensor

select(hypotheses: list[str], expected_scores: Tensor, nbest: int = 1, source: str | None = None, maximize: bool = True, **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:

Selected hypotheses.

Return type:

Selector.Output