def tf_discounted_cumulative_reward(self, terminal, reward, discount=None, final_reward=<NUM_LIT:0.0>, horizon=<NUM_LIT:0>):
<EOL>if discount is None:<EOL><INDENT>discount = self.discount<EOL><DEDENT>def cumulate(cumulative, reward_terminal_horizon_subtract):<EOL><INDENT>rew, is_terminal, is_over_horizon, sub = reward_terminal_horizon_subtract<EOL>return tf.where(<EOL>condition=is_terminal,<EOL>x=rew,<EOL>y=tf.where(<EOL>condition=is_over_horizon,<EOL>x=(rew + cumulative * discount - sub),<EOL>y=(rew + cumulative * discount)<EOL>)<EOL>)<EOL><DEDENT>def len_(cumulative, term):<EOL><INDENT>return tf.where(<EOL>condition=term,<EOL>x=tf.ones(shape=(), dtype=tf.int32),<EOL>y=cumulative + <NUM_LIT:1><EOL>)<EOL><DEDENT>reward = tf.reverse(tensor=reward, axis=(<NUM_LIT:0>,))<EOL>terminal = tf.reverse(tensor=terminal, axis=(<NUM_LIT:0>,))<EOL>lengths = tf.scan(fn=len_, elems=terminal, initializer=<NUM_LIT:0>)<EOL>off_horizon = tf.greater(lengths, tf.fill(dims=tf.shape(lengths), value=horizon))<EOL>if horizon > <NUM_LIT:0>:<EOL><INDENT>horizon_subtractions = tf.map_fn(lambda x: (discount ** horizon) * x, reward, dtype=tf.float32)<EOL>horizon_subtractions = tf.concat([np.zeros(shape=(horizon,)), horizon_subtractions], axis=<NUM_LIT:0>)<EOL>horizon_subtractions = tf.slice(horizon_subtractions, begin=(<NUM_LIT:0>,), size=tf.shape(reward))<EOL><DEDENT>else:<EOL><INDENT>horizon_subtractions = tf.zeros(shape=tf.shape(reward))<EOL><DEDENT>reward = tf.scan(<EOL>fn=cumulate,<EOL>elems=(reward, terminal, off_horizon, horizon_subtractions),<EOL>initializer=final_reward if horizon != <NUM_LIT:1> else <NUM_LIT:0.0><EOL>)<EOL>return tf.reverse(tensor=reward, axis=(<NUM_LIT:0>,))<EOL>
Creates and returns the TensorFlow operations for calculating the sequence of discounted cumulative rewards
for a given sequence of single rewards.

Example:
single rewards = 2.0 1.0 0.0 0.5 1.0 -1.0
terminal = False, False, False, False True False
gamma = 0.95
final_reward = 100.0 (only matters for last episode (r=-1.0) as this episode has no terminal signal)
horizon=3
output = 2.95 1.45 1.38 1.45 1.0 94.0

Args:
    terminal: Tensor (bool) holding the is-terminal sequence. This sequence may contain more than one
        True value. If its very last element is False (not terminating), the given `final_reward` value
        is assumed to follow the last value in the single rewards sequence (see below).
    reward: Tensor (float) holding the sequence of single rewards. If the last element of `terminal` is False,
        an assumed last reward of the value of `final_reward` will be used.
    discount (float): The discount factor (gamma). By default, take the Model's discount factor.
    final_reward (float): Reward value to use if last episode in sequence does not terminate (terminal sequence
        ends with False). This value will be ignored if horizon == 1 or discount == 0.0.
    horizon (int): The length of the horizon (e.g. for n-step cumulative rewards in continuous tasks
        without terminal signals). Use 0 (default) for an infinite horizon. Note that horizon=1 leads to the
        exact same results as a discount factor of 0.0.

Returns:
    Discounted cumulative reward tensor with the same shape as `reward`.