I just discovered Keras-Tuner and am trying to use it to tune my CNN's hyperparameters, but it looks like there's a mistake in my function to define the model. Here's my function:
from keras import models, layers, optimizers, losses, metrics
import keras_tuner
input_size = 479
output_size = 699
def construct_cnn(hp):
model = models.Sequential()
# Add Convolutional layers
model.add(layers.Conv1D(filters=hp.Choice('filters_0', [16,32,48,64], ordered=False), kernel_size=3, activation='relu', input_shape=(input_size,1)))
model.add(layers.MaxPooling1D(pool_size=2))
N_conv = hp.Int('num_conv_layers', min_value=1, max_value=4, step=1)
if N_conv>1:
for i in range(N_conv-1):
model.add(layers.Conv1D(filters=hp.Choice(f'filters_{i+1}', [16,32,48,64], ordered=False), kernel_size=3, activation='relu'))
model.add(layers.MaxPooling1D(pool_size=2))
model.add(layers.Flatten())
# Add Dense layers
N_dense = hp.Int('num_dense_layers', min_value=1, max_value=4, step=1)
for d in range(N_dense):
model.add(layers.Dense(units=hp.Choice(f'units_{d}', [32,64,96,128,160,192,224,256], ordered=False), activation='relu'))
model.add(layers.Dense(units=output_size, activation='linear'))
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['mean_absolute_percentage_error'])
return model
Basically, I'm trying to start with N_conv convolutional layers where the number of filters are randomly chosen out of some options for each layer, and then I'd like to have N_dense dense layers (not including the output layer) after this where the number of nodes are also randomly chosen out of the options. Finally it should finish with the output dense layer.
But during the search process, I noticed this output for one of the trials:
Value |Best Value So Far |Hyperparameter
64 |64 |filters_0
2 |1 |num_conv_layers
4 |1 |num_dense_layers
96 |256 |units_0
Even though there were supposedly 2 convolutional layers and 4 dense layers, it only picked one number for filters and one other number for nodes in one layer. Does anyone know how to fix this in my function?