This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Move decomposable attention predictor * Move default predictor * Remove temporary shim
- Loading branch information
Showing
11 changed files
with
79 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from allennlp_models.nli.snli_reader import SnliReader | ||
from allennlp_models.nli.decomposable_attention_model import DecomposableAttention | ||
from allennlp_models.nli.decomposable_attention_predictor import DecomposableAttentionPredictor | ||
from allennlp_models.nli.bimpm_model import BiMpm | ||
from allennlp_models.nli.esim_model import ESIM | ||
from allennlp_models.nli.quora_paraphrase_reader import QuoraParaphraseDatasetReader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from typing import List, Dict | ||
from copy import deepcopy | ||
|
||
import numpy | ||
from overrides import overrides | ||
|
||
from allennlp.common.util import JsonDict | ||
from allennlp.data import Instance | ||
from allennlp.predictors.predictor import Predictor | ||
from allennlp.data.fields import LabelField | ||
|
||
|
||
@Predictor.register("textual-entailment") | ||
class DecomposableAttentionPredictor(Predictor): | ||
""" | ||
Predictor for the [`DecomposableAttention`](../models/decomposable_attention.md) model. | ||
Registered as a `Predictor` with name "textual-entailment". | ||
""" | ||
|
||
def predict(self, premise: str, hypothesis: str) -> JsonDict: | ||
""" | ||
Predicts whether the hypothesis is entailed by the premise text. | ||
# Parameters | ||
premise : `str` | ||
A passage representing what is assumed to be true. | ||
hypothesis : `str` | ||
A sentence that may be entailed by the premise. | ||
# Returns | ||
`JsonDict` | ||
A dictionary where the key "label_probs" determines the probabilities of each of | ||
[entailment, contradiction, neutral]. | ||
""" | ||
return self.predict_json({"premise": premise, "hypothesis": hypothesis}) | ||
|
||
@overrides | ||
def _json_to_instance(self, json_dict: JsonDict) -> Instance: | ||
""" | ||
Expects JSON that looks like `{"premise": "...", "hypothesis": "..."}`. | ||
""" | ||
premise_text = json_dict["premise"] | ||
hypothesis_text = json_dict["hypothesis"] | ||
return self._dataset_reader.text_to_instance(premise_text, hypothesis_text) | ||
|
||
@overrides | ||
def predictions_to_labeled_instances( | ||
self, instance: Instance, outputs: Dict[str, numpy.ndarray] | ||
) -> List[Instance]: | ||
new_instance = deepcopy(instance) | ||
label = numpy.argmax(outputs["label_logits"]) | ||
# Skip indexing, we have integer representations of the strings "entailment", etc. | ||
new_instance.add_field("label", LabelField(int(label), skip_indexing=True)) | ||
return [new_instance] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters