sign_language_translator.models.language_models.beam_sampling module

This module provides BeamSampling class for generating completions using beam search sampling.

class sign_language_translator.models.language_models.beam_sampling.BeamSampling(model: ~sign_language_translator.models.language_models.abstract_language_model.LanguageModel, beam_width: int = 3, start_of_sequence_token='[', end_of_sequence_token=']', max_length: int = 37, scoring_function: ~typing.Callable[[~typing.Iterable, float], float] = <function BeamSampling.<lambda>>, return_log_of_probability: bool = True)[source]

Bases: object

BeamSampling class for generating completions using beam search sampling.

Parameters:
  • model (LanguageModel) – The language model used for generating completions.

  • beam_width (int, optional) – The beam width for beam search. Defaults to 3.

  • start_of_sequence_token (str, optional) – The start of sequence token. Defaults to “[“.

  • end_of_sequence_token (str, optional) – The end of sequence token. Defaults to “]”.

  • max_length (int, optional) – The maximum length of the generated completions. Defaults to 33.

  • scoring_function (Callable[[Iterable, float], float], optional) – The scoring function used to score the completions. It should accept the generated sequence and its overall log probability as arguments. Defaults to a linear function.

  • return_log_of_probability (bool, optional) – A flag indicating whether to return the probability of the completions or log2 of it. Defaults to True.

complete(initial_context: ~typing.Iterable | None = None, append_func: ~typing.Callable[[~typing.Any, ~typing.Any], ~typing.Any] = <function BeamSampling.<lambda>>) Tuple[Iterable, float][source]

Generate completions based on the given initial context.

Parameters:
  • initial_context (Iterable | None, optional) – The initial context for completion generation. Defaults to None.

  • append_func (Callable[[Any, Any], Any], optional) – a function that can append the generated next token to provided context. Defaults to a lambda function that can append to list, tuple & str.

Returns:

One generated completion and its score.

Return type:

Tuple[Iterable, float]

sign_language_translator.models.language_models.beam_sampling.random() x in the interval [0, 1).