-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_svmrank.py
229 lines (181 loc) · 7.37 KB
/
run_svmrank.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
import argparse
import os
import svmrank
from sklearn import preprocessing
from scipy.stats import *
from sklearn.metrics import ndcg_score
from sentence_transformers import SentenceTransformer
from sklearn.model_selection import train_test_split
import numpy as np
import pandas as pd
from flair.embeddings import TransformerDocumentEmbeddings
from flair.data import Sentence
from transformers import *
from tqdm import *
import warnings
warnings.filterwarnings('ignore')
def get_sbert_embeddings(model_path, data_path, output_emb_path):
data = pd.read_csv(data_path)
model = SentenceTransformer(model_path)
sentence_embeddings = model.encode(data.claim_text)
emb = pd.DataFrame(sentence_embeddings).apply(pd.Series)
emb = emb.add_prefix('feature_')
temp = pd.concat([data, emb], axis=1)
if not os.path.exists(output_emb_path):
os.makedirs(output_emb_path)
temp.to_csv(f'{output_emb_path}')
def get_bert_embeddings(model_path, data_path, output_emb_path):
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModel.from_pretrained(model_path, from_tf=True)
# read dataset
dataset = pd.read_csv(data_path)
# init embedding
embedding = TransformerDocumentEmbeddings(model, fine_tune=False)
embs = []
for i in tqdm_notebook(list(dataset.claim_text)):
sentence = Sentence(i)
# embed the sentence
embedding.embed(sentence)
embs.append(sentence.embedding.cpu().numpy())
features = pd.DataFrame(embs).apply(pd.Series)
features = features.add_prefix('feature_')
features.to_csv(f'{output_emb_path}')
def run_svm_rank(data, state):
CLAIM_IDS = data.claim_id.unique()
train_claim_ids, test_claim_ids = train_test_split(CLAIM_IDS, train_size=0.8, random_state=state)
train = data[data.claim_id.isin(train_claim_ids)]
test = data[data.claim_id.isin(test_claim_ids)]
filter_train = [col for col in train if col.startswith('feature')]
filter_test = [col for col in test if col.startswith('feature')]
train_xs = np.array(train[filter_train])
train_ys = np.array(train.revision_id)
le = preprocessing.LabelEncoder()
train_groups = le.fit_transform(train.claim_id)
test_xs = np.array(test[filter_test])
test_groups = le.fit_transform(test.claim_id)
m = svmrank.Model({'-c': 3})
m.fit(train_xs, train_ys, train_groups)
test['pred'] = m.predict(test_xs, test_groups)
names = 'sbert_' + str(state)
test[['claim_id', 'revision_id', 'pred']].to_csv(names + ".csv")
return run_eval(names, test)
def run_eval(files, test):
print("--Evaluating--")
pearson = []
spearman = []
kendal = []
top_1 = []
ndcg = []
mrr = []
for cur_id in test.claim_id.unique():
temp_data = test[test.claim_id == cur_id].sort_values('revision_id')
pearson.append(pearsonr(temp_data.revision_id, temp_data.pred)[0])
spearman.append(spearmanr(temp_data.revision_id, temp_data.pred)[0])
kendal.append(kendalltau(temp_data.revision_id, temp_data.pred)[0])
top_1.append(1 if (list(temp_data.pred).index(max(temp_data.pred)) + 1) == len(temp_data.pred) else 0)
ndcg.append(ndcg_score([temp_data.revision_id], [temp_data.pred]))
mrr.append(1.0 / (list(reversed(list(temp_data.pred))).index(max(temp_data.pred)) + 1))
res = pd.DataFrame()
res['sess_id'] = test.claim_id.unique()
res['pearson'] = pearson
res['spearman'] = spearman
res['kendal'] = kendal
res['top_1'] = top_1
res['ndcg'] = ndcg
res['mrr'] = mrr
chain_len = pd.DataFrame(test.groupby(['claim_id'])['revision_id'].nunique()).reset_index()
res = pd.merge(chain_len, res, left_on='claim_id', right_on='sess_id')
res['bins'] = pd.cut(res['revision_id'], [0, 2, 3, 4, 5, 6, 30], labels=[1, 2, 3, 4, 5, '6+'])
res['topic'] = files
return res
if __name__ == "__main__":
# Define arguments
parser = argparse.ArgumentParser()
parser.add_argument("--input_data", help="path to csv containing input data (comma separated)", required=True,
type=str)
parser.add_argument("--pretrained_model", help="Model to use to get unlabelled sample weights", type=str,
default='bert-base-cased')
parser.add_argument("--emb_output_file", help="where to save embeddings for future reuse", required=True, type=str)
parser.add_argument("--output_file", help="where to save scores", required=True, type=str)
parser.add_argument("--seed", type=str, help="Random seed", default='401')
parser.add_argument("--exp_setup", type=str, help="random split or cross-category", default='random')
parser.add_argument("--model_type", type=str, help="bert or sbert", default='sbert')
args = parser.parse_args()
INPUT_DATA = args.input_data
EMB_OUTPUT_DIR = args.emb_output_file
MODEL = args.pretrained_model
SEED = int(args.seed)
OUTPUT_FILE = args.output_file
EXP_SETUP = args.exp_setup
MODEL_TYPE = args.model_type
COLUMNS_TO_ITERATE = [
'Children',
'ClimateChange',
'Democracy',
'Economics',
'Education',
'Equality',
'Ethics',
'Europe',
'Gender',
'Government',
'Health',
'Justice',
'Law',
'Philosophy',
'Politics',
'Religion',
'Science',
'Society',
'Technology',
'USA'
]
# generate proper embeddings
if MODEL_TYPE == 'sbert':
get_sbert_embeddings(MODEL, INPUT_DATA, EMB_OUTPUT_DIR)
else:
get_bert_embeddings(MODEL, INPUT_DATA, EMB_OUTPUT_DIR)
dataset = pd.read_csv(INPUT_DATA)
num_queries = len(dataset.claim_id.unique())
features = pd.read_csv(f'{EMB_OUTPUT_DIR}')
features = features.drop(['claim_id', 'revision_id'], axis=1)
temp = pd.concat([dataset.reset_index(drop=True), features.reset_index(drop=True)], axis=1)
if EXP_SETUP == 'cc':
acc = []
mccs = []
results = []
res_list = []
for col in COLUMNS_TO_ITERATE:
print("Starting: " + col)
train = temp[temp[col] == 0]
test = temp[temp[col] == 1]
filter_train = [col for col in train if col.startswith('feature')]
filter_test = [col for col in test if col.startswith('feature')]
train_xs = np.array(train[filter_train])
train_ys = np.array(train.revision_id)
le = preprocessing.LabelEncoder()
train_groups = le.fit_transform(train.claim_id)
test_xs = np.array(test[filter_test])
test_ys = np.array(test.revision_id)
test_groups = le.fit_transform(test.claim_id)
m = svmrank.Model({'-c': 3})
m.fit(train_xs, train_ys, train_groups)
test['pred'] = m.predict(test_xs, test_groups)
names = 'cc' + str(col)
test[['changes_affectedIds_id', 'revision_id', 'pred']].to_csv(names + ".csv")
res_list.append(run_eval(names, test))
full_res = pd.concat(res_list).reset_index(drop=True)
full_res.groupby(['split']).describe().unstack(1).loc[:, ("mean")].to_csv('svmrank_output_cc.txt')
print('Pearson correlation: ', full_res.groupby(['split']).describe()['pearson']['mean'].mean())
print('Spearman correlation: ', full_res.groupby(['split']).describe()['spearman']['mean'].mean())
print('Top-1: ', full_res.groupby(['split']).describe()['top_1']['mean'].mean())
print('MRR: ', np.mean(full_res.groupby(['topic'])['mrr'].sum() / num_queries))
else:
res_list = []
res_list.append(run_svm_rank(temp, SEED))
full_res = pd.concat(res_list).reset_index(drop=True)
full_res.groupby(['split']).describe().unstack(1).loc[:, ("mean")].to_csv('svmrank_output_random.txt')
print('Pearson correlation: ', full_res.groupby(['split']).describe()['pearson']['mean'].mean())
print('Spearman correlation: ', full_res.groupby(['split']).describe()['spearman']['mean'].mean())
print('Top-1: ', full_res.groupby(['split']).describe()['top_1']['mean'].mean())
print('MRR: ', np.mean(full_res.groupby(['topic'])['mrr'].sum() / num_queries))