pytext.models.language_models package

Submodules

pytext.models.language_models.lmlstm module

class pytext.models.language_models.lmlstm.LMLSTM(embedding: pytext.models.embeddings.embedding_base.EmbeddingBase = <pytext.config.field_config.WordFeatConfig object>, representation: pytext.models.representations.representation_base.RepresentationBase = <pytext.models.representations.bilstm.BiLSTM.Config object>, decoder: pytext.models.decoders.decoder_base.DecoderBase = <pytext.models.decoders.mlp_decoder.MLPDecoder.Config object>, output_layer: pytext.models.output_layers.output_layer_base.OutputLayerBase = <pytext.models.output_layers.lm_output_layer.LMOutputLayer.Config object>, stateful: bool = False, exporter: object = <class 'pytext.exporters.exporter.ModelExporter'>)[source]

Bases: pytext.models.model.BaseModel

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

arrange_model_inputs(tensor_dict)[source]
arrange_targets(tensor_dict)[source]
caffe2_export(tensorizers, tensor_dict, path, export_onnx_path=None)[source]
classmethod checkTokenConfig(tokens: Optional[pytext.data.tensorizers.TokenTensorizer.Config])[source]
cpu()[source]

Moves all model parameters and buffers to the CPU.

Returns:self
Return type:Module
forward(tokens: torch.Tensor, seq_len: torch.Tensor) → 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(config: pytext.models.language_models.lmlstm.LMLSTM.Config, tensorizers: Dict[str, pytext.data.tensorizers.Tensorizer])[source]
get_export_input_names(tensorizers)[source]
get_export_output_names(tensorizers)[source]
get_num_examples_from_batch(batch)[source]
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]
vocab_to_export(tensorizers)[source]
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