pytext.models.output_layers package

Submodules

pytext.models.output_layers.distance_output_layer module

class pytext.models.output_layers.distance_output_layer.DenseRetrievalOutputLayer(target_names: Optional[List[str]] = None, loss_fn: Union[pytext.loss.loss.BinaryCrossEntropyLoss, pytext.loss.loss.CosineEmbeddingLoss, pytext.loss.loss.MAELoss, pytext.loss.loss.MSELoss, pytext.loss.loss.NLLLoss] = None, score_threshold: bool = 0.9, score_type: pytext.models.output_layers.distance_output_layer.OutputScore = <OutputScore.norm_cosine: 2>)[source]

Bases: pytext.models.output_layers.distance_output_layer.PairwiseCosineDistanceOutputLayer

get_loss(logits: Tuple[torch.Tensor, torch.Tensor], targets: torch.Tensor, context: Optional[Dict[str, Any]] = None, reduce: bool = True) → torch.Tensor[source]

Compute and return the loss given logits and targets.

Parameters:
  • logit (torch.Tensor) – Logits returned Model.
  • target (torch.Tensor) – True label/target to compute loss against.
  • context (Optional[Dict[str, Any]]) – Context is a dictionary of items that’s passed as additional metadata by the DataHandler. Defaults to None.
  • reduce (bool) – Whether to reduce loss over the batch. Defaults to True.
Returns:

Model loss.

Return type:

torch.Tensor

get_pred(logits: torch.Tensor, targets: torch.Tensor, *args, **kwargs)[source]

Compute and return prediction and scores from the model.

Parameters:
  • logit (torch.Tensor) – Logits returned Model.
  • targets (Optional[torch.Tensor]) – True label/target. Only used by LMOutputLayer. Defaults to None.
  • context (Optional[Dict[str, Any]]) – Context is a dictionary of items that’s passed as additional metadata by the DataHandler. Defaults to None.
Returns:

Model prediction and scores.

Return type:

Tuple[torch.Tensor, torch.Tensor]

class pytext.models.output_layers.distance_output_layer.OutputScore[source]

Bases: enum.IntEnum

An enumeration.

norm_cosine = 2
raw_cosine = 1
sigmoid_cosine = 3
class pytext.models.output_layers.distance_output_layer.PairwiseCosineDistanceOutputLayer(target_names: Optional[List[str]] = None, loss_fn: Union[pytext.loss.loss.BinaryCrossEntropyLoss, pytext.loss.loss.CosineEmbeddingLoss, pytext.loss.loss.MAELoss, pytext.loss.loss.MSELoss, pytext.loss.loss.NLLLoss] = None, score_threshold: bool = 0.9, score_type: pytext.models.output_layers.distance_output_layer.OutputScore = <OutputScore.norm_cosine: 2>)[source]

Bases: pytext.models.output_layers.output_layer_base.OutputLayerBase

classmethod from_config(config, metadata: Optional[pytext.fields.field.FieldMeta] = None, labels: Optional[pytext.data.utils.Vocabulary] = None)[source]
get_loss(logits: Tuple[torch.Tensor, torch.Tensor], targets: torch.Tensor, context: Optional[Dict[str, Any]] = None, reduce: bool = True) → torch.Tensor[source]

Compute and return the loss given logits and targets.

Parameters:
  • logit (torch.Tensor) – Logits returned Model.
  • target (torch.Tensor) – True label/target to compute loss against.
  • context (Optional[Dict[str, Any]]) – Context is a dictionary of items that’s passed as additional metadata by the DataHandler. Defaults to None.
  • reduce (bool) – Whether to reduce loss over the batch. Defaults to True.
Returns:

Model loss.

Return type:

torch.Tensor

get_pred(logits: torch.Tensor, targets: torch.Tensor, *args, **kwargs)[source]

Compute and return prediction and scores from the model.

Parameters:
  • logit (torch.Tensor) – Logits returned Model.
  • targets (Optional[torch.Tensor]) – True label/target. Only used by LMOutputLayer. Defaults to None.
  • context (Optional[Dict[str, Any]]) – Context is a dictionary of items that’s passed as additional metadata by the DataHandler. Defaults to None.
Returns:

Model prediction and scores.

Return type:

Tuple[torch.Tensor, torch.Tensor]

pytext.models.output_layers.distance_output_layer.get_norm_cosine_scores(cosine_sim_scores)[source]
pytext.models.output_layers.distance_output_layer.get_sigmoid_scores(cosine_sim_scores)[source]

pytext.models.output_layers.doc_classification_output_layer module

class pytext.models.output_layers.doc_classification_output_layer.BinaryClassificationOutputLayer(target_names: Optional[List[str]] = None, loss_fn: Optional[pytext.loss.loss.Loss] = None, *args, **kwargs)[source]

Bases: pytext.models.output_layers.doc_classification_output_layer.ClassificationOutputLayer

export_to_caffe2(workspace: <module 'caffe2.python.workspace' from '/home/docs/checkouts/readthedocs.org/user_builds/pytext/envs/master/lib/python3.7/site-packages/caffe2/python/workspace.py'>, init_net: caffe2.python.core.Net, predict_net: caffe2.python.core.Net, model_out: torch.Tensor, output_name: str) → List[caffe2.python.core.BlobReference][source]

See OutputLayerBase.export_to_caffe2().

get_pred(logit, *args, **kwargs)[source]

See OutputLayerBase.get_pred().

torchscript_predictions()[source]
class pytext.models.output_layers.doc_classification_output_layer.ClassificationOutputLayer(target_names: Optional[List[str]] = None, loss_fn: Optional[pytext.loss.loss.Loss] = None, *args, **kwargs)[source]

Bases: pytext.models.output_layers.output_layer_base.OutputLayerBase

Output layer for document classification models. It supports CrossEntropyLoss and BinaryCrossEntropyLoss per document.

Parameters:loss_fn (Union[CrossEntropyLoss, BinaryCrossEntropyLoss]) – The loss function to use for computing loss. Defaults to None.
loss_fn

The loss function to use for computing loss.

classmethod from_config(config: pytext.models.output_layers.doc_classification_output_layer.ClassificationOutputLayer.Config, metadata: Optional[pytext.fields.field.FieldMeta] = None, labels: Optional[pytext.data.utils.Vocabulary] = None)[source]
get_pred(logit, *args, **kwargs)[source]

Compute and return prediction and scores from the model.

Prediction is computed using argmax over the document label/target space.

Scores are sigmoid or softmax scores over the model logits depending on the loss component being used.

Parameters:logit (torch.Tensor) – Logits returned DocModel.
Returns:Model prediction and scores.
Return type:Tuple[torch.Tensor, torch.Tensor]
class pytext.models.output_layers.doc_classification_output_layer.ClassificationScores(classes, score_function, score_function_dim=None)[source]

Bases: torch.jit._script.ScriptModule

class pytext.models.output_layers.doc_classification_output_layer.MultiLabelOutputLayer(target_names: Optional[List[str]] = None, loss_fn: Optional[pytext.loss.loss.Loss] = None, *args, **kwargs)[source]

Bases: pytext.models.output_layers.doc_classification_output_layer.ClassificationOutputLayer

export_to_caffe2(workspace: <module 'caffe2.python.workspace' from '/home/docs/checkouts/readthedocs.org/user_builds/pytext/envs/master/lib/python3.7/site-packages/caffe2/python/workspace.py'>, init_net: caffe2.python.core.Net, predict_net: caffe2.python.core.Net, model_out: torch.Tensor, output_name: str) → List[caffe2.python.core.BlobReference][source]

See OutputLayerBase.export_to_caffe2().

get_pred(logit, *args, **kwargs)[source]

See OutputLayerBase.get_pred().

torchscript_predictions()[source]
class pytext.models.output_layers.doc_classification_output_layer.MulticlassOutputLayer(target_names: Optional[List[str]] = None, loss_fn: Optional[pytext.loss.loss.Loss] = None, *args, **kwargs)[source]

Bases: pytext.models.output_layers.doc_classification_output_layer.ClassificationOutputLayer

export_to_caffe2(workspace: <module 'caffe2.python.workspace' from '/home/docs/checkouts/readthedocs.org/user_builds/pytext/envs/master/lib/python3.7/site-packages/caffe2/python/workspace.py'>, init_net: caffe2.python.core.Net, predict_net: caffe2.python.core.Net, model_out: torch.Tensor, output_name: str) → List[caffe2.python.core.BlobReference][source]

See OutputLayerBase.export_to_caffe2().

get_pred(logit, *args, **kwargs)[source]

See OutputLayerBase.get_pred().

torchscript_predictions()[source]

pytext.models.output_layers.doc_regression_output_layer module

class pytext.models.output_layers.doc_regression_output_layer.PairwiseCosineRegressionOutputLayer(loss_fn: Union[pytext.loss.loss.MSELoss, pytext.loss.loss.MAELoss])[source]

Bases: pytext.models.output_layers.output_layer_base.OutputLayerBase

Output layer for pair (two-tower) regression models. Accepts two embedding tensors, and computes cosine similarity. The loss is between regression label and cosine similarity.

classmethod from_config(config: pytext.models.output_layers.doc_regression_output_layer.PairwiseCosineRegressionOutputLayer.Config, **kwargs)[source]
get_loss(logits: Tuple[torch.Tensor, torch.Tensor], target: torch.Tensor, context: Optional[Dict[str, Any]] = None, reduce: bool = True) → torch.Tensor[source]
Parameters:
  • logits (Tuple[torch.Tensor, torch.Tensor]) – Logits returned from pairwise model
  • target (torch.Tensor) – Float target to compute loss against
  • context (Optional[Dict[str, Any]]) – Context is a dictionary from data handler
  • reduce (bool) – Whether to reduce loss over the batch. Defaults to True.
Returns:

Model loss.

Return type:

torch.Tensor

get_pred(logits, *args, **kwargs)[source]
Parameters:logits (Tuple[torch.Tensor, torch.Tensor]) – Logits returned from pairwise model
Returns:Model prediction and scores.
Return type:Tuple[torch.Tensor, torch.Tensor]
class pytext.models.output_layers.doc_regression_output_layer.RegressionOutputLayer(loss_fn: pytext.loss.loss.MSELoss, squash_to_unit_range: bool = False)[source]

Bases: pytext.models.output_layers.output_layer_base.OutputLayerBase

Output layer for doc regression models. Currently only supports Mean Squared Error loss.

Parameters:
  • loss (MSELoss) – config for MSE loss
  • squash_to_unit_range (bool) – whether to clamp the output to the range [0, 1], via a sigmoid.
classmethod from_config(config: pytext.models.output_layers.doc_regression_output_layer.RegressionOutputLayer.Config)[source]
get_loss(logit: torch.Tensor, target: torch.Tensor, context: Optional[Dict[str, Any]] = None, reduce: bool = True) → torch.Tensor[source]

Compute regression loss from logits and targets.

Parameters:
  • logit (torch.Tensor) – Logits returned Model.
  • target (torch.Tensor) – True label/target to compute loss against.
  • context (Optional[Dict[str, Any]]) – Context is a dictionary of items that’s passed as additional metadata by the DataHandler. Defaults to None.
  • reduce (bool) – Whether to reduce loss over the batch. Defaults to True.
Returns:

Model loss.

Return type:

torch.Tensor

get_pred(logit, *args, **kwargs)[source]

Compute predictions and scores from the model (unlike in classification, where prediction = “most likely class” and scores = “log probs”, here these are the same values). If squash_to_unit_range is True, fit prediction to [0, 1] via a sigmoid.

Parameters:logit (torch.Tensor) – Logits returned from the model.
Returns:Model prediction and scores.
Return type:Tuple[torch.Tensor, torch.Tensor]
torchscript_predictions()[source]
class pytext.models.output_layers.doc_regression_output_layer.RegressionScores(squash_to_unit_range: bool)[source]

Bases: torch.jit._script.ScriptModule

pytext.models.output_layers.intent_slot_output_layer module

class pytext.models.output_layers.intent_slot_output_layer.IntentSlotOutputLayer(doc_output: pytext.models.output_layers.doc_classification_output_layer.ClassificationOutputLayer, word_output: pytext.models.output_layers.word_tagging_output_layer.WordTaggingOutputLayer)[source]

Bases: pytext.models.output_layers.output_layer_base.OutputLayerBase

Output layer for joint intent classification and slot-filling models. Intent classification is a document classification problem and slot filling is a word tagging problem. Thus terms these can be used interchangeably in the documentation.

Parameters:
  • doc_output (ClassificationOutputLayer) – Output layer for intent classification task. See ClassificationOutputLayer for details.
  • word_output (WordTaggingOutputLayer) – Output layer for slot filling task. See WordTaggingOutputLayer for details.
doc_output

Output layer for intent classification task.

Type:type
word_output

Output layer for slot filling task.

Type:type
export_to_caffe2(workspace: <module 'caffe2.python.workspace' from '/home/docs/checkouts/readthedocs.org/user_builds/pytext/envs/master/lib/python3.7/site-packages/caffe2/python/workspace.py'>, init_net: caffe2.python.core.Net, predict_net: caffe2.python.core.Net, model_out: List[torch.Tensor], doc_out_name: str, word_out_name: str) → List[caffe2.python.core.BlobReference][source]

Exports the intent slot output layer to Caffe2. See OutputLayerBase.export_to_caffe2() for details.

classmethod from_config(config: pytext.models.output_layers.intent_slot_output_layer.IntentSlotOutputLayer.Config, doc_labels: pytext.data.utils.Vocabulary, word_labels: pytext.data.utils.Vocabulary)[source]
get_loss(logits: Tuple[torch.Tensor, torch.Tensor], targets: Tuple[torch.Tensor, torch.Tensor], context: Dict[str, Any] = None, *args, **kwargs) → torch.Tensor[source]

Compute and return the averaged intent and slot-filling loss.

Parameters:
  • logit (Tuple[torch.Tensor, torch.Tensor]) – Logits returned by JointModel. It is a tuple containing logits for intent classification and slot filling.
  • targets (Tuple[torch.Tensor, torch.Tensor]) – Tuple of target Tensors containing true document label/target and true word labels/targets.
  • context (Dict[str, Any]) – Context is a dictionary of items that’s passed as additional metadata. Defaults to None.
Returns:

Averaged intent and slot loss.

Return type:

torch.Tensor

get_pred(logits: Tuple[torch.Tensor, torch.Tensor], targets: Optional[torch.Tensor] = None, context: Optional[Dict[str, Any]] = None) → Tuple[torch.Tensor, torch.Tensor][source]

Compute and return prediction and scores from the model.

Parameters:
  • logit (Tuple[torch.Tensor, torch.Tensor]) – Logits returned by JointModel. It’s tuple containing logits for intent classification and slot filling.
  • targets (Optional[torch.Tensor]) – Not applicable. Defaults to None.
  • context (Optional[Dict[str, Any]]) – Context is a dictionary of items that’s passed as additional metadata. Defaults to None.
Returns:

Model prediction and scores.

Return type:

Tuple[torch.Tensor, torch.Tensor]

torchscript_predictions()[source]
class pytext.models.output_layers.intent_slot_output_layer.IntentSlotScores(doc_scores: torch.jit._script.ScriptModule, word_scores: torch.jit._script.ScriptModule)[source]

Bases: torch.nn.modules.module.Module

forward(logits: Tuple[torch.Tensor, torch.Tensor], context: Dict[str, torch.Tensor]) → Tuple[List[Dict[str, float]], List[List[Dict[str, float]]]][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.

pytext.models.output_layers.lm_output_layer module

class pytext.models.output_layers.lm_output_layer.LMOutputLayer(target_names: List[str], loss_fn: pytext.loss.loss.Loss = None, config=None, pad_token_idx=-100)[source]

Bases: pytext.models.output_layers.output_layer_base.OutputLayerBase

Output layer for language models. It supports CrossEntropyLoss per word.

Parameters:loss_fn (CrossEntropyLoss) – Cross-entropy loss component. Defaults to None.
loss_fn

Cross-entropy loss component for computing loss.

static calculate_perplexity(sequence_loss: torch.Tensor) → torch.Tensor[source]
classmethod from_config(config: pytext.models.output_layers.lm_output_layer.LMOutputLayer.Config, metadata: Optional[pytext.fields.field.FieldMeta] = None, labels: Optional[pytext.data.utils.Vocabulary] = None)[source]
get_loss(logit: torch.Tensor, target: torch.Tensor, context: Dict[str, Any], reduce=True) → torch.Tensor[source]

Compute word prediction loss by comparing prediction of each word in the sentence with the true word.

Parameters:
  • logit (torch.Tensor) – Logit returned by LMLSTM.
  • targets (torch.Tensor) – Not applicable for language models.
  • context (Dict[str, Any]) – Not applicable. Defaults to None.
  • reduce (bool) – Whether to reduce loss over the batch. Defaults to True.
Returns:

Word prediction loss.

Return type:

torch.Tensor

get_pred(logits: torch.Tensor, *args, **kwargs) → Tuple[torch.Tensor, torch.Tensor][source]

Compute and return prediction and scores from the model. Prediction is computed using argmax over the word label/target space. Scores are softmax scores over the model logits.

Parameters:
  • logits (torch.Tensor) – Logits returned LMLSTM.
  • targets (torch.Tensor) – True words.
Returns:

Model prediction and scores.

Return type:

Tuple[torch.Tensor, torch.Tensor]

pytext.models.output_layers.multi_label_classification_layer module

class pytext.models.output_layers.multi_label_classification_layer.MultiLabelClassificationLayer(outputs: Dict[str, pytext.models.output_layers.doc_classification_output_layer.ClassificationOutputLayer], label_set_weights: Optional[Dict[str, float]] = None)[source]

Bases: pytext.models.output_layers.output_layer_base.OutputLayerBase

Output layer for multilabel sequence classification models.

Parameters:
  • outputs (Dict[str, ClassificationOutputLayer]) – Output for multilabels
  • label_weights (optional) – Dict of label_names along with the
  • for label (weight) –
export_to_caffe2(workspace: <module 'caffe2.python.workspace' from '/home/docs/checkouts/readthedocs.org/user_builds/pytext/envs/master/lib/python3.7/site-packages/caffe2/python/workspace.py'>, init_net: caffe2.python.core.Net, predict_net: caffe2.python.core.Net, model_out: List[torch.Tensor], out_name: str) → List[caffe2.python.core.BlobReference][source]

Exports the multilabel output layer to Caffe2. See OutputLayerBase.export_to_caffe2() for details.

classmethod from_config(config: pytext.models.output_layers.multi_label_classification_layer.MultiLabelClassificationLayer.Config, label_tensorizers: [typing.Dict[str, pytext.data.tensorizers.Tensorizer]])[source]
get_loss(logits, targets: List[torch.Tensor], context: Optional[Dict[str, Any]] = None, *args, **kwargs) → torch.Tensor[source]

Compute and return the averaged intent and slot-filling loss.

Parameters:
  • logits (List[torch.Tensor]) – Logits returned by MultiLabelDecoder. It’s list containing logits for all label tasks here pTSR, autoUSM and EA.
  • targets (List[torch.Tensor]) – Targets as computed by the true labels
  • context (Optional[torch.Tensor]) – Not applicable. Defaults to None.
Returns:

Averaged Loss across all label losses.

Return type:

torch.Tensor

get_pred(logits: List[torch.Tensor], targets: List[torch.Tensor], context: Optional[Dict[str, Any]] = None, *args, **kwargs) → Tuple[torch.Tensor, torch.Tensor][source]

Compute and return prediction and scores from the model.

Prediction is computed using argmax over the document label/target space.

Scores are sigmoid or softmax scores over the model logits depending on the loss component being used.

Parameters:
  • logits (List[torch.Tensor]) – Logits returned by MultiLabelDecoder. It’s list containing logits for all label tasks here pTSR, autoUSM and EA.
  • targets (List[torch.Tensor]) – Targets as computed by the true labels
  • of logits corresponding the respective label. (ordering) –
  • context (Optional[torch.Tensor]) – Not applicable. Defaults to None.
Returns:

Model prediction and scores.

Return type:

Tuple[torch.Tensor, torch.Tensor]

torchscript_predictions()[source]
class pytext.models.output_layers.multi_label_classification_layer.MultiLabelClassificationScores(scores: List[torch.jit._script.ScriptModule])[source]

Bases: torch.nn.modules.module.Module

forward(logits: List[torch.Tensor]) → List[List[Dict[str, float]]][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.

pytext.models.output_layers.output_layer_base module

class pytext.models.output_layers.output_layer_base.OutputLayerBase(target_names: Optional[List[str]] = None, loss_fn: Optional[pytext.loss.loss.Loss] = None, *args, **kwargs)[source]

Bases: pytext.models.module.Module

Base class for all output layers in PyText. The responsibilities of this layer are

  1. Implement how loss is computed from logits and targets.
  2. Implement how to get predictions from logits.
  3. Implement the Caffe2 operator for performing the above tasks. This is
    used when PyText exports PyTorch model to Caffe2.
Parameters:loss_fn (type) – The loss function object to use for computing loss. Defaults to None.
loss_fn

The loss function object to use for computing loss.

export_to_caffe2(workspace: <module 'caffe2.python.workspace' from '/home/docs/checkouts/readthedocs.org/user_builds/pytext/envs/master/lib/python3.7/site-packages/caffe2/python/workspace.py'>, init_net: caffe2.python.core.Net, predict_net: caffe2.python.core.Net, model_out: torch.Tensor, output_name: str) → List[caffe2.python.core.BlobReference][source]

Exports the output layer to Caffe2 by manually adding the necessary operators to the init_net and predict_net and, returns the list of external output blobs to be added to the model. By default this does nothing, so any sub-class must override this method (if necessary).

To learn about Caffe2 computation graphs and why we need two networks, init_net and predict_net/exec_net read https://caffe2.ai/docs/intro-tutorial#null__nets-and-operators.

Parameters:
  • workspace (core.workspace) – Caffe2 workspace to use for adding the operator. See https://caffe2.ai/docs/workspace.html to learn about Caffe2 workspace.
  • init_net (core.Net) – Caffe2 init_net to add the operator to.
  • predict_net (core.Net) – Caffe2 predict_net to add the operator to.
  • model_out (torch.Tensor) – Output logit Tensor from the model to .
  • output_name (str) – Name of model_out to use in Caffe2 net.
  • label_names (List[str]) – List of names of the targets/labels to expose from the Caffe2 net.
Returns:

List of output blobs that the output_layer

generates.

Return type:

List[core.BlobReference]

get_loss(logit: torch.Tensor, target: torch.Tensor, context: Optional[Dict[str, Any]] = None, reduce: bool = True) → torch.Tensor[source]

Compute and return the loss given logits and targets.

Parameters:
  • logit (torch.Tensor) – Logits returned Model.
  • target (torch.Tensor) – True label/target to compute loss against.
  • context (Optional[Dict[str, Any]]) – Context is a dictionary of items that’s passed as additional metadata by the DataHandler. Defaults to None.
  • reduce (bool) – Whether to reduce loss over the batch. Defaults to True.
Returns:

Model loss.

Return type:

torch.Tensor

get_pred(logit: torch.Tensor, targets: Optional[torch.Tensor] = None, context: Optional[Dict[str, Any]] = None) → Tuple[torch.Tensor, torch.Tensor][source]

Compute and return prediction and scores from the model.

Parameters:
  • logit (torch.Tensor) – Logits returned Model.
  • targets (Optional[torch.Tensor]) – True label/target. Only used by LMOutputLayer. Defaults to None.
  • context (Optional[Dict[str, Any]]) – Context is a dictionary of items that’s passed as additional metadata by the DataHandler. Defaults to None.
Returns:

Model prediction and scores.

Return type:

Tuple[torch.Tensor, torch.Tensor]

pytext.models.output_layers.pairwise_ranking_output_layer module

class pytext.models.output_layers.pairwise_ranking_output_layer.PairwiseRankingOutputLayer(target_names: Optional[List[str]] = None, loss_fn: Optional[pytext.loss.loss.Loss] = None, *args, **kwargs)[source]

Bases: pytext.models.output_layers.output_layer_base.OutputLayerBase

classmethod from_config(config)[source]
get_pred(logit, targets, context)[source]

Compute and return prediction and scores from the model.

Parameters:
  • logit (torch.Tensor) – Logits returned Model.
  • targets (Optional[torch.Tensor]) – True label/target. Only used by LMOutputLayer. Defaults to None.
  • context (Optional[Dict[str, Any]]) – Context is a dictionary of items that’s passed as additional metadata by the DataHandler. Defaults to None.
Returns:

Model prediction and scores.

Return type:

Tuple[torch.Tensor, torch.Tensor]

pytext.models.output_layers.squad_output_layer module

class pytext.models.output_layers.squad_output_layer.SquadOutputLayer(loss_fn: pytext.loss.loss.Loss, ignore_impossible: bool = True, pos_loss_weight: float = 0.5, has_answer_loss_weight: float = 0.5, has_answer_labels: Iterable[str] = ('False', 'True'), false_label: str = 'False', max_answer_len: int = 30, hard_weight: float = 0.0, is_kd: bool = False)[source]

Bases: pytext.models.output_layers.output_layer_base.OutputLayerBase

classmethod from_config(config, metadata: Optional[pytext.fields.field.FieldMeta] = None, labels: Optional[Iterable[str]] = None, is_kd: bool = False)[source]
get_loss(logits: Tuple[torch.Tensor, ...], targets: Tuple[torch.Tensor, ...], contexts: Optional[Dict[str, Any]] = None, *args, **kwargs) → torch.Tensor[source]

Compute and return the loss given logits and targets.

Parameters:
  • logit (torch.Tensor) – Logits returned Model.
  • target (torch.Tensor) – True label/target to compute loss against.
  • context (Optional[Dict[str, Any]]) – Context is a dictionary of items that’s passed as additional metadata by the DataHandler. Defaults to None.=
Returns:

Model loss.

Return type:

torch.Tensor

get_position_preds(start_pos_logits: torch.Tensor, end_pos_logits: torch.Tensor, max_span_length: int)[source]
get_pred(logits: torch.Tensor, targets: torch.Tensor, contexts: Dict[str, List[Any]]) → Tuple[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor, torch.Tensor]][source]

Compute and return prediction and scores from the model.

Parameters:
  • logit (torch.Tensor) – Logits returned Model.
  • targets (Optional[torch.Tensor]) – True label/target. Only used by LMOutputLayer. Defaults to None.
  • context (Optional[Dict[str, Any]]) – Context is a dictionary of items that’s passed as additional metadata by the DataHandler. Defaults to None.
Returns:

Model prediction and scores.

Return type:

Tuple[torch.Tensor, torch.Tensor]

pytext.models.output_layers.utils module

class pytext.models.output_layers.utils.OutputLayerUtils[source]

Bases: object

static gen_additional_blobs(predict_net: caffe2.python.core.Net, probability_out, model_out: torch.Tensor, output_name: str, label_names: List[str]) → List[caffe2.python.core.BlobReference][source]

Utility method to generate additional blobs for human readable result for models that use explicit labels.

pytext.models.output_layers.utils.query_word_reprs(encoder_repr: torch.Tensor, token_indices: torch.Tensor) → torch.Tensor[source]

Given an encoder_repr (B x T_1 x H) and token_indices (B x T_2) where T_2 <= T_1, collect embeddings from encoder_repr pertaining to indices in token_indices. In the context of fine-tuning pre-trained encoders on sequence labeling, our goal is to build token-level representations as opposed to subword-level represenatations for alignment with other token-level cues, such as dictionary features. Currently, a token representation is built by taking its first subword representation.

pytext.models.output_layers.word_tagging_output_layer module

class pytext.models.output_layers.word_tagging_output_layer.CRFOutputLayer(num_tags, labels: pytext.data.utils.Vocabulary, *args)[source]

Bases: pytext.models.output_layers.output_layer_base.OutputLayerBase

Output layer for word tagging models that use Conditional Random Field.

Parameters:num_tags (int) – Total number of possible word tags.
num_tags

Total number of possible word tags.

export_to_caffe2(workspace: <module 'caffe2.python.workspace' from '/home/docs/checkouts/readthedocs.org/user_builds/pytext/envs/master/lib/python3.7/site-packages/caffe2/python/workspace.py'>, init_net: caffe2.python.core.Net, predict_net: caffe2.python.core.Net, model_out: torch.Tensor, output_name: str) → List[caffe2.python.core.BlobReference][source]

Exports the CRF output layer to Caffe2. See OutputLayerBase.export_to_caffe2() for details.

classmethod from_config(config: pytext.config.component.ComponentMeta.__new__.<locals>.Config, labels: pytext.data.utils.Vocabulary)[source]
get_loss(logit: torch.Tensor, target: torch.Tensor, context: Dict[str, Any], reduce=True)[source]

Compute word tagging loss by using CRF.

Parameters:
  • logit (torch.Tensor) – Logit returned by WordTaggingModel.
  • targets (torch.Tensor) – True document label/target.
  • context (Dict[str, Any]) – Context is a dictionary of items that’s passed as additional metadata. Defaults to None.
  • reduce (bool) – Whether to reduce loss over the batch. Defaults to True.
Returns:

Model prediction and scores.

Return type:

Tuple[torch.Tensor, torch.Tensor]

get_pred(logit: torch.Tensor, target: Optional[torch.Tensor] = None, context: Optional[Dict[str, Any]] = None)[source]

Compute and return prediction and scores from the model.

Prediction is computed using CRF decoding.

Scores are softmax scores over the model logits where the logits are computed by rearranging the word logits such that decoded word tag has the highest valued logits. This is done because with CRF, the highest valued word tag for a given may not be part of the overall set of word tags. In order for argmax to work, we rearrange the logit values.

Parameters:
  • logit (torch.Tensor) – Logits returned WordTaggingModel.
  • target (torch.Tensor) – Not applicable. Defaults to None.
  • context (Optional[Dict[str, Any]]) – Context is a dictionary of items that’s passed as additional metadata. Defaults to None.
Returns:

Model prediction and scores.

Return type:

Tuple[torch.Tensor, torch.Tensor]

torchscript_predictions()[source]
class pytext.models.output_layers.word_tagging_output_layer.CRFWordTaggingScores(classes: List[str], crf)[source]

Bases: pytext.models.output_layers.word_tagging_output_layer.WordTaggingScores

forward(logits: torch.Tensor, context: Dict[str, torch.Tensor]) → List[List[Dict[str, float]]][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.

class pytext.models.output_layers.word_tagging_output_layer.WordTaggingOutputLayer(target_names: Optional[List[str]] = None, loss_fn: Optional[pytext.loss.loss.Loss] = None, *args, **kwargs)[source]

Bases: pytext.models.output_layers.output_layer_base.OutputLayerBase

Output layer for word tagging models. It supports CrossEntropyLoss per word.

Parameters:loss_fn (CrossEntropyLoss) – Cross-entropy loss component. Defaults to None.
loss_fn

Cross-entropy loss component.

export_to_caffe2(workspace: <module 'caffe2.python.workspace' from '/home/docs/checkouts/readthedocs.org/user_builds/pytext/envs/master/lib/python3.7/site-packages/caffe2/python/workspace.py'>, init_net: caffe2.python.core.Net, predict_net: caffe2.python.core.Net, model_out: torch.Tensor, output_name: str) → List[caffe2.python.core.BlobReference][source]

Exports the word tagging output layer to Caffe2.

classmethod from_config(config: pytext.models.output_layers.word_tagging_output_layer.WordTaggingOutputLayer.Config, labels: pytext.data.utils.Vocabulary)[source]
get_loss(logit: torch.Tensor, target: Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor, torch.Tensor]], context: Dict[str, Any], reduce: bool = True) → torch.Tensor[source]

Compute word tagging loss by comparing prediction of each word in the sentence with its true label/target.

Parameters:
  • logit (torch.Tensor) – Logit returned by WordTaggingModel.
  • targets (torch.Tensor) – True document label/target.
  • context (Dict[str, Any]) – Context is a dictionary of items that’s passed as additional metadata. Defaults to None.
  • reduce (bool) – Whether to reduce loss over the batch. Defaults to True.
Returns:

Word tagging loss for all words in the sentence.

Return type:

torch.Tensor

get_pred(logit: torch.Tensor, *args, **kwargs) → Tuple[torch.Tensor, torch.Tensor][source]

Compute and return prediction and scores from the model. Prediction is computed using argmax over the word label/target space. Scores are softmax scores over the model logits.

Parameters:logit (torch.Tensor) – Logits returned WordTaggingModel.
Returns:Model prediction and scores.
Return type:Tuple[torch.Tensor, torch.Tensor]
torchscript_predictions()[source]
class pytext.models.output_layers.word_tagging_output_layer.WordTaggingScores(classes)[source]

Bases: torch.nn.modules.module.Module

forward(logits: torch.Tensor, context: Optional[Dict[str, torch.Tensor]] = None) → List[List[Dict[str, float]]][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.

Module contents

class pytext.models.output_layers.OutputLayerBase(target_names: Optional[List[str]] = None, loss_fn: Optional[pytext.loss.loss.Loss] = None, *args, **kwargs)[source]

Bases: pytext.models.module.Module

Base class for all output layers in PyText. The responsibilities of this layer are

  1. Implement how loss is computed from logits and targets.
  2. Implement how to get predictions from logits.
  3. Implement the Caffe2 operator for performing the above tasks. This is
    used when PyText exports PyTorch model to Caffe2.
Parameters:loss_fn (type) – The loss function object to use for computing loss. Defaults to None.
loss_fn

The loss function object to use for computing loss.

export_to_caffe2(workspace: <module 'caffe2.python.workspace' from '/home/docs/checkouts/readthedocs.org/user_builds/pytext/envs/master/lib/python3.7/site-packages/caffe2/python/workspace.py'>, init_net: caffe2.python.core.Net, predict_net: caffe2.python.core.Net, model_out: torch.Tensor, output_name: str) → List[caffe2.python.core.BlobReference][source]

Exports the output layer to Caffe2 by manually adding the necessary operators to the init_net and predict_net and, returns the list of external output blobs to be added to the model. By default this does nothing, so any sub-class must override this method (if necessary).

To learn about Caffe2 computation graphs and why we need two networks, init_net and predict_net/exec_net read https://caffe2.ai/docs/intro-tutorial#null__nets-and-operators.

Parameters:
  • workspace (core.workspace) – Caffe2 workspace to use for adding the operator. See https://caffe2.ai/docs/workspace.html to learn about Caffe2 workspace.
  • init_net (core.Net) – Caffe2 init_net to add the operator to.
  • predict_net (core.Net) – Caffe2 predict_net to add the operator to.
  • model_out (torch.Tensor) – Output logit Tensor from the model to .
  • output_name (str) – Name of model_out to use in Caffe2 net.
  • label_names (List[str]) – List of names of the targets/labels to expose from the Caffe2 net.
Returns:

List of output blobs that the output_layer

generates.

Return type:

List[core.BlobReference]

get_loss(logit: torch.Tensor, target: torch.Tensor, context: Optional[Dict[str, Any]] = None, reduce: bool = True) → torch.Tensor[source]

Compute and return the loss given logits and targets.

Parameters:
  • logit (torch.Tensor) – Logits returned Model.
  • target (torch.Tensor) – True label/target to compute loss against.
  • context (Optional[Dict[str, Any]]) – Context is a dictionary of items that’s passed as additional metadata by the DataHandler. Defaults to None.
  • reduce (bool) – Whether to reduce loss over the batch. Defaults to True.
Returns:

Model loss.

Return type:

torch.Tensor

get_pred(logit: torch.Tensor, targets: Optional[torch.Tensor] = None, context: Optional[Dict[str, Any]] = None) → Tuple[torch.Tensor, torch.Tensor][source]

Compute and return prediction and scores from the model.

Parameters:
  • logit (torch.Tensor) – Logits returned Model.
  • targets (Optional[torch.Tensor]) – True label/target. Only used by LMOutputLayer. Defaults to None.
  • context (Optional[Dict[str, Any]]) – Context is a dictionary of items that’s passed as additional metadata by the DataHandler. Defaults to None.
Returns:

Model prediction and scores.

Return type:

Tuple[torch.Tensor, torch.Tensor]

class pytext.models.output_layers.CRFOutputLayer(num_tags, labels: pytext.data.utils.Vocabulary, *args)[source]

Bases: pytext.models.output_layers.output_layer_base.OutputLayerBase

Output layer for word tagging models that use Conditional Random Field.

Parameters:num_tags (int) – Total number of possible word tags.
num_tags

Total number of possible word tags.

export_to_caffe2(workspace: <module 'caffe2.python.workspace' from '/home/docs/checkouts/readthedocs.org/user_builds/pytext/envs/master/lib/python3.7/site-packages/caffe2/python/workspace.py'>, init_net: caffe2.python.core.Net, predict_net: caffe2.python.core.Net, model_out: torch.Tensor, output_name: str) → List[caffe2.python.core.BlobReference][source]

Exports the CRF output layer to Caffe2. See OutputLayerBase.export_to_caffe2() for details.

classmethod from_config(config: pytext.config.component.ComponentMeta.__new__.<locals>.Config, labels: pytext.data.utils.Vocabulary)[source]
get_loss(logit: torch.Tensor, target: torch.Tensor, context: Dict[str, Any], reduce=True)[source]

Compute word tagging loss by using CRF.

Parameters:
  • logit (torch.Tensor) – Logit returned by WordTaggingModel.
  • targets (torch.Tensor) – True document label/target.
  • context (Dict[str, Any]) – Context is a dictionary of items that’s passed as additional metadata. Defaults to None.
  • reduce (bool) – Whether to reduce loss over the batch. Defaults to True.
Returns:

Model prediction and scores.

Return type:

Tuple[torch.Tensor, torch.Tensor]

get_pred(logit: torch.Tensor, target: Optional[torch.Tensor] = None, context: Optional[Dict[str, Any]] = None)[source]

Compute and return prediction and scores from the model.

Prediction is computed using CRF decoding.

Scores are softmax scores over the model logits where the logits are computed by rearranging the word logits such that decoded word tag has the highest valued logits. This is done because with CRF, the highest valued word tag for a given may not be part of the overall set of word tags. In order for argmax to work, we rearrange the logit values.

Parameters:
  • logit (torch.Tensor) – Logits returned WordTaggingModel.
  • target (torch.Tensor) – Not applicable. Defaults to None.
  • context (Optional[Dict[str, Any]]) – Context is a dictionary of items that’s passed as additional metadata. Defaults to None.
Returns:

Model prediction and scores.

Return type:

Tuple[torch.Tensor, torch.Tensor]

torchscript_predictions()[source]
class pytext.models.output_layers.ClassificationOutputLayer(target_names: Optional[List[str]] = None, loss_fn: Optional[pytext.loss.loss.Loss] = None, *args, **kwargs)[source]

Bases: pytext.models.output_layers.output_layer_base.OutputLayerBase

Output layer for document classification models. It supports CrossEntropyLoss and BinaryCrossEntropyLoss per document.

Parameters:loss_fn (Union[CrossEntropyLoss, BinaryCrossEntropyLoss]) – The loss function to use for computing loss. Defaults to None.
loss_fn

The loss function to use for computing loss.

classmethod from_config(config: pytext.models.output_layers.doc_classification_output_layer.ClassificationOutputLayer.Config, metadata: Optional[pytext.fields.field.FieldMeta] = None, labels: Optional[pytext.data.utils.Vocabulary] = None)[source]
get_pred(logit, *args, **kwargs)[source]

Compute and return prediction and scores from the model.

Prediction is computed using argmax over the document label/target space.

Scores are sigmoid or softmax scores over the model logits depending on the loss component being used.

Parameters:logit (torch.Tensor) – Logits returned DocModel.
Returns:Model prediction and scores.
Return type:Tuple[torch.Tensor, torch.Tensor]
class pytext.models.output_layers.RegressionOutputLayer(loss_fn: pytext.loss.loss.MSELoss, squash_to_unit_range: bool = False)[source]

Bases: pytext.models.output_layers.output_layer_base.OutputLayerBase

Output layer for doc regression models. Currently only supports Mean Squared Error loss.

Parameters:
  • loss (MSELoss) – config for MSE loss
  • squash_to_unit_range (bool) – whether to clamp the output to the range [0, 1], via a sigmoid.
classmethod from_config(config: pytext.models.output_layers.doc_regression_output_layer.RegressionOutputLayer.Config)[source]
get_loss(logit: torch.Tensor, target: torch.Tensor, context: Optional[Dict[str, Any]] = None, reduce: bool = True) → torch.Tensor[source]

Compute regression loss from logits and targets.

Parameters:
  • logit (torch.Tensor) – Logits returned Model.
  • target (torch.Tensor) – True label/target to compute loss against.
  • context (Optional[Dict[str, Any]]) – Context is a dictionary of items that’s passed as additional metadata by the DataHandler. Defaults to None.
  • reduce (bool) – Whether to reduce loss over the batch. Defaults to True.
Returns:

Model loss.

Return type:

torch.Tensor

get_pred(logit, *args, **kwargs)[source]

Compute predictions and scores from the model (unlike in classification, where prediction = “most likely class” and scores = “log probs”, here these are the same values). If squash_to_unit_range is True, fit prediction to [0, 1] via a sigmoid.

Parameters:logit (torch.Tensor) – Logits returned from the model.
Returns:Model prediction and scores.
Return type:Tuple[torch.Tensor, torch.Tensor]
torchscript_predictions()[source]
class pytext.models.output_layers.WordTaggingOutputLayer(target_names: Optional[List[str]] = None, loss_fn: Optional[pytext.loss.loss.Loss] = None, *args, **kwargs)[source]

Bases: pytext.models.output_layers.output_layer_base.OutputLayerBase

Output layer for word tagging models. It supports CrossEntropyLoss per word.

Parameters:loss_fn (CrossEntropyLoss) – Cross-entropy loss component. Defaults to None.
loss_fn

Cross-entropy loss component.

export_to_caffe2(workspace: <module 'caffe2.python.workspace' from '/home/docs/checkouts/readthedocs.org/user_builds/pytext/envs/master/lib/python3.7/site-packages/caffe2/python/workspace.py'>, init_net: caffe2.python.core.Net, predict_net: caffe2.python.core.Net, model_out: torch.Tensor, output_name: str) → List[caffe2.python.core.BlobReference][source]

Exports the word tagging output layer to Caffe2.

classmethod from_config(config: pytext.models.output_layers.word_tagging_output_layer.WordTaggingOutputLayer.Config, labels: pytext.data.utils.Vocabulary)[source]
get_loss(logit: torch.Tensor, target: Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor, torch.Tensor]], context: Dict[str, Any], reduce: bool = True) → torch.Tensor[source]

Compute word tagging loss by comparing prediction of each word in the sentence with its true label/target.

Parameters:
  • logit (torch.Tensor) – Logit returned by WordTaggingModel.
  • targets (torch.Tensor) – True document label/target.
  • context (Dict[str, Any]) – Context is a dictionary of items that’s passed as additional metadata. Defaults to None.
  • reduce (bool) – Whether to reduce loss over the batch. Defaults to True.
Returns:

Word tagging loss for all words in the sentence.

Return type:

torch.Tensor

get_pred(logit: torch.Tensor, *args, **kwargs) → Tuple[torch.Tensor, torch.Tensor][source]

Compute and return prediction and scores from the model. Prediction is computed using argmax over the word label/target space. Scores are softmax scores over the model logits.

Parameters:logit (torch.Tensor) – Logits returned WordTaggingModel.
Returns:Model prediction and scores.
Return type:Tuple[torch.Tensor, torch.Tensor]
torchscript_predictions()[source]
class pytext.models.output_layers.PairwiseRankingOutputLayer(target_names: Optional[List[str]] = None, loss_fn: Optional[pytext.loss.loss.Loss] = None, *args, **kwargs)[source]

Bases: pytext.models.output_layers.output_layer_base.OutputLayerBase

classmethod from_config(config)[source]
get_pred(logit, targets, context)[source]

Compute and return prediction and scores from the model.

Parameters:
  • logit (torch.Tensor) – Logits returned Model.
  • targets (Optional[torch.Tensor]) – True label/target. Only used by LMOutputLayer. Defaults to None.
  • context (Optional[Dict[str, Any]]) – Context is a dictionary of items that’s passed as additional metadata by the DataHandler. Defaults to None.
Returns:

Model prediction and scores.

Return type:

Tuple[torch.Tensor, torch.Tensor]

class pytext.models.output_layers.PairwiseCosineDistanceOutputLayer(target_names: Optional[List[str]] = None, loss_fn: Union[pytext.loss.loss.BinaryCrossEntropyLoss, pytext.loss.loss.CosineEmbeddingLoss, pytext.loss.loss.MAELoss, pytext.loss.loss.MSELoss, pytext.loss.loss.NLLLoss] = None, score_threshold: bool = 0.9, score_type: pytext.models.output_layers.distance_output_layer.OutputScore = <OutputScore.norm_cosine: 2>)[source]

Bases: pytext.models.output_layers.output_layer_base.OutputLayerBase

classmethod from_config(config, metadata: Optional[pytext.fields.field.FieldMeta] = None, labels: Optional[pytext.data.utils.Vocabulary] = None)[source]
get_loss(logits: Tuple[torch.Tensor, torch.Tensor], targets: torch.Tensor, context: Optional[Dict[str, Any]] = None, reduce: bool = True) → torch.Tensor[source]

Compute and return the loss given logits and targets.

Parameters:
  • logit (torch.Tensor) – Logits returned Model.
  • target (torch.Tensor) – True label/target to compute loss against.
  • context (Optional[Dict[str, Any]]) – Context is a dictionary of items that’s passed as additional metadata by the DataHandler. Defaults to None.
  • reduce (bool) – Whether to reduce loss over the batch. Defaults to True.
Returns:

Model loss.

Return type:

torch.Tensor

get_pred(logits: torch.Tensor, targets: torch.Tensor, *args, **kwargs)[source]

Compute and return prediction and scores from the model.

Parameters:
  • logit (torch.Tensor) – Logits returned Model.
  • targets (Optional[torch.Tensor]) – True label/target. Only used by LMOutputLayer. Defaults to None.
  • context (Optional[Dict[str, Any]]) – Context is a dictionary of items that’s passed as additional metadata by the DataHandler. Defaults to None.
Returns:

Model prediction and scores.

Return type:

Tuple[torch.Tensor, torch.Tensor]

class pytext.models.output_layers.PairwiseCosineRegressionOutputLayer(loss_fn: Union[pytext.loss.loss.MSELoss, pytext.loss.loss.MAELoss])[source]

Bases: pytext.models.output_layers.output_layer_base.OutputLayerBase

Output layer for pair (two-tower) regression models. Accepts two embedding tensors, and computes cosine similarity. The loss is between regression label and cosine similarity.

classmethod from_config(config: pytext.models.output_layers.doc_regression_output_layer.PairwiseCosineRegressionOutputLayer.Config, **kwargs)[source]
get_loss(logits: Tuple[torch.Tensor, torch.Tensor], target: torch.Tensor, context: Optional[Dict[str, Any]] = None, reduce: bool = True) → torch.Tensor[source]
Parameters:
  • logits (Tuple[torch.Tensor, torch.Tensor]) – Logits returned from pairwise model
  • target (torch.Tensor) – Float target to compute loss against
  • context (Optional[Dict[str, Any]]) – Context is a dictionary from data handler
  • reduce (bool) – Whether to reduce loss over the batch. Defaults to True.
Returns:

Model loss.

Return type:

torch.Tensor

get_pred(logits, *args, **kwargs)[source]
Parameters:logits (Tuple[torch.Tensor, torch.Tensor]) – Logits returned from pairwise model
Returns:Model prediction and scores.
Return type:Tuple[torch.Tensor, torch.Tensor]
class pytext.models.output_layers.DenseRetrievalOutputLayer(target_names: Optional[List[str]] = None, loss_fn: Union[pytext.loss.loss.BinaryCrossEntropyLoss, pytext.loss.loss.CosineEmbeddingLoss, pytext.loss.loss.MAELoss, pytext.loss.loss.MSELoss, pytext.loss.loss.NLLLoss] = None, score_threshold: bool = 0.9, score_type: pytext.models.output_layers.distance_output_layer.OutputScore = <OutputScore.norm_cosine: 2>)[source]

Bases: pytext.models.output_layers.distance_output_layer.PairwiseCosineDistanceOutputLayer

get_loss(logits: Tuple[torch.Tensor, torch.Tensor], targets: torch.Tensor, context: Optional[Dict[str, Any]] = None, reduce: bool = True) → torch.Tensor[source]

Compute and return the loss given logits and targets.

Parameters:
  • logit (torch.Tensor) – Logits returned Model.
  • target (torch.Tensor) – True label/target to compute loss against.
  • context (Optional[Dict[str, Any]]) – Context is a dictionary of items that’s passed as additional metadata by the DataHandler. Defaults to None.
  • reduce (bool) – Whether to reduce loss over the batch. Defaults to True.
Returns:

Model loss.

Return type:

torch.Tensor

get_pred(logits: torch.Tensor, targets: torch.Tensor, *args, **kwargs)[source]

Compute and return prediction and scores from the model.

Parameters:
  • logit (torch.Tensor) – Logits returned Model.
  • targets (Optional[torch.Tensor]) – True label/target. Only used by LMOutputLayer. Defaults to None.
  • context (Optional[Dict[str, Any]]) – Context is a dictionary of items that’s passed as additional metadata by the DataHandler. Defaults to None.
Returns:

Model prediction and scores.

Return type:

Tuple[torch.Tensor, torch.Tensor]

class pytext.models.output_layers.OutputLayerUtils[source]

Bases: object

static gen_additional_blobs(predict_net: caffe2.python.core.Net, probability_out, model_out: torch.Tensor, output_name: str, label_names: List[str]) → List[caffe2.python.core.BlobReference][source]

Utility method to generate additional blobs for human readable result for models that use explicit labels.