pytext.models.language_models package


pytext.models.language_models.lmlstm module

class pytext.models.language_models.lmlstm.LMLSTM(*inputs)[source]

Bases: pytext.models.model.Model

LMLSTM implements a word-level language model that uses LSTMs to represent the document.


alias of LMLSTM.Config

forward(tokens, *inputs) → List[torch.Tensor][source]

Defines the computation performed at every call.

Should be overridden by all subclasses.


Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

classmethod from_config(model_config, feat_config, metadata:[source]

Factory method to construct an instance of LMLSTM from the module’s config object and the field’s metadata object.

  • config (LMLSTM.Config) – Configuration object specifying all the parameters of LMLSTM.
  • feat_config (FeatureConfig) – Configuration object specifying all the parameters of all input features.
  • metadata (FieldMeta) – Object containing this field’s metadata.

An instance of LMLSTM.

Return type:


init_hidden(bsz: int) → Tuple[torch.Tensor, torch.Tensor][source]

Initialize the hidden states of the LSTM if the language model is stateful.

Parameters:bsz (int) – Batch size.
Returns:Initialized hidden state and cell state of the LSTM.
Return type:Tuple[torch.Tensor, torch.Tensor]
pytext.models.language_models.lmlstm.repackage_hidden(hidden: Union[torch.Tensor, Tuple[torch.Tensor, ...]]) → Union[torch.Tensor, Tuple[torch.Tensor, ...]][source]

Wraps hidden states in new Tensors, to detach them from their history.

Parameters:hidden (Union[torch.Tensor, Tuple[torch.Tensor, ..]]) – Tensor or a tuple of tensors to repackage.
Returns:Repackaged output
Return type:Union[torch.Tensor, Tuple[torch.Tensor, ..]]

Module contents