code: def main():
      """Entrypoint."""
      config_data.train["batch_size"] *= FLAGS.n_samples
      config_data.val["batch_size"] *= FLAGS.n_samples
      config_data.test["batch_size"] *= FLAGS.n_samples
  
      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)
      data_iterator = tx.data.TrainTestDataIterator(
          train=train_data, val=val_data, test=test_data
      )
  
      batch = data_iterator.get_next()
      rewards_ts = tf.placeholder(
          dtype=tf.float32,
          shape=[
              None,
          ],
          name="training_rewards",
      )
  
      train_op, infer_outputs = build_model(
          batch, train_data, rewards_ts
      )
  
      raml_train_data = read_raml_sample_file()
  
      def _train_epoch(sess, epoch_no):
          data_iterator.switch_to_train_data(sess)
          training_log_file = open(
              log_dir + "training_log" + str(epoch_no) + ".txt",
              "w",
              encoding="utf-8",
          )
  
          step = 0
          source_buffer, target_buffer = [], []
          random.shuffle(raml_train_data)
          for training_pair in raml_train_data:
              for target in training_pair["targets"]:
                  source_buffer.append(training_pair["source"])
                  target_buffer.append(target)
  
              if len(target_buffer) != train_data.batch_size:
                  continue
  
              source_ids = []
              source_length = []
              target_ids = []
              target_length = []
              scores = []
  
              trunc_len_src = (
                  train_data.hparams.source_dataset.max_seq_length
              )
              trunc_len_tgt = (
                  train_data.hparams.target_dataset.max_seq_length
              )
  
              for sentence in source_buffer:
                  ids = [
                      train_data.source_vocab.token_to_id_map_py[token]
                      for token in sentence.split()
                  ][:trunc_len_src]
                  ids = ids + [train_data.source_vocab.eos_token_id]
  
                  source_ids.append(ids)
                  source_length.append(len(ids))
  
              for sentence, score_str in target_buffer:
                  ids = [train_data.target_vocab.bos_token_id]
                  ids = (
                      ids
                      + [
                          train_data.target_vocab.token_to_id_map_py[
                              token
                          ]
                          for token in sentence.split()
                      ][:trunc_len_tgt]
                  )
                  ids = ids + [train_data.target_vocab.eos_token_id]
  
                  target_ids.append(ids)
                  scores.append(eval(score_str))
                  target_length.append(len(ids))
  
              rewards = []
              for i in range(0, train_data.batch_size, FLAGS.n_samples):
                  tmp = np.array(scores[i : i + FLAGS.n_samples])
                  tmp = np.exp(tmp / FLAGS.tau) / np.sum(
                      np.exp(tmp / FLAGS.tau)
                  )
                  for j in range(0, FLAGS.n_samples):
                      rewards.append(tmp[j])
  
              for value in source_ids:
                  while len(value) < max(source_length):
                      value.append(0)
              for value in target_ids:
                  while len(value) < max(target_length):
                      value.append(0)
  
              feed_dict = {
                  batch["source_text_ids"]: np.array(source_ids),
                  batch["target_text_ids"]: np.array(target_ids),
                  batch["source_length"]: np.array(source_length),
                  batch["target_length"]: np.array(target_length),
                  rewards_ts: np.array(rewards),
              }
              source_buffer = []
              target_buffer = []
  
              loss = sess.run(train_op, feed_dict=feed_dict)
              print(
                  "step={}, loss={:.4f}".format(step, loss),
                  file=training_log_file,
              )
              if step % config_data.observe_steps == 0:
                  print("step={}, loss={:.4f}".format(step, loss))
              training_log_file.flush()
              step += 1
  
      # code below this line is exactly the same as baseline_seq2seq_attn_main.py
  
      def _eval_epoch(sess, mode, epoch_no):
          if mode == "val":
              data_iterator.switch_to_val_data(sess)
          else:
              data_iterator.switch_to_test_data(sess)
  
          refs, hypos = [], []
          while True:
              try:
                  fetches = [
                      batch["target_text"][:, 1:],
                      infer_outputs.predicted_ids[:, :, 0],
                  ]
                  feed_dict = {
                      tx.global_mode(): tf.estimator.ModeKeys.EVAL
                  }
                  target_texts_ori, output_ids = sess.run(
                      fetches, feed_dict=feed_dict
                  )
  
                  target_texts = tx.utils.strip_special_tokens(
                      target_texts_ori.tolist(), is_token_list=True
                  )
                  target_texts = tx.utils.str_join(target_texts)
                  output_texts = tx.utils.map_ids_to_strs(
                      ids=output_ids, vocab=val_data.target_vocab
                  )
  
                  tx.utils.write_paired_text(
                      target_texts,
                      output_texts,
                      log_dir
                      + mode
                      + "_results"
                      + str(epoch_no)
                      + ".txt",
                      append=True,
                      mode="h",
                      sep=" ||| ",
                  )
  
                  for hypo, ref in zip(output_texts, target_texts):
                      if config_data.eval_metric == "bleu":
                          hypos.append(hypo)
                          refs.append([ref])
                      elif config_data.eval_metric == "rouge":
                          hypos.append(tx.utils.compat_as_text(hypo))
                          refs.append(tx.utils.compat_as_text(ref))
              except tf.errors.OutOfRangeError:
                  break
  
          if config_data.eval_metric == "bleu":
              return tx.evals.corpus_bleu_moses(
                  list_of_references=refs, hypotheses=hypos
              )
          elif config_data.eval_metric == "rouge":
              rouge = Rouge()
              return rouge.get_scores(hyps=hypos, refs=refs, avg=True)
  
      def _calc_reward(score):
          """
          Return the bleu score or the sum of (Rouge-1, Rouge-2, Rouge-L).
          """
          if config_data.eval_metric == "bleu":
              return score
          elif config_data.eval_metric == "rouge":
              return sum([value["f"] for key, value in score.items()])
  
      with tf.Session() as sess:
          sess.run(tf.global_variables_initializer())
          sess.run(tf.local_variables_initializer())
          sess.run(tf.tables_initializer())
  
          best_val_score = -1.0
          scores_file = open(
              log_dir + "scores.txt", "w", encoding="utf-8"
          )
          for i in range(config_data.num_epochs):
              _train_epoch(sess, i)
  
              val_score = _eval_epoch(sess, "val", i)
              test_score = _eval_epoch(sess, "test", i)
  
              best_val_score = max(
                  best_val_score, _calc_reward(val_score)
              )
  
              if config_data.eval_metric == "bleu":
                  print_stdout_and_file(
                      "val epoch={}, BLEU={:.4f}; best-ever={:.4f}".format(
                          i, val_score, best_val_score
                      ),
                      file=scores_file,
                  )
  
                  print_stdout_and_file(
                      "test epoch={}, BLEU={:.4f}".format(
                          i, test_score
                      ),
                      file=scores_file,
                  )
                  print_stdout_and_file("=" * 50, file=scores_file)
  
              elif config_data.eval_metric == "rouge":
                  print_stdout_and_file(
                      "valid epoch {}:".format(i), file=scores_file
                  )
                  for key, value in val_score.items():
                      print_stdout_and_file(
                          "{}: {}".format(key, value), file=scores_file
                      )
                  print_stdout_and_file(
                      "fsum: {}; best_val_fsum: {}".format(
                          _calc_reward(val_score), best_val_score
                      ),
                      file=scores_file,
                  )
  
                  print_stdout_and_file(
                      "test epoch {}:".format(i), file=scores_file
                  )
                  for key, value in test_score.items():
                      print_stdout_and_file(
                          "{}: {}".format(key, value), file=scores_file
                      )
                  print_stdout_and_file("=" * 110, file=scores_file)
  
              scores_file.flush()
  
file_path: texar/examples/seq2seq_exposure_bias/raml_main.py
imports: from io import open
  import importlib
  import tensorflow
  import texar.tf
  import numpy
  import random
  from rouge import Rouge
label_desc: Use image node_id 5 for calling a global function with example usage: main() and returns: tx, score, rouge, sum