-
Notifications
You must be signed in to change notification settings - Fork 0
/
Experiments.py
345 lines (281 loc) · 11.6 KB
/
Experiments.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
import Models
import Features
import Evaluation
import numpy as np
import tensorflow as tf
from sklearn.model_selection import StratifiedKFold
from sklearn.utils import class_weight
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import GridSearchCV
physical_devices = tf.config.experimental.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(physical_devices[0], True)
def ETFIDFD():
import Endava
data = Endava.Endava()
__TFIDFD( data, "ETFIDFD" )
def WTFIDFD():
import Wesleyan
data = Wesleyan.Wesleyan()
__TFIDFD( data, "WTFIDFD" )
def ECVD():
import Endava
data = Endava.Endava()
__CVD( data, "ECVD" )
def WCVD():
import Wesleyan
data = Wesleyan.Wesleyan()
__CVD( data, "WCVD" )
def WGLD():
import Wesleyan
data = Wesleyan.Wesleyan()
filepath = 'glove.42B.300d.txt'
__EmbeddedD( data, 'WGLD', filepath )
def EGLD():
import Endava
data = Endava.Endava()
filepath = 'glove.42B.300d.txt'
__EmbeddedD( data, 'EGLD', filepath )
def WW2VD():
import Wesleyan
data = Wesleyan.Wesleyan()
filepath = 'enwiki_20180420_300d.txt'
__EmbeddedD( data, 'WW2VD', filepath )
def WW2VCNN():
import Wesleyan
data = Wesleyan.Wesleyan()
filepath = 'enwiki_20180420_300d.txt'
__EmbeddedCNN( data, 'WW2VCNN', filepath )
def EW2VCNN():
import Endava
data = Endava.Endava()
filepath = 'enwiki_20180420_300d.txt'
__EmbeddedCNN( data, 'EW2VCNN', filepath )
def WCVCNN():
import Wesleyan
data = Wesleyan.Wesleyan()
filepath = None
__EmbeddedCNN( data, 'WCVCNN', filepath )
def ECVCNN():
import Endava
data = Endava.Endava()
filepath = None
__EmbeddedCNN( data, 'ECVCNN', filepath )
def EGLCNN():
import Endava
data = Endava.Endava()
filepath = 'glove.42B.300d.txt'
__EmbeddedCNN( data, 'EGLCNN', filepath )
def EW2VD():
import Endava
data = Endava.Endava()
filepath = 'enwiki_20180420_300d.txt'
__EmbeddedD( data, 'EW2VD', filepath )
def __EmbeddedRNN( data, exp, filepath, network ):
kn = Evaluation.Evaluator()
X,Y,y_raw = Features.getSamples( kn, data )
data.maxWords = 10000
kf = StratifiedKFold( n_splits=10, shuffle=True )
k = 0
for train, test in kf.split( X, y_raw ):
print( "K-Fold: " + str( k + 1 ) );
x_train_raw, x_test_raw = X[train], X[test]
y_train, y_test = Y[train], Y[test]
Models.embedding, x_train, x_test = Features.getEmbedded( x_train_raw, x_test_raw,
y_train, y_test, y_raw, filepath, kn )
batches = [64,218]
neurons = [100,200]
dropouts = [2,3]
param_grid = dict( batch_size=batches, neuron=neurons, dropout=dropouts, output_size=[len(y_train[0])] )
model = None
if network == 'lstm':
model = KerasClassifier(build_fn=Models.create_lstm_model, epochs=30, verbose=2)
else:
model = KerasClassifier(build_fn=Models.create_gru_model, epochs=30, verbose=2)
y_ints = [y.argmax() for y in y_train]
cweights = class_weight.compute_class_weight( 'balanced', np.unique( y_ints ), y_ints )
grid = GridSearchCV(estimator=model, param_grid=param_grid)
grid_result = grid.fit(x_train, y_train, class_weight=cweights)
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
model, scores = kn.evaluateModel( x_test, y_test, grid.best_estimator_.model, data, k )
k = k + 1
kn.saveResults( exp )
def __EmbeddedCNN( data, exp, filepath ):
kn = Evaluation.Evaluator()
X,Y,y_raw = Features.getSamples( kn, data )
data.maxWords = 10000
kf = StratifiedKFold( n_splits=10, shuffle=True )
k = 0
for train, test in kf.split( X, y_raw ):
print( "K-Fold: " + str( k + 1 ) );
x_train_raw, x_test_raw = X[train], X[test]
y_train, y_test = Y[train], Y[test]
Models.embedding, x_train, x_test = Features.getEmbedded( x_train_raw, x_test_raw,
y_train, y_test, y_raw, filepath, kn )
batches = [16]
pool_sizes = [3,5]
layer_sizes = [128, 64]
param_grid = dict( batch_size=batches, pool_size=pool_sizes, layer_size=layer_sizes, output_size=[len(y_train[0])] )
model = KerasClassifier(build_fn=Models.create_cnn_model, epochs=15, verbose=2)
y_ints = [y.argmax() for y in y_train]
cweights = class_weight.compute_class_weight( 'balanced', np.unique( y_ints ), y_ints )
grid = GridSearchCV(estimator=model, param_grid=param_grid)
grid_result = grid.fit(x_train, y_train, class_weight=cweights)
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
model, scores = kn.evaluateModel( x_test, y_test, grid.best_estimator_.model, data, k )
k = k + 1
kn.saveResults( exp )
def WStack():
import Wesleyan
data = Wesleyan.Wesleyan()
kn = Evaluation.Evaluator()
exp = "WStack"
filepath = 'enwiki_20180420_300d.txt'
X,Y,y_raw = Features.getSamples( kn, data )
data.maxWords = 10000
kf = StratifiedKFold( n_splits=10, shuffle=True )
k = 0
for train, test in kf.split( X, y_raw ):
print( "K-Fold: " + str( k + 1 ) );
x_train_raw, x_test_raw = X[train], X[test]
y_train, y_test = Y[train], Y[test]
y_ints = [y.argmax() for y in y_train]
cweights = class_weight.compute_class_weight( 'balanced', np.unique( y_ints ), y_ints )
Models.embedding, x_train, x_test = Features.getEmbedded( x_train_raw, x_test_raw,
y_train, y_test, y_raw, filepath, kn )
y_pred_train = None
y_pred_test = None
def do_cnn():
model1 = Models.create_cnn_model( pool_size=3, layer_size=128, output_size=len(y_train[0]) )
model1.fit(x_train, y_train, epochs=15, verbose=2, batch_size=32, class_weight=cweights)
y_pred_train = model1.predict( x_train, verbose=0 )
y_pred_test = model1.predict( x_test, verbose=0 )
model1 = None
do_cnn()
model2 = Models.create_ann_model( dropout=3, denseSize=512, output_length=len(y_train[0]) )
model2.fit(x_train, y_train, batch_size=64, verbose=2, epochs=30, class_weight=cweights)
y_pred_train2 = model2.predict( x_train, verbose=0 )
y_pred_test2 = model2.predict( x_test, verbose=0 )
model2 = None
x_train, x_test = Features.getCVvectors( x_train_raw, x_test_raw, data )
model3 = Models.create_ann_model( batch_size=64, denseSize=1024,
dropout=3, input_length=[len(x_train[0])],
output_length=[len(y_train[0])] )
model3.fit(x_train, y_train, epochs=100, class_weight=cweights)
y_pred_train3 = model3.predict( x_train, verbose=0 )
y_pred_test3 = model3.predict( x_test, verbose=0 )
new_x_train = np.stack( (y_pred_train, y_pred_train2, y_train_3 ), axis=-1)
new_x_test = np.stack( (y_pred_test, y_pred_test2, y_test_3 ), axis=-1)
model = Models.create_stack_model( input_size=len(new_x_train[0]), output_size=len(y_train[0]) )
history = model.fit(new_x_train, y_train, epochs=100, verbose=2, batch_size=128, class_weight=cweights )
model, scores = kn.evaluateModel( new_x_test, y_test, model, data, k )
k = k + 1
kn.saveResults( exp )
def __EmbeddedD( data, exp, filepath ):
kn = Evaluation.Evaluator()
X,Y,y_raw = Features.getSamples( kn, data )
data.maxWords = 10000
kf = StratifiedKFold( n_splits=10, shuffle=True )
k = 0
for train, test in kf.split( X, y_raw ):
print( "K-Fold: " + str( k + 1 ) );
x_train_raw, x_test_raw = X[train], X[test]
y_train, y_test = Y[train], Y[test]
Models.embedding, x_train, x_test = Features.getEmbedded( x_train_raw, x_test_raw,
y_train, y_test, y_raw, filepath, kn )
denseSizes = [512, 256]
batches = [32, 16]
dropouts = [2,3]
param_grid = dict( batch_size=batches, denseSize=denseSizes,
dropout=dropouts,
output_length=[len(y_train[0])] )
model = KerasClassifier(build_fn=Models.create_ann_model, epochs=100, verbose=2)
y_ints = [y.argmax() for y in y_train]
cweights = class_weight.compute_class_weight( 'balanced', np.unique( y_ints ), y_ints )
grid = GridSearchCV(estimator=model, param_grid=param_grid)
grid_result = grid.fit(x_train, y_train, class_weight=cweights)
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
model, scores = kn.evaluateModel( x_test, y_test, grid.best_estimator_.model, data, k )
k = k + 1
kn.saveResults( exp )
def __TFIDFD( data, exp ):
kn = Evaluation.Evaluator()
X,Y,y_raw = Features.getSamples( kn, data )
data.maxWords = 10000
kf = StratifiedKFold( n_splits=10, shuffle=True )
k = 0
for train, test in kf.split( X, y_raw ):
print( "K-Fold: " + str( k + 1 ) );
x_train_raw, x_test_raw = X[train], X[test]
y_train, y_test = Y[train], Y[test]
x_train, x_test = Features.getTFIDFvectors( x_train_raw, x_test_raw, data )
denseSizes = [512,1024]
batches = [64,128]
dropouts = [2,3]
param_grid = dict( batch_size=batches, denseSize=denseSizes,
dropout=dropouts, input_length=[len(x_train[0])],
output_length=[len(y_train[0])] )
model = KerasClassifier(build_fn=Models.create_ann_model, epochs=30, verbose=2)
y_ints = [y.argmax() for y in y_train]
cweights = class_weight.compute_class_weight( 'balanced', np.unique( y_ints ), y_ints )
grid = GridSearchCV(estimator=model, param_grid=param_grid)
grid_result = grid.fit(x_train, y_train, class_weight=cweights)
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
model, scores = kn.evaluateModel( x_test, y_test, grid.best_estimator_.model, data, k )
k = k + 1
kn.saveResults( exp )
def __CVD( data, exp ):
kn = Evaluation.Evaluator()
X,Y,y_raw = Features.getSamples( kn, data )
data.maxWords = 10000
kf = StratifiedKFold( n_splits=10, shuffle=True )
k = 0
for train, test in kf.split( X, y_raw ):
print( "K-Fold: " + str( k + 1 ) );
x_train_raw, x_test_raw = X[train], X[test]
y_train, y_test = Y[train], Y[test]
x_train, x_test = Features.getCVvectors( x_train_raw, x_test_raw, data )
denseSizes = [512,1024]
batches = [64,128]
dropouts = [2,3]
param_grid = dict( batch_size=batches, denseSize=denseSizes,
dropout=dropouts, input_length=[len(x_train[0])],
output_length=[len(y_train[0])] )
model = KerasClassifier(build_fn=Models.create_ann_model, epochs=30, verbose=2)
y_ints = [y.argmax() for y in y_train]
cweights = class_weight.compute_class_weight( 'balanced', np.unique( y_ints ), y_ints )
grid = GridSearchCV(estimator=model, param_grid=param_grid)
grid_result = grid.fit(x_train, y_train, class_weight=cweights)
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
model, scores = kn.evaluateModel( x_test, y_test, grid.best_estimator_.model, data, k )
k = k + 1
kn.saveResults( exp )
# Complete
#EW2VCNN()
EGLCNN()
# TODO
# ECVD()
# ETFIDFD()
# EW2VD()
# EGLD()
# EBERT()
# EGLL()
# EGLG()
# EW2VL()
# EW2VG()
# ECVG()
# ECVL()
# ECVCNN()
# WCVD()
# WTFIDFD()
# WW2VD()
# WGLD()
# WBERT()
# WGLL()
# WGLG()
# WW2VL()
# WW2VG()
# WCVG()
# WCVL()
# WCVCNN()
# WW2VCNN()
# WGLCNN()