This repository has been archived by the owner on Dec 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logistic_classifier.py
512 lines (408 loc) · 16.9 KB
/
logistic_classifier.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
from __future__ import annotations
from itertools import combinations
from spacy.util import filter_spans
from spacy.matcher import Matcher
import spacy
import matplotlib.pyplot as plt
import seaborn as sns
from numpy.linalg import norm
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.metrics import confusion_matrix
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score
from torch.utils.tensorboard import SummaryWriter
import argparse
import json
import math
import os
import pickle
import random
import sys
import time
from operator import le
import numpy as np
import pandas as pd
from typing_extensions import final
from utils import asMinutes
from utils import DatasetManager
from utils import evaluate
from utils import timeSince
from utils import train
np.random.seed(0)
random.seed(0)
nlp = spacy.load("en_core_web_sm")
# Load the embeddings of the specified dataset
def load_embeddings(fname1, fname2, flabel):
PATH = "paraphrase/data/"
full_file_path1 = PATH + fname1 + ".pkl"
full_file_path2 = PATH + fname2 + ".pkl"
full_path_label = PATH + flabel + ".pkl"
# Load full dataset with combined NLI pairs
with open(full_file_path1, "rb") as em1:
stored_data_1 = pickle.load(em1)
with open(full_file_path2, "rb") as em2:
stored_data_2 = pickle.load(em2)
with open(full_path_label, "rb") as lbl:
stored_labels = pickle.load(lbl)
input_vectors1 = stored_data_1["embeddings"]
print(type(input_vectors1), input_vectors1.shape) # Shape (9076, 384)
input_vectors2 = stored_data_2["embeddings"]
print(type(input_vectors2), input_vectors2.shape) # Shape (9076, 384)
abs_diff_vectors = np.abs(input_vectors1 - input_vectors2)
print(type(abs_diff_vectors), abs_diff_vectors.shape) # Shape (9076, 384)
# product_of_vectors = np.einsum('ij,ij->i', input_vectors1, input_vectors2)[..., None]
product_of_vectors = input_vectors1 * input_vectors2
print(type(product_of_vectors), product_of_vectors.shape) # Shape (9076, 384)
input_combined_vectors1 = np.concatenate(
(input_vectors1, input_vectors2, abs_diff_vectors, product_of_vectors), axis=1
) # Shape (9076, 1536)
input_combined_vectors2 = np.concatenate(
(input_vectors2, input_vectors1, abs_diff_vectors, product_of_vectors), axis=1
) # Shape (9076, 1536)
input_combined_vectors_all = np.concatenate(
(input_combined_vectors1, input_combined_vectors2), axis=0
) # Shape (18152, 1536)
print(type(input_combined_vectors_all), input_combined_vectors_all.shape)
labels = np.array(stored_labels["labels"])
labels_all = np.concatenate([labels] * 2, axis=0)
print(type(labels_all), labels_all.shape)
X_train, X_test, y_train, y_test = train_test_split(
input_combined_vectors_all,
labels_all,
test_size=0.2,
shuffle=True,
random_state=0,
)
print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)
return X_train, X_test, y_train, y_test
# Get all verbs using a Spacy based function
def get_verbs(input_sentence):
sentence = input_sentence
pattern = [
{"POS": "VERB", "OP": "?"},
{"POS": "ADV", "OP": "*"},
{"POS": "AUX", "OP": "*"},
{"POS": "VERB", "OP": "+"},
]
# instantiate a Matcher instance
matcher = Matcher(nlp.vocab)
matcher.add("Verb phrase", [pattern])
doc = nlp(sentence)
# call the matcher to find matches
matches = matcher(doc)
spans = [doc[start:end] for _, start, end in matches]
return filter_spans(spans)
# Train the model and take 20% of the training set as the dev/val set
def run_model(X_train, X_test, y_train, y_test, out_file_train):
clf = LogisticRegression(
penalty="l2", random_state=0, max_iter=1000, n_jobs=-1
).fit(X_train, y_train)
preds = clf.predict(X_test)
pred_probs = clf.predict_proba(X_test)
# print("Predictions for 100 are", preds[0:100])
# print("Prediction probs for 100 are", pred_probs[0:100])
print("Accuracy is:", clf.score(X_test, y_test))
print("F1score is: ", f1_score(y_test, preds, average=None))
# Form and print confusion matrix, plot heatmap
c_matrix = confusion_matrix(y_test, preds, labels=[0, 1], normalize="true")
print(c_matrix)
df_cm = pd.DataFrame(c_matrix, index=[0, 1], columns=[0, 1])
matrix = sns.heatmap(df_cm, annot=True, cmap="Blues")
# plt.figure()
figure = matrix.get_figure()
SAVE_PATH = "paraphrase/figs/cm_train_" + out_file_train + ".png"
figure.savefig(SAVE_PATH)
plt.close(figure)
# SAVE MODEL FOR FUTURE USE (with training dataset name)
MODEL_PATH = "paraphrase/saved_models/"
filename = MODEL_PATH + out_file_train + ".sav"
pickle.dump(clf, open(filename, "wb"))
return clf
# Run the trained model on the test dataset, which the model has not seen
def evaluate_model(clf, fname1, fname2, flabel, out_file_train, out_file_test):
print(
"Testing on {} dataset, trained on {} dataset".format(
out_file_train, out_file_test
)
)
if out_file_train == "full":
print("Full means MPRC train set + NLI contradiction pairs.........")
PATH = "paraphrase/data/"
full_file_path1 = PATH + fname1 + ".pkl"
full_file_path2 = PATH + fname2 + ".pkl"
full_path_label = PATH + flabel + ".pkl"
# Load test dataset
with open(full_file_path1, "rb") as _em1:
test_data_1 = pickle.load(_em1)
with open(full_file_path2, "rb") as _em2:
test_data_2 = pickle.load(_em2)
with open(full_path_label, "rb") as _lbl:
test_labels = pickle.load(_lbl)
test_vectors1, test_vectors2 = test_data_1["embeddings"], test_data_2["embeddings"]
abs_diff = np.abs(test_vectors1 - test_vectors2)
elem_prod = test_vectors1 * test_vectors2
combined_test = np.concatenate(
(test_vectors1, test_vectors2, abs_diff, elem_prod), axis=1
)
print(combined_test.shape)
t_labels = np.array(test_labels["labels"])
print(t_labels.shape)
print("Metrics for test dataset......")
t_preds = clf.predict(combined_test)
t_pred_probs = clf.predict_proba(combined_test)
print("Predictions for 10 are", t_preds[0:10])
print("Prediction probs for 10 are", t_pred_probs[0:10])
print("Accuracy for test set is:", clf.score(combined_test, t_labels))
print("F1score for test set is: ", f1_score(
t_labels, t_preds, average=None))
ct_matrix = confusion_matrix(t_labels, t_preds, labels=[
0, 1], normalize="true")
print(ct_matrix)
df_cm = pd.DataFrame(ct_matrix, index=[0, 1], columns=[0, 1])
tmatrix = sns.heatmap(df_cm, annot=True, cmap="Blues")
# plt.figure()
figure1 = tmatrix.get_figure()
SAVE_PATH = (
"paraphrase/figs/cm_train_" + out_file_train + "_test_" + out_file_test + ".png"
)
figure1.savefig(SAVE_PATH)
plt.close(figure1)
return clf, combined_test, test_data_1, test_data_2, test_labels
# Identify the verbs in the sentences, get probability scores for pairs, identify indirect speech
def generate_scored_file(
clf,
combined_test,
test_data_1,
test_data_2,
test_labels,
threshold_max,
threshold_min,
out_file_train,
out_file_test,
):
# Get paraphrase pairs with high probability ( >= 95)
df1 = pd.DataFrame(
columns=[
"sent1",
"length1",
"indirect words sent1",
"count of verbs sent1",
"verbs in sent1",
]
)
df2 = pd.DataFrame(
columns=[
"sent2",
"length2",
"indirect words sent2",
"count of verbs sent2",
"verbs in sent2",
"prob_score",
]
)
count1 = 0
count2 = 0
# set threshold to floats
threshold_max = float(threshold_max)
threshold_min = float(threshold_min)
# Check for these words in the sentence pair
indirect_quotes = ["said", "added", "according"]
for item in combined_test:
# print(item.reshape(1,-1).shape) # Shape (1,1536)
pred_item = clf.predict_proba(item.reshape(1, -1))
# print(pred_item.shape) # Shape (1,2)
# Threshold for paraphrase probability
if pred_item.item((0, 1)) >= threshold_max:
# print(item.shape) # Shape (1536,)
new_item = item # preserve shape (1536,)
# print(new_item.shape) # Shape (1, 1536)
new_item = np.hsplit(new_item, 4)
# print(new_item[0].shape, new_item[1].shape) # Shape (384, )
# Retrieve first sentence
for num, vector in enumerate(test_data_1["embeddings"]):
# print(vector.shape) # Shape (384, )
if np.array_equal(vector, new_item[0]):
count1 += 1
# print("yes with {:.2f} prob \t".format(pred_item.item((0,1))), end = '')
# print(test_data_1['sentences'][num])
# Make a dataset with sentence, length and scores
list_of_words1 = test_data_1["sentences"][num].split(" ")
# Get list of verbs
verbs = get_verbs(test_data_1["sentences"][num])
num_words1 = len(list_of_words1)
temp_indirect_list = []
quote_count = 0
# Check for indirect quotes (if more words, make a list of the words )
"""for word in list_of_words1:
if word.lower() == "said" or word.lower() == "according" or word.lower() == "added":
temp_indirect_list.append(word)
quote_count+=1
if quote_count==0:
temp_indirect_list.append("No")"""
# Faster method to do the same thing
overap_words = set(indirect_quotes) & set(
[word.lower().rstrip(".") for word in list_of_words1]
)
if bool(overap_words) == True:
temp_indirect_list = list(overap_words)
else:
temp_indirect_list.append("no")
df1.loc[count1] = (
[test_data_1["sentences"][num]]
+ [num_words1]
+ [temp_indirect_list]
+ [len(verbs)]
+ [verbs]
)
break
# Retrieve second sentence
for num, vector in enumerate(test_data_2["embeddings"]):
# print(vector.shape) # Shape (384, )
if np.array_equal(vector, new_item[1]):
count2 += 1
# print("yes with {:.2f} prob \t".format(pred_item.item((0,1))), end = '')
# print(test_data_2['sentences'][num])
# Make a dataset with sentence, length and scores
list_of_words2 = test_data_2["sentences"][num].split(" ")
# Get list of verbs
verbs = get_verbs(test_data_2["sentences"][num])
num_words2 = len(list_of_words2)
temp_indirect_list = []
quote_count = 0
# Check for indirect quotes (if more words, make a list of the words )
"""for word in list_of_words2:
if word.lower() == "said" or word.lower() == "according" or word.lower() == "added":
temp_indirect_list.append(word)
quote_count+=1
if quote_count==0:
temp_indirect_list.append("No")"""
overap_words = set(indirect_quotes) & set(
[word.lower().rstrip(".") for word in list_of_words2]
)
if bool(overap_words) == True:
temp_indirect_list = list(overap_words)
else:
temp_indirect_list.append("no")
df2.loc[count2] = (
[test_data_2["sentences"][num]]
+ [num_words2]
+ [temp_indirect_list]
+ [len(verbs)]
+ [verbs]
+ [pred_item.item((0, 1))]
)
break
elif pred_item.item((0, 1)) <= threshold_min:
# print(item.shape) # Shape (1536,)
new_item = item # preserve shape (1536,)
# print(new_item.shape) # Shape (1, 1536)
new_item = np.hsplit(new_item, 4)
# print(new_item[0].shape, new_item[1].shape) # Shape (384, )
# Retrieve first sentence
for num, vector in enumerate(test_data_1["embeddings"]):
# print(vector.shape) # Shape (384, )
if np.array_equal(vector, new_item[0]):
print("yes with 5% prob \t", end="")
print(test_data_1["sentences"][num])
break
# Retrieve second sentence
for num, vector in enumerate(test_data_2["embeddings"]):
# print(vector.shape) # Shape (384, )
if np.array_equal(vector, new_item[1]):
print("yes with 5% prob \t", end="")
print(test_data_2["sentences"][num])
break
# Compute Cosine Similarities betweeen corresponding vectors (FAST)
v1 = test_data_1["embeddings"]
v2 = test_data_2["embeddings"]
product_of_vectors = np.einsum("ij,ij->i", v1, v2)[..., None]
normedv1 = (v1 * v1).sum(axis=1) ** 0.5
normedv2 = (v2 * v2).sum(axis=1) ** 0.5
inverse_prod_norms = np.reciprocal(normedv1 * normedv2).reshape(-1, 1)
cosine_similarites = product_of_vectors * inverse_prod_norms
# print(cosine_similarites.shape)
labels = np.array(test_labels["labels"])
print(labels)
df3 = pd.DataFrame(cosine_similarites, columns=["cosine_sim"])
df3.index = np.arange(1, len(df3) + 1)
print(df3.head())
df4 = pd.DataFrame(labels, columns=["true_labels"])
df4.index = np.arange(1, len(df4) + 1)
print(df4.head())
# print(df1.head())
# print(df2.head())
final_df = pd.concat([df1, df2, df3, df4], axis=1)
print(final_df.head())
SAVE_PATH = (
"paraphrase/figs/paraphr_trainset_"
+ out_file_train
+ "_testset_"
+ out_file_test
+ ".csv"
)
final_df.to_csv(SAVE_PATH)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-tr", "--train", help="train dataset specifier")
parser.add_argument("-ev", "--eval", help="eval dataset specifier")
parser.add_argument("-ts", "--test", help="test corpus dataset specifier")
parser.add_argument(
"-th_min", "--threshold_minimum", help="threshold for paraphrase similarity"
)
parser.add_argument(
"-th_max", "--threshold_maximum", help="threshold for paraphrase similarity"
)
args = parser.parse_args()
if args.train == "full":
fname1 = "embeddings_1"
fname2 = "embeddings_2"
flabel = "labels"
elif args.train == "paws":
fname1 = "train_embeddings_paws1"
fname2 = "train_embeddings_paws2"
flabel = "train_labels_paws"
elif args.train == "mprc":
fname1 = "mprc_embeddings_1"
fname2 = "mprc_embeddings_2"
flabel = "mprc_labels"
else:
print("Invalid train dataset")
exit()
# Get the embeddings in the train and eval subsets
X_train, X_test, y_train, y_test = load_embeddings(fname1, fname2, flabel)
# Run the classifier model (logistic regression for now)
classifier = run_model(X_train, X_test, y_train, y_test, args.train)
if args.eval == "mprc":
eval_fname1 = "test_embeddings_1"
eval_fname2 = "test_embeddings_2"
eval_flabel = "test_labels"
elif args.eval == "paws":
eval_fname1 = "test_embeddings_paws1"
eval_fname2 = "test_embeddings_paws2"
eval_flabel = "test_labels_paws"
else:
print("Invalid test dataset")
exit()
if args.test == "corp1":
test_fname = "test_corpus1"
# Evaluate the model on the test dataset
test_classifier, combined_vec, s_vec1, s_vec2, slabels = evaluate_model(
classifier, eval_fname1, eval_fname2, eval_flabel, args.train, args.eval
)
# Generate the .csv file with the scored sentence pairs
generate_scored_file(
test_classifier,
combined_vec,
s_vec1,
s_vec2,
slabels,
args.threshold_maximum,
args.threshold_minimum,
args.train,
args.eval,
)
# Generate pairwise similarities on the test corpus
# pairwise_similarities_on_corpus(classifier, test_fname, args.train, args.test)
if __name__ == "__main__":
main()