class_docstr: Tests KerasLayer in an all-Keras environment.
class_docstr_tok: ["Tests","KerasLayer","in","an","all-Keras","environment","."]
docstr: Tests imported batch norm with trainable=True.
docstr_tok: ["Tests","imported","batch","norm","with","trainable=True","."]
code: def testBatchNormRetraining(self, save_from_keras):
      """Tests imported batch norm with trainable=True."""
      export_dir = os.path.join(self.get_temp_dir(), "batch-norm")
      _save_batch_norm_model(
          export_dir, save_from_keras=save_from_keras
      )
      inp = tf_keras_v2.layers.Input(shape=(1,), dtype=tf.float32)
      imported = hub.KerasLayer(export_dir, trainable=True)
      (
          var_beta,
          var_gamma,
          var_mean,
          var_variance,
      ) = _get_batch_norm_vars(imported)
      outp = imported(inp)
      model = tf_keras_v2.Model(inp, outp)
      # Retrain the imported batch norm layer on a fixed batch of inputs,
      # which has mean 12.0 and some variance of a less obvious value.
      # The module learns scale and offset parameters that achieve the
      # mapping x --> 2*x for the observed mean and variance.
      model.compile(
          tf_keras_v2.optimizers.SGD(0.1),
          "mean_squared_error",
          run_eagerly=True,
      )
      x = [[11.0], [12.0], [13.0]]
      y = [[2 * xi[0]] for xi in x]
      model.fit(np.array(x), np.array(y), batch_size=len(x), epochs=100)
      self.assertAllClose(var_mean.numpy(), np.array([12.0]))
      self.assertAllClose(var_beta.numpy(), np.array([24.0]))
      self.assertAllClose(model(np.array(x, np.float32)), np.array(y))
      # Evaluating the model operates batch norm in inference mode:
      # - Batch statistics are ignored in favor of aggregated statistics,
      #   computing x --> 2*x independent of input distribution.
      # - Update ops are not run, so this doesn't change over time.
      for _ in range(100):
          self.assertAllClose(
              model(np.array([[10.0], [20.0], [30.0]], np.float32)),
              np.array([[20.0], [40.0], [60.0]]),
          )
      self.assertAllClose(var_mean.numpy(), np.array([12.0]))
      self.assertAllClose(var_beta.numpy(), np.array([24.0]))
  
imports: import json
  import os
  from absl.testing import parameterized
  import numpy
  import tensorflow
  import tensorflow_hub
label_desc: Use image node_id 3 for calling the KerasTest obj's underlying member method code with example usage: obj.testBatchNormRetraining(save_from_keras) without return types