class_docstr: Tests
  :func:`texar.tf.modules.decoders.beam_search_decode.beam_search_decode`.
class_docstr_tok: ["Tests",":","func",":","`","texar.tf.modules.decoders.beam_search_decode.beam_search_decode","`","."]
code: def _test_beam_search(
      self,
      decoder,
      initial_state=None,
      tiled_initial_state=None,
      tf_initial_state=None,
      beam_width_1=1,
      initiated=False,
  ):
      # Compare with tf built-in BeamSearchDecoder
      outputs, final_state, _ = beam_search_decode(
          decoder_or_cell=decoder,
          embedding=self._embedding,
          start_tokens=[1] * self._batch_size,
          end_token=2,
          beam_width=beam_width_1,
          max_decoding_length=20,
      )
  
      self.assertIsInstance(
          outputs, tf.contrib.seq2seq.FinalBeamSearchDecoderOutput
      )
      self.assertIsInstance(
          final_state, tf.contrib.seq2seq.BeamSearchDecoderState
      )
  
      num_trainable_variables = len(tf.trainable_variables())
      _ = decoder(
          decoding_strategy="infer_greedy",
          embedding=self._embedding,
          start_tokens=[1] * self._batch_size,
          end_token=2,
          max_decoding_length=20,
      )
      self.assertEqual(
          num_trainable_variables, len(tf.trainable_variables())
      )
  
      if tf_initial_state is None:
          tf_initial_state = decoder.cell.zero_state(
              self._batch_size * beam_width_1, tf.float32
          )
      beam_decoder = BeamSearchDecoder(
          cell=decoder.cell,
          embedding=self._embedding,
          start_tokens=[1] * self._batch_size,
          end_token=2,
          initial_state=tf_initial_state,
          beam_width=beam_width_1,
          output_layer=decoder.output_layer,
      )
  
      outputs_1, final_state_1, _ = dynamic_decode(
          decoder=beam_decoder, maximum_iterations=20
      )
  
      # Tests time major
      outputs_2, _, _ = beam_search_decode(
          decoder_or_cell=decoder,
          embedding=self._embedding,
          start_tokens=[1] * self._batch_size,
          end_token=2,
          beam_width=self._beam_width,
          initial_state=initial_state,
          tiled_initial_state=tiled_initial_state,
          max_decoding_length=21,
      )
      outputs_3, _, _ = beam_search_decode(
          decoder_or_cell=decoder,
          embedding=self._embedding,
          start_tokens=[1] * self._batch_size,
          end_token=2,
          beam_width=self._beam_width,
          initial_state=initial_state,
          tiled_initial_state=tiled_initial_state,
          max_decoding_length=21,
          output_time_major=True,
      )
  
      with self.test_session() as sess:
          if not initiated:
              sess.run(tf.global_variables_initializer())
  
          outputs_, final_state_, outputs_1_, final_state_1_ = sess.run(
              [outputs, final_state, outputs_1, final_state_1],
              feed_dict={
                  context.global_mode(): tf.estimator.ModeKeys.PREDICT
              },
          )
  
          np.testing.assert_array_equal(
              outputs_.predicted_ids, outputs_1_.predicted_ids
          )
          np.testing.assert_array_equal(
              outputs_.beam_search_decoder_output.scores,
              outputs_1_.beam_search_decoder_output.scores,
          )
          np.testing.assert_array_equal(
              outputs_.beam_search_decoder_output.predicted_ids,
              outputs_1_.beam_search_decoder_output.predicted_ids,
          )
          np.testing.assert_array_equal(
              outputs_.beam_search_decoder_output.parent_ids,
              outputs_1_.beam_search_decoder_output.parent_ids,
          )
          np.testing.assert_array_equal(
              final_state_.log_probs, final_state_1_.log_probs
          )
          np.testing.assert_array_equal(
              final_state_.lengths, final_state_1_.lengths
          )
  
          outputs_2_, outputs_3_ = sess.run(
              [outputs_2, outputs_3],
              feed_dict={
                  context.global_mode(): tf.estimator.ModeKeys.PREDICT
              },
          )
          self.assertEqual(
              outputs_2_.predicted_ids.shape,
              tuple([self._batch_size, 21, 11]),
          )
          self.assertEqual(
              outputs_3_.predicted_ids.shape,
              tuple([21, self._batch_size, 11]),
          )
  
file_path: texar/tests/modules/decoders/beam_search_decode_test.py
imports: import numpy
  import tensorflow
  from tensorflow.contrib.seq2seq import dynamic_decode
  from tensorflow.contrib.seq2seq import BeamSearchDecoder, tile_batch
  import texar.tf
  from texar.tf.modules.decoders.beam_search_decode import beam_search_decode
  from texar.tf import context
label_desc: Use image node_id 2 for calling the BeamSearchDecodeTest obj's underlying member method code with example usage: obj._test_beam_search(decoder, initial_state, tiled_initial_state, tf_initial_state, beam_width_1, initiated) without return types