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=False.
docstr_tok: ["Tests","imported","batch","norm","with","trainable=False","."]
code: def testBatchNormFreezing(self, save_from_keras):
      """Tests imported batch norm with trainable=False."""
      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=False)
      (
          var_beta,
          var_gamma,
          var_mean,
          var_variance,
      ) = _get_batch_norm_vars(imported)
      dense = tf_keras_v2.layers.Dense(
          units=1,
          kernel_initializer=tf_keras_v2.initializers.Constant([[1.5]]),
          use_bias=False,
      )
      outp = dense(imported(inp))
      model = tf_keras_v2.Model(inp, outp)
      # Training the model to x --> 2*x leaves the batch norm layer entirely
      # unchanged (both trained beta&gamma and aggregated mean&variance).
      self.assertAllClose(var_beta.numpy(), np.array([0.0]))
      self.assertAllClose(var_gamma.numpy(), np.array([1.0]))
      self.assertAllClose(var_mean.numpy(), np.array([0.0]))
      self.assertAllClose(var_variance.numpy(), np.array([1.0]))
      model.compile(
          tf_keras_v2.optimizers.SGD(0.1),
          "mean_squared_error",
          run_eagerly=True,
      )
      x = [[1.0], [2.0], [3.0]]
      y = [[2 * xi[0]] for xi in x]
      model.fit(np.array(x), np.array(y), batch_size=len(x), epochs=20)
      self.assertAllClose(var_beta.numpy(), np.array([0.0]))
      self.assertAllClose(var_gamma.numpy(), np.array([1.0]))
      self.assertAllClose(var_mean.numpy(), np.array([0.0]))
      self.assertAllClose(var_variance.numpy(), np.array([1.0]))
      self.assertAllClose(model(np.array(x, np.float32)), np.array(y))
  
imports: import json
  import os
  from absl.testing import parameterized
  import numpy
  import tensorflow
  import tensorflow_hub
label_desc: Use image node_id 4 for calling the KerasTest obj's underlying member method code with example usage: obj.testBatchNormFreezing(save_from_keras) without return types