-
Notifications
You must be signed in to change notification settings - Fork 0
/
deep_learning_grid_search.py
206 lines (155 loc) · 6.85 KB
/
deep_learning_grid_search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# -*- coding: utf-8 -*-
"""Deep Learning Grid Search.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1eoSKjy_8u4hxhgl1aq__gMLVb-4WUXlZ
"""
# First Download the dataset from here https://www.kaggle.com/kumargh/pimaindiansdiabetescsv#pima-indians-diabetes.csv
import pandas as pd
import numpy as np
names = ['n_pregnant', 'glucose_concentration', 'blood_pressuer (mm Hg)', 'skin_thickness (mm)', 'serum_insulin (mu U/ml)',
'BMI', 'pedigree_function', 'age', 'class']
data = pd.read_csv('pima-indians-diabetes.csv', names = names)
data.head()
# descrribe the dataset
data.describe()
# no missing data
data.isnull().sum()
# Drop rows with missing values if in case any
data.dropna(inplace=True)
# summarize the number of rows and columns in df
data.describe()
# Convert dataframe to numpy array
dataset = data.values
print(dataset.shape)
# split into input (X) and an output (Y)
X = dataset[:,0:8]
Y = dataset[:, 8].astype(int)
print(X.shape)
print(Y.shape)
print(Y[:5])
# Normalize the data using sklearn StandardScaler
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler().fit(X)
# Transform and display the training data
X_standardized = scaler.transform(X)
data = pd.DataFrame(X_standardized)
data.describe()
# import necessary sklearn and keras packages
from sklearn.model_selection import GridSearchCV, KFold
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from keras.optimizers import Adam
# Start defining the model
def create_model():
# create model
model = Sequential()
model.add(Dense(8, input_dim = 8, kernel_initializer='normal', activation='relu'))
model.add(Dense(4, input_dim = 8, kernel_initializer='normal', activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# compile the model
adam = Adam(lr = 0.01)
model.compile(loss = 'binary_crossentropy', optimizer = adam, metrics = ['accuracy'])
return model
model = create_model()
print(model.summary())
# define a random seed
seed = 6
np.random.seed(6)
# initializing the model with Keras Classifier
model = KerasClassifier(build_fn=create_model,verbose=0)
# define the grid search parameters
batch_size = [10, 20, 30] # number of steps to take while updating the gradient parameters
epochs = [10, 20, 30]
# make a dictionary of the grid search parameters
grid_params = {'batch_size':batch_size,'epochs':epochs}
# build and fit the GridSearchCV
grid = GridSearchCV(estimator=model,param_grid = grid_params,cv=KFold(random_state=seed),verbose=10)
grid_search = grid.fit(X_standardized,Y)
# summarize the results
print("Best: {0}, using {1}".format(grid_search.best_score_, grid_search.best_params_))
# finding mean and standard deviation
means = grid_search.cv_results_['mean_test_score']
stds = grid_search.cv_results_['std_test_score']
params = grid_search.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
print('{0} ({1}) with: {2}'.format(mean, stdev, param))
"""<h1>Grid Search for DropOut and Learning Rate</h1>"""
# Do a grid search for learning rate and dropout rate
# import necessary packages
from keras.layers import Dropout
# Define a random seed
seed = 6
np.random.seed(seed)
# Start defining the model
def create_model(learn_rate, dropout_rate):
# create model
model = Sequential()
model.add(Dense(8, input_dim = 8, kernel_initializer='normal', activation='relu'))
model.add(Dropout(dropout_rate))
model.add(Dense(4, input_dim = 8, kernel_initializer='normal', activation='relu'))
model.add(Dropout(dropout_rate))
model.add(Dense(1, activation='sigmoid'))
# compile the model
adam = Adam(lr = learn_rate)
model.compile(loss = 'binary_crossentropy', optimizer = adam, metrics = ['accuracy'])
return model
# create the model
model = KerasClassifier(build_fn = create_model, epochs = 100, batch_size = 20, verbose = 0)
# define the grid search parameters
learn_rate = [0.001, 0.01, 0.1]
dropout_rate = [0.0, 0.1, 0.2]
# make a dictionary of the grid search parameters
param_grid = dict(learn_rate=learn_rate, dropout_rate=dropout_rate)
# build and fit the GridSearchCV
grid = GridSearchCV(estimator = model, param_grid = param_grid, cv = KFold(random_state=seed), verbose = 10)
grid_results = grid.fit(X_standardized, Y)
# summarize the results
print("Best: {0}, using {1}".format(grid_results.best_score_, grid_results.best_params_))
means = grid_results.cv_results_['mean_test_score']
stds = grid_results.cv_results_['std_test_score']
params = grid_results.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
print('{0} ({1}) with: {2}'.format(mean, stdev, param))
"""<h1>Grid search to optimize kernel initialization and activation functions</h1>"""
# Do a grid search to optimize kernel initialization and activation functions
# import necessary packages
# Define a random seed
seed = 6
np.random.seed(seed)
# Start defining the model
def create_model(activation, init):
# create model
model = Sequential()
model.add(Dense(8, input_dim = 8, kernel_initializer= init, activation= activation))
model.add(Dense(4, input_dim = 8, kernel_initializer= init, activation= activation))
model.add(Dense(1, activation='sigmoid'))
# compile the model
adam = Adam(lr = 0.001)
model.compile(loss = 'binary_crossentropy', optimizer = adam, metrics = ['accuracy'])
return model
# create the model
model = KerasClassifier(build_fn = create_model, epochs = 100, batch_size = 20, verbose = 0)
# define the grid search parameters
activation = ['softmax', 'relu', 'tanh', 'linear'] # most common activations
init = ['uniform', 'normal', 'zero'] #kernel initializer
# make a dictionary of the grid search parameters
param_grid = dict(activation = activation, init = init)
# build and fit the GridSearchCV
grid = GridSearchCV(estimator = model, param_grid = param_grid, cv = KFold(random_state=seed), verbose = 10)
grid_results = grid.fit(X_standardized, Y)
# summarize the results
print("Best: {0}, using {1}".format(grid_results.best_score_, grid_results.best_params_))
means = grid_results.cv_results_['mean_test_score']
stds = grid_results.cv_results_['std_test_score']
params = grid_results.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
print('{0} ({1}) with: {2}'.format(mean, stdev, param))
"""<h4>So After Tuning the Hyperparameters with Grid Search we see that 'batch_size': 30, 'epochs': 10 'dropout_rate': 0.1, 'learn_rate': 0.001 'activation': 'tanh', 'init': 'normal' are the best parameters to train this model on diabetes Dataset</h4>"""
# generate predictions with optimal hyperparameters
y_pred = grid.predict(X_standardized)
# Generate a classification report
from sklearn.metrics import classification_report, accuracy_score
print(accuracy_score(Y, y_pred))
print(classification_report(Y, y_pred))