pytext.models.ensembles package

Submodules

pytext.models.ensembles.bagging_doc_ensemble module

class pytext.models.ensembles.bagging_doc_ensemble.BaggingDocEnsembleModel(config: pytext.models.ensembles.ensemble.EnsembleModel.Config, models: List[pytext.models.model.Model], *args, **kwargs)[source]

Bases: pytext.models.ensembles.ensemble.EnsembleModel

Ensemble class that uses bagging for ensembling document classification models.

forward(*args, **kwargs) → torch.Tensor[source]

Call forward() method of each document classification sub-model by passing all arguments and named arguments to the sub-models, collect the logits from them and average their values.

Returns:Logits from the ensemble.
Return type:torch.Tensor

pytext.models.ensembles.bagging_intent_slot_ensemble module

class pytext.models.ensembles.bagging_intent_slot_ensemble.BaggingIntentSlotEnsembleModel(config: pytext.models.ensembles.bagging_intent_slot_ensemble.BaggingIntentSlotEnsembleModel.Config, models: List[pytext.models.model.Model], *args, **kwargs)[source]

Bases: pytext.models.ensembles.ensemble.EnsembleModel

Ensemble class that uses bagging for ensembling intent-slot models.

Parameters:
  • config (Config) – Configuration object specifying all the parameters of BaggingIntentSlotEnsemble.
  • models (List[Model]) – List of intent-slot model objects.
use_crf

Whether to use CRF for word tagging task.

Type:bool
output_layer

Output layer of intent-slot model responsible for computing loss and predictions.

Type:IntentSlotOutputLayer
forward(*args, **kwargs) → Tuple[torch.Tensor, torch.Tensor][source]

Call forward() method of each intent-slot sub-model by passing all arguments and named arguments to the sub-models, collect the logits from them and average their values.

Returns:Logits from the ensemble.
Return type:torch.Tensor
load_state_dict(state_dict: Dict[str, torch.Tensor], strict: bool = True)[source]

Copies parameters and buffers from state_dict into this module and its descendants. If strict is True, then the keys of state_dict must exactly match the keys returned by this module’s state_dict() function.

Parameters:
  • state_dict (dict) – a dict containing parameters and persistent buffers.
  • strict (bool, optional) – whether to strictly enforce that the keys in state_dict match the keys returned by this module’s state_dict() function. Default: True
Returns:

  • missing_keys is a list of str containing the missing keys
  • unexpected_keys is a list of str containing the unexpected keys

Return type:

NamedTuple with missing_keys and unexpected_keys fields

merge_sub_models() → None[source]

Merges all sub-models’ transition matrices when using CRF. Otherwise does nothing.

torchscriptify(tensorizers, traced_model)[source]

pytext.models.ensembles.ensemble module

class pytext.models.ensembles.ensemble.EnsembleModel(config: pytext.models.ensembles.ensemble.EnsembleModel.Config, models: List[pytext.models.model.Model], *args, **kwargs)[source]

Bases: pytext.models.model.Model

Base class for ensemble models.

Parameters:
  • config (Config) – Configuration object specifying all the parameters of Ensemble.
  • models (List[Model]) – List of sub-model objects.
output_layer

Responsible for computing loss and predictions.

Type:OutputLayerBase
models

ModuleList container for sub-model objects.

Type:nn.ModuleList]
arrange_model_context(tensor_dict)[source]
arrange_model_inputs(tensor_dict)[source]
arrange_targets(tensor_dict)[source]
forward(*args, **kwargs)[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.ensembles.ensemble.EnsembleModel.Config, tensorizers: Dict[str, pytext.data.tensorizers.Tensorizer], *args, **kwargs)[source]

Factory method to construct an instance of Ensemble or one its derived classes from the module’s config object and tensorizers It creates sub-models in the ensemble using the sub-model’s configuration.

Parameters:
  • config (Config) – Configuration object specifying all the parameters of Ensemble.
  • tensorizers (Dict[str, Tensorizer]) – Tensorizer specifying all the parameters of the input features to the model.
Returns:

An instance of Ensemble.

Return type:

type

get_export_input_names(tensorizers)[source]
get_export_output_names(tensorizers)[source]
merge_sub_models()[source]
save_modules(base_path: str = '', suffix: str = '') → None[source]

Saves the modules of all sub_models in the Ensemble.

Parameters:
  • base_path (str) – Path of base directory. Defaults to “”.
  • suffix (str) – Suffix to add to the file name to save. Defaults to “”.
torchscriptify(tensorizers, traced_model)[source]
vocab_to_export(tensorizers)[source]

Module contents

class pytext.models.ensembles.BaggingDocEnsembleModel(config: pytext.models.ensembles.ensemble.EnsembleModel.Config, models: List[pytext.models.model.Model], *args, **kwargs)[source]

Bases: pytext.models.ensembles.ensemble.EnsembleModel

Ensemble class that uses bagging for ensembling document classification models.

forward(*args, **kwargs) → torch.Tensor[source]

Call forward() method of each document classification sub-model by passing all arguments and named arguments to the sub-models, collect the logits from them and average their values.

Returns:Logits from the ensemble.
Return type:torch.Tensor
class pytext.models.ensembles.BaggingIntentSlotEnsembleModel(config: pytext.models.ensembles.bagging_intent_slot_ensemble.BaggingIntentSlotEnsembleModel.Config, models: List[pytext.models.model.Model], *args, **kwargs)[source]

Bases: pytext.models.ensembles.ensemble.EnsembleModel

Ensemble class that uses bagging for ensembling intent-slot models.

Parameters:
  • config (Config) – Configuration object specifying all the parameters of BaggingIntentSlotEnsemble.
  • models (List[Model]) – List of intent-slot model objects.
use_crf

Whether to use CRF for word tagging task.

Type:bool
output_layer

Output layer of intent-slot model responsible for computing loss and predictions.

Type:IntentSlotOutputLayer
forward(*args, **kwargs) → Tuple[torch.Tensor, torch.Tensor][source]

Call forward() method of each intent-slot sub-model by passing all arguments and named arguments to the sub-models, collect the logits from them and average their values.

Returns:Logits from the ensemble.
Return type:torch.Tensor
load_state_dict(state_dict: Dict[str, torch.Tensor], strict: bool = True)[source]

Copies parameters and buffers from state_dict into this module and its descendants. If strict is True, then the keys of state_dict must exactly match the keys returned by this module’s state_dict() function.

Parameters:
  • state_dict (dict) – a dict containing parameters and persistent buffers.
  • strict (bool, optional) – whether to strictly enforce that the keys in state_dict match the keys returned by this module’s state_dict() function. Default: True
Returns:

  • missing_keys is a list of str containing the missing keys
  • unexpected_keys is a list of str containing the unexpected keys

Return type:

NamedTuple with missing_keys and unexpected_keys fields

merge_sub_models() → None[source]

Merges all sub-models’ transition matrices when using CRF. Otherwise does nothing.

torchscriptify(tensorizers, traced_model)[source]
class pytext.models.ensembles.EnsembleModel(config: pytext.models.ensembles.ensemble.EnsembleModel.Config, models: List[pytext.models.model.Model], *args, **kwargs)[source]

Bases: pytext.models.model.Model

Base class for ensemble models.

Parameters:
  • config (Config) – Configuration object specifying all the parameters of Ensemble.
  • models (List[Model]) – List of sub-model objects.
output_layer

Responsible for computing loss and predictions.

Type:OutputLayerBase
models

ModuleList container for sub-model objects.

Type:nn.ModuleList]
arrange_model_context(tensor_dict)[source]
arrange_model_inputs(tensor_dict)[source]
arrange_targets(tensor_dict)[source]
forward(*args, **kwargs)[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.ensembles.ensemble.EnsembleModel.Config, tensorizers: Dict[str, pytext.data.tensorizers.Tensorizer], *args, **kwargs)[source]

Factory method to construct an instance of Ensemble or one its derived classes from the module’s config object and tensorizers It creates sub-models in the ensemble using the sub-model’s configuration.

Parameters:
  • config (Config) – Configuration object specifying all the parameters of Ensemble.
  • tensorizers (Dict[str, Tensorizer]) – Tensorizer specifying all the parameters of the input features to the model.
Returns:

An instance of Ensemble.

Return type:

type

get_export_input_names(tensorizers)[source]
get_export_output_names(tensorizers)[source]
merge_sub_models()[source]
save_modules(base_path: str = '', suffix: str = '') → None[source]

Saves the modules of all sub_models in the Ensemble.

Parameters:
  • base_path (str) – Path of base directory. Defaults to “”.
  • suffix (str) – Suffix to add to the file name to save. Defaults to “”.
torchscriptify(tensorizers, traced_model)[source]
vocab_to_export(tensorizers)[source]