Last active
July 29, 2021 14:14
-
-
Save GastonMazzei/0e78cbf048e9a03a16692a56e7ece404 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#******************************************************************************** | |
# we are using the conditional_scope of the keras_tuner hyperparameter class | |
# | |
# link: https://keras.io/api/keras_tuner/hyperparameters/#hyperparameters-class | |
# | |
# example by Gaston Mazzei, https://gastonmazzei.github.io/ | |
#******************************************************************************** | |
def MyHyperModel(HyperModel): | |
""" | |
Build a Keras model that can tune | |
the # of hidden layers using "model_type" | |
as a variable that indicates the depth of | |
the model, i.e. 1,2,3... hidden layers | |
(toy version, with the activation fixed at "relu" | |
and the number of neurons fixed at "32") | |
""" | |
def build(self, hp, sh, depth, nclasses): | |
# Instantiate a sequential model with "sh" as | |
# input shape | |
model = Sequential() | |
model.add(Input(shape=sh)) | |
# Set the possible network's depth as choices | |
model_type = hp.Choice("model_type", [str(i) for i in range(1,depth+1)]) | |
for i in range(1,depth+1): | |
if model_type == str(i): | |
with hp.conditional_scope("model_type", [str(i)]): | |
for _ in range(i): | |
model.add(Dense(32, activation='relu')) # 32 neurons and "ReLu" for illustration purposes | |
# Add the final classification layer and return the model | |
model.add(Dense(nclasses, activation='softmax')) | |
return model |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment