code: def main():
      """Entrypoint."""
      train_data = tx.data.PairedTextData(hparams=config_data.train)
      val_data = tx.data.PairedTextData(hparams=config_data.val)
      test_data = tx.data.PairedTextData(hparams=config_data.test)
      iterator = tx.data.FeedableDataIterator(
          {"train": train_data, "val": val_data, "test": test_data}
      )
  
      batch = iterator.get_next()
  
      outputs, sequence_length, infer_outputs = build_model(
          batch, train_data
      )
  
      agent = tx.agents.SeqPGAgent(
          samples=outputs.sample_id,
          logits=outputs.logits,
          sequence_length=sequence_length,
          hparams=config_model.agent,
      )
  
      def _train_and_eval(sess, agent):
          iterator.restart_dataset(sess, "train")
  
          best_val_bleu = -1.0
          step = 0
          while True:
              try:
                  # Samples
                  extra_fetches = {
                      "truth": batch["target_text_ids"],
                  }
                  feed_dict = {
                      iterator.handle: iterator.get_handle(
                          sess, "train"
                      )
                  }
                  fetches = agent.get_samples(
                      extra_fetches=extra_fetches, feed_dict=feed_dict
                  )
  
                  sample_text = tx.utils.map_ids_to_strs(
                      fetches["samples"],
                      train_data.target_vocab,
                      strip_eos=False,
                      join=False,
                  )
                  truth_text = tx.utils.map_ids_to_strs(
                      fetches["truth"],
                      train_data.target_vocab,
                      strip_eos=False,
                      join=False,
                  )
  
                  # Computes rewards
                  reward = []
                  for ref, hyp in zip(truth_text, sample_text):
                      r = tx.evals.sentence_bleu(
                          [ref], hyp, smooth=True
                      )
                      reward.append(r)
  
                  # Updates
                  loss = agent.observe(reward=reward)
  
                  # Displays & evaluates
                  step += 1
                  if step == 1 or step % config_data.display == 0:
                      print(
                          "step={}, loss={:.4f}, reward={:.4f}".format(
                              step, loss, np.mean(reward)
                          )
                      )
  
                  if step % config_data.display_eval == 0:
                      val_bleu = _eval_epoch(sess, "val")
                      best_val_bleu = max(best_val_bleu, val_bleu)
                      print(
                          "val step={}, BLEU={:.4f}; best-ever={:.4f}".format(
                              step, val_bleu, best_val_bleu
                          )
                      )
  
                      test_bleu = _eval_epoch(sess, "test")
                      print(
                          "test step={}, BLEU={:.4f}".format(
                              step, test_bleu
                          )
                      )
                      print("=" * 50)
  
              except tf.errors.OutOfRangeError:
                  break
  
      def _eval_epoch(sess, mode):
          """`mode` is one of {'val', 'test'}"""
          iterator.restart_dataset(sess, mode)
  
          refs, hypos = [], []
          while True:
              try:
                  fetches = [
                      batch["target_text"][:, 1:],
                      infer_outputs.predicted_ids[:, :, 0],
                  ]
                  feed_dict = {
                      tx.global_mode(): tf.estimator.ModeKeys.PREDICT,
                      iterator.handle: iterator.get_handle(sess, mode),
                  }
                  target_texts, output_ids = sess.run(
                      fetches, feed_dict=feed_dict
                  )
  
                  target_texts = tx.utils.strip_special_tokens(
                      target_texts
                  )
                  output_texts = tx.utils.map_ids_to_strs(
                      ids=output_ids, vocab=val_data.target_vocab
                  )
  
                  for hypo, ref in zip(output_texts, target_texts):
                      hypos.append(hypo)
                      refs.append([ref])
              except tf.errors.OutOfRangeError:
                  break
  
          return tx.evals.corpus_bleu_moses(
              list_of_references=refs, hypotheses=hypos
          )
  
      with tf.Session() as sess:
          sess.run(tf.global_variables_initializer())
          sess.run(tf.local_variables_initializer())
          sess.run(tf.tables_initializer())
  
          agent.sess = sess
  
          _train_and_eval(sess, agent)
  
imports: import importlib
  import numpy
  import tensorflow
  import texar.tf
label_desc: Use image node_id 2 for calling a global function with example usage: main() and returns: tx