-
Notifications
You must be signed in to change notification settings - Fork 1
/
mr_cls_Transformer.py
332 lines (237 loc) · 11.1 KB
/
mr_cls_Transformer.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
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 6 10:01:15 2020
@author: Venelin Kovatchev
The class for the BILSTM classifier for the "What is on your mind"
COLING2020 paper
The class uses tensor flow BILSTM as core model
Different methods take care of processing the data in a standardized way
"""
import pandas as pd
import numpy as np
import scipy
import nltk
import spacy
import gensim
import glob
import csv
import matplotlib
import matplotlib.pyplot as plt
import sklearn
from sklearn.model_selection import cross_val_score
import sklearn.model_selection
import sklearn.pipeline
import re
from sklearn import svm
from sklearn import *
from sklearn.feature_selection import SelectKBest, VarianceThreshold
from sklearn.feature_selection import chi2
from sklearn.model_selection import KFold
from sklearn.base import BaseEstimator, TransformerMixin
import gensim.models.wrappers.fasttext
from scipy import sparse
import tensorflow_datasets as tfds
import tensorflow as tf
import collections
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from sklearn.model_selection import LeaveOneOut,KFold,train_test_split
from sklearn.utils import shuffle
import ktrain
from ktrain import text
from sklearn.metrics import accuracy_score
# Custom imports
# Some of those functions can probably be incorporated as methods in the class
from mr_generic_scripts import *
class MR_transformer:
def __init__(self, text_cols, age_list, class_names, max_len):
# Initialize the core variables
# The current classifier
self.mr_c = None
# The current text model
self.mr_t = None
# Initialize model variables
self.mr_set_model_vars(text_cols, age_list, class_names, max_len)
# Function that sets model variables
# Input: list of questions, list of ages, size of vocabulary, max len of sentence
# Also includes certain pre-build variables for truncating
# Also includes certain pre-built variables for dataset creation (batch size, shuffle buffer)
def mr_set_model_vars(self, text_cols, age_list, class_names, max_len,
model_name = 'distilbert-base-uncased', batch_size = 6,
l_rate = 8e-5, train_iter = 4):
# List of questions
self.q_list = text_cols
# List of ages
self.age_list = age_list
# Size of the vocabulary
self.class_names = class_names
# Padding length
self.max_len = max_len
# Transformer to use
self.model_name = model_name
# Batch size
self.batch_size = batch_size
# Learning rate
self.l_rate = l_rate
# Number of training iteratioins
self.train_iter = train_iter
# Function that sets evaluation variables
def mr_set_eval_vars(self, eval_q, eval_age, return_err = False):
# Whether or not to perform evaluation by question
self.eval_q = eval_q
# Whether or not to perform evaluation by age
self.eval_age = eval_age
# Whether or not to return wrong predictions
self.return_err = return_err
# Function that trains the classifier
# Input - a train set, and a val set
def mr_train(self, train_df, val_df):
# Reset the model at the start of each training
self.mr_t = text.Transformer(self.model_name, maxlen = self.max_len,
class_names = self.class_names)
# Preprocess the training
train_data = self.mr_t.preprocess_train(train_df["Answer"].values, train_df["Score"].values)
# Preprocess the testing
val_data = self.mr_t.preprocess_test(val_df["Answer"].values, val_df["Score"].values)
# Get the actual classifier
model = self.mr_t.get_classifier()
learner = ktrain.get_learner(model, train_data=train_data, val_data=val_data,
batch_size=self.batch_size)
# Train the model
learner.fit_onecycle(self.l_rate, self.train_iter)
# Print results for validation
learner.validate(class_names=self.mr_t.get_classes())
self.mr_c = ktrain.get_predictor(learner.model, preproc=self.mr_t)
# Function that evaluates the model on a test set
# Input - test set
def mr_test(self, test_df):
# Initialize output vars
acc_scores = []
f1_scores = []
X_test = test_df['Answer'].values
y_test = test_df['Score'].values
print("Testing the model on the test set:")
# Get the actual predictions of the model for the test set
y_pred = self.mr_c.predict(X_test)
# Calculate accuracy
test_acc = accuracy_score(y_test.tolist(), [float(ele) for ele in y_pred])
# Calculate macro F1
macro_score = sklearn.metrics.f1_score(y_test.tolist(),
[float(ele) for ele in y_pred],
average='macro')
print('Test Accuracy: {} \n'.format(round(test_acc,2)))
print('Test Macro F1: {} \n'.format(round(macro_score,2)))
# Add the results to the output
acc_scores.append(round(test_acc,2))
f1_scores.append(round(macro_score,2))
# Test by question (if requested)
# Add the scores to the output
# Otherwise add empty list
if self.eval_q:
qa_scores, qf_scores = self.mr_eval_col(test_df,"Question",self.q_list)
acc_scores.append(qa_scores)
f1_scores.append(qf_scores)
else:
acc_scores.append([])
f1_scores.append([])
# Test by age (if requested)
# Add the scores to the output
# Otherwise add empty list
if self.eval_age:
aa_scores, af_scores = self.mr_eval_col(test_df,"Age",self.age_list)
acc_scores.append(aa_scores)
f1_scores.append(af_scores)
else:
acc_scores.append([])
f1_scores.append([])
return(acc_scores,f1_scores)
# Function that evaluates the model by a specific column
# Can also return the actual wrong predictions
# Input - test set, column, values
def mr_eval_col(self, test_df, col_name, col_vals):
# Initialize output
acc_scores = []
f1_scores = []
# Initialize output for wrong predictions, if needed
if self.return_err:
wrong_pred = []
# Loop through all values
for col_val in col_vals:
# Initialize output for wrong predictions, if needed
if self.return_err:
cur_wrong = []
# Get only the entries for the current value
cur_q = test_df[test_df[col_name] == col_val].copy()
# Convert dataframe to dataset
X_test = cur_q['Answer'].values
y_test = cur_q['Score'].values
print("Evaluating column {} with value {}".format(col_name,col_val))
# Get the actual predictions of the model for the test set
y_pred = self.mr_c.predict(X_test)
# Calculate accuracy
test_acc = accuracy_score(y_test.tolist(), [float(ele) for ele in y_pred])
# Calculate macro F1
macro_score = sklearn.metrics.f1_score(y_test.tolist(),
[float(ele) for ele in y_pred],
average='macro')
print('Accuracy: {} \n'.format(round(test_acc,2)))
print('Macro F1: {} \n'.format(round(macro_score,2)))
# Add the results to the output
acc_scores.append(round(test_acc,2))
f1_scores.append(round(macro_score,2))
if self.return_err:
# Loop through all predictions and keep the incorrect ones
# cur_q["Answer"], y_test, and y_pred are all matched, since they
# are not shuffled (shuffle only applies to the test_dataset)
for c_text,c_gold,c_pred in zip(cur_q["Answer"],y_test.tolist(),
[float(ele) for ele in y_pred]):
if c_pred != c_gold:
cur_wrong.append([c_text,c_gold,c_pred])
wrong_pred.append(cur_wrong)
# Return the output
if self.return_err:
return(acc_scores,f1_scores, wrong_pred)
else:
return(acc_scores, f1_scores)
# Function for a dummy one run on train-test
# Input - full df, ratio for splitting on train/val/test, return errors or not
def mr_one_train_test(self, full_df, test_r, val_r=0):
# Split train and test
train_df, test_df = train_test_split(full_df, test_size = test_r)
# Check if we also need val
if val_r > 0:
train_df, val_df = train_test_split(train_df, test_size = val_r)
else:
# If not, validation is same as test
val_df = test_df
# Train the classifier
self.mr_train(train_df, val_df)
# Test the classifier
return(self.mr_test(test_df))
# Function for a dummy one-run on a provided train-test split
# Input - train_df, test_df, ratio for splitting val
def mr_one_run_pre_split(self,train_df, test_df, val_r = 0):
# Check if we also need val
if val_r > 0:
train_df, val_df = train_test_split(train_df, test_size = val_r)
else:
# If not, validation is same as test
val_df = test_df
# Train the classifier
self.mr_train(train_df, val_df)
# Test the classifier
return(self.mr_test(test_df))
#Function for a dummy 10-fold cross validation
# Input - full df, ratio for splitting on train/val/test, number of runs
def mr_kfold_train_test(self, full_df, val_r=0.25, num_runs=10, r_state = 42):
# Initialize output
all_results = []
# Run k-fold split
kf = KFold(n_splits=num_runs, shuffle=True, random_state = r_state)
# Run different splits
for train_index, test_index in kf.split(full_df):
train_df = full_df.iloc[train_index]
test_df = full_df.iloc[test_index]
cur_acc, cur_f1 = self.mr_one_run_pre_split(train_df, test_df, val_r)
all_results.append((cur_acc, cur_f1))
return(all_results)