paddlespeech.s2t.models.lm_interface module

Language model interface.

class paddlespeech.s2t.models.lm_interface.LMInterface[source]

Bases: ScorerInterface

LM Interface model implementation.

Methods

add_arguments(parser)

Add arguments to command line argument parser.

build(n_vocab, **kwargs)

Initialize this class with python-level args.

final_score(state)

Score eos (optional).

forward(x, t)

Compute LM loss value from buffer sequences.

init_state(x)

Get an initial state for decoding (optional).

score(y, state, x)

Score new token (required).

select_state(state, i[, new_id])

Select state with relative ids in the main beam search.

static add_arguments(parser)[source]

Add arguments to command line argument parser.

classmethod build(n_vocab: int, **kwargs)[source]

Initialize this class with python-level args.

Args:

idim (int): The number of vocabulary.

Returns:

LMinterface: A new instance of LMInterface.

forward(x, t)[source]

Compute LM loss value from buffer sequences.

Args:

x (torch.Tensor): Input ids. (batch, len) t (torch.Tensor): Target ids. (batch, len)

Returns:
tuple[torch.Tensor, torch.Tensor, torch.Tensor]: Tuple of

loss to backward (scalar), negative log-likelihood of t: -log p(t) (scalar) and the number of elements in x (scalar)

Notes:

The last two return values are used in perplexity: p(t)^{-n} = exp(-log p(t) / n)

paddlespeech.s2t.models.lm_interface.dynamic_import_lm(module)[source]

Import LM class dynamically.

Args:

module (str): module_name:class_name or alias in predefined_lms

Returns:

type: LM class