Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] Use of Normalization layer with keras_tuner #128

Open
hahampis opened this issue Apr 8, 2024 · 0 comments
Open

[QUESTION] Use of Normalization layer with keras_tuner #128

hahampis opened this issue Apr 8, 2024 · 0 comments

Comments

@hahampis
Copy link

hahampis commented Apr 8, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant