Skip to content

bidaf_ensemble

allennlp_models.rc.models.bidaf_ensemble

[SOURCE]


BidafEnsemble#

@Model.register("bidaf-ensemble")
class BidafEnsemble(Model):
 | def __init__(
 |     self,
 |     submodels: List[BidirectionalAttentionFlow]
 | ) -> None

This class ensembles the output from multiple BiDAF models.

It combines results from the submodels by averaging the start and end span probabilities.

forward#

class BidafEnsemble(Model):
 | ...
 | def forward(
 |     self,
 |     question: Dict[str, torch.LongTensor],
 |     passage: Dict[str, torch.LongTensor],
 |     span_start: torch.IntTensor = None,
 |     span_end: torch.IntTensor = None,
 |     metadata: List[Dict[str, Any]] = None
 | ) -> Dict[str, torch.Tensor]

The forward method runs each of the submodels, then selects the best span from the subresults. The best span is determined by averaging the probabilities for the start and end of the spans.

Parametersquestion : Dict[str, torch.LongTensor]

From a ``TextField``.

passage : Dict[str, torch.LongTensor] From a TextField. The model assumes that this passage contains the answer to the question, and predicts the beginning and ending positions of the answer within the passage. span_start : torch.IntTensor, optional From an IndexField. This is one of the things we are trying to predict - the beginning position of the answer with the passage. This is an inclusive token index. If this is given, we will compute a loss that gets included in the output dictionary. span_end : torch.IntTensor, optional From an IndexField. This is one of the things we are trying to predict - the ending position of the answer with the passage. This is an inclusive token index. If this is given, we will compute a loss that gets included in the output dictionary. metadata : List[Dict[str, Any]], optional If present, this should contain the question ID, original passage text, and token offsets into the passage for each instance in the batch. We use this for computing official metrics using the official SQuAD evaluation script. The length of this list should be the batch size, and each dictionary should have the keys id, original_passage, and token_offsets. If you only want the best span string and don't care about official metrics, you can omit the id key.

ReturnsAn output dictionary consisting of:

best_span : torch.IntTensor The result of a constrained inference over span_start_logits and span_end_logits to find the most probable span. Shape is (batch_size, 2) and each offset is a token index. best_span_str : List[str] If sufficient metadata was provided for the instances in the batch, we also return the string from the original passage that the model thinks is the best answer to the question.

get_metrics#

class BidafEnsemble(Model):
 | ...
 | def get_metrics(self, reset: bool = False) -> Dict[str, float]

from_params#

class BidafEnsemble(Model):
 | ...
 | @classmethod
 | def from_params(
 |     cls,
 |     vocab: Vocabulary,
 |     params: Params
 | ) -> "BidafEnsemble"

default_predictor#

class BidafEnsemble(Model):
 | ...
 | default_predictor = "reading_comprehension"

ensemble#

def ensemble(
    subresults: List[Dict[str, torch.Tensor]]
) -> torch.Tensor

Identifies the best prediction given the results from the submodels.

Parameterssubresults : List[Dict[str, torch.Tensor]]

Results of each submodel.

ReturnsThe index of the best submodel.