-
Notifications
You must be signed in to change notification settings - Fork 3
/
active.py
362 lines (270 loc) · 10.7 KB
/
active.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
import crowd
import util
import models
import scipy
import numpy as np
import pandas as pd
class sim:
"""
simulate active label collection
"""
def __init__(self, train_data, X_train, test_data, X_test, cds, cdv, \
cds_test, k = 50):
"""
cd = crowd data
train_data: have some labels (crowd or expert)
test_data: have no labels
"""
self.train_data = train_data
self.X_train = X_train
self.test_data = test_data
self.X_test = X_test
self.cds = cds
self.cdv = cdv
self.cds_test = cds_test
self.k = k
self.selected = np.zeros((len(test_data,)))
self.res = []
def get_aids(self, indices):
"""
return aids
exclude selected
"""
res = []
for i in range(len(indices)):
j = indices[i]
if self.selected[j] == 1: continue # exclude selected
res.append(self.test_data.articleCount.iloc[j])
return res
def query(self, indices):
"""
indices = indices of articles in test_data to get crowd labels
"""
# keep train_data and test_data, just move crowd labels
# from cds_test to cds
aids = self.get_aids(indices) # aids of selected articles
self.selected[indices] = 1
f1 = map(lambda x: x in aids, self.cds_test.data.aid)
#f0 = map(lambda x: x not in aids, self.cds_test.data.aid)
new_cds_data = pd.concat( [self.cds.data.copy(), \
self.cds_test.data.iloc[f1]])
#new_cds_test_data = self.cds_test.data.iloc[f0]
self.cds = crowd.CD(new_cds_data)
#self.cds_test = crowd.CD(new_cds_test_data)
def run(self, method, n_run = 10):
method.k = self.k
for i in range(n_run):
(ps, pt, pp_t, ids) = method.run()
r = util.get_acc(self.test_data, ps, pt, pp_t)
self.res.append(r)
print i, 'stance_acc, claim_acc, brier_score=', r
self.query(ids)
method.update_cds(self.cds)
###############################################################################
class active_selector:
"""
select article to get stance labels from crowd
"""
def __init__(self, train_data, X_train, test_data, X_test, cds, cdv, k = 50\
, seed = 1):
"""
"""
self.train_data = train_data
self.X_train = X_train
self.test_data = test_data
self.X_test = X_test
self.cds = cds
self.cdv = cdv
self.k = k
self.n_test = len(test_data)
# flag for selected test articles
self.selected = np.zeros((len(test_data)))
self.rs = np.random.RandomState(seed = seed)
self.n_train = len(train_data)
self.n_train_vera = len(train_data.claimCount.unique())
def eval_score_uncer(self, pp_s):
"""
eval. scores by uncertainty sampling
"""
self.scores = []
for i in range(pp_s.shape[0]):
self.scores.append(scipy.stats.entropy(pp_s[i, :]))
def run_method(self):
"""
run the method to predict stance/vera and calculate scores
"""
(self.ps, self.pt, self.pp_s, self.pp_t) = util.baseline_crowd(self.train_data,\
self.X_train, self.test_data, self.X_test, self.cds, self.cdv, True)
self.eval_score_uncer(pp_s)
def run(self):
"""
return predicted stance/vera and selection scores
"""
self.run_method()
self.scores = self.scores * (1 - self.selected)
#self.sorted_score_ids = np.argsort(self.scores)[::-1] # reverse array
#self.selected[self.sorted_score_ids[:self.k]] = 1
self.sl_ids = self.rs.choice(range(self.n_test), size = self.k, replace = False, \
p = util.softmax(self.scores))
self.selected[self.sl_ids] = 1
return (self.ps, self.pt, self.pp_t, self.sl_ids)
def update_cds(self, cds):
self.cds = cds
def cal_scores(self, clf_vera):
"""
score = hc * claim entropy + hr * Reputation + hs * stance entropy
"""
test_mat = self.test_data[['claimCount', 'articleCount',\
'sourceCount']].as_matrix()
self.scores = []
list_claim_en = []
list_rep = []
list_stance_en = []
for i in range(len(test_mat)):
stance_en = scipy.stats.entropy(self.pp_s[i, :])
source = test_mat[i][2] - 1
rep = clf_vera.coef_[:, source]
rep = np.sum(np.abs(rep))
claimCount = test_mat[i][0] - self.n_train_vera - 1
claim_en = scipy.stats.entropy(self.pp_t[claimCount, :])
list_claim_en.append(claim_en)
list_rep.append(rep)
list_stance_en.append(stance_en)
# normalize
list_claim_en = np.asarray(list_claim_en) / np.sum(list_claim_en)
list_rep = np.asarray(list_rep) / np.sum(list_rep)
list_stance_en = np.asarray(list_stance_en) / np.sum(list_stance_en)
# calcuate scores
for i in range(len(test_mat)):
self.scores.append(self.hc * list_claim_en[i] + \
self.hr * list_rep[i] + \
self.hs * list_stance_en[i])
def set_hp(self, hc, hr, hs):
"""
set hyper-params
"""
self.hc = hc
self.hr = hr
self.hs = hs
class selector1(active_selector):
"""
selector baseline
"""
def run_method(self):
(self.ps, self.pt, self.pp_s, self.pp_t, clf_st, clf_vera) = \
util.baseline_crowd(self.train_data, self.X_train, self.test_data,\
self.X_test, self.cds, self.cdv, True)
self.cal_scores(clf_vera)
class selector2(active_selector):
"""
selector by cmv (crowd model w variatinal inference)
"""
def run_method(self):
"""
hc, hr, hs: hyper-params
n_train = # train stances
n_train_vera = # train claims
"""
data_all = pd.concat([self.train_data, self.test_data], \
ignore_index = True)
X = scipy.sparse.vstack((self.X_train, self.X_test))
cmv = models.model_cv(data_all, X, self.cds, self.cdv)
cmv.init_model()
cmv.em(3)
(self.ps, self.pt, self.pp_s, self.pp_t) = cmv.get_res(n_train = self.n_train, \
n_train_vera = self.n_train_vera)
self.cal_scores(cmv.clf_vera)
class selector3(active_selector):
"""
selector by cmv (crowd model w gibbs sampling)
"""
def run_method(self):
"""
hc, hr, hs: hyper-params
n_train = # train stances
n_train_vera = # train claims
"""
data_all = pd.concat([self.train_data, self.test_data], \
ignore_index = True)
X = scipy.sparse.vstack((self.X_train, self.X_test))
cm = models.crowd_model(data_all, X, self.cds, self.cdv)
cm.init_model()
cm.em(3)
(self.ps, self.pt, self.pp_s, self.pp_t) = cm.get_res(n_train = self.n_train, \
n_train_vera = self.n_train_vera)
self.cal_scores(cm.clf_vera)
class experiment:
"""
class for running experiment
"""
def __init__(self, train_data, X_train, test_data, X_test, cds, cdv, \
cds_test, runs = 10):
self.train_data = train_data
self.X_train = X_train
self.test_data = test_data
self.X_test = X_test
self.cds = cds
self.cdv = cdv
self.cds_test = cds_test
self.res = {}
self.runs = runs
def do_exp(self, selector, hc, hr, hs):
"""
do experiments w a configuration
save to self.res
selector 1 = baseline
2 = variational
3 = gibbs
"""
self.ss = []
self.save_res = []
self.run(self.train_data, self.X_train, self.test_data, self.X_test, \
self.cds, self.cdv, self.cds_test, hc = hc, hr = hr, hs = hs,\
selector = selector)
self.res[(selector, hc, hr, hs)] = self.save_res
def run(self, train_data, X_train, test_data, X_test, cds, cdv, cds_test,\
seeds = None, hc = 1, hr = 1, hs = 1, selector = 1):
"""
"""
runs = self.runs
if seeds == None: seeds = range(runs)
for seed in seeds:
S = sim(train_data, X_train, test_data, X_test, cds, cdv, cds_test)
if selector == 1:
s = selector1(train_data, X_train, test_data, X_test, cds, cdv, seed=seed)
elif selector == 2:
s = selector2(train_data, X_train, test_data, X_test, cds, cdv, seed=seed)
elif selector == 3:
s = selector3(train_data, X_train, test_data, X_test, cds, cdv, seed=seed)
else:
raise "no such selector"
s.set_hp(hc, hr, hs)
S.run(s)
self.save_res.append(S.res)
#self.save_sim.append(S)
#self.save_sel.append(s)
def take_res(res, p):
x = []
for r in res:
x.append(zip(*r)[p])
return x
def plot_curves(saves, conds = ['Baseline', 'Ours'], time = None, xlab = '', ylab = '',\
save_name = 'abc.png', xl=None):
#import matplotlib
#matplotlib.rcParams['pdf.fonttype'] = 3
import seaborn as sns
sns.plt.figure(figsize=(8,6))
sns.plt.xlabel(xlab, fontsize = 18)
sns.plt.ylabel(ylab, fontsize = 18)
#sns.plt.xlim([0, 4000])
#colors = ['red', 'blue', 'black']
colors = sns.color_palette('colorblind')
markers = ['s', 'o', '^', 'v', '<']
if time == None: time = range(len(saves[0][0]))
for i, save in enumerate(saves):
g = sns.tsplot(data= save, condition = conds[i], \
color = colors[i], marker = markers[i], time=time, markersize = 10)
if xl:
g.set(xlim=(0, xl))
sns.plt.legend(loc = 'best', fontsize=16)
sns.plt.savefig(save_name, bbox_inches = 'tight', pad_inches = 0.1)