class_docstr: This class implements a trainer that minimizes the expected value of a cost function over the
  space of some candidate sequences produced by a decoder. We generate the candidate sequences by
  performing beam search (which is one of the two popular ways of getting these sequences, the
  other one being sampling; see "Classical Structured Prediction Losses for Sequence to Sequence
  Learning" by Edunov et al., 2017 for more details).
  Note that we do not have a notion of targets here, so we're breaking the API of DecoderTrainer
  a bit.
  
  Parameters
  ----------
  beam_size : ``int``
  noramlize_by_length : ``bool``
      Should the log probabilities be normalized by length before renormalizing them? Edunov et
      al. do this in their work.
  max_decoding_steps : ``int``
      The maximum number of steps we should take during decoding.
  max_num_decoded_sequences : ``int``, optional (default=1)
      Maximum number of sorted decoded sequences to return. Defaults to 1.
  max_num_finished_states : ``int``, optional (default = None)
      Maximum number of finished states to keep after search. This is to finished states as
      ``beam_size`` is to unfinished ones. Costs are computed for only these number of states per
      instance. If not set, we will keep all the finished states.
class_docstr_tok: ["This","class","implements","a","trainer","that","minimizes","the","expected","value","of","a","cost","function","over","the","space","of","some","candidate","sequences","produced","by","a","decoder",".","We","generate","the","candidate","sequences","by","performing","beam","search","(","which","is","one","of","the","two","popular","ways","of","getting","these","sequences",",","the","other","one","being","sampling",";","see","``","Classical","Structured","Prediction","Losses","for","Sequence","to","Sequence","Learning","''","by","Edunov","et","al.",",","2017","for","more","details",")",".","Note","that","we","do","not","have","a","notion","of","targets","here",",","so","we","'re","breaking","the","API","of","DecoderTrainer","a","bit",".","Parameters","--","--","--","--","--","beam_size",":","``","int","``","noramlize_by_length",":","``","bool","``","Should","the","log","probabilities","be","normalized","by","length","before","renormalizing","them","?","Edunov","et","al",".","do","this","in","their","work",".","max_decoding_steps",":","``","int","``","The","maximum","number","of","steps","we","should","take","during","decoding",".","max_num_decoded_sequences",":","``","int","``",",","optional","(","default=1",")","Maximum","number","of","sorted","decoded","sequences","to","return",".","Defaults","to","1.","max_num_finished_states",":","``","int","``",",","optional","(","default","=","None",")","Maximum","number","of","finished","states","to","keep","after","search",".","This","is","to","finished","states","as","``","beam_size","``","is","to","unfinished","ones",".","Costs","are","computed","for","only","these","number","of","states","per","instance",".","If","not","set",",","we","will","keep","all","the","finished","states","."]
docstr: Returns the best action sequences for each item based on model scores. We return at most
  ``self._max_num_decoded_sequences`` number of sequences per instance.
docstr_tok: ["Returns","the","best","action","sequences","for","each","item","based","on","model","scores",".","We","return","at","most","``","self._max_num_decoded_sequences","``","number","of","sequences","per","instance","."]
code: def _get_best_action_sequences(self, finished_states):
      """
      Returns the best action sequences for each item based on model scores. We return at most
      ``self._max_num_decoded_sequences`` number of sequences per instance.
      """
      batch_action_histories = defaultdict(list)
      for state in finished_states:
          for batch_index, action_history in izip(
              state.batch_indices, state.action_history
          ):
              batch_action_histories[batch_index].append(action_history)
  
      batch_scores = self._get_model_scores_by_batch(finished_states)
      best_action_sequences = {}
      for batch_index, scores in list(batch_scores.items()):
          _, sorted_indices = torch.cat(
              [score.view(-1) for score in scores]
          ).sort(-1, descending=True)
          cpu_indices = [
              int(index)
              for index in sorted_indices.detach().cpu().numpy()
          ]
          best_action_indices = cpu_indices[
              : self._max_num_decoded_sequences
          ]
          instance_best_sequences = [
              batch_action_histories[batch_index][i]
              for i in best_action_indices
          ]
          best_action_sequences[batch_index] = instance_best_sequences
      return best_action_sequences
  
file_path: magnitude/pymagnitude/third_party/allennlp/nn/decoding/decoder_trainers/expected_risk_minimization.py
imports: from __future__ import division
  from __future__ import absolute_import
  from collections import defaultdict
  import torch
  from allennlp.nn.decoding.decoder_step import DecoderStep
  from allennlp.nn.decoding.decoder_state import DecoderState
  from allennlp.nn.decoding.decoder_trainers.decoder_trainer import DecoderTrainer
  from allennlp.nn import util
label_desc: Use image node_id 7 for calling the ExpectedRiskMinimization obj's underlying member method code with example usage: obj._get_best_action_sequences(finished_states) and returns: best_action_sequences