Startonix / Modular-AI

Advanced AI Training and Building Repository
0 stars 0 forks source link

Keras Tuner for Hyperparameter Optimization #140

Open Startonix opened 1 month ago

Startonix commented 1 month ago

keras_tuner_example.py

import tensorflow as tf from keras_tuner import RandomSearch

def build_model(hp): model = tf.keras.Sequential() model.add(tf.keras.layers.Dense(units=hp.Int('units', min_value=32, max_value=512, step=32), activation='relu')) model.add(tf.keras.layers.Dense(1, activation='sigmoid')) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) return model

tuner = RandomSearch(build_model, objective='val_accuracy', max_trials=5, executions_per_trial=3)

Example dataset

x_train, y_train = np.random.rand(100, 10), np.random.randint(2, size=100) x_val, y_val = np.random.rand(20, 10), np.random.randint(2, size=20) tuner.search(x_train, y_train, epochs=10, validation_data=(x_val, y_val))