如何使用 Keras Tuner 微调具有可变层数的 ANN,每一层具有可变数量的神经元?

How can I fine tune an ANN with variable number of layers, each one with a variable number of neurons, using Keras Tuner?

我有以下超级模型:

def model(hp):
    units_choice = hp.Int('units', min_value=80, max_value=420, step=10)
    activation_choice = hp.Choice('activation', ['relu', 'tanh'])
    number_of_layers = hp.Int('num_layers', 2, 10)
    learning_rate = hp.Choice('learning_rate', [1e-2, 1e-3, 1e-4])

    ANN = Sequential()
    ANN.add(Dense(units=40, input_shape=(40,)))
    for i in range(number_of_layers):
        ANN.add(Dense(units=units_choice, activation=activation_choice))
    ANN.add(Dense(1))
    ANN.compile(loss='mean_squared_error', optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate),
                metrics=['mean_squared_error'])
    return ANN

我想要的是使用 Keras 调谐器微调这个 ANN。在这种情况下,我希望调谐器找到最佳层数,每一层都有其最佳神经元数。上面的代码为调谐器找到的每一层提供了相同数量的神经元。

如何修改我的代码以满足上述要求?

您只需要使用 number_of_layers 定义一个 Int 超参数列表,稍后传递给层:

def model(hp):
    activation_choice = hp.Choice('activation', ['relu', 'tanh'])
    number_of_layers = hp.Int('num_layers', 2, 10)
    learning_rate = hp.Choice('learning_rate', [1e-2, 1e-3, 1e-4])
    
    # One hyper parameter for each layer.
    units_choices = [hp.Int(f'units_layer_{i}', min_value=80, max_value=420, step=10) for i in range(number_of_layers)]

    ANN = tf.keras.models.Sequential()
    ANN.add(layers.Dense(units=40, input_shape=(40,)))
    for i in range(number_of_layers):
        # Use the list here.
        ANN.add(layers.Dense(units=units_choices[i], activation=activation_choice))
    ANN.add(layers.Dense(1))
    ANN.compile(loss='mean_squared_error', optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate),
                metrics=['mean_squared_error'])
    return ANN