code: def testNotGreedyBeamTwoWithStopEarly(self):
      batch_size = 1
      beam_size = 2
      vocab_size = 3
      decode_length = 3
  
      initial_ids = tf.constant([0] * batch_size)  # GO
      probabilities = tf.constant(
          [
              [[0.1, 0.1, 0.8], [0.1, 0.1, 0.8]],
              [[0.4, 0.5, 0.1], [0.2, 0.4, 0.4]],
              [[0.05, 0.9, 0.05], [0.4, 0.4, 0.2]],
          ]
      )
  
      def symbols_to_logits(ids):
          pos = tf.shape(ids)[1]
          logits = tf.to_float(tf.log(probabilities[pos - 1, :]))
          return logits
  
      final_ids, final_probs, _ = beam_search.beam_search(
          symbols_to_logits,
          initial_ids,
          beam_size,
          decode_length,
          vocab_size,
          0.0,
          eos_id=1,
          stop_early=True,
      )  # default value, but just to make this explicit
  
      with self.test_session():
          ids = final_ids.eval()
          probs = final_probs.eval()
      # given stop_early = True, the only 'assurance' is w.r.t. the first beam
      # (i.e., other beams may not even be completed)
      # so, we check only the first beam
      first_beam = ids[:, 0]
      first_probs = probs[:, 0]
      self.assertAllEqual([[0, 2, 1]], first_beam)
      self.assertAllClose([0.8 * 0.5], np.exp(first_probs))
  
file_path: tensor2tensor/tensor2tensor/utils/beam_search_test.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import numpy
  from tensor2tensor.utils import beam_search
  import tensorflow.compat.v1
label_desc: Use image node_id 4 for calling the BeamSearchTest obj's underlying member method code with example usage: obj.testNotGreedyBeamTwoWithStopEarly() and returns: logits