text_plain_output_35.png application_vnd.jupyter.stderr_output_24.png application_vnd.jupyter.stderr_output_16.png application_vnd.jupyter.stderr_output_52.png text_plain_output_43.png text_plain_output_37.png application_vnd.jupyter.stderr_output_32.png text_plain_output_5.png application_vnd.jupyter.stderr_output_48.png application_vnd.jupyter.stderr_output_2.png text_plain_output_15.png application_vnd.jupyter.stderr_output_18.png text_plain_output_9.png application_vnd.jupyter.stderr_output_38.png application_vnd.jupyter.stderr_output_58.png text_plain_output_31.png application_vnd.jupyter.stderr_output_4.png application_vnd.jupyter.stderr_output_26.png application_vnd.jupyter.stderr_output_6.png text_plain_output_13.png text_plain_output_45.png application_vnd.jupyter.stderr_output_12.png text_plain_output_29.png application_vnd.jupyter.stderr_output_8.png text_plain_output_49.png text_plain_output_27.png text_plain_output_57.png application_vnd.jupyter.stderr_output_10.png text_plain_output_21.png text_plain_output_47.png text_plain_output_25.png application_vnd.jupyter.stderr_output_34.png text_plain_output_3.png application_vnd.jupyter.stderr_output_44.png application_vnd.jupyter.stderr_output_42.png application_vnd.jupyter.stderr_output_60.png text_plain_output_7.png application_vnd.jupyter.stderr_output_30.png text_plain_output_59.png application_vnd.jupyter.stderr_output_28.png application_vnd.jupyter.stderr_output_46.png text_plain_output_41.png text_plain_output_53.png application_vnd.jupyter.stderr_output_20.png text_plain_output_23.png application_vnd.jupyter.stderr_output_36.png application_vnd.jupyter.stderr_output_22.png text_plain_output_51.png application_vnd.jupyter.stderr_output_56.png application_vnd.jupyter.stderr_output_50.png text_plain_output_1.png text_plain_output_33.png text_plain_output_39.png text_plain_output_55.png text_plain_output_19.png text_plain_output_17.png text_plain_output_11.png application_vnd.jupyter.stderr_output_14.png application_vnd.jupyter.stderr_output_54.png text_plain_output_61.png application_vnd.jupyter.stderr_output_40.png
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D

from keras.models import Sequential, Model, load_model

from sklearn.model_selection import GridSearchCV, RandomizedSearchCV

from tensorflow.keras import regularizers

from tensorflow.keras.layers import Conv2D, MaxPool2D, Activation, Dropout, BatchNormalization, LeakyReLU

from tensorflow.keras.layers import Input, Dense, Flatten, GlobalAveragePooling2D, concatenate

from tensorflow.keras.models import Model, Sequential

from tensorflow.keras.optimizers import Adam, RMSprop

from tensorflow.keras.wrappers.scikit_learn import KerasClassifier

y_train_ex = to_categorical(y_train_ex)
y_test_ex = to_categorical(y_test_ex)

def create_model(neurons, dropout_rate, kernel_regularizer, learning_rate):
    input_shape = (X_train_ex.shape[1],)
    model = Sequential()
    model.add(Dense(neurons, activation='relu', input_shape=input_shape))
    model.add(Dropout(dropout_rate))
    model.add(Dense(neurons // 2, activation='relu'))
    model.add(Dropout(dropout_rate))
    model.add(Dense(neurons // 4, activation='relu'))
    model.add(Dropout(dropout_rate))
    model.add(Dense(neurons // 8, activation='relu', kernel_regularizer=regularizers.l2(kernel_regularizer)))
    model.add(Dropout(dropout_rate))
    model.add(Dense(3, activation='softmax'))
    opt = Adam(learning_rate=learning_rate)
    model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
    return model
model = KerasClassifier(build_fn=create_model, verbose=0)
neurons = [64, 128, 256, 512, 1024, 2048]
dropout_rate = [0, 0.25, 0.5, 0.75]
kernel_regularizer = [0.01, 0.001, 0.0001]
learning_rate = [0.01, 0.05, 0.001, 0.005, 0.0001, 0.0005]
batch_size = [16, 32, 64]
epochs = [50, 100, 150, 300, 500, 1000]
param_grid = dict(neurons=neurons, dropout_rate=dropout_rate, kernel_regularizer=kernel_regularizer, learning_rate=learning_rate, batch_size=batch_size, epochs=epochs)
n_iter_search = 50
random_search = RandomizedSearchCV(model, param_distributions=param_grid, n_iter=n_iter_search, cv=5, n_jobs=-1, scoring='accuracy')
random_search.fit(X_train_ex, y_train_ex)
print('Best parameters: ', random_search.best_params_)
print('Best score: ', random_search.best_score_)