mbrs.selectors package#

Submodules#

Module contents#

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

Bases: ABC

Selector base class.

class Config[source]#

Bases: object

Configuration for the selector.

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, maximize: bool = True) Tensor[source]#

Return the index of the best element.

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

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

Returns:

A scalar tensor of the best index.

Return type:

Tensor

abstract 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

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

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

Parameters:
  • a (float) – A score.

  • b (float) – A score.

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

Returns:

Return True when a is superior to b.

Return type:

bool

topk(x: Tensor, k: int = 1, maximize: bool = True) 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.

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

Returns:

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

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

class mbrs.selectors.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.

nbest_diversity_score: float#
nbest_expected_score: float#
nbest_objective_score: float#
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

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

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

Bases: Selector

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

mbrs.selectors.get_selector(name: str) type[T]#

Get a class type.

Parameters:

name – A registered name.

Returns:

Class type.

Return type:

type[T]

mbrs.selectors.register(name: str) Callable[[type[T]], type[T]]#

Register a type as the given name.

Parameters:

name (str) – The name of a type.

Returns:

Register decorator function.

Return type:

Callable[[type[T]], type[T]]

Raises:

ValueError – The type is already registered.