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:
ConfigConfiguration for the selector.
- diversity_metric: Metrics = 'bleu'#
- class Objective(score: float, expected_score: float, diversity_score: float)[source]#
Bases:
objectscore (float): The objective score.
expected_score (float): Expected score for the n-best list.
diversity_score (float): Diversity score for the n-best list.
- 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:
Outputidx (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.
- 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:
- 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:
- 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