class_docstr: Tests dp_optimizer_keras_sparse optimizers.
class_docstr_tok: ["Tests","dp_optimizer_keras_sparse","optimizers","."]
docstr: Tests that DP optimizer works with keras optimizer.
docstr_tok: ["Tests","that","DP","optimizer","works","with","keras","optimizer","."]
code: def testNoiseMultiplier(
      self,
      cls,
      use_embeddings,
      l2_norm_clip,
      noise_multiplier,
      num_microbatches,
      gradient_accumulation_steps,
  ):
      """Tests that DP optimizer works with keras optimizer."""
      dp_optimizer._KEEP_SPARSE_THRESHOLD = 0
      inputs = {
          "x": tf.keras.Input(shape=(1000)),
          "i": tf.keras.Input(shape=(1)),
      }
      if use_embeddings:
          # Emulates a linear layer using embeddings.
          layer = tf.keras.layers.Embedding(
              10, 1000, embeddings_initializer="zeros"
          )
          preds = tf.reduce_sum(
              tf.multiply(layer(inputs["i"]), inputs["x"]),
              axis=1,
              keepdims=True,
          )
          weights = layer.embeddings
      else:
          layer = tf.keras.layers.Dense(
              1,
              activation="linear",
              name="dense",
              kernel_initializer="zeros",
              bias_initializer="zeros",
          )
          preds = layer(inputs["x"])
          weights = layer.kernel
      model = tf.keras.Model(inputs, preds)
  
      loss = tf.keras.losses.MeanSquaredError(
          reduction=tf.keras.losses.Reduction.NONE
      )
      optimizer = cls(
          l2_norm_clip=l2_norm_clip,
          noise_multiplier=noise_multiplier,
          num_microbatches=num_microbatches,
          gradient_accumulation_steps=gradient_accumulation_steps,
          learning_rate=1.0,
      )
  
      model.compile(optimizer=optimizer, loss=loss)
  
      true_weights = np.zeros((1000, 1), dtype=np.float32)
      true_bias = np.array([0.0]).astype(np.float32)
      for _ in range(9 * gradient_accumulation_steps):
          x = np.zeros((16, 1000), dtype=np.float32)
          i = np.random.randint(2, size=(16, 1))
          y = np.matmul(x, true_weights) + true_bias
          model.fit(x={"x": x, "i": i}, y=y)
  
      if num_microbatches is None:
          num_microbatches = 16
      noise_stddev = (
          3
          * l2_norm_clip
          * noise_multiplier
          / num_microbatches
          / gradient_accumulation_steps
      )
      self.assertNear(np.std(weights), noise_stddev, 0.5)
  
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 8 for calling the DPOptimizerTest obj's underlying member method code with example usage: obj.testNoiseMultiplier(cls, use_embeddings, l2_norm_clip, noise_multiplier, num_microbatches, gradient_accumulation_steps) without return types