class_docstr: A ``WikiTablesSemanticParser`` is a :class:`Model` which takes as input a table and a question,
  and produces a logical form that answers the question when executed over the table.  The
  logical form is generated by a `type-constrained`, `transition-based` parser. This is an
  abstract class that defines most of the functionality related to the transition-based parser. It
  does not contain the implementation for actually training the parser. You may want to train it
  using a learning-to-search algorithm, in which case you will want to use
  ``WikiTablesErmSemanticParser``, or if you have a set of approximate logical forms that give the
  correct denotation, you will want to use ``WikiTablesMmlSemanticParser``.
  
  Parameters
  ----------
  vocab : ``Vocabulary``
  question_embedder : ``TextFieldEmbedder``
      Embedder for questions.
  action_embedding_dim : ``int``
      Dimension to use for action embeddings.
  encoder : ``Seq2SeqEncoder``
      The encoder to use for the input question.
  entity_encoder : ``Seq2VecEncoder``
      The encoder to used for averaging the words of an entity.
  max_decoding_steps : ``int``
      When we're decoding with a beam search, what's the maximum number of steps we should take?
      This only applies at evaluation time, not during training.
  use_neighbor_similarity_for_linking : ``bool``, optional (default=False)
      If ``True``, we will compute a max similarity between a question token and the `neighbors`
      of an entity as a component of the linking scores.  This is meant to capture the same kind
      of information as the ``related_column`` feature.
  dropout : ``float``, optional (default=0)
      If greater than 0, we will apply dropout with this probability after all encoders (pytorch
      LSTMs do not apply dropout to their last layer).
  num_linking_features : ``int``, optional (default=10)
      We need to construct a parameter vector for the linking features, so we need to know how
      many there are.  The default of 8 here matches the default in the ``KnowledgeGraphField``,
      which is to use all eight defined features. If this is 0, another term will be added to the
      linking score. This term contains the maximum similarity value from the entity's neighbors
      and the question.
  rule_namespace : ``str``, optional (default=rule_labels)
      The vocabulary namespace to use for production rules.  The default corresponds to the
      default used in the dataset reader, so you likely don't need to modify this.
  tables_directory : ``str``, optional (default=/wikitables/)
      The directory to find tables when evaluating logical forms.  We rely on a call to SEMPRE to
      evaluate logical forms, and SEMPRE needs to read the table from disk itself.  This tells
      SEMPRE where to find the tables.
docstr: Constructs a map from ``(batch_index, action_index)`` to ``(batch_index * entity_index)``.
  That is, some actions correspond to terminal productions of entities from our table.  We
  need to find those actions and map them to their corresponding entity indices, where the
  entity index is its position in the list of entities returned by the ``world``.  This list
  is what defines the second dimension of the ``linking_scores`` tensor, so we can use this
  index to look up linking scores for each action in that tensor.
  
  For easier processing later, the mapping that we return is `flattened` - we really want to
  map ``(batch_index, action_index)`` to ``(batch_index, entity_index)``, but we are going to
  have to use the result of this mapping to do ``index_selects`` on the ``linking_scores``
  tensor.  You can't do ``index_select`` with tuples, so we flatten ``linking_scores`` to
  have shape ``(batch_size * num_entities, num_question_tokens)``, and return shifted indices
  into this flattened tensor.
  
  Parameters
  ----------
  linking_scores : ``torch.Tensor``
      A tensor representing linking scores between each table entity and each question token.
      Has shape ``(batch_size, num_entities, num_question_tokens)``.
  worlds : ``List[WikiTablesWorld]``
      The ``World`` for each batch instance.  The ``World`` contains a reference to the
      ``TableKnowledgeGraph`` that defines the set of entities in the linking.
  actions : ``List[List[ProductionRuleArray]]``
      The list of possible actions for each batch instance.  Our action indices are defined
      in terms of this list, so we'll find entity productions in this list and map them to
      entity indices from the entity list we get from the ``World``.
  
  Returns
  -------
  flattened_linking_scores : ``torch.Tensor``
      A flattened version of ``linking_scores``, with shape ``(batch_size * num_entities,
      num_question_tokens)``.
  actions_to_entities : ``Dict[Tuple[int, int], int]``
      A mapping from ``(batch_index, action_index)`` to ``(batch_size * num_entities)``,
      representing which action indices correspond to which entity indices in the returned
      ``flattened_linking_scores`` tensor.
docstr_tok: ["Constructs","a","map","from","``","(","batch_index",",","action_index",")","``","to","``","(","batch_index","*","entity_index",")","``",".","That","is",",","some","actions","correspond","to","terminal","productions","of","entities","from","our","table",".","We","need","to","find","those","actions","and","map","them","to","their","corresponding","entity","indices",",","where","the","entity","index","is","its","position","in","the","list","of","entities","returned","by","the","``","world","``",".","This","list","is","what","defines","the","second","dimension","of","the","``","linking_scores","``","tensor",",","so","we","can","use","this","index","to","look","up","linking","scores","for","each","action","in","that","tensor",".","For","easier","processing","later",",","the","mapping","that","we","return","is","`","flattened","`","-","we","really","want","to","map","``","(","batch_index",",","action_index",")","``","to","``","(","batch_index",",","entity_index",")","``",",","but","we","are","going","to","have","to","use","the","result","of","this","mapping","to","do","``","index_selects","``","on","the","``","linking_scores","``","tensor",".","You","ca","n't","do","``","index_select","``","with","tuples",",","so","we","flatten","``","linking_scores","``","to","have","shape","``","(","batch_size","*","num_entities",",","num_question_tokens",")","``",",","and","return","shifted","indices","into","this","flattened","tensor",".","Parameters","--","--","--","--","--","linking_scores",":","``","torch.Tensor","``","A","tensor","representing","linking","scores","between","each","table","entity","and","each","question","token",".","Has","shape","``","(","batch_size",",","num_entities",",","num_question_tokens",")","``",".","worlds",":","``","List","[","WikiTablesWorld","]","``","The","``","World","``","for","each","batch","instance",".","The","``","World","``","contains","a","reference","to","the","``","TableKnowledgeGraph","``","that","defines","the","set","of","entities","in","the","linking",".","actions",":","``","List","[","List","[","ProductionRuleArray","]","]","``","The","list","of","possible","actions","for","each","batch","instance",".","Our","action","indices","are","defined","in","terms","of","this","list",",","so","we","'ll","find","entity","productions","in","this","list","and","map","them","to","entity","indices","from","the","entity","list","we","get","from","the","``","World","``",".","Returns","--","--","--","-","flattened_linking_scores",":","``","torch.Tensor","``","A","flattened","version","of","``","linking_scores","``",",","with","shape","``","(","batch_size","*","num_entities",",","num_question_tokens",")","``",".","actions_to_entities",":","``","Dict","[","Tuple","[","int",",","int","]",",","int","]","``","A","mapping","from","``","(","batch_index",",","action_index",")","``","to","``","(","batch_size","*","num_entities",")","``",",","representing","which","action","indices","correspond","to","which","entity","indices","in","the","returned","``","flattened_linking_scores","``","tensor","."]
code: def _map_entity_productions(linking_scores, worlds, actions):
      """
      Constructs a map from ``(batch_index, action_index)`` to ``(batch_index * entity_index)``.
      That is, some actions correspond to terminal productions of entities from our table.  We
      need to find those actions and map them to their corresponding entity indices, where the
      entity index is its position in the list of entities returned by the ``world``.  This list
      is what defines the second dimension of the ``linking_scores`` tensor, so we can use this
      index to look up linking scores for each action in that tensor.
  
      For easier processing later, the mapping that we return is `flattened` - we really want to
      map ``(batch_index, action_index)`` to ``(batch_index, entity_index)``, but we are going to
      have to use the result of this mapping to do ``index_selects`` on the ``linking_scores``
      tensor.  You can't do ``index_select`` with tuples, so we flatten ``linking_scores`` to
      have shape ``(batch_size * num_entities, num_question_tokens)``, and return shifted indices
      into this flattened tensor.
  
      Parameters
      ----------
      linking_scores : ``torch.Tensor``
          A tensor representing linking scores between each table entity and each question token.
          Has shape ``(batch_size, num_entities, num_question_tokens)``.
      worlds : ``List[WikiTablesWorld]``
          The ``World`` for each batch instance.  The ``World`` contains a reference to the
          ``TableKnowledgeGraph`` that defines the set of entities in the linking.
      actions : ``List[List[ProductionRuleArray]]``
          The list of possible actions for each batch instance.  Our action indices are defined
          in terms of this list, so we'll find entity productions in this list and map them to
          entity indices from the entity list we get from the ``World``.
  
      Returns
      -------
      flattened_linking_scores : ``torch.Tensor``
          A flattened version of ``linking_scores``, with shape ``(batch_size * num_entities,
          num_question_tokens)``.
      actions_to_entities : ``Dict[Tuple[int, int], int]``
          A mapping from ``(batch_index, action_index)`` to ``(batch_size * num_entities)``,
          representing which action indices correspond to which entity indices in the returned
          ``flattened_linking_scores`` tensor.
      """
      (
          batch_size,
          num_entities,
          num_question_tokens,
      ) = linking_scores.size()
      entity_map = {}
      for batch_index, world in enumerate(worlds):
          for entity_index, entity in enumerate(
              world.table_graph.entities
          ):
              entity_map[(batch_index, entity)] = (
                  batch_index * num_entities + entity_index
              )
      actions_to_entities = {}
      for batch_index, action_list in enumerate(actions):
          for action_index, action in enumerate(action_list):
              if not action[0]:
                  # This action is padding.
                  continue
              _, production = action[0].split(" -> ")
              entity_index = entity_map.get(
                  (batch_index, production), None
              )
              if entity_index is not None:
                  actions_to_entities[
                      (batch_index, action_index)
                  ] = entity_index
      flattened_linking_scores = linking_scores.view(
          batch_size * num_entities, num_question_tokens
      )
      return flattened_linking_scores, actions_to_entities
  
imports: from __future__ import absolute_import
  import torch
  from allennlp.common.checks import check_dimensions_match
  from allennlp.common.util import pad_sequence_to_length
  from allennlp.data import Vocabulary
  from allennlp.data.fields.production_rule_field import ProductionRuleArray
  from allennlp.models.model import Model
  from allennlp.models.semantic_parsing.wikitables.wikitables_decoder_state import WikiTablesDecoderState
  from allennlp.modules import Embedding, Seq2SeqEncoder, Seq2VecEncoder, TextFieldEmbedder, TimeDistributed
  from allennlp.modules.seq2vec_encoders import BagOfEmbeddingsEncoder
  from allennlp.nn import util
  from allennlp.nn.decoding import GrammarState, RnnState, ChecklistState
  from allennlp.semparse.type_declarations import type_declaration
  from allennlp.semparse.type_declarations.type_declaration import START_SYMBOL
  from allennlp.semparse.worlds import WikiTablesWorld
  from allennlp.training.metrics import Average, WikiTablesAccuracy
label_desc: Use image node_id 10 for calling the WikiTablesSemanticParser obj's underlying member method code with example usage: obj._map_entity_productions(linking_scores, worlds, actions) and returns: flattened_linking_scores,  actions_to_entities