def get_gradients(self, loss, params):
    if self._changing_clipping:
        self._l2_norm_clip *= 0.99
        tf.print('cur C:', self._l2_norm_clip, output_stream=sys.stdout)
    'DP version of superclass method.'
    self._was_dp_gradients_called = True
    if (self._global_state is None):
        self._global_state = self._dp_sum_query.initial_global_state()
    microbatch_losses = tf.reshape(loss, [self._num_microbatches, (- 1)])
    sample_params = self._dp_sum_query.derive_sample_params(self._global_state)

    def process_microbatch(i, sample_state):
        'Process one microbatch (record) with privacy helper.'
        mean_loss = tf.reduce_mean(input_tensor=tf.gather(microbatch_losses, [i]))
        grads = tf.gradients(mean_loss, params)
        sample_state = self._dp_sum_query.accumulate_record(sample_params, sample_state, grads)
        return sample_state
    sample_state = self._dp_sum_query.initial_sample_state(params)
    for idx in range(self._num_microbatches):
        sample_state = process_microbatch(idx, sample_state)
    (grad_sums, self._global_state) = self._dp_sum_query.get_noised_result(sample_state, self._global_state)

    def normalize(v):
        try:
            return tf.truediv(v, tf.cast(self._num_microbatches, tf.float32))
        except TypeError:
            return None
    final_grads = tf.nest.map_structure(normalize, grad_sums)
    return final_grads
def get_gradients(self, loss, params):
    if self._changing_clipping:
        self._l2_norm_clip *= 0.99
        tf.print('cur C:', self._l2_norm_clip, output_stream=sys.stdout)
    'DP version of superclass method.'
    self._was_dp_gradients_called = True
    if (self._global_state is None):
        self._global_state = self._dp_sum_query.initial_global_state()
    microbatch_losses = tf.reshape(loss, [self._num_microbatches, (- 1)])
    sample_params = self._dp_sum_query.derive_sample_params(self._global_state)

    def process_microbatch(i, sample_state):
        'Process one microbatch (record) with privacy helper.'
        mean_loss = tf.reduce_mean(input_tensor=tf.gather(microbatch_losses, [i]))
        grads = tf.gradients(mean_loss, params)
        sample_state = self._dp_sum_query.accumulate_record(sample_params, sample_state, grads)
        return sample_state
    sample_state = self._dp_sum_query.initial_sample_state(params)
    for idx in range(self._num_microbatches):
        sample_state = process_microbatch(idx, sample_state)
    (grad_sums, self._global_state) = self._dp_sum_query.get_noised_result(sample_state, self._global_state)

    def normalize(v):
        try:
            return tf.truediv(v, tf.cast(self._num_microbatches, tf.float32))
        except TypeError:
            return None
    final_grads = tf.nest.map_structure(normalize, grad_sums)
    return final_grads