pytext.models.language_models package

Submodules

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.

Config[source]

alias of LMLSTM.Config

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

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

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: pytext.data.data_handler.CommonMetadata)[source]

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

Parameters:
  • 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.
Returns:

An instance of LMLSTM.

Return type:

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