class_docstr: Tests for _compute_gradients method.
class_docstr_tok: ["Tests","for","_compute_gradients","method","."]
code: def testClippingNormMultipleVariables(
      self, cls, num_microbatches, requires_varlist
  ):
      var0 = tf.Variable([1.0, 2.0])
      var1 = tf.Variable([3.0])
      data0 = tf.Variable(
          [[3.0, 6.0], [5.0, 6.0], [4.0, 8.0], [-1.0, 0.0]]
      )
      data1 = tf.Variable([[8.0], [2.0], [3.0], [1.0]])
      l2_clip_norm = 2.5
  
      varlist_kwarg = (
          {"var_list_or_model": [var0, var1]}
          if requires_varlist
          else {}
      )
  
      opt = cls(
          l2_norm_clip=l2_clip_norm,
          noise_multiplier=0.0,
          num_microbatches=num_microbatches,
          learning_rate=2.0,
          **varlist_kwarg,
      )
  
      loss = lambda: self._loss(data0, var0) + self._loss(data1, var1)
  
      # Expected gradient is sum of differences.
      grads_and_vars = opt._compute_gradients(loss, [var0, var1])
  
      # Compute expected gradients.
      batch_size = data0.shape[0]
      grad0 = (data0 - var0).numpy()
      grad1 = (data1 - var1).numpy()
      grads = np.concatenate([grad0, grad1], axis=1)
  
      grads = np.reshape(
          grads,
          (num_microbatches, int(batch_size / num_microbatches), -1),
      )
      grads = np.mean(grads, axis=1)
  
      norms = np.apply_along_axis(np.linalg.norm, axis=1, arr=grads)
      grad_factors = l2_clip_norm / np.maximum(l2_clip_norm, norms)
  
      scaled_grads = grads * grad_factors[:, None]
      mean_scaled_grads = -np.mean(scaled_grads, axis=0)
      expected0, expected1 = np.split(mean_scaled_grads, [2], axis=0)
  
      # Compare expected with actual gradients.
      self.assertAllCloseAccordingToType(
          expected0, grads_and_vars[0][0]
      )
      self.assertAllCloseAccordingToType(
          expected1, grads_and_vars[1][0]
      )
  
imports: from absl.testing import parameterized
  import numpy
  import tensorflow
  from tensorflow import estimator
  from tensorflow_privacy.privacy.optimizers import dp_optimizer_keras
  from tensorflow_privacy.privacy.optimizers import dp_optimizer_keras_vectorized
label_desc: Use image node_id 8 for calling the DPOptimizerComputeGradientsTest obj's underlying member method code with example usage: obj.testClippingNormMultipleVariables(cls, num_microbatches, requires_varlist) without return types