-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathrcnn_retrain.py
442 lines (401 loc) · 18.7 KB
/
rcnn_retrain.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
from keras.backend.tensorflow_backend import set_session
import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
set_session(tf.Session(config=config))
import random
random.seed = 42
import pandas as pd
from tensorflow import set_random_seed
set_random_seed(42)
from keras.preprocessing import text, sequence
from keras.callbacks import ModelCheckpoint, Callback
from sklearn.metrics import f1_score, recall_score, precision_score
from keras.layers import *
from classifier_rcnn import TextClassifier
from gensim.models.keyedvectors import KeyedVectors
import pickle
import gc
def getClassification(arr):
arr = list(arr)
if arr.index(max(arr)) == 0:
return -2
elif arr.index(max(arr)) == 1:
return -1
elif arr.index(max(arr)) == 2:
return 0
else:
return 1
class Metrics(Callback):
def on_total_begin(self, logs={}):
self.val_f1s = []
self.val_recalls = []
self.val_precisions = []
def on_epoch_end(self, epoch, logs={}):
val_predict = list(map(getClassification, self.model.predict(self.validation_data[0])))
val_targ = list(map(getClassification, self.validation_data[1]))
_val_f1 = f1_score(val_targ, val_predict, average="macro")
_val_recall = recall_score(val_targ, val_predict, average="macro")
_val_precision = precision_score(val_targ, val_predict, average="macro")
self.val_f1s.append(_val_f1)
self.val_recalls.append(_val_recall)
self.val_precisions.append(_val_precision)
print(_val_f1, _val_precision, _val_recall)
print("max f1")
print(max(self.val_f1s))
return
data = pd.read_csv("preprocess/train_char.csv")
data["content"] = data.apply(lambda x: eval(x[1]), axis=1)
validation = pd.read_csv("preprocess/validation_char.csv")
validation["content"] = validation.apply(lambda x: eval(x[1]), axis=1)
total = data.append(validation)
model_dir = "model_rcnn_char/"
model_dir_retrain = "model_rcnn_char_retrain/"
maxlen = 1
max_features = 20000
batch_size = 128
epochs = 2
tokenizer = text.Tokenizer(num_words=None)
tokenizer.fit_on_texts(data["content"].values)
with open('tokenizer_char.pickle', 'wb') as handle:
pickle.dump(tokenizer, handle, protocol=pickle.HIGHEST_PROTOCOL)
word_index = tokenizer.word_index
w2_model = KeyedVectors.load_word2vec_format("word2vec/chars.vector", binary=True, encoding='utf8',
unicode_errors='ignore')
embeddings_index = {}
embeddings_matrix = np.zeros((len(word_index) + 1, w2_model.vector_size))
word2idx = {"_PAD": 0}
vocab_list = [(k, w2_model.wv[k]) for k, v in w2_model.wv.vocab.items()]
for word, i in word_index.items():
if word in w2_model:
embedding_vector = w2_model[word]
else:
embedding_vector = None
if embedding_vector is not None:
embeddings_matrix[i] = embedding_vector
X_total = total["content"].values
Y_total_ltc = pd.get_dummies(total["location_traffic_convenience"])[[-2, -1, 0, 1]].values
Y_total_ldfbd = pd.get_dummies(total["location_distance_from_business_district"])[[-2, -1, 0, 1]].values
Y_total_letf = pd.get_dummies(total["location_easy_to_find"])[[-2, -1, 0, 1]].values
Y_total_swt = pd.get_dummies(total["service_wait_time"])[[-2, -1, 0, 1]].values
Y_total_swa = pd.get_dummies(total["service_waiters_attitude"])[[-2, -1, 0, 1]].values
Y_total_spc = pd.get_dummies(total["service_parking_convenience"])[[-2, -1, 0, 1]].values
Y_total_ssp = pd.get_dummies(total["service_serving_speed"])[[-2, -1, 0, 1]].values
Y_total_pl = pd.get_dummies(total["price_level"])[[-2, -1, 0, 1]].values
Y_total_pce = pd.get_dummies(total["price_cost_effective"])[[-2, -1, 0, 1]].values
Y_total_pd = pd.get_dummies(total["price_discount"])[[-2, -1, 0, 1]].values
Y_total_ed = pd.get_dummies(total["environment_decoration"])[[-2, -1, 0, 1]].values
Y_total_en = pd.get_dummies(total["environment_noise"])[[-2, -1, 0, 1]].values
Y_total_es = pd.get_dummies(total["environment_space"])[[-2, -1, 0, 1]].values
Y_total_ec = pd.get_dummies(total["environment_cleaness"])[[-2, -1, 0, 1]].values
Y_total_dp = pd.get_dummies(total["dish_portion"])[[-2, -1, 0, 1]].values
Y_total_dt = pd.get_dummies(total["dish_taste"])[[-2, -1, 0, 1]].values
Y_total_dl = pd.get_dummies(total["dish_look"])[[-2, -1, 0, 1]].values
Y_total_dr = pd.get_dummies(total["dish_recommendation"])[[-2, -1, 0, 1]].values
Y_total_ooe = pd.get_dummies(total["others_overall_experience"])[[-2, -1, 0, 1]].values
Y_total_owta = pd.get_dummies(total["others_willing_to_consume_again"])[[-2, -1, 0, 1]].values
X_validation = validation["content"].values
Y_validation_ltc = pd.get_dummies(validation["location_traffic_convenience"])[[-2, -1, 0, 1]].values
Y_validation_ldfbd = pd.get_dummies(validation["location_distance_from_business_district"])[[-2, -1, 0, 1]].values
Y_validation_letf = pd.get_dummies(validation["location_easy_to_find"])[[-2, -1, 0, 1]].values
Y_validation_swt = pd.get_dummies(validation["service_wait_time"])[[-2, -1, 0, 1]].values
Y_validation_swa = pd.get_dummies(validation["service_waiters_attitude"])[[-2, -1, 0, 1]].values
Y_validation_spc = pd.get_dummies(validation["service_parking_convenience"])[[-2, -1, 0, 1]].values
Y_validation_ssp = pd.get_dummies(validation["service_serving_speed"])[[-2, -1, 0, 1]].values
Y_validation_pl = pd.get_dummies(validation["price_level"])[[-2, -1, 0, 1]].values
Y_validation_pce = pd.get_dummies(validation["price_cost_effective"])[[-2, -1, 0, 1]].values
Y_validation_pd = pd.get_dummies(validation["price_discount"])[[-2, -1, 0, 1]].values
Y_validation_ed = pd.get_dummies(validation["environment_decoration"])[[-2, -1, 0, 1]].values
Y_validation_en = pd.get_dummies(validation["environment_noise"])[[-2, -1, 0, 1]].values
Y_validation_es = pd.get_dummies(validation["environment_space"])[[-2, -1, 0, 1]].values
Y_validation_ec = pd.get_dummies(validation["environment_cleaness"])[[-2, -1, 0, 1]].values
Y_validation_dp = pd.get_dummies(validation["dish_portion"])[[-2, -1, 0, 1]].values
Y_validation_dt = pd.get_dummies(validation["dish_taste"])[[-2, -1, 0, 1]].values
Y_validation_dl = pd.get_dummies(validation["dish_look"])[[-2, -1, 0, 1]].values
Y_validation_dr = pd.get_dummies(validation["dish_recommendation"])[[-2, -1, 0, 1]].values
Y_validation_ooe = pd.get_dummies(validation["others_overall_experience"])[[-2, -1, 0, 1]].values
Y_validation_owta = pd.get_dummies(validation["others_willing_to_consume_again"])[[-2, -1, 0, 1]].values
list_tokenized_validation = tokenizer.texts_to_sequences(X_validation)
input_validation = sequence.pad_sequences(list_tokenized_validation, maxlen=maxlen)
list_tokenized_total = tokenizer.texts_to_sequences(X_total)
input_total = sequence.pad_sequences(list_tokenized_total, maxlen=maxlen)
print("model1")
model1 = TextClassifier().model(embeddings_matrix, maxlen, word_index, 4)
file_path = model_dir + "model_ltc.hdf5"
retrain_path = model_dir_retrain + "model_ltc_{epoch:02d}.hdf5"
model1.load_weights(file_path)
checkpoint = ModelCheckpoint(retrain_path, verbose=2, save_weights_only=True)
metrics = Metrics()
callbacks_list = [checkpoint, metrics]
history = model1.fit(input_total, Y_total_ltc, batch_size=batch_size, epochs=epochs,
validation_data=(input_validation, Y_validation_ltc), callbacks=callbacks_list, verbose=2)
del model1
del history
gc.collect()
K.clear_session()
print("model2")
model2 = TextClassifier().model(embeddings_matrix, maxlen, word_index, 4)
file_path = model_dir + "model_ldfbd.hdf5"
retrain_path = model_dir_retrain + "model_ldfbd_{epoch:02d}.hdf5"
model2.load_weights(file_path)
checkpoint = ModelCheckpoint(retrain_path, verbose=2, save_weights_only=True)
metrics = Metrics()
callbacks_list = [checkpoint, metrics]
history = model2.fit(input_total, Y_total_ldfbd, batch_size=batch_size, epochs=epochs,
validation_data=(input_validation, Y_validation_ldfbd), callbacks=callbacks_list, verbose=2)
del model2
del history
gc.collect()
K.clear_session()
print("model3")
model3 = TextClassifier().model(embeddings_matrix, maxlen, word_index, 4)
file_path = model_dir + "model_letf.hdf5"
retrain_path = model_dir_retrain + "model_letf_{epoch:02d}.hdf5"
model3.load_weights(file_path)
checkpoint = ModelCheckpoint(retrain_path, verbose=2, save_weights_only=True)
metrics = Metrics()
callbacks_list = [checkpoint, metrics]
history = model3.fit(input_total, Y_total_letf, batch_size=batch_size, epochs=epochs,
validation_data=(input_validation, Y_validation_letf), callbacks=callbacks_list, verbose=2)
del model3
del history
gc.collect()
K.clear_session()
print("model4")
model4 = TextClassifier().model(embeddings_matrix, maxlen, word_index, 4)
file_path = model_dir + "model_swt.hdf5"
retrain_path = model_dir_retrain + "model_swt_{epoch:02d}.hdf5"
model4.load_weights(file_path)
checkpoint = ModelCheckpoint(retrain_path, verbose=2, save_weights_only=True)
metrics = Metrics()
callbacks_list = [checkpoint, metrics]
history = model4.fit(input_total, Y_total_swt, batch_size=batch_size, epochs=epochs,
validation_data=(input_validation, Y_validation_swt), callbacks=callbacks_list, verbose=2)
del model4
del history
gc.collect()
K.clear_session()
print("model5")
model5 = TextClassifier().model(embeddings_matrix, maxlen, word_index, 4)
file_path = model_dir + "model_swa.hdf5"
retrain_path = model_dir_retrain + "model_swa_{epoch:02d}.hdf5"
model5.load_weights(file_path)
checkpoint = ModelCheckpoint(retrain_path, verbose=2, save_weights_only=True)
metrics = Metrics()
callbacks_list = [checkpoint, metrics]
history = model5.fit(input_total, Y_total_swa, batch_size=batch_size, epochs=epochs,
validation_data=(input_validation, Y_validation_swa), callbacks=callbacks_list, verbose=2)
del model5
del history
gc.collect()
K.clear_session()
print("model6")
model6 = TextClassifier().model(embeddings_matrix, maxlen, word_index, 4)
file_path = model_dir + "model_spc.hdf5"
retrain_path = model_dir_retrain + "model_spc_{epoch:02d}.hdf5"
model6.load_weights(file_path)
checkpoint = ModelCheckpoint(retrain_path, verbose=2, save_weights_only=True)
metrics = Metrics()
callbacks_list = [checkpoint, metrics]
history = model6.fit(input_total, Y_total_spc, batch_size=batch_size, epochs=epochs,
validation_data=(input_validation, Y_validation_spc), callbacks=callbacks_list, verbose=2)
del model6
del history
gc.collect()
K.clear_session()
print("model7")
model7 = TextClassifier().model(embeddings_matrix, maxlen, word_index, 4)
file_path = model_dir + "model_ssp.hdf5"
retrain_path = model_dir_retrain + "model_ssp_{epoch:02d}.hdf5"
model7.load_weights(file_path)
checkpoint = ModelCheckpoint(retrain_path, verbose=2, save_weights_only=True)
metrics = Metrics()
callbacks_list = [checkpoint, metrics]
history = model7.fit(input_total, Y_total_ssp, batch_size=batch_size, epochs=epochs,
validation_data=(input_validation, Y_validation_ssp), callbacks=callbacks_list, verbose=2)
del model7
del history
gc.collect()
K.clear_session()
print("model8")
model8 = TextClassifier().model(embeddings_matrix, maxlen, word_index, 4)
file_path = model_dir + "model_pl.hdf5"
retrain_path = model_dir_retrain + "model_pl_{epoch:02d}.hdf5"
model8.load_weights(file_path)
checkpoint = ModelCheckpoint(retrain_path, verbose=2, save_weights_only=True)
metrics = Metrics()
callbacks_list = [checkpoint, metrics]
history = model8.fit(input_total, Y_total_pl, batch_size=batch_size, epochs=epochs,
validation_data=(input_validation, Y_validation_pl), callbacks=callbacks_list, verbose=2)
del model8
del history
gc.collect()
K.clear_session()
print("model9")
model9 = TextClassifier().model(embeddings_matrix, maxlen, word_index, 4)
file_path = model_dir + "model_pce.hdf5"
retrain_path = model_dir_retrain + "model_pce_{epoch:02d}.hdf5"
model9.load_weights(file_path)
checkpoint = ModelCheckpoint(retrain_path, verbose=2, save_weights_only=True)
metrics = Metrics()
callbacks_list = [checkpoint, metrics]
history = model9.fit(input_total, Y_total_pce, batch_size=batch_size, epochs=epochs,
validation_data=(input_validation, Y_validation_pce), callbacks=callbacks_list, verbose=2)
del model9
del history
gc.collect()
K.clear_session()
print("model10")
model10 = TextClassifier().model(embeddings_matrix, maxlen, word_index, 4)
file_path = model_dir + "model_pd.hdf5"
retrain_path = model_dir_retrain + "model_pd_{epoch:02d}.hdf5"
model10.load_weights(file_path)
checkpoint = ModelCheckpoint(retrain_path, verbose=2, save_weights_only=True)
metrics = Metrics()
callbacks_list = [checkpoint, metrics]
history = model10.fit(input_total, Y_total_pd, batch_size=batch_size, epochs=epochs,
validation_data=(input_validation, Y_validation_pd), callbacks=callbacks_list, verbose=2)
del model10
del history
gc.collect()
K.clear_session()
print("model11")
model11 = TextClassifier().model(embeddings_matrix, maxlen, word_index, 4)
file_path = model_dir + "model_ed.hdf5"
retrain_path = model_dir_retrain + "model_ed_{epoch:02d}.hdf5"
model11.load_weights(file_path)
checkpoint = ModelCheckpoint(retrain_path, verbose=2, save_weights_only=True)
metrics = Metrics()
callbacks_list = [checkpoint, metrics]
history = model11.fit(input_total, Y_total_ed, batch_size=batch_size, epochs=epochs,
validation_data=(input_validation, Y_validation_ed), callbacks=callbacks_list, verbose=2)
del model11
del history
gc.collect()
K.clear_session()
print("model12")
model12 = TextClassifier().model(embeddings_matrix, maxlen, word_index, 4)
file_path = model_dir + "model_en.hdf5"
retrain_path = model_dir_retrain + "model_en_{epoch:02d}.hdf5"
model12.load_weights(file_path)
checkpoint = ModelCheckpoint(retrain_path, verbose=2, save_weights_only=True)
metrics = Metrics()
callbacks_list = [checkpoint, metrics]
history = model12.fit(input_total, Y_total_en, batch_size=batch_size, epochs=epochs,
validation_data=(input_validation, Y_validation_en), callbacks=callbacks_list, verbose=2)
del model12
del history
gc.collect()
K.clear_session()
print("model13")
model13 = TextClassifier().model(embeddings_matrix, maxlen, word_index, 4)
file_path = model_dir + "model_es.hdf5"
retrain_path = model_dir_retrain + "model_es_{epoch:02d}.hdf5"
model13.load_weights(file_path)
checkpoint = ModelCheckpoint(retrain_path, verbose=2, save_weights_only=True)
metrics = Metrics()
callbacks_list = [checkpoint, metrics]
history = model13.fit(input_total, Y_total_es, batch_size=batch_size, epochs=epochs,
validation_data=(input_validation, Y_validation_es), callbacks=callbacks_list, verbose=2)
del model13
del history
gc.collect()
K.clear_session()
print("model14")
model14 = TextClassifier().model(embeddings_matrix, maxlen, word_index, 4)
file_path = model_dir + "model_ec.hdf5"
retrain_path = model_dir_retrain + "model_ec_{epoch:02d}.hdf5"
model14.load_weights(file_path)
checkpoint = ModelCheckpoint(retrain_path, verbose=2, save_weights_only=True)
metrics = Metrics()
callbacks_list = [checkpoint, metrics]
history = model14.fit(input_total, Y_total_ec, batch_size=batch_size, epochs=epochs,
validation_data=(input_validation, Y_validation_ec), callbacks=callbacks_list, verbose=2)
del model14
del history
gc.collect()
K.clear_session()
print("model15")
model15 = TextClassifier().model(embeddings_matrix, maxlen, word_index, 4)
file_path = model_dir + "model_dp.hdf5"
retrain_path = model_dir_retrain + "model_dp_{epoch:02d}.hdf5"
model15.load_weights(file_path)
checkpoint = ModelCheckpoint(retrain_path, verbose=2, save_weights_only=True)
metrics = Metrics()
callbacks_list = [checkpoint, metrics]
history = model15.fit(input_total, Y_total_dp, batch_size=batch_size, epochs=epochs,
validation_data=(input_validation, Y_validation_dp), callbacks=callbacks_list, verbose=2)
del model15
del history
gc.collect()
K.clear_session()
print("model16")
model16 = TextClassifier().model(embeddings_matrix, maxlen, word_index, 4)
file_path = model_dir + "model_dt.hdf5"
retrain_path = model_dir_retrain + "model_dt_{epoch:02d}.hdf5"
model16.load_weights(file_path)
checkpoint = ModelCheckpoint(retrain_path, verbose=2, save_weights_only=True)
metrics = Metrics()
callbacks_list = [checkpoint, metrics]
history = model16.fit(input_total, Y_total_dt, batch_size=batch_size, epochs=epochs,
validation_data=(input_validation, Y_validation_dt), callbacks=callbacks_list, verbose=2)
del model16
del history
gc.collect()
K.clear_session()
print("model17")
model17 = TextClassifier().model(embeddings_matrix, maxlen, word_index, 4)
file_path = model_dir + "model_dl.hdf5"
retrain_path = model_dir_retrain + "model_dl_{epoch:02d}.hdf5"
model17.load_weights(file_path)
checkpoint = ModelCheckpoint(retrain_path, verbose=2, save_weights_only=True)
metrics = Metrics()
callbacks_list = [checkpoint, metrics]
history = model17.fit(input_total, Y_total_dl, batch_size=batch_size, epochs=epochs,
validation_data=(input_validation, Y_validation_dl), callbacks=callbacks_list, verbose=2)
del model17
del history
gc.collect()
K.clear_session()
print("model18")
model18 = TextClassifier().model(embeddings_matrix, maxlen, word_index, 4)
file_path = model_dir + "model_dr.hdf5"
retrain_path = model_dir_retrain + "model_dr_{epoch:02d}.hdf5"
model18.load_weights(file_path)
checkpoint = ModelCheckpoint(retrain_path, verbose=2, save_weights_only=True)
metrics = Metrics()
callbacks_list = [checkpoint, metrics]
history = model18.fit(input_total, Y_total_dr, batch_size=batch_size, epochs=epochs,
validation_data=(input_validation, Y_validation_dr), callbacks=callbacks_list, verbose=2)
del model18
del history
gc.collect()
K.clear_session()
print("model19")
model19 = TextClassifier().model(embeddings_matrix, maxlen, word_index, 4)
file_path = model_dir + "model_ooe.hdf5"
retrain_path = model_dir_retrain + "model_ooe_{epoch:02d}.hdf5"
model19.load_weights(file_path)
checkpoint = ModelCheckpoint(retrain_path, verbose=2, save_weights_only=True)
metrics = Metrics()
callbacks_list = [checkpoint, metrics]
history = model19.fit(input_total, Y_total_ooe, batch_size=batch_size, epochs=epochs,
validation_data=(input_validation, Y_validation_ooe), callbacks=callbacks_list, verbose=2)
del model19
del history
gc.collect()
K.clear_session()
print("model20")
model20 = TextClassifier().model(embeddings_matrix, maxlen, word_index, 4)
file_path = model_dir + "model_owta.hdf5"
retrain_path = model_dir_retrain + "model_owta_{epoch:02d}.hdf5"
model20.load_weights(file_path)
checkpoint = ModelCheckpoint(retrain_path, verbose=2, save_weights_only=True)
metrics = Metrics()
callbacks_list = [checkpoint, metrics]
history = model20.fit(input_total, Y_total_owta, batch_size=batch_size, epochs=epochs,
validation_data=(input_validation, Y_validation_owta), callbacks=callbacks_list, verbose=2)
del model20
del history
gc.collect()
K.clear_session()