|
| 1 | +""" |
| 2 | +
|
| 3 | +Hyperparameter optimization script for the updated Ames script |
| 4 | +
|
| 5 | +""" |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +import optuna |
| 9 | +import pendulum |
| 10 | +import pandas as pd |
| 11 | +import tensorflow as tf |
| 12 | +from cerebros.simplecerebrosrandomsearch.simple_cerebros_random_search\ |
| 13 | + import SimpleCerebrosRandomSearch |
| 14 | +from cerebros.units.units import DenseUnit |
| 15 | +from cerebros.denseautomlstructuralcomponent.dense_automl_structural_component\ |
| 16 | + import zero_7_exp_decay, zero_95_exp_decay, simple_sigmoid |
| 17 | +from ast import literal_eval |
| 18 | + |
| 19 | +# Define constants |
| 20 | +LABEL_COLUMN = 'price' |
| 21 | +NUMBER_OF_TRAILS_PER_BATCH = 2 |
| 22 | +NUMBER_OF_BATCHES_OF_TRIALS = 2 |
| 23 | + |
| 24 | +# Load data |
| 25 | +raw_data = pd.read_csv('ames.csv') |
| 26 | +needed_cols = [ |
| 27 | + col for col in raw_data.columns |
| 28 | + if raw_data[col].dtype != 'object' |
| 29 | + and col != LABEL_COLUMN] |
| 30 | +data_numeric = raw_data[needed_cols].fillna(0).astype(float) |
| 31 | +label = raw_data.pop(LABEL_COLUMN) |
| 32 | + |
| 33 | +data_np = data_numeric.values |
| 34 | + |
| 35 | +tensor_x = tf.constant(data_np) |
| 36 | + |
| 37 | +training_x = [tensor_x] |
| 38 | + |
| 39 | +INPUT_SHAPES = [training_x[i].shape[1] for i in np.arange(len(training_x))] |
| 40 | + |
| 41 | +train_labels = [tf.constant(label.values.astype(float))] |
| 42 | + |
| 43 | +OUTPUT_SHAPES = [1] |
| 44 | + |
| 45 | +def objective(trial): |
| 46 | + # Define hyperparameter space |
| 47 | + minimum_levels = trial.suggest_int('minimum_levels', 1, 8) |
| 48 | + maximum_levels = trial.suggest_int('maximum_levels', minimum_levels, 8) |
| 49 | + minimum_units_per_level = trial.suggest_int('minimum_units_per_level', 1, 8) |
| 50 | + maximum_units_per_level = trial.suggest_int('maximum_units_per_level', minimum_units_per_level, 8) |
| 51 | + minimum_neurons_per_unit = trial.suggest_int('minimum_neurons_per_unit', 1, 8) |
| 52 | + maximum_neurons_per_unit = trial.suggest_int('maximum_neurons_per_unit', minimum_neurons_per_unit, 8) |
| 53 | + activation = trial.suggest_categorical('activation', ['relu', 'elu', 'gelu', 'swish', 'softplus']) |
| 54 | + predecessor_level_connection_affinity_factor_first = trial.suggest_loguniform('predecessor_level_connection_affinity_factor_first', 0.1, 40.0) |
| 55 | + predecessor_level_connection_affinity_factor_main = trial.suggest_loguniform('predecessor_level_connection_affinity_factor_main', 0.1, 40.0) |
| 56 | + max_consecutive_lateral_connections = trial.suggest_int('max_consecutive_lateral_connections', 1, 40) |
| 57 | + p_lateral_connection = trial.suggest_loguniform('p_lateral_connection', 0.1, 40.0) |
| 58 | + num_lateral_connection_tries_per_unit = trial.suggest_int('num_lateral_connection_tries_per_unit', 1, 40) |
| 59 | + learning_rate = trial.suggest_loguniform('learning_rate', 10**-6, 0.6) |
| 60 | + epochs = trial.suggest_int('epochs', 1, 150) |
| 61 | + batch_size = trial.suggest_int('batch_size', 1, 800) |
| 62 | + |
| 63 | + meta_trial_number = 0 |
| 64 | + |
| 65 | + TIME = pendulum.now().__str__()[:16]\ |
| 66 | + .replace('T', '_')\ |
| 67 | + .replace(':', '_')\ |
| 68 | + .replace('-', '_') |
| 69 | + PROJECT_NAME = f'{TIME}_cerebros_auto_ml_test' |
| 70 | + |
| 71 | + cerebros = SimpleCerebrosRandomSearch( |
| 72 | + unit_type=DenseUnit, |
| 73 | + input_shapes=INPUT_SHAPES, |
| 74 | + output_shapes=OUTPUT_SHAPES, |
| 75 | + training_data=training_x, |
| 76 | + labels=train_labels, |
| 77 | + validation_split=0.35, |
| 78 | + direction='minimize', |
| 79 | + metric_to_rank_by='val_root_mean_squared_error', |
| 80 | + minimum_levels=minimum_levels, |
| 81 | + maximum_levels=maximum_levels, |
| 82 | + minimum_units_per_level=minimum_units_per_level, |
| 83 | + maximum_units_per_level=maximum_units_per_level, |
| 84 | + minimum_neurons_per_unit=minimum_neurons_per_unit, |
| 85 | + maximum_neurons_per_unit=maximum_neurons_per_unit, |
| 86 | + activation=activation, |
| 87 | + final_activation=None, |
| 88 | + number_of_architecture_moities_to_try=7, |
| 89 | + number_of_tries_per_architecture_moity=1, |
| 90 | + number_of_generations=3, |
| 91 | + minimum_skip_connection_depth=1, |
| 92 | + maximum_skip_connection_depth=7, |
| 93 | + predecessor_level_connection_affinity_factor_first=predecessor_level_connection_affinity_factor_first, |
| 94 | + predecessor_level_connection_affinity_factor_first_rounding_rule='ceil', |
| 95 | + predecessor_level_connection_affinity_factor_main=predecessor_level_connection_affinity_factor_main, |
| 96 | + predecessor_level_connection_affinity_factor_main_rounding_rule='ceil', |
| 97 | + predecessor_level_connection_affinity_factor_decay_main=zero_7_exp_decay, |
| 98 | + seed=8675309, |
| 99 | + max_consecutive_lateral_connections=max_consecutive_lateral_connections, |
| 100 | + gate_after_n_lateral_connections=3, |
| 101 | + gate_activation_function=simple_sigmoid, |
| 102 | + p_lateral_connection=p_lateral_connection, |
| 103 | + p_lateral_connection_decay=zero_95_exp_decay, |
| 104 | + num_lateral_connection_tries_per_unit=num_lateral_connection_tries_per_unit, |
| 105 | + learning_rate=learning_rate, |
| 106 | + loss='mse', |
| 107 | + metrics=[tf.keras.metrics.RootMeanSquaredError()], |
| 108 | + epochs=epochs, |
| 109 | + patience=7, |
| 110 | + project_name=f"{PROJECT_NAME}_meta_{meta_trial_number}", |
| 111 | + model_graphs='model_graphs', |
| 112 | + batch_size=batch_size, |
| 113 | + meta_trial_number=meta_trial_number) |
| 114 | + |
| 115 | + result = cerebros.run_random_search() |
| 116 | + return result |
| 117 | + |
| 118 | +def main(): |
| 119 | + study = optuna.create_study(direction='minimize') |
| 120 | + study.optimize(objective, n_trials=NUMBER_OF_TRAILS_PER_BATCH * NUMBER_OF_BATCHES_OF_TRIALS) |
| 121 | + print('Best trial:') |
| 122 | + best_trial = study.best_trial |
| 123 | + print(' Value: ', best_trial.value) |
| 124 | + print(' Params: ') |
| 125 | + for key, value in best_trial.params.items(): |
| 126 | + print(f' {key}: {value}') |
| 127 | + |
| 128 | +if __name__ == '__main__': |
| 129 | + main() |
0 commit comments