class_docstr: Tests dp_optimizer_keras_sparse optimizers.
class_docstr_tok: ["Tests","dp_optimizer_keras_sparse","optimizers","."]
code: def testClippingNormMultipleVariables(
      self, cls, l2_norm_clip, num_microbatches
  ):
      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]])
  
      opt = cls(
          l2_norm_clip=l2_norm_clip,
          noise_multiplier=0.0,
          num_microbatches=num_microbatches,
          learning_rate=2.0,
      )
  
      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_norm_clip / np.maximum(l2_norm_clip, 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_privacy.privacy.optimizers import dp_optimizer_keras_sparse
label_desc: Use image node_id 6 for calling the DPOptimizerTest obj's underlying member method code with example usage: obj.testClippingNormMultipleVariables(cls, l2_norm_clip, num_microbatches) without return types