<jupyter_start><jupyter_text>Table of Contents1&nbsp;&nbsp;Hyperparameter Tuning Demo using Keras Tuner1.0.1&nbsp;&nbsp;Load the data1.0.2&nbsp;&nbsp;Check tensorflow and kerastuner versions1.0.3&nbsp;&nbsp;Import libraries1.0.4&nbsp;&nbsp;Set random seeed1.0.5&nbsp;&nbsp;Model without Hyperparameter Tuning1.0.6&nbsp;&nbsp;Building the Hypermodel1.0.7&nbsp;&nbsp;Initialize hypermodel1.0.8&nbsp;&nbsp;Build Random Search Tuner1.0.9&nbsp;&nbsp;Run Random Search1.0.10&nbsp;&nbsp;Evaluate Random Search1.0.11&nbsp;&nbsp;Build, Run and Evaluate Hyperband Tuner1.0.12&nbsp;&nbsp;Build, Run and Evaluate Bayesian Optimization Tuner  Hyperparameter Tuning Demo using Keras Tuner  Load the data<jupyter_code>from tensorflow.keras.datasets import boston_housing

(x_train, y_train), (x_test, y_test) = boston_housing.load_data()<jupyter_output><empty_output><jupyter_text>Check tensorflow and kerastuner versions<jupyter_code>import tensorflow as tf
import keras_tuner as kt

from tensorflow.keras import backend as K
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import categorical_crossentropy

print(tf.__version__)<jupyter_output>2.5.0<jupyter_text>Import libraries<jupyter_code>from sklearn.preprocessing import StandardScaler
from tensorflow.keras import models, layers
from keras_tuner import HyperModel, RandomSearch, Hyperband, BayesianOptimization<jupyter_output><empty_output><jupyter_text>Set random seeed<jupyter_code>from numpy.random import seed
seed(42)
import tensorflow
tensorflow.random.set_seed(42)<jupyter_output><empty_output><jupyter_text>Model without Hyperparameter Tuning<jupyter_code>scaler = StandardScaler()
scaler.fit(x_train)

x_train_scaled = scaler.transform(x_train)
x_test_scaled = scaler.transform(x_test)

model = models.Sequential()
model.add(layers.Dense(8, activation='relu', input_shape=(x_train.shape[1],),
                       kernel_initializer='zeros', bias_initializer='zeros'))
model.add(layers.Dense(16, activation='relu', kernel_initializer='zeros',
                       bias_initializer='zeros'))
model.add(layers.Dropout(0.1))
model.add(layers.Dense(1))

model.compile(optimizer='rmsprop',loss='mse',metrics=['mse'])
history = model.fit(x_train_scaled, y_train, validation_split=0.2, epochs=10)
mse = model.evaluate(x_test_scaled, y_test)[1]
print('MSE without tuning: {}'.format(mse))<jupyter_output>MSE without tuning: 610.3947143554688<jupyter_text>Building the Hypermodel<jupyter_code>class RegressionHyperModel(HyperModel):
    def __init__(self, input_shape):
        self.input_shape = input_shape

    def build(self, hp):
        model = Sequential()
        model.add(
            layers.Dense(
                units=hp.Int('units', 8, 64, 4, default=8),
                activation=hp.Choice(
                    'dense_activation',
                    values=['relu', 'tanh', 'sigmoid'],
                    default='relu'),
                input_shape=input_shape,
                kernel_initializer='zeros', bias_initializer='zeros'
            )
        )
        
        model.add(
            layers.Dense(
                units=hp.Int('units', 16, 64, 4, default=16),
                activation=hp.Choice(
                    'dense_activation',
                    values=['relu', 'tanh', 'sigmoid'],
                    default='relu'),
                kernel_initializer='zeros', bias_initializer='zeros'
            )
        )
        
        model.add(
            layers.Dropout(
                hp.Float(
                    'dropout',
                    min_value=0.0,
                    max_value=0.1,
                    default=0.005,
                    step=0.01)
            )
        )
        
        model.add(layers.Dense(1, kernel_initializer='zeros', bias_initializer='zeros'))
        
        model.compile(
            optimizer='rmsprop',loss='mse',metrics=['mse']
        )
        
        return model<jupyter_output><empty_output><jupyter_text>Initialize hypermodel<jupyter_code>input_shape = (x_train.shape[1],)
hypermodel = RegressionHyperModel(input_shape)<jupyter_output><empty_output><jupyter_text>Build Random Search Tuner<jupyter_code>tuner_rs = RandomSearch(
            hypermodel,
            objective='mse',
            seed=42,
            max_trials=10,
            executions_per_trial=2, overwrite=True
        )<jupyter_output><empty_output><jupyter_text>Run Random Search<jupyter_code>tuner_rs.search(x_train_scaled, y_train, epochs=10, validation_split=0.2, verbose=0)<jupyter_output>INFO:tensorflow:Oracle triggered exit<jupyter_text>Evaluate Random Search<jupyter_code>best_model = tuner_rs.get_best_models(num_models=1)[0]
mse_rs = best_model.evaluate(x_test_scaled, y_test)[1]
print('Random search MSE: ', mse_rs)<jupyter_output>Random search MSE:  561.71337890625<jupyter_text>Build, Run and Evaluate Hyperband Tuner<jupyter_code>tuner_hb = Hyperband(
            hypermodel,
            max_epochs=5,
            objective='mse',
            seed=42,
            executions_per_trial=2,
            directory='hb'
        )

tuner_hb.search(x_train_scaled, y_train, epochs=10, validation_split=0.2, verbose=0)

best_model = tuner_hb.get_best_models(num_models=1)[0]
mse_hb = best_model.evaluate(x_test_scaled, y_test)[1]
print('Hyperband MSE: ', mse_hb)<jupyter_output>Hyperband MSE:  589.93017578125<jupyter_text>Build, Run and Evaluate Bayesian Optimization Tuner<jupyter_code>tuner_bo = BayesianOptimization(
            hypermodel,
            objective='mse',
            max_trials=10,
            seed=42,
            executions_per_trial=2,
            directory='bo'
        )

tuner_bo.search(x_train_scaled, y_train, epochs=10, validation_split=0.2, verbose=0)

best_model = tuner_bo.get_best_models(num_models=1)[0]
mse_bo = best_model.evaluate(x_test_scaled, y_test)[1]
print('Bayesian Optimization MSE: ', mse_bo)<jupyter_output>Bayesian Optimization MSE:  320.13336181640625
{'fraction_non_alphanumeric': 0.10775295663600526, 'fraction_numerical': 0.030059132720105124, 'mean_word_length': 3.6268996960486324, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '512270', 'license': 'MIT', 'n_tokens_mistral': 2165, 'n_tokens_neox': 2018, 'n_words': 312, 'path': 'Modules/keras_tuner/example_01/Hyperparameter Tuning Demo with Keras Tuner.ipynb', 'repo_name': 'bhishanpdl/Deep_Learning', 'size': '6088'}