class_docstr: Helper for interpolation algirithm.
  New token is sample from model, ground_truth or reward according to lambdas
  
  Args:
      embedding: A callable that takes a vector tensor of `ids` (argmax ids),
          or the `params` argument for `embedding_lookup`. The returned tensor
          will be passed to the decoder input.
      start_tokens: `int32` vector shaped `[batch_size]`, the start tokens.
      end_token: `int32` scalar, the token that marks end of decoding.
      vocab: texar.Vocab, the vocabularies of training set
      reward_metric: 'bleu' or 'rouge', the metric of reward
      ground_truth: the ground truth in training set
      ground_truth_length: the length of ground truth sentences
      lambdas: 'float32' vector shapes [3], according to which
          decide the way of generate the next token in training
class_docstr_tok: ["Helper","for","interpolation","algirithm",".","New","token","is","sample","from","model",",","ground_truth","or","reward","according","to","lambdas","Args",":","embedding",":","A","callable","that","takes","a","vector","tensor","of","`","ids","`","(","argmax","ids",")",",","or","the","`","params","`","argument","for","`","embedding_lookup","`",".","The","returned","tensor","will","be","passed","to","the","decoder","input",".","start_tokens",":","`","int32","`","vector","shaped","`","[","batch_size","]","`",",","the","start","tokens",".","end_token",":","`","int32","`","scalar",",","the","token","that","marks","end","of","decoding",".","vocab",":","texar.Vocab",",","the","vocabularies","of","training","set","reward_metric",":","'bleu","'","or","'rouge","'",",","the","metric","of","reward","ground_truth",":","the","ground","truth","in","training","set","ground_truth_length",":","the","length","of","ground","truth","sentences","lambdas",":","'float32","'","vector","shapes","[","3","]",",","according","to","which","decide","the","way","of","generate","the","next","token","in","training"]
code: def _sample_by_reward(self, time, state):
      def _get_rewards(
          time, prefix_ids, target_ids, ground_truth_length
      ):
          batch_size = np.shape(target_ids)[0]
          words_in_target = [
              np.unique(target_ids[i]) for i in range(batch_size)
          ]
          unk_id = self._vocab.unk_token_id
          eos_id = self._vocab.eos_token_id
  
          # before append
          baseline_scores = []
          baseline_ids = prefix_ids[:, :time]
          for i in range(batch_size):
              ref = target_ids[i].tolist()
              if self._vocab.eos_token_id in ref:
                  ref = ref[: ref.index(self._vocab.eos_token_id)]
  
              hypo = baseline_ids[i].tolist()
              if self._vocab.eos_token_id in hypo:
                  hypo = hypo[: hypo.index(self._vocab.eos_token_id)]
  
              baseline_scores.append(
                  calc_reward(
                      refs=[ref],
                      hypo=hypo,
                      unk_id=unk_id,
                      metric=self._metric,
                  )
              )
  
          # append UNK
          syn_ids = np.concatenate(
              [
                  prefix_ids[:, :time],
                  np.ones((batch_size, 1), dtype=np.int32) * unk_id,
              ],
              axis=1,
          )
  
          reward_unk = []
          for i in range(batch_size):
              ref = target_ids[i].tolist()
              if self._vocab.eos_token_id in ref:
                  ref = ref[: ref.index(self._vocab.eos_token_id)]
  
              hypo = syn_ids[i].tolist()
              if self._vocab.eos_token_id in hypo:
                  hypo = hypo[: hypo.index(self._vocab.eos_token_id)]
  
              reward = calc_reward(
                  refs=[ref],
                  hypo=hypo,
                  unk_id=unk_id,
                  metric=self._metric,
              )
              reward_unk.append(
                  np.ones((1, self._vocab.size), dtype=np.float32)
                  * reward
                  - baseline_scores[i]
              )
          result = np.concatenate(reward_unk, axis=0)
  
          # append tokens
          for i in range(batch_size):
              for id in words_in_target[i]:
                  if id == unk_id:
                      continue
  
                  syn_id = np.concatenate(
                      [
                          prefix_ids[i : i + 1, :time],
                          np.array(
                              [
                                  [
                                      id,
                                  ]
                              ]
                          ),
                      ],
                      axis=1,
                  )
                  hypo = syn_id[0].tolist()
                  if self._vocab.eos_token_id in hypo:
                      hypo = hypo[
                          : hypo.index(self._vocab.eos_token_id)
                      ]
  
                  ref = target_ids[i].tolist()
                  if self._vocab.eos_token_id in ref:
                      ref = ref[: ref.index(self._vocab.eos_token_id)]
  
                  dup = (
                      1.0
                      if prefix_ids[i][time] == id and id != unk_id
                      else 0.0
                  )
                  eos = (
                      1.0
                      if time < ground_truth_length[i] - 1
                      and id == eos_id
                      else 0.0
                  )
  
                  reward = calc_reward(
                      refs=[ref],
                      hypo=hypo,
                      unk_id=unk_id,
                      metric=self._metric,
                  )
                  result[i][id] = (
                      reward - baseline_scores[i] - dup - eos
                  )
  
          return result
  
      sampler = tfpd.Categorical(
          logits=tf.py_func(
              _get_rewards,
              [
                  time,
                  state[0],
                  self._ground_truth,
                  self._ground_truth_length,
              ],
              tf.float32,
          )
      )
      return tf.reshape(
          sampler.sample(), (tf.shape(self._ground_truth)[0],)
      )
  
file_path: texar/examples/seq2seq_exposure_bias/interpolation_helper.py
imports: import tensorflow
  import numpy
  from tensorflow_probability import distributions
  from tensorflow.contrib.seq2seq import SampleEmbeddingHelper
  from texar.tf.evals.bleu import sentence_bleu
  from rouge import Rouge
label_desc: Use image node_id 4 for calling the InterpolationHelper obj's underlying member method code with example usage: obj._sample_by_reward(time, state) and returns: tf, result