import tensorflow as tf

                    ref=self.internals_memory[name],
                    indices=indices,
                    updates=internals[name]
                ))
            for name in sorted(actions):
                assignments.append(tf.scatter_update(
                    ref=self.actions_memory[name],
                    indices=indices,
                    updates=actions[name]
                ))
            assignments.append(tf.scatter_update(ref=self.terminal_memory, indices=indices, updates=terminal))
            assignments.append(tf.scatter_update(ref=self.reward_memory, indices=indices, updates=reward))

        # Add episode indices.
        with tf.control_dependencies(control_inputs=assignments):
            num_episodes = tf.count_nonzero(input_tensor=terminal, axis=0, dtype=util.tf_dtype('int'))
            assignment = tf.assign(
                ref=self.episode_indices[self.episode_count: self.episode_count + num_episodes],
                value=tf.boolean_mask(tensor=indices, mask=terminal)
            )

        # Increment episode count.
        with tf.control_dependencies(control_inputs=(assignment,)):
            assignment = tf.assign_add(ref=self.episode_count, value=num_episodes)

        # Increment memory index.
        with tf.control_dependencies(control_inputs=(assignment,)):
            assignment = tf.assign(
