-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassification_gridsearch.py
380 lines (263 loc) · 14.2 KB
/
classification_gridsearch.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
"""
This is the script I used to perform the grid search.
"""
import numpy as np
import pickle
from sklearn.model_selection import train_test_split
from sklearn.metrics import precision_recall_fscore_support
from keras.models import Sequential
from keras.layers import Activation, Dropout, Flatten, Dense, BatchNormalization, LSTM, Embedding, Reshape
from keras.layers import Bidirectional
from keras.utils import np_utils
from keras.layers import Conv1D,MaxPooling1D
from sklearn.metrics import accuracy_score
from sklearn.utils.class_weight import compute_class_weight
import itertools
from keras.callbacks import ModelCheckpoint
from keras.callbacks import EarlyStopping
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
"""
This functions reads the processed data empeddings and divides the data into train and test validation set.
"""
def loadInputData():
data=pickle.load(open("processed_data_embed.p","rb"))
x = []
y = []
for data_row in data:
x.append(data_row[0].tolist())
y.append(data_row[1])
x = x[1:]
y = y[1:]
x = np.array(x)
x_dummy = np.reshape(x,(len(x),50,1))
dummy_y = np_utils.to_categorical(y)
X_train, X_test, y_train, y_test = train_test_split(x_dummy, dummy_y, test_size=0.2, random_state=42)
return X_train,X_test,y_train,y_test,y,dummy_y
"""
This function creates the keras model for the input parameters.
"""
def create_model(num_classes,num_lstm_units,X_train_shape,kernel_size):
# create model
model = Sequential()
model.add(Bidirectional(LSTM(units = num_lstm_units ,return_sequences=True),
input_shape=(X_train_shape[1],X_train_shape[2])))
model.add(Conv1D(filters=16, kernel_size=kernel_size, padding='same', activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(Conv1D(filters=32, kernel_size=kernel_size, padding='same', activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(Conv1D(filters=64, kernel_size=kernel_size, padding='same', activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(Dropout(0.3))
model.add(Flatten())
model.add(Dense(150, activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(num_classes,activation = 'softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['categorical_accuracy'])
model.summary()
return model
"""
I need to decide the parametes: I can take either 3x3 or 5x5 or 7x7 as the kernel dimesion, for
lstm i can take 32 or either 64
keep patience at 10 , its more than enought
Parametes i am trying to optimize in the following function are num_lstm_units and kernel_size
"""
def gridSearchLstmKer(X_train,X_test,y_train,y_test,y,dummy_y):
lstm_units = [32,64]
kernels = [3,5,7]
"""constant parameters"""
num_classes = dummy_y.shape[1]
class_weight = compute_class_weight('balanced', np.unique(y), y)
num_classes = dummy_y.shape[1]
input_shape = X_train.shape
grid_search_lst = list(itertools.product(*[lstm_units,kernels]))
batch_size = 100
epochs = 100
for lst,ker in grid_search_lst:
model = create_model(num_classes =num_classes,num_lstm_units = lst,
X_train_shape = input_shape,kernel_size = ker)
E_Stop = EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=0, mode='auto')
checkpointer = ModelCheckpoint(filepath='results/{0}_{1}.weights.best.hdf5'.format(lst,ker),
verbose=1,save_best_only=True)
model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs,
validation_data=(X_test, y_test),class_weight = class_weight,
callbacks=[checkpointer,E_Stop],shuffle = True)
# now lets test things and output the results into the text file.
class_weight_norm = class_weight/np.linalg.norm(class_weight)
f = open('out_inverse_and_mean.txt', 'w')
for lst,ker in grid_search_lst:
f.write('For the model with lstm units = {0} and kernels = {1} \n'.format(lst,ker))
model = create_model(num_classes =num_classes,num_lstm_units = lst,X_train_shape = input_shape,kernel_size = ker)
model.load_weights('results/{0}_{1}.weights.best.hdf5'.format(lst,ker))
score = model.evaluate(X_test,y_test,verbose = 0)
f.write('The total loss is {0} and the average accuracy is {1} \n'.format(score[0],score[1]))
y_pred = np_utils.to_categorical(model.predict_classes(X_test))
precision, recall,fscore,_ = precision_recall_fscore_support(y_test,y_pred)
f.write('The precision is {0} and average precision is {1}\n'.format(precision,precision.mean()))
f.write('The recall is {0} and the average recall is {1}\n'.format(recall,recall.mean()))
f.write('The fscore is {0} , the average fscore is {1}, the inverse weighted fscore is {2}'.format(
fscore,fscore.mean(),np.inner(class_weight_norm,fscore)))
f.write('\n \n \n')
f.close()
"""
This function perform the gridSearch over the batch size
"""
def gridSearchBatchSize(X_train,X_test,y_train,y_test,y,dummy_y,lstm,ker):
num_classes = dummy_y.shape[1]
input_shape = X_train.shape
class_weight = compute_class_weight('balanced', np.unique(y), y)
class_weight_norm = class_weight/np.linalg.norm(class_weight)
batch_size_lst = [64,128,256]
for batch in batch_size_lst:
if Path('results/batch/{0}_{1}_{2}.weights.best.hdf5'.format(lstm,ker,batch)).is_file():
continue
model = create_model(num_classes =num_classes,num_lstm_units = lstm,
X_train_shape = input_shape,kernel_size = ker)
E_Stop = EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=0, mode='auto')
checkpointer = ModelCheckpoint(filepath='results/batch/{0}_{1}_{2}.weights.best.hdf5'.format(lstm,ker,batch),
verbose=1,save_best_only=True)
model.fit(X_train, y_train, batch_size=batch, epochs=epochs,
validation_data=(X_test, y_test),class_weight = class_weight,
callbacks=[checkpointer,E_Stop],shuffle = True)
# Training is done, now let's test all of them in put the results in a file
f = open('out_inverse_and_mean_batch.txt', 'w')
for b in batch_size_lst:
f.write('For the model with lstm units = {0} and kernels = {1} and batch size {2}\n'.format(lstm,ker,b))
model = create_model(num_classes =num_classes,num_lstm_units = lstm,X_train_shape = input_shape,kernel_size = ker)
model.load_weights('results/batch/{0}_{1}_{2}.weights.best.hdf5'.format(lstm,ker,b))
score = model.evaluate(X_test,y_test,verbose = 0)
f.write('The total loss is {0} and the average accuracy is {1} \n'.format(score[0],score[1]))
y_pred = np_utils.to_categorical(model.predict_classes(X_test))
precision, recall,fscore,_ = precision_recall_fscore_support(y_test,y_pred)
f.write('The precision is {0} and average precision is {1}\n'.format(precision,precision.mean()))
f.write('The recall is {0} and the average recall is {1}\n'.format(recall,recall.mean()))
f.write('The fscore is {0} , the average fscore is {1}, the inverse weighted fscore is {2}'.format(
fscore,fscore.mean(),np.inner(class_weight_norm,fscore)))
f.write('\n \n \n')
f.close()
# now evaluating the model with the optimzied parameters and plot the training and testing cureves
def plotTrainTestCurves(X_train,X_test,y_train,y_test,y,dummy_y,lstm,ker,batch)
num_classes = dummy_y.shape[1]
input_shape = X_train.shape
class_weight = compute_class_weight('balanced', np.unique(y), y)
epochs = 100
#batch_size = 128
batch_size = batch
model = create_model(num_classes =num_classes,num_lstm_units = lstm,
X_train_shape = input_shape,kernel_size = ker)
E_Stop = EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=0, mode='auto')
history = model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs,
validation_data=(X_test, y_test),class_weight = class_weight,
callbacks=[E_Stop],shuffle = True)
#print(history.history)
#%%
plt.plot(history.history['categorical_accuracy'])
plt.plot(history.history['val_categorical_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.savefig('results/model_accuracy.png')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.savefig('results/model_loss.png')
plt.show()
"""
This function performs the jittering on the test data to see how the model gets affected by noise
"""
def checkingModelStabality(X_train,X_test,y_train,y_test,y,dummy_y,lstm,ker,batch)
num_classes = dummy_y.shape[1]
input_shape = X_train.shape
class_weight = compute_class_weight('balanced', np.unique(y), y)
class_weight_norm = class_weight/np.linalg.norm(class_weight)
f = open('out_peerturb.txt', 'w')
for frac in [0.05,0.0001,0.001,0.01]:
f.write('Noise fraction added = {0}'.format(frac))
X_test_noised = X_test.reshape(X_test.shape[0],50).copy()
noise_add = np.random.rand(X_test_noised.shape[0],50)*frac
X_test_noised += noise_add
X_test_noised = X_test_noised.reshape(X_test.shape[0],50,1)
model = create_model(num_classes =num_classes,num_lstm_units = lstm,X_train_shape = input_shape,kernel_size = ker)
model.load_weights('results/batch/{0}_{1}_{2}.weights.best.hdf5'.format(lstm,ker,batch))
score = model.evaluate(X_test_noised,y_test,verbose = 0)
f.write('The total loss is {0} and the average accuracy is {1} \n'.format(score[0],score[1]))
y_pred = model.predict_classes(X_test_noised)
y_test_lab = [np.where(r==1)[0][0] for r in y_test]
precision, recall,fscore,_ = precision_recall_fscore_support(y_test_lab,y_pred)
f.write('The precision is {0} and average precision is {1}\n'.format(precision,precision.mean()))
f.write('The recall is {0} and the average recall is {1}\n'.format(recall,recall.mean()))
f.write('The fscore is {0} , the average fscore is {1}, the inverse weighted fscore is {2}'.format(
fscore,fscore.mean(),np.inner(class_weight_norm,fscore)))
f.write('\n \n \n')
f.close()
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.savefig('Confusion_matrix_{0}'.format('normalized' if normalize is True else 'non_normalized'))
# Making the confusion matrix
def makeConfusionMatrix(X_train,X_test,y_train,y_test,y,dummy_y,lstm,ker,batch)
num_classes = dummy_y.shape[1]
input_shape = X_train.shape
class_weight = compute_class_weight('balanced', np.unique(y), y)
class_weight_norm = class_weight/np.linalg.norm(class_weight)
model = create_model(num_classes =num_classes,num_lstm_units = lstm,X_train_shape = input_shape,kernel_size = ker)
model.load_weights('results/batch/{0}_{1}_{2}.weights.best.hdf5'.format(lstm,ker,batch))
y_pred = model.predict_classes(X_test)
y_test_lab = [np.where(r==1)[0][0] for r in y_test]
cnf_mat = confusion_matrix(y_test_lab, y_pred)
classes = ["unrelated","discuss","agree","disagree"]
plt.figure()
plot_confusion_matrix(cnf_mat, classes=classes,title='Confusion matrix, without normalization')
# Plot normalized confusion matrix
plt.figure()
plot_confusion_matrix(cnf_mat, classes=classes, normalize=True,title='Normalized confusion matrix')
if __name__ == '__main__':
#first thing is to load the data and split it into test and train
X_train,X_test,y_train,y_test,y,dummy_y = loadInputData()
# Perform a Grid search on the number of Bi-lstm units and Number of kernels
# The function produces the output file out_inverse_and_mean.txt . View it and decide the parameters
gridSearchLstmKer(X_train,X_test,y_train,y_test,y,dummy_y)
# Now optimize the batch size. Results will be in the txt file : out_inverse_and_mean_batch
lstm = int(input("view the file and decide on the number of Bi-lstm units"))
ker = int(input("view the file and decide on the dimension of the Kernels"))
gridSearchBatchSize(X_train,X_test,y_train,y_test,y,dummy_y,lstm,ker)
batch = int(input("view the file and decide on the batch size"))
# We have now optimzed the Bilstm, kernels and the batch size
# Plot the training curves and testing curves
plotTrainTestCurves(X_train,X_test,y_train,y_test,y,dummy_y,lstm,ker,batch)
# check how stable the model is with respect to the noise
checkingModelStabality(X_train,X_test,y_train,y_test,y,dummy_y,lstm,ker,batch)
# As a final step, make the confusion matrix,
makeConfusionMatrix(X_train,X_test,y_train,y_test,y,dummy_y,lstm,ker,batch)