code: def _terminal_experience_iterative(
      self,
      *,
      episode_length,
      reward_discount,
      states,
      internals,
      auxiliaries,
      actions,
      reward,
      terminal,
  ):
      zero = tf_util.constant(value=0, dtype="int")
      one = tf_util.constant(value=1, dtype="int")
      zero_float = tf_util.constant(value=0.0, dtype="float")
      internals = (
          internals["baseline"]
          if self.separate_baseline
          else internals["policy"]
      )
  
      if self.trace_decay.is_constant(value=1.0):
          # Whether to predict horizon/terminal values now
          if self.predict_horizon_values != "early":
              # Whether to predict all or only abort-terminals
              # (-reward[-1] since terminal state value will be predicted)
              terminal_value = -reward[-1] / reward_discount
              if not self.predict_terminal_values:
                  is_terminal = tf.math.equal(x=terminal[-1], y=one)
                  terminal_value = tf.where(
                      condition=is_terminal,
                      x=zero_float,
                      y=terminal_value,
                  )
  
          else:
  
              def predict_terminal_value():
                  # Baseline horizon
                  baseline_horizon = self.baseline.past_horizon(
                      on_policy=True
                  )
                  baseline_horizon = tf.math.minimum(
                      x=baseline_horizon, y=episode_length
                  )
  
                  # Single-step horizon
                  horizon_start = (
                      episode_length - one - baseline_horizon
                  )
                  horizons = tf.expand_dims(
                      input=tf.stack(
                          values=(zero, baseline_horizon + one)
                      ),
                      axis=0,
                  )
  
                  # Predict values
                  if self.predict_action_values:
                      # TODO: option to re-sample action deterministically?
                      # Use given actions since early estimate
                      # if self.separate_baseline:
                      #     policy_horizon = self.policy.past_horizon(on_policy=True)
                      #     policy_horizon = tf.math.minimum(x=policy_horizon, y=episode_length)
                      #     policy_horizon_start = terminal_index - policy_horizon
                      # else:
                      #     policy_horizon_start = past_horizon_start
                      # deterministic = tf_util.constant(value=True, dtype='bool')
                      # _actions, _ = self.policy.act(
                      #     states=states[policy_horizon_start:], horizons=horizons[:maybe_one],
                      #     internals=internals['policy'][policy_horizon_start: policy_horizon_start + maybe_one],
                      #     auxiliaries=auxiliaries[terminal_index:], deterministic=deterministic,
                      #     independent=True
                      # )
                      terminal_value = self.baseline.action_value(
                          states=states[horizon_start:],
                          horizons=horizons,
                          internals=internals[
                              horizon_start : horizon_start + one
                          ],
                          auxiliaries=auxiliaries[-1:],
                          actions=actions[-1:],
                      )
                  else:
                      terminal_value = self.baseline.state_value(
                          states=states[horizon_start:],
                          horizons=horizons,
                          internals=internals[
                              horizon_start : horizon_start + one
                          ],
                          auxiliaries=auxiliaries[-1:],
                      )
  
                  # Modification to correct for use as initializer in tf.scan
                  # (-reward[-1] since terminal state value will be predicted)
                  return (
                      terminal_value[0] - reward[-1]
                  ) / reward_discount
  
              # Whether to predict all or only abort-terminals
              if self.predict_terminal_values:
                  terminal_value = predict_terminal_value()
              else:
                  is_terminal = tf.math.equal(x=terminal[-1], y=one)
                  terminal_value = tf.cond(
                      pred=is_terminal,
                      true_fn=(lambda: zero_float),
                      false_fn=predict_terminal_value,
                  )
  
          # Discounted cumulative sum return
          def recursive_return(next_return, current_reward):
              return current_reward + reward_discount * next_return
  
          return tf.scan(
              fn=recursive_return,
              elems=reward,
              initializer=terminal_value,
              reverse=True,
          )
  
      else:
          # Baseline horizon
          baseline_horizon = self.baseline.past_horizon(on_policy=True)
          assertion = tf.debugging.assert_equal(
              x=baseline_horizon,
              y=zero,
              message="Baseline cannot have on-policy horizon if trace_decay != 1.0.",
          )
  
          with tf.control_dependencies(control_inputs=(assertion,)):
              # Baseline-horizon-sequence per timestep, as horizons indexing tensor
              horizons_start = tf.range(episode_length - one)
              horizons_length = tf.fill(
                  dims=(episode_length - one,), value=one
              )
              horizons = tf.stack(
                  values=(horizons_start, horizons_length), axis=1
              )
  
              if self.predict_action_values:
                  # TODO: option to re-sample action deterministically?
                  values = self.baseline.action_value(
                      states=states[1:],
                      horizons=horizons,
                      internals=internals[1:],
                      auxiliaries=auxiliaries[1:],
                      actions=actions[1:],
                  )
              else:
                  values = self.baseline.state_value(
                      states=states[1:],
                      horizons=horizons,
                      internals=internals[1:],
                      auxiliaries=auxiliaries[1:],
                  )
  
              # Modification to correct for use as initializer in tf.scan
              # (-reward[-1] since terminal state value will be predicted)
              terminal_value = (
                  values[-1] - reward[-1]
              ) / reward_discount
  
              # Whether to predict all or only abort-terminals
              if not self.predict_terminal_values:
                  is_terminal = tf.math.equal(x=terminal[-1], y=one)
                  terminal_value = tf.where(
                      condition=is_terminal,
                      x=zero_float,
                      y=terminal_value,
                  )
  
              values = tf.concat(
                  values=(values, [terminal_value]), axis=0
              )
  
              # TD-lambda return
              one_float = tf_util.constant(value=1.0, dtype="float")
              trace_decay = self.trace_decay.value()
  
              def recursive_return(next_return, reward_value):
                  current_reward, next_value = reward_value
                  next_return = (
                      one_float - trace_decay
                  ) * next_value + trace_decay * next_return
                  return current_reward + reward_discount * next_return
  
              return tf.scan(
                  fn=recursive_return,
                  elems=(reward, values),
                  initializer=terminal_value,
                  reverse=True,
              )
  
imports: import logging
  import tensorflow
  from tensorforce import TensorforceError
  from tensorforce.core import ModuleDict, memory_modules, optimizer_modules, parameter_modules, SignatureDict, TensorDict, TensorSpec, TensorsSpec, tf_function, tf_util, VariableDict
  from tensorforce.core.models import Model
  from tensorforce.core.networks import Preprocessor
  from tensorforce.core.objectives import objective_modules
  from tensorforce.core.policies import policy_modules, StochasticPolicy
label_desc: Use image node_id 16 for calling the TensorforceModel obj's underlying member method code with example usage: obj._terminal_experience_iterative() and returns: tf, unknown, tf, unknown, unknown