-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws_synth_cls_workflow.py
330 lines (260 loc) · 8.38 KB
/
ws_synth_cls_workflow.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
import sys
sys.path.append('../')
#sys.path.append('../snorkel/')
from ranking_utils import *
from mallows import *
from ws_ranking import *
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score
from sklearn.metrics import top_k_accuracy_score
from snorkel.labeling.model import LabelModel
from collections import defaultdict
#from sklearn.neural_network import MLPClassifier
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import pickle
import os
import logging
logging.basicConfig(level=logging.WARNING)
logger = logging.getLogger(__name__)
def generate_dataset(num_pts, num_classes):
"""
Parameters
----------
num_pts: the number of samples
num_classes: the number of classes
Returns
-------
"""
# X dim (num_pts, n_informative (10))
# y dim (num_pts,)
X, y = make_classification(n_samples=num_pts, n_classes=num_classes, n_informative=10,random_state=1)
X_train, X_test, Y_train, Y_test = train_test_split(X, y, stratify=y,random_state=1)
return X_train, Y_train, X_test, Y_test
# obtain ranking using nearest neighbor
def transformToRankedLabels(X, Y_cls):
"""
transform Y_cls to Y_ranks based on distances to each cluster
Parameters
----------
X
Y_cls
Returns
-------
"""
n = len(Y_cls)
# collect clusters
clusters = defaultdict(list)
for i in range(n):
clusters[Y_cls[i]].append(X[i])
classes = list(sorted(clusters.keys()))
logger.debug(classes)
# determine ranks based on the distances to each cluster
Y_ranks = []
for i in range(n):
# calculate distances to the clusters (take min distance)
dists = [(cls, min([np.linalg.norm(X[i]-clusters[cls][j])
for j in range(len(clusters[cls]))])) for cls in classes]
dists = sorted(dists, key=lambda x: x[1])
ranks = Ranking([x[0] for x in dists])
Y_ranks.append(ranks)
return Y_ranks
def generate_ranking_LFs(Y_ranks, thetas):
"""
Generate ranking LFs from Y_ranks and parameter thetas
Parameters
----------
Y_ranks
thetas
Returns
-------
"""
d = len(Y_ranks[0])
r_utils = RankingUtils(d)
lst_mlw = [Mallows(r_utils,theta) for theta in thetas ]
L = [[mlw.sample(y)[0] for mlw in lst_mlw] for y in Y_ranks]
return L
def generate_class_LFs(lst_P, Y_cls):
"""
lst_P has a P matrix for each LF
P[i][j] = prob of outputing label i given the true class is j ; i>0
P[i][j] = prob of outputing Abstain , i = 0
Parameters
----------
lst_P
Y_cls
Returns
-------
"""
L = []
for y in Y_cls:
lfs = []
for P in lst_P:
y_ = np.nonzero(np.random.multinomial(1, P[:,y]))[0][0] - 1
lfs.append(y_)
L.append(lfs)
L = np.array(L)
return L
def get_LF_class_probs(lst_acc,lst_abstains,num_classes):
"""
Parameters
----------
lst_acc
lst_abstains
num_classes
Returns
-------
"""
m = len(lst_acc)
card = num_classes
lst_P = []
for i in range(m):
P = np.zeros((card,card))
for j in range(card):
P[:,j] = (1-lst_acc[i])/(card-1)
P[j][j] = lst_acc[i]
P[:,j] = P[:,j] * (1-lst_abstains[i])
P = np.vstack((np.ones(card)*lst_abstains[i],P))
lst_P.append(P)
return lst_P
def run_ws_workflow(X_train, Y_train, conf):
"""
Parameters
----------
X_train
Y_train
conf
Returns
-------
"""
d = conf['num_classes']
n = len(X_train)
out = {}
r_utils = RankingUtils(d)
Y_train_ranks = transformToRankedLabels(X_train,Y_train)
if conf['ws_method']=='rankings':
thetas = conf['thetas']
print('got Y_train_ranks')
Y_train_class = [Y_train_ranks[i][0] for i in range(len(Y_train_ranks))]
#accuracy_score(Y_train_class,y_train)
L = generate_ranking_LFs(Y_train_ranks, thetas)
print('generated LFs, ', L.shape)
lst_D = np.array([r_utils.get_pair_wise_dists(L[i, :, :]) for i in range(n)])
print(lst_D.shape)
wsr = WeakSupRanking(r_utils)
wsr.train(conf, L)
Y_train_ranks_tilde = wsr.inferRanking(conf, L, lst_D)
out['parameter_error'] = np.linalg.norm(wsr.thetas-thetas)#/len(thetas)
elif conf['ws_method']=='snorkel':
lst_acc = conf['lst_acc']
lst_abstains = conf['lst_abstains']
lst_P = get_LF_class_probs(lst_acc,lst_abstains,d)
L = generate_class_LFs(lst_P,Y_train)
label_model = LabelModel(cardinality=d, verbose=True)
label_model.fit(L_train=L, n_epochs=500, log_freq=100, seed=123)
Y_train_class_tilde,probs = label_model.predict(L, return_probs=True)
Y_train_ranks_tilde = [np.argsort(-prob) for prob in probs]
estimated_lst_P = list(label_model.parameters())[0]
estimated_lst_P = estimated_lst_P.detach().numpy()
lst_P = np.vstack([P[1:] for P in lst_P])
out['parameter_error'] = np.linalg.norm(estimated_lst_P-lst_P)
'''
#label_model_acc = label_model.score(L=L, Y=np.array(Y), tie_break_policy="random")["accuracy"]
#print(label_model_acc)
'''
Y_train_class_tilde = [Y_train_ranks_tilde[i][0] for i in range(n)]
mean_kt = r_utils.mean_kt_distance(Y_train_ranks, Y_train_ranks_tilde)
out['mean_kt'] = mean_kt
accc = evaluate(Y_train_ranks_tilde, Y_train, lst_k=[1, 2, 3])
out['ac-1'] = accc[0]
out['ac-2'] = accc[1]
out['ac-3'] = accc[2]
per_class_acc = confusion_matrix(Y_train, Y_train_class_tilde, normalize='true').diagonal()
out['per_class_acc'] = per_class_acc
y = np.bincount(Y_train)
out['class_count'] = [(i, y[i]) for i in np.nonzero(y)[0]]
return Y_train_class_tilde, out
def evaluate(Y_tilde_ranks, Y_class, lst_k=[1]):
"""
evaluate class c
Parameters
----------
Y_tilde_ranks: estimated ranks
Y_class: class
lst_k: the list of paramter k for top_k_accuracy_score
Returns
-------
"""
Y_tilde_scores = []
for y in Y_tilde_ranks:
y = list(y)
y_tilde_scores = np.zeros(len(y))
j = 1
for i in y:
y_tilde_scores[i] = 1/j
j += 1
Y_tilde_scores.append(y_tilde_scores)
return [top_k_accuracy_score(Y_class, Y_tilde_scores, k=k) for k in lst_k]
def estimate_theta_acc(theta, d):
"""
estimate the acurracy of parameter theta
Parameters
----------
theta
d
Returns
-------
"""
r_utils = RankingUtils(d)
mlw = Mallows(r_utils, theta)
y = r_utils.get_random_ranking()
L = np.array([mlw.sample(y)[0] for i in range(4000)])
L1 = np.array([L[i][0] for i in range(len(L))])
L2 = np.array([L[i][1] for i in range(len(L))])
L3 = np.array([L[i][2] for i in range(len(L))])
z1 = 1*(L1 == y[0])
z2 = 1*(L2 == y[0])
z3 = 1*(L3 == y[0])
return (np.mean(z1), np.mean(z2), np.mean(z3))
def build_dict_d_theta_mu(lst_d,lst_thetas,save_to_file_name):
"""
build dictionary with d, theta, mu and save it as pickle
Parameters
----------
lst_d
lst_thetas
save_to_file_name
Returns
-------
"""
dict_d_theta_mu = {}
# already exist --> update
if os.path.isfile(save_to_file_name):
dict_d_theta_mu = pickle.load(open(save_to_file_name, 'rb'))
for d in lst_d:
dict_theta_mu = {}
# save d
if d in dict_d_theta_mu:
dict_theta_mu = dict_d_theta_mu[d]
# save theta and accuracy
for theta in lst_thetas:
key_theta = int(theta*100)
if not key_theta in dict_theta_mu:
print('estimating for ', d, theta)
estimated_acc = estimate_theta_acc(theta, d)
dict_theta_mu[key_theta]=estimated_acc
# dump pickle
pickle.dump(dict_d_theta_mu, open(save_to_file_name, 'wb'))
return dict_d_theta_mu
def load_dict_d_theta_mu(load_from_file_name):
"""
help function to load experiment parameters
Parameters
----------
load_from_file_name
Returns
-------
"""
return pickle.load(open(load_from_file_name, 'rb'))