what is the best strategy for hidden layer size tuning (for large models) in python?

Viewed 25

I am looking for the best values of hidden layer size of a neural network mod in python (Keras). The model is like this:

x1 = Input((window_size, 3), name='x1')
x2 = Input((window_size, 3), name='x2')
convA1 = Conv1D(150,11,padding='same',activation='relu')(x1)
convA2 = Conv1D(150,11,padding='same',activation='relu')(convA1)
poolA = MaxPooling1D(3)(convA1)
convB1 = Conv1D(150,11,padding='same',activation='relu')(x2)
convB2 = Conv1D(150,11,padding='same',activation='relu')(convB1)
poolB = MaxPooling1D(3)(convB1)
AB = concatenate([poolA, poolB])
lstm1 = Bidirectional(LSTM(50, return_sequences=True))(AB)
drop1 = Dropout(0.25)(lstm1)
lstm2 = Bidirectional(LSTM(150))(drop1)
drop2 = Dropout(0.25)(lstm2)
y1_pred = Dense(4, activation='linear')(drop2)
model = Model(inputs =[x1, x2], outputs = [y1_pred])

Which algorithm would be the best (or better) to find the best hidden layer size values?

Also, I have used PBT, Grid Search, Random Search, and Bayesian Optimization by using Ray Tune, Keras Tuner, and Sherpa, but I could not find the lowest val_loss. This is the configuration for Bayesian optimization in Keras Tuner:

tuner = BayesianOptimization(
build_model,
objective="val_loss",
max_trials=20, 
executions_per_trial=4, 
directory=LOG_DIR,
project_name='tuning-BayesianOptimization')

And this is the PBT configuration in Ray

pbt = PopulationBasedTraining(
        time_attr="training_iteration",
        perturbation_interval=1,
        quantile_fraction=0.25,
        hyperparam_mutations={
            "dropout":tune.uniform(0.1,0.5),
            "lr":tune.uniform(1e-5,1e-3),
            "Conv1DA": tune.randint(50,150),
            "Conv1DA2": tune.randint(50,150),
            "Conv1DB": tune.randint(50,150),
            "Conv1DB2": tune.randint(50,150),
            "LSTM1": tune.randint(50,150),
            "LSTM2": tune.randint(50,150)})
    resources_per_trial = {"cpu": 10 , "gpu": 0}
    tuner = tune.Tuner(
         tune.with_resources(
        BroadModel,
        resources=resources_per_trial),
        run_config=air.RunConfig(
            name="BroadPBT"+timestr,
            stop={"training_iteration": 100}),
        tune_config=tune.TuneConfig(
            reuse_actors=True,
            scheduler=pbt,
            metric="loss",
            mode="min",
            num_samples=2),
        param_space={
            "finish_fast": False,
            "dropout":tune.uniform(0.1,0.5),
            "lr":tune.uniform(1e-5,1e-3),
            "Conv1DA": tune.randint(50,150),
            "Conv1DA2": tune.randint(50,150),
            "Conv1DB": tune.randint(50,150),
            "Conv1DB2": tune.randint(50,150),
            "LSTM1": tune.randint(50,150),
            "LSTM2": tune.randint(50,150)})

Am I doing anything wrong? Is there anything else I should consider? Thanks.

0 Answers
Related