class_docstr: This ``Module`` implements the matching layer of BiMPM model described in `Bilateral
  Multi-Perspective Matching for Natural Language Sentences <https://arxiv.org/abs/1702.03814>`_
  by Zhiguo Wang et al., 2017.
  Also please refer to the `TensorFlow implementation <https://github.com/zhiguowang/BiMPM/>`_ and
  `PyTorch implementation <https://github.com/galsang/BIMPM-pytorch>`_.
  
  Parameters
  ----------
  hidden_dim : ``int``, optional (default = 100)
      The hidden dimension of the representations
  num_perspectives : ``int``, optional (default = 20)
      The number of perspectives for matching
  share_weights_between_directions : ``bool``, optional (default = True)
      If True, share weight between matching from sentence1 to sentence2 and from sentence2
      to sentence1, useful for non-symmetric tasks
  is_forward : ``bool``, optional (default = None)
      Whether the matching is for forward sequence or backward sequence, useful in finding last
      token in full matching. It can not be None if with_full_match is True.
  with_full_match : ``bool``, optional (default = True)
      If True, include full match
  with_maxpool_match : ``bool``, optional (default = True)
      If True, include max pool match
  with_attentive_match : ``bool``, optional (default = True)
      If True, include attentive match
  with_max_attentive_match : ``bool``, optional (default = True)
      If True, include max attentive match
class_docstr_tok: ["This","``","Module","``","implements","the","matching","layer","of","BiMPM","model","described","in","`","Bilateral","Multi-Perspective","Matching","for","Natural","Language","Sentences","<","https",":","\/\/arxiv.org\/abs\/1702.03814",">","`","_","by","Zhiguo","Wang","et","al.",",","2017",".","Also","please","refer","to","the","`","TensorFlow","implementation","<","https",":","\/\/github.com\/zhiguowang\/BiMPM\/",">","`","_","and","`","PyTorch","implementation","<","https",":","\/\/github.com\/galsang\/BIMPM-pytorch",">","`","_",".","Parameters","--","--","--","--","--","hidden_dim",":","``","int","``",",","optional","(","default","=","100",")","The","hidden","dimension","of","the","representations","num_perspectives",":","``","int","``",",","optional","(","default","=","20",")","The","number","of","perspectives","for","matching","share_weights_between_directions",":","``","bool","``",",","optional","(","default","=","True",")","If","True",",","share","weight","between","matching","from","sentence1","to","sentence2","and","from","sentence2","to","sentence1",",","useful","for","non-symmetric","tasks","is_forward",":","``","bool","``",",","optional","(","default","=","None",")","Whether","the","matching","is","for","forward","sequence","or","backward","sequence",",","useful","in","finding","last","token","in","full","matching",".","It","can","not","be","None","if","with_full_match","is","True",".","with_full_match",":","``","bool","``",",","optional","(","default","=","True",")","If","True",",","include","full","match","with_maxpool_match",":","``","bool","``",",","optional","(","default","=","True",")","If","True",",","include","max","pool","match","with_attentive_match",":","``","bool","``",",","optional","(","default","=","True",")","If","True",",","include","attentive","match","with_max_attentive_match",":","``","bool","``",",","optional","(","default","=","True",")","If","True",",","include","max","attentive","match"]
code: def __init__(
      self,
      hidden_dim=100,
      num_perspectives=20,
      share_weights_between_directions=True,
      is_forward=None,
      with_full_match=True,
      with_maxpool_match=True,
      with_attentive_match=True,
      with_max_attentive_match=True,
  ):
      super(BiMpmMatching, self).__init__()
  
      self.hidden_dim = hidden_dim
      self.num_perspectives = num_perspectives
      self.is_forward = is_forward
  
      self.with_full_match = with_full_match
      self.with_maxpool_match = with_maxpool_match
      self.with_attentive_match = with_attentive_match
      self.with_max_attentive_match = with_max_attentive_match
  
      if not (
          with_full_match
          or with_maxpool_match
          or with_attentive_match
          or with_max_attentive_match
      ):
          raise ConfigurationError(
              "At least one of the matching method should be enabled"
          )
  
      def create_parameter():  # utility function to create and initialize a parameter
          param = nn.Parameter(
              torch.zeros(num_perspectives, hidden_dim)
          )
          torch.nn.init.kaiming_normal_(param)
          return param
  
      def share_or_create(
          weights_to_share,
      ):  # utility function to create or share the weights
          return (
              weights_to_share
              if share_weights_between_directions
              else create_parameter()
          )
  
      output_dim = 2  # used to calculate total output dimension, 2 is for cosine max and cosine min
      if with_full_match:
          if is_forward is None:
              raise ConfigurationError(
                  "Must specify is_forward to enable full matching"
              )
          self.full_match_weights = create_parameter()
          self.full_match_weights_reversed = share_or_create(
              self.full_match_weights
          )
          output_dim += num_perspectives + 1
  
      if with_maxpool_match:
          self.maxpool_match_weights = create_parameter()
          output_dim += num_perspectives * 2
  
      if with_attentive_match:
          self.attentive_match_weights = create_parameter()
          self.attentive_match_weights_reversed = share_or_create(
              self.attentive_match_weights
          )
          output_dim += num_perspectives + 1
  
      if with_max_attentive_match:
          self.max_attentive_match_weights = create_parameter()
          self.max_attentive_match_weights_reversed = share_or_create(
              self.max_attentive_match_weights
          )
          output_dim += num_perspectives + 1
  
      self.output_dim = output_dim
  
file_path: magnitude/pymagnitude/third_party/allennlp/modules/bimpm_matching.py
imports: from __future__ import division
  from __future__ import absolute_import
  import torch
  import torch.nn
  import torch.nn.functional
  from allennlp.common.checks import ConfigurationError
  from allennlp.common.registrable import FromParams
  from allennlp.nn.util import get_lengths_from_binary_sequence_mask, masked_max, masked_mean, masked_softmax
label_desc: Use image node_id 1 to create a new BiMpmMatching object from inherited base classes: nn, FromParams with example: obj = BiMpmMatching(hidden_dim, num_perspectives, share_weights_between_directions, is_forward, with_full_match, with_maxpool_match, with_attentive_match, with_max_attentive_match)