Created
December 1, 2020 13:01
-
-
Save zahash/08f3f18c09763ca7de10c21ed5186099 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
def model_builder(hp): | |
if TASK == "r": | |
loss_fn = "mean_absolute_error" | |
elif TASK == "c": | |
if OUTPUT_NODES == 1: | |
loss_fn = tf.keras.losses.BinaryCrossentropy(from_logits=True) | |
else: | |
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) | |
if TASK == "r": | |
metrics = None | |
elif TASK == "c": | |
metrics = ["accuracy"] | |
kernel_hp = hp.Choice( | |
"kernel_regularization", values=[0.01, 0.001, 0.0001, 0.00001] | |
) | |
activation_hp = hp.Choice("activation", values=["elu", "relu"]) | |
lr_schedule = tf.keras.optimizers.schedules.InverseTimeDecay( | |
0.01, decay_steps=train_size * 1000, decay_rate=1, staircase=False | |
) | |
body = tf.keras.Sequential() | |
current_nodes = num_preprocessed_outputs | |
while current_nodes > OUTPUT_NODES: | |
body.add( | |
tf.keras.layers.Dense( | |
current_nodes, | |
kernel_regularizer=tf.keras.regularizers.l2(kernel_hp), | |
activation=activation_hp, | |
) | |
) | |
body.add(tf.keras.layers.Dropout(0.1)) | |
current_nodes = current_nodes // 2 | |
body.add(tf.keras.layers.Dense(OUTPUT_NODES)) | |
result = body(preprocessed_outputs) | |
model = tf.keras.Model(model_inputs, result) | |
model.compile( | |
loss=loss_fn, | |
optimizer=tf.keras.optimizers.Adam(learning_rate=lr_schedule), | |
metrics=metrics, | |
) | |
return model |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment