class_docstr: Implements the beam search algorithm for decoding the most likely sequences.
  
  # Parameters
  
  end_index : `int`
      The index of the "stop" or "end" token in the target vocabulary.
  
  max_steps : `int`, optional (default = `50`)
      The maximum number of decoding steps to take, i.e. the maximum length
      of the predicted sequences.
  
  beam_size : `int`, optional (default = `10`)
      The width of the beam used.
  
  per_node_beam_size : `int`, optional (default = `beam_size`)
      The maximum number of candidates to consider per node, at each step in the search.
      If not given, this just defaults to `beam_size`. Setting this parameter
      to a number smaller than `beam_size` may give better results, as it can introduce
      more diversity into the search. See
      [*Beam Search Strategies for Neural Machine Translation*, Freitag and Al-Onaizan, 2017]
      (https://api.semanticscholar.org/CorpusID:2229477).
  
  sampler : `Sampler`, optional (default = `None`)
      An optional `Sampler` which is used to pick next candidate nodes and beams.
      If not specified, `DeterministicSampler` will be used, which just takes the
      `per_node_beam_size` most likely nodes and the `beam_size` most likely beams.
  
      Using the [`GumbelSampler`](#gumbelsampler), on the other hand, will give you
      [Stochastic Beam Search](https://api.semanticscholar.org/CorpusID:76662039).
  
  min_steps : `int`, optional (default = `None`)
      The minimum number of decoding steps to take, i.e. the minimum length of
      the predicted sequences. This does not include the start or end tokens. If `None`,
      no minimum is enforced.
  
  final_sequence_scorer : `FinalSequenceScorer`, optional (default = `None`)
      An optional `FinalSequenceScorer` which is used to score the final generated sequences.
      The output from this module is what is returned by the `search` method. If not
      specified, `SequenceLogProbabilityScorer` will be used, which scores the sequences
      by the sum of the token log probabilities.
  
  constraints: `List[Constraint]`, optional (default = `None`)
      An optional list of `Constraint`s which should be applied during beam search. If not
      provided, no constraints will be enforced.
  
  vocab: `Vocabulary`
      If `constraints` is not `None`, then `Vocabulary` will be passed to each constraint
      during its initialization. Having access to the vocabulary may be useful for certain
      contraints, e.g., to mask out invalid predictions during structured prediction.
  
      In a typical AllenNLP configuration file, this parameter does not get an entry under the
      "model", it gets specified as a top-level parameter, then is passed in to the model
      separately.
class_docstr_tok: ["Implements","the","beam","search","algorithm","for","decoding","the","most","likely","sequences",".","#","Parameters","end_index",":","`","int","`","The","index","of","the","``","stop","''","or","``","end","''","token","in","the","target","vocabulary",".","max_steps",":","`","int","`",",","optional","(","default","=","`","50","`",")","The","maximum","number","of","decoding","steps","to","take",",","i.e",".","the","maximum","length","of","the","predicted","sequences",".","beam_size",":","`","int","`",",","optional","(","default","=","`","10","`",")","The","width","of","the","beam","used",".","per_node_beam_size",":","`","int","`",",","optional","(","default","=","`","beam_size","`",")","The","maximum","number","of","candidates","to","consider","per","node",",","at","each","step","in","the","search",".","If","not","given",",","this","just","defaults","to","`","beam_size","`",".","Setting","this","parameter","to","a","number","smaller","than","`","beam_size","`","may","give","better","results",",","as","it","can","introduce","more","diversity","into","the","search",".","See","[","*","Beam","Search","Strategies","for","Neural","Machine","Translation","*",",","Freitag","and","Al-Onaizan",",","2017","]","(","https",":","\/\/api.semanticscholar.org\/CorpusID:2229477",")",".","sampler",":","`","Sampler","`",",","optional","(","default","=","`","None","`",")","An","optional","`","Sampler","`","which","is","used","to","pick","next","candidate","nodes","and","beams",".","If","not","specified",",","`","DeterministicSampler","`","will","be","used",",","which","just","takes","the","`","per_node_beam_size","`","most","likely","nodes","and","the","`","beam_size","`","most","likely","beams",".","Using","the","[","`","GumbelSampler","`","]","(","#","gumbelsampler",")",",","on","the","other","hand",",","will","give","you","[","Stochastic","Beam","Search","]","(","https",":","\/\/api.semanticscholar.org\/CorpusID:76662039",")",".","min_steps",":","`","int","`",",","optional","(","default","=","`","None","`",")","The","minimum","number","of","decoding","steps","to","take",",","i.e",".","the","minimum","length","of","the","predicted","sequences",".","This","does","not","include","the","start","or","end","tokens",".","If","`","None","`",",","no","minimum","is","enforced",".","final_sequence_scorer",":","`","FinalSequenceScorer","`",",","optional","(","default","=","`","None","`",")","An","optional","`","FinalSequenceScorer","`","which","is","used","to","score","the","final","generated","sequences",".","The","output","from","this","module","is","what","is","returned","by","the","`","search","`","method",".","If","not","specified",",","`","SequenceLogProbabilityScorer","`","will","be","used",",","which","scores","the","sequences","by","the","sum","of","the","token","log","probabilities",".","constraints",":","`","List","[","Constraint","]","`",",","optional","(","default","=","`","None","`",")","An","optional","list","of","`","Constraint","`","s","which","should","be","applied","during","beam","search",".","If","not","provided",",","no","constraints","will","be","enforced",".","vocab",":","`","Vocabulary","`","If","`","constraints","`","is","not","`","None","`",",","then","`","Vocabulary","`","will","be","passed","to","each","constraint","during","its","initialization",".","Having","access","to","the","vocabulary","may","be","useful","for","certain","contraints",",","e.g.",",","to","mask","out","invalid","predictions","during","structured","prediction",".","In","a","typical","AllenNLP","configuration","file",",","this","parameter","does","not","get","an","entry","under","the","``","model","''",",","it","gets","specified","as","a","top-level","parameter",",","then","is","passed","in","to","the","model","separately","."]
code: def _reconstruct_sequences(predictions, backpointers):
      # Reconstruct the sequences.
      # shape: [(batch_size, beam_size, 1)]
      reconstructed_predictions = [predictions[-1].unsqueeze(2)]
  
      if not backpointers:
          return reconstructed_predictions
  
      # shape: (batch_size, beam_size)
      cur_backpointers = backpointers[-1]
  
      for timestep in range(len(predictions) - 2, 0, -1):
          # shape: (batch_size, beam_size, 1)
          cur_preds = (
              predictions[timestep]
              .gather(1, cur_backpointers)
              .unsqueeze(2)
          )
  
          reconstructed_predictions.append(cur_preds)
  
          # shape: (batch_size, beam_size)
          cur_backpointers = backpointers[timestep - 1].gather(
              1, cur_backpointers
          )
  
      # shape: (batch_size, beam_size, 1)
      final_preds = (
          predictions[0].gather(1, cur_backpointers).unsqueeze(2)
      )
  
      reconstructed_predictions.append(final_preds)
  
      return reconstructed_predictions
  
imports: from inspect import signature
  from typing import Any, List, Callable, Tuple, Dict, cast, TypeVar, Optional
  import copy
  import warnings
  import torch
  from allennlp.common import Lazy, Registrable
  from allennlp.common.checks import ConfigurationError
  from allennlp.data import Vocabulary
  from allennlp.nn.util import min_value_of_dtype
label_desc: Use image node_id 2 for calling the BeamSearch obj's underlying member method code with example usage: obj._reconstruct_sequences(predictions, backpointers) and returns: reconstructed_predictions, reconstructed_predictions