docstr: Trains two logistic regression models with two normalizations.
docstr_tok: ["Trains","two","logistic","regression","models","with","two","normalizations","."]
code: def testTrainingLogistic(self):
      """Trains two logistic regression models with two normalizations."""
      dm_config = test_util.default_dm_config(
          per_cluster_buffer_size=500,
          distance_to_cluster_threshold=0.9,
          max_num_clusters=1,
          bootstrap_steps=10,
      )
      x = np.array(
          [
              [1, 0, 0, 0],
              [0, 1, 0, 0],
              [0, 0, 1, 0],
              [0, 0, 0, 1],
              [1, 0, 1, 0],
              [0, 1, 0, 1],
          ]
      )
      y = np.array([[1], [1], [1], [1], [0], [0]])  # 0/1 label
      mode = tf.constant(dm_ops.LOOKUP_WITH_UPDATE, dtype=tf.int32)
  
      def _create_model(enable_dynamic_normalization: bool):
          hidden_layer = tf.keras.layers.Dense(5, activation="relu")
          if enable_dynamic_normalization:
              normalized_layer = dn.DynamicNormalization(
                  dm_config,
                  mode=mode,
                  axis=1,
                  epsilon=0.001,
                  use_batch_normalization=False,
                  service_address=self._kbs_address,
              )
          else:
              normalized_layer = tf.keras.layers.BatchNormalization(
                  axis=1, center=True, scale=True, momentum=0
              )
          output_layer = tf.keras.layers.Dense(
              1, kernel_initializer="ones"
          )
          model = tf.keras.Sequential(
              [hidden_layer, normalized_layer, output_layer]
          )
          return model
  
      def _loss(model, x, y):
          output = model(x)
          pred = 1 / (1 + tf.exp(-output))
          loss = y * tf.math.log(pred) + (1 - y) * tf.math.log(1 - pred)
          loss = tf.reduce_mean(-tf.math.reduce_sum(loss, axis=1))
          return loss
  
      def _grad(model, x, y):
          with tf.GradientTape() as tape:
              loss_value = _loss(model, x, y)
          return loss_value, tape.gradient(
              loss_value, model.trainable_variables
          )
  
      bn_model = _create_model(False)  # Model with batch normalization.
      dn_model = _create_model(
          True
      )  # Model with dynamic normalization.
      optimizer_bn = tf.keras.optimizers.SGD(learning_rate=0.01)
      optimizer_dn = tf.keras.optimizers.SGD(learning_rate=0.01)
      for i in range(100):
          bn_loss_value, bn_grads = _grad(bn_model, x, y)
          dn_loss_value, dn_grads = _grad(dn_model, x, y)
  
          print(
              "Step: {}, BN loss: {}, DN loss: {}".format(
                  i, bn_loss_value.numpy(), dn_loss_value.numpy()
              )
          )
          # Update the trainable variables w.r.t. the logistic loss
          optimizer_bn.apply_gradients(
              zip(bn_grads, bn_model.trainable_variables)
          )
          optimizer_dn.apply_gradients(
              zip(dn_grads, dn_model.trainable_variables)
          )
  
      # Checks that DynamicNormalization consistently outperforms
      # BatchNormalization in terms of finding lower loss.
      self.assertGreater(bn_loss_value.numpy(), dn_loss_value.numpy())
  
file_path: neural-structured-learning/research/carls/dynamic_normalization_test.py
imports: from research.carls import context
  from research.carls import dynamic_memory_ops
  from research.carls import dynamic_normalization
  from research.carls.testing import test_util
  import numpy
  import tensorflow
label_desc: Use image node_id 5 for calling the DynamicNormalizationTest obj's underlying member method code with example usage: obj.testTrainingLogistic() and returns: model, loss, loss_value,  tape