keras-team / keras-tuner

A Hyperparameter Tuning Library for Keras
https://keras.io/keras_tuner/
Apache License 2.0
2.86k stars 397 forks source link

How to tune a custom generator using Keras Tuner? #355

Open volkansevim opened 4 years ago

volkansevim commented 4 years ago

I have a model that employs a custom generator to generate batches. I want to optimize a hyperparameter inside the generator, for example, the number of classes per batch, P.

train_gen = DataGenerator(P=64)

tuner = RandomSearch(
    build_model,
    objective="loss",
    max_trials=100,
    executions_per_trial=1,
)

tuner.search(
    train_gen,
    verbose=1,
    epochs=10,
)

I tune model-related parameters inside build_model(). However, I can't figure out how to tune P.

I tried:

hp = kt.HyperParameters()
train_gen = DataGenerator(P = hp.Choice("P", [32, 64, 128, 256])

The code runs without error, but P does not show up under the search space summary.

Is there any way to tune a generator in Keras tuner?

yixingfu commented 4 years ago

Here is how tuner works: tuner creates a hp, pass it into build_model as an argument. This is different from the hp that you created. Hence the tuner would not see the hp.Choice in generator as a tuning knob. Search space will only include those created using hp that gets passed to build_model, or a HyperModel object.

There might be a way to tune generator if you subclass HyperModel to include the generator, and somehow pass the hp between build() and the generator. But I think the simplest thing to try is using preprocessing layers (see https://www.tensorflow.org/api_docs/python/tf/keras/layers/experimental/preprocessing?version=nightly) to put preprocessing in the model itself. Then you can use the normal KT workflow to tune your preprocessing steps.