keras-team/keras-tuner

Model not built with KerasCore when loading best model

Closed this issue · 2 comments

To avoid having to load custom objects, when the user wants to load the best models after the search, KerasTuner would always create a new model with the best hyperparameters and load the best weights.

However, with Keras Core, the weights cannot be loaded if the model is built with the actual input_shape.
So, if the user did not build the model with input_shape in their code, they would get an error when loading the best model.

We have a few possible solutions:

Solution 1: Support loading weights before building the model in Keras Core.

Solution 2: Save the entire model instead of just the weights, and allow the user to pass custom_objects arg to the tuner.get_best_models() function.

Solution 3: We save the model.input_shape with the Trial and build the model for the user when the model is loaded. However, there is no clear way to get the input_shape from a subclass model.

Solution 4: We ask the user to build the model in the HyperModel.build() function before returning it to KerasTuner. Throw an error if they don't.

However, with Keras Core, the weights cannot be loaded if the model is built with the actual input_shape.

Can you provided more context here, e.g. a reproducible code snippet?

Based on our discussion, the solution would be similar to solution 3, but with model.get_build_config() and model.build_from_config(), and saving the build config with the model.