original_docstring: Use beam search to generate the captions for a batch of images.
docstring: Use beam search to generate the captions for a batch of images.
docstring_tokens: Use beam search to generate the captions for a batch of images .
code: def beam_search(self, sess, images,vocabulary):
          """Use beam search to generate the captions for a batch of images."""
          # Feed in the images to get the contexts and the initial LSTM states
  
          # import pdb
          # pdb.set_trace()
  
          contexts, initial_memory, initial_output = sess.run(
              [self.model.conv_feats, self.model.initial_memory, self.model.initial_output],
              feed_dict = {self.model.images: images})
          partial_caption_data = []
          complete_caption_data = []
          for k in range(self.batch_size):
              initial_alpha_k = [np.zeros([self.model.num_ctx],dtype=np.float32)]
              initial_beam = CaptionData(sentence = [],
                                         memory = initial_memory[k],
                                         output = initial_output[k],
                                         alpha = initial_alpha_k,
                                         score = 1.0)
              partial_caption_data.append(TopN(self.beam_size))
              partial_caption_data[-1].push(initial_beam)
              complete_caption_data.append(TopN(self.beam_size))
  
          # Run beam search
          for idx in range(self.max_caption_length):
              partial_caption_data_lists = []
              for k in range(self.batch_size):
                  data = partial_caption_data[k].extract()
                  partial_caption_data_lists.append(data)
                  partial_caption_data[k].reset()
  
              num_steps = 1 if idx == 0 else self.beam_size
              for b in range(num_steps):
                  if idx == 0:
                      last_word = np.zeros((self.batch_size), np.int32)
                  else:
                      last_word = np.array([pcl[b].sentence[-1]
                                          for pcl in partial_caption_data_lists],
                                          np.int32)
  
                  last_memory = np.array([pcl[b].memory
                                          for pcl in partial_caption_data_lists],
                                          np.float32)
                  last_output = np.array([pcl[b].output
                                          for pcl in partial_caption_data_lists],
                                          np.float32)
  
                  memory, output, scores, alpha = sess.run(
                      [self.model.memory, self.model.output, self.model.probs, self.model.alpha],
                      feed_dict = {self.model.contexts: contexts,
                                   self.model.last_word: last_word,
                                   self.model.last_memory: last_memory,
                                   self.model.last_output: last_output})
                  ''' 
                  # For Debug
                  aaaa = sess.run(
                      fetches='attend/Softmax:0',
                      feed_dict= {'Placeholder_1:0': contexts,
                                  'Placeholder_3:0': last_output}
                      )
  
                  haha = 100
                  '''
  
                  # Find the beam_size most probable next words
                  for k in range(self.batch_size):
                      caption_data = partial_caption_data_lists[k][b]
                      words_and_scores = list(enumerate(scores[k]))
                      words_and_scores.sort(key=lambda x: -x[1])
                      words_and_scores = words_and_scores[0:self.beam_size+1]
  
                      # Append each of these words to the current partial caption
                      for w, s in words_and_scores:
                          sentence = caption_data.sentence + [w]
                          score = caption_data.score * s
                          alpha_sent = caption_data.alpha +[alpha[k]]
                          beam = CaptionData(sentence,
                                             memory[k],
                                             output[k],
                                             alpha_sent,
                                             score)
                          if vocabulary.words[w] == '.':
                              complete_caption_data[k].push(beam)
                          else:
                              partial_caption_data[k].push(beam)
  
          results = []
          for k in range(self.batch_size):
              if complete_caption_data[k].size() == 0:
                  complete_caption_data[k] = partial_caption_data[k]
              results.append(complete_caption_data[k].extract(sort=True))
  
          return results
code_tokens: def beam_search ( self , sess , images , vocabulary ) : contexts , initial_memory , initial_output = sess . run ( [ self . model . conv_feats , self . model . initial_memory , self . model . initial_output ] , feed_dict = { self . model . images : images } ) partial_caption_data = [ ] complete_caption_data = [ ] for k in range ( self . batch_size ) : initial_alpha_k = [ np . zeros ( [ self . model . num_ctx ] , dtype = np . float32 ) ] initial_beam = CaptionData ( sentence = [ ] , memory = initial_memory [ k ] , output = initial_output [ k ] , alpha = initial_alpha_k , score = 1.0 ) partial_caption_data . append ( TopN ( self . beam_size ) ) partial_caption_data [ - 1 ] . push ( initial_beam ) complete_caption_data . append ( TopN ( self . beam_size ) ) for idx in range ( self . max_caption_length ) : partial_caption_data_lists = [ ] for k in range ( self . batch_size ) : data = partial_caption_data [ k ] . extract ( ) partial_caption_data_lists . append ( data ) partial_caption_data [ k ] . reset ( ) num_steps = 1 if idx == 0 else self . beam_size for b in range ( num_steps ) : if idx == 0 : last_word = np . zeros ( ( self . batch_size ) , np . int32 ) else : last_word = np . array ( [ pcl [ b ] . sentence [ - 1 ] for pcl in partial_caption_data_lists ] , np . int32 ) last_memory = np . array ( [ pcl [ b ] . memory for pcl in partial_caption_data_lists ] , np . float32 ) last_output = np . array ( [ pcl [ b ] . output for pcl in partial_caption_data_lists ] , np . float32 ) memory , output , scores , alpha = sess . run ( [ self . model . memory , self . model . output , self . model . probs , self . model . alpha ] , feed_dict = { self . model . contexts : contexts , self . model . last_word : last_word , self . model . last_memory : last_memory , self . model . last_output : last_output } ) ''' 
                  # For Debug
                  aaaa = sess.run(
                      fetches='attend/Softmax:0',
                      feed_dict= {'Placeholder_1:0': contexts,
                                  'Placeholder_3:0': last_output}
                      )
  
                  haha = 100
                  ''' for k in range ( self . batch_size ) : caption_data = partial_caption_data_lists [ k ] [ b ] words_and_scores = list ( enumerate ( scores [ k ] ) ) words_and_scores . sort ( key = lambda x : - x [ 1 ] ) words_and_scores = words_and_scores [ 0 : self . beam_size + 1 ] for w , s in words_and_scores : sentence = caption_data . sentence + [ w ] score = caption_data . score * s alpha_sent = caption_data . alpha + [ alpha [ k ] ] beam = CaptionData ( sentence , memory [ k ] , output [ k ] , alpha_sent , score ) if vocabulary . words [ w ] == '.' : complete_caption_data [ k ] . push ( beam ) else : partial_caption_data [ k ] . push ( beam ) results = [ ] for k in range ( self . batch_size ) : if complete_caption_data [ k ] . size ( ) == 0 : complete_caption_data [ k ] = partial_caption_data [ k ] results . append ( complete_caption_data [ k ] . extract ( sort = True ) ) return results
short_docstring: Use beam search to generate the captions for a batch of images.
short_docstring_tokens: Use beam search to generate the captions for a batch of images .
comment: """Use beam search to generate the captions for a batch of images."""$SEP$# Feed in the images to get the contexts and the initial LSTM states$SEP$# import pdb$SEP$# pdb.set_trace()$SEP$# Run beam search$SEP$''' 
                  # For Debug
                  aaaa = sess.run(
                      fetches='attend/Softmax:0',
                      feed_dict= {'Placeholder_1:0': contexts,
                                  'Placeholder_3:0': last_output}
                      )
  
                  haha = 100
                  '''$SEP$# Find the beam_size most probable next words$SEP$# Append each of these words to the current partial caption