def _define_end_episode(self, agent_indices):
    """Implement the branch of end_episode() entered during training."""
    episodes, length = self._current_episodes.data(agent_indices)
    space_left = self._config.update_every - self._num_finished_episodes
    use_episodes = tf.range(tf.minimum(
        tf.shape(agent_indices)[0], space_left))
    episodes = tools.nested.map(lambda x: tf.gather(x, use_episodes), episodes)
    append = self._finished_episodes.replace(
        episodes, tf.gather(length, use_episodes),
        use_episodes + self._num_finished_episodes)
    with tf.control_dependencies([append]):
      increment_index = self._num_finished_episodes.assign_add(
          tf.shape(use_episodes)[0])
    with tf.control_dependencies([increment_index]):
      memory_full = self._num_finished_episodes >= self._config.update_every
      return tf.cond(memory_full, self._training, str)
Implement the branch of end_episode() entered during training.