# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# >>>>>>>>>>>>>   <p style="font-size:50px"><b> Keras Tuner

# Keras Tuner Introduction ::: https://keras-team.github.io/keras-tuner/

# +
import pandas as pd
from tensorflow import keras
from tensorflow.keras import layers
from kerastuner.tuners import RandomSearch
from sklearn.preprocessing import StandardScaler

# Importing the dataset
dataset = pd.read_csv('Churn_Modelling.csv')
X = dataset.iloc[:, 3:13]
y = dataset.iloc[:, 13]

#Create dummy variables
geography=pd.get_dummies(X["Geography"],drop_first=True)
gender=pd.get_dummies(X['Gender'],drop_first=True)

## Concatenate the Data Frames
X=pd.concat([X,geography,gender],axis=1)

## Drop Unnecessary columns
X=X.drop(['Geography','Gender'],axis=1)

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)

# Sanydardization
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# -

X_train.shape


# +
def build_model(hp):
    model = keras.Sequential()
    for i in range(hp.Int('num_layers', 2, 20)):
        model.add(layers.Dense(units=hp.Int('units_' + str(i),
                                            min_value=32,
                                            max_value=512,
                                            step=32),
                               activation='relu'))
    model.add(layers.Dense(1, activation='sigmoid'))
    model.compile(
        optimizer=keras.optimizers.Adam(
            hp.Choice('learning_rate', [1e-2, 1e-3, 1e-4])),
        loss='binary_crossentropy',
        metrics=['accuracy'])
    return model

 #####################  OR ##############################

def build_model2(hp):
    model = keras.Sequential()
    n_layers = hp.Int('num_layers', 2, 20)
    for i in range(n_layers): # No. of hidden layers
        n_neurons = hp.Int('units_'+str(i) ,min_value=32,max_value=512,step=32) # Number of neurons
        model.add(layers.Dense(units=n_neurons ,activation='relu', kernel_initializer = 'he_uniform'))

    model.add(layers.Dense(1, activation='sigmoid',kernel_initializer = 'glorot_uniform'))

    learning_rate_adam =  hp.Choice('learning_rate', [1e-2, 1e-3, 1e-4])
    optimizer_adam = keras.optimizers.Adam(learning_rate_adam)

    model.compile(optimizer=optimizer_adam,loss='binary_crossentropy',metrics=['accuracy'])
    
    return model



# +

tuner = RandomSearch(
    build_model,
    objective='val_accuracy',  # we can see this in model_history.history.keys(), and when we provide the validation dataset
    max_trials=5, # How many cominations to try
    executions_per_trial=3, # How many time should the model be trainde for each combination, to reduce the variance in the results
    directory='project1',
    project_name='Churn')
# -

# `Note: the purpose of having multiple executions per trial is to reduce results variance and therefore be able to more accurately assess the performance of a model. If you want to get results faster, you could set executions_per_trial=1 (single round of training for each model configuration).`

tuner.search_space_summary()

tuner.search(X_train, y_train,
             epochs=5,
             validation_data=(X_test, y_test))

tuner.get_best_hyperparameters

tuner.get_best_models()



StarcoderdataJupyterScriptsDedupFiltered