class_docstr: Tests KerasLayer in an all-Keras environment.
class_docstr_tok: ["Tests","KerasLayer","in","an","all-Keras","environment","."]
code: def testHalfPlusOneRetraining(self, model_format):
      export_dir = os.path.join(self.get_temp_dir(), "half-plus-one")
      _dispatch_model_format(
          model_format, _save_half_plus_one_model, export_dir
      )
      # Import the half-plus-one model into a consumer model.
      inp = tf_keras_v2.layers.Input(shape=(1,), dtype=tf.float32)
      imported = hub.KerasLayer(export_dir, trainable=True)
      outp = imported(inp)
      model = tf_keras_v2.Model(inp, outp)
      # The consumer model computes y = x/2 + 1 as expected.
      self.assertAllEqual(
          model(
              np.array([[0.0], [8.0], [10.0], [12.0]], dtype=np.float32)
          ),
          np.array([[1.0], [5.0], [6.0], [7.0]], dtype=np.float32),
      )
      self.assertAllEqual(
          model.losses, np.array([0.0025], dtype=np.float32)
      )
      # The kernel weight is trainable but the bias is not.
      self.assertEqual(len(model.trainable_weights), 1)
      self.assertEqual(
          model.trainable_weights[0].shape.rank, 2
      )  # Kernel w.
      self.assertEqual(len(model.non_trainable_weights), 2)
      self.assertCountEqual(
          [v.shape.rank for v in model.non_trainable_weights], [2, 1]
      )  # Kernel and bias from the plus_1 layer.
      self.assertNoCommonElements(
          _tensors_names_set(model.trainable_weights),
          _tensors_names_set(model.non_trainable_weights),
      )
      # Retrain on y = x/2 + 6 for x near 10.
      # (Console output should show loss below 0.2.)
      model.compile(
          tf_keras_v2.optimizers.SGD(0.002),
          "mean_squared_error",
          run_eagerly=True,
      )
      x = [[9.0], [10.0], [11.0]] * 10
      y = [[xi[0] / 2.0 + 6] for xi in x]
      model.fit(
          np.array(x),
          np.array(y),
          batch_size=len(x),
          epochs=10,
          verbose=2,
      )
      # The bias is non-trainable and has to stay at 1.0.
      self.assertAllEqual(
          model(np.array([[0.0]], dtype=np.float32)),
          np.array([[1.0]], dtype=np.float32),
      )
      # To compensate, the kernel weight will grow to almost 1.0.
      self.assertAllClose(
          model(np.array([[10.0]], dtype=np.float32)),
          np.array([[11.0]], dtype=np.float32),
          atol=0.0,
          rtol=0.03,
      )
      self.assertAllClose(
          model.losses,
          np.array([0.01], dtype=np.float32),
          atol=0.0,
          rtol=0.06,
      )
  
imports: import json
  import os
  from absl.testing import parameterized
  import numpy
  import tensorflow
  import tensorflow_hub
label_desc: Use image node_id 1 for calling the KerasTest obj's underlying member method code with example usage: obj.testHalfPlusOneRetraining(model_format) without return types