code: def _discount_reward_py_1d(
      reward, sequence_length, discount=1.0, dtype=None
  ):
      if sequence_length is None:
          raise ValueError(
              "sequence_length must not be `None` for 1D reward."
          )
  
      reward = np.array(reward)
      sequence_length = np.array(sequence_length)
  
      batch_size = reward.shape[0]
      max_seq_length = np.max(sequence_length)
      dtype = dtype or reward.dtype
  
      if discount == 1.0:
          dmat = np.ones([batch_size, max_seq_length], dtype=dtype)
      else:
          steps = np.tile(np.arange(max_seq_length), [batch_size, 1])
          mask = np.asarray(
              steps < (sequence_length - 1)[:, None], dtype=dtype
          )
          # Make each row = [discount, ..., discount, 1, ..., 1]
          dmat = mask * discount + (1 - mask)
          dmat = np.cumprod(dmat[:, ::-1], axis=1)[:, ::-1]
  
      disc_reward = dmat * reward[:, None]
      disc_reward = mask_sequences(
          disc_reward, sequence_length, dtype=dtype
      )
      # mask = np.asarray(steps < sequence_length[:, None], dtype=dtype)
      # disc_reward = mask * disc_reward
  
      return disc_reward
  
imports: import numpy
  import tensorflow
  from texar.tf.utils.shapes import mask_sequences
label_desc: Use image node_id 2 for calling a global function with example usage: _discount_reward_py_1d(reward, sequence_length, discount, dtype) and returns: disc_reward