bimpm
allennlp_models.pair_classification.models.bimpm
BiMPM (Bilateral Multi-Perspective Matching) model implementation.
BiMpm#
@Model.register("bimpm")
class BiMpm(Model):
| def __init__(
| self,
| vocab: Vocabulary,
| text_field_embedder: TextFieldEmbedder,
| matcher_word: BiMpmMatching,
| encoder1: Seq2SeqEncoder,
| matcher_forward1: BiMpmMatching,
| matcher_backward1: BiMpmMatching,
| encoder2: Seq2SeqEncoder,
| matcher_forward2: BiMpmMatching,
| matcher_backward2: BiMpmMatching,
| aggregator: Seq2VecEncoder,
| classifier_feedforward: FeedForward,
| dropout: float = 0.1,
| initializer: InitializerApplicator = InitializerApplicator(),
| **kwargs
| ) -> None
This Model
implements BiMPM model described in Bilateral Multi-Perspective Matching
for Natural Language Sentences by Zhiguo Wang et al., 2017.
Also please refer to the TensorFlow implementation and
PyTorch implementation.
Registered as a Model
with name "bimpm".
Parameters¶
- vocab :
Vocabulary
- text_field_embedder :
TextFieldEmbedder
Used to embed thepremise
andhypothesis
TextFields
we get as input to the model. - matcher_word :
BiMpmMatching
BiMPM matching on the output of word embeddings of premise and hypothesis. - encoder1 :
Seq2SeqEncoder
First encoder layer for the premise and hypothesis - matcher_forward1 :
BiMPMMatching
BiMPM matching for the forward output of first encoder layer - matcher_backward1 :
BiMPMMatching
BiMPM matching for the backward output of first encoder layer - encoder2 :
Seq2SeqEncoder
Second encoder layer for the premise and hypothesis - matcher_forward2 :
BiMPMMatching
BiMPM matching for the forward output of second encoder layer - matcher_backward2 :
BiMPMMatching
BiMPM matching for the backward output of second encoder layer - aggregator :
Seq2VecEncoder
Aggregator of all BiMPM matching vectors - classifier_feedforward :
FeedForward
Fully connected layers for classification. - dropout :
float
, optional (default =0.1
)
Dropout percentage to use. - initializer :
InitializerApplicator
, optional (default =InitializerApplicator()
)
If provided, will be used to initialize the model parameters.
forward#
class BiMpm(Model):
| ...
| def forward(
| self,
| premise: TextFieldTensors,
| hypothesis: TextFieldTensors,
| label: torch.LongTensor = None,
| metadata: List[Dict[str, Any]] = None
| ) -> Dict[str, torch.Tensor]
Parameters¶
- premise :
TextFieldTensors
The premise from aTextField
- hypothesis :
TextFieldTensors
The hypothesis from aTextField
- label :
torch.LongTensor
, optional (default =None
)
The label for the pair of the premise and the hypothesis - metadata :
List[Dict[str, Any]]
, optional (default =None
)
Additional information about the pair
Returns¶
-
An output dictionary consisting of:
-
logits :
torch.FloatTensor
A tensor of shape(batch_size, num_labels)
representing unnormalised log probabilities of the entailment label. - loss :
torch.FloatTensor
, optional
A scalar loss to be optimised.
get_metrics#
class BiMpm(Model):
| ...
| def get_metrics(self, reset: bool = False) -> Dict[str, float]
make_output_human_readable#
class BiMpm(Model):
| ...
| def make_output_human_readable(
| self,
| output_dict: Dict[str, torch.Tensor]
| ) -> Dict[str, torch.Tensor]
Does a simple argmax over the probabilities, converts index to string label, and
add "label"
key to the dictionary with the result.
default_predictor#
class BiMpm(Model):
| ...
| default_predictor = "textual_entailment"