[QUESTION] Use of Normalization layer with keras_tuner
hahampis opened this issue · 0 comments
hahampis commented
Hi,
in Chapter 10, there is this piece of code about (conditionally) using a normalization layer within the .fit()
method of a kt.HyperModel
:
class MyClassificationHyperModel(kt.HyperModel):
def build(self, hp):
return build_model(hp)
def fit(self, hp, model, X, y, **kwargs):
if hp.Boolean("normalize"):
norm_layer = tf.keras.layers.Normalization()
X = norm_layer(X)
return model.fit(X, y, **kwargs)
My question is, shouldn't we call the .adapt()
method of the Normalization layer before calling .fit()
?
Also, I have found that using hp.Boolean()
directly inside the .fit() method results in the normalize
hyperparameter always being equal to False
. If I initialize the parameter in the build()
method instead and then use it inside fit(), then it can be either True
or False
during the search:
class MyClassificationHyperModel(kt.HyperModel):
def build(self, hp: kt.HyperParameters) -> tf.keras.Model:
normalize = hp.Boolean("normalize")
...
def fit(self, hp: kt.HyperParameters, model, X, y, **kwargs):
normalize = hp.get("normalize")
if normalize:
norm_layer = tf.keras.layers.Normalization()
norm_layer.adapt(X)
X = norm_layer(X)
return model.fit(X, y, **kwargs)
Versions:
OS: MacOSX 14.4.1
Python: 3.10.7
TensorFlow: 2.14
Keras-tuner: 1.4.7
Scikit-Learn: 1.14.1.post1