C3_W1_Lab_2_TFX_Tuner_and_Trainer model_builder question

Hello Learners,

While running the lab I see the model_builder function is defined in both the _tuner_model_file and _trainer_module_file. Can anyone explain why it has to be defined twice? This seems redundant and I would expect the trainer to pick-up on the model defined in the tuner.
Any info regarding the topic is much appreciated

Happy learning!

In the latest version of keras_tuner, you can use best_model = tuner.get_best_models()[0].

Hi Balaji,

Thanks for your answer, that wasn’t quite what I meant.

The lab defines below function twice, in seperate files and passes it to the Tuner and Trainer. I was wondering why the Trainer isn’t ‘aware’ of the model past to the Tuner

def model_builder(hp):
  Builds the model and sets up the hyperparameters to tune.

    hp - Keras tuner object

    model with hyperparameters to tune

  # Initialize the Sequential API and start stacking the layers
  model = keras.Sequential()
  model.add(keras.layers.Flatten(input_shape=(28, 28, 1)))

  # Tune the number of units in the first Dense layer
  # Choose an optimal value between 32-512
  hp_units = hp.Int('units', min_value=32, max_value=512, step=32)
  model.add(keras.layers.Dense(units=hp_units, activation='relu', name='dense_1'))

  # Add next layers
  model.add(keras.layers.Dense(10, activation='softmax'))

  # Tune the learning rate for the optimizer
  # Choose an optimal value from 0.01, 0.001, or 0.0001
  hp_learning_rate = hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-4])


  return model

Look at the hyperparameters parameter of the Trainer constructor and notice how the tuner output is used for this.