docstr: Tests that clipping norm works.
docstr_tok: ["Tests","that","clipping","norm","works","."]
code: def testClippingNorm(
      self, l2_norm_clip, fast_clipping, sequential, weighted
  ):
      """Tests that clipping norm works."""
      train_data, train_labels = get_data()
  
      # Simple linear model returns w * x + b.
      layer = tf.keras.layers.Dense(
          1, kernel_initializer="zeros", bias_initializer="zeros"
      )
      if sequential:
          model = dp_keras_model.DPSequential(
              l2_norm_clip=l2_norm_clip,
              noise_multiplier=0.0,
              layer_registry=layer_registry.make_default_layer_registry()
              if fast_clipping
              else None,
              layers=[
                  tf.keras.layers.InputLayer(input_shape=(2,)),
                  layer,
              ],
          )
      else:
          inputs = tf.keras.Input(shape=(2,), dtype=tf.float32)
          outputs = layer(inputs)
          model = dp_keras_model.DPModel(
              l2_norm_clip=l2_norm_clip,
              noise_multiplier=0.0,
              layer_registry=layer_registry.make_default_layer_registry()
              if fast_clipping
              else None,
              inputs=inputs,
              outputs=outputs,
          )
      learning_rate = 0.01
      optimizer = tf.keras.optimizers.SGD(learning_rate=learning_rate)
      loss = tf.keras.losses.MeanSquaredError()
      model.compile(optimizer=optimizer, loss=loss)
  
      weights = None
      data = tf.data.Dataset.from_tensors((train_data, train_labels))
      expected_grad_w = np.array([90.0, 120.0])
      expected_grad_b = np.array([30.0])
      if weighted:
          # Apply a weight to the (single) example.
          weights = [0.18]
          data = tf.data.Dataset.from_tensors(
              (train_data, train_labels, weights)
          )
          expected_grad_w *= 0.18
          expected_grad_b *= 0.18
  
      unclipped_norm = np.linalg.norm(
          np.concatenate([expected_grad_w, expected_grad_b])
      )
      scale = min(1.0, l2_norm_clip / unclipped_norm)
      expected_weights = expected_grad_w * scale * learning_rate
      expected_bias = expected_grad_b * scale * learning_rate
      expected_loss = loss(train_labels, model(train_data), weights)
      results = model.fit(data, epochs=1, batch_size=1)
      weights, bias = model.get_weights()
  
      # Check parameters are as expected, taking into account the learning rate.
      self.assertAllClose(np.squeeze(weights), expected_weights)
      self.assertAllClose(bias, expected_bias)
  
      # Check the value of the loss.
      actual_loss = results.history["loss"][0]
      self.assertAllClose(expected_loss, actual_loss)
  
file_path: privacy/tensorflow_privacy/privacy/keras_models/dp_keras_model_test.py
imports: from absl.testing import parameterized
  import numpy
  import tensorflow
  from tensorflow_privacy.privacy.fast_gradient_clipping import layer_registry
  from tensorflow_privacy.privacy.keras_models import dp_keras_model
label_desc: Use image node_id 2 for calling the DPKerasModelTest obj's underlying member method code with example usage: obj.testClippingNorm(l2_norm_clip, fast_clipping, sequential, weighted) without return types