forked from marcotcr/anchor-experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anchor_base.py
400 lines (383 loc) · 17.4 KB
/
anchor_base.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
"""Base anchor functions"""
from __future__ import print_function
import numpy as np
import operator
import copy
import sklearn
import collections
def matrix_subset(matrix, n_samples):
if matrix.shape[0] == 0:
return matrix
n_samples = min(matrix.shape[0], n_samples)
return matrix[np.random.choice(matrix.shape[0], n_samples, replace=False)]
class AnchorBaseBeam(object):
def __init__(self):
pass
@staticmethod
def kl_bernoulli(p, q):
p = min(0.9999999999999999, max(0.0000001, p))
q = min(0.9999999999999999, max(0.0000001, q))
return (p * np.log(float(p) / q) + (1 - p) *
np.log(float(1 - p) / (1 - q)))
@staticmethod
def dup_bernoulli(p, level):
lm = p
um = min(min(1, p + np.sqrt(level / 2.)), 1)
for j in range(1, 17):
qm = (um + lm) / 2.
# print 'lm', lm, 'qm', qm, kl_bernoulli(p, qm)
if AnchorBaseBeam.kl_bernoulli(p, qm) > level:
um = qm
else:
lm = qm
return um
@staticmethod
def dlow_bernoulli(p, level):
um = p
lm = max(min(1, p - np.sqrt(level / 2.)), 0)
for j in range(1, 17):
qm = (um + lm) / 2.
# print 'lm', lm, 'qm', qm, kl_bernoulli(p, qm)
if AnchorBaseBeam.kl_bernoulli(p, qm) > level:
lm = qm
else:
um = qm
return lm
@staticmethod
def compute_beta(n_features, t, delta):
alpha = 1.1
k = 405.5
temp = np.log(k * n_features * (t ** alpha) / delta)
return temp + np.log(temp)
@staticmethod
def lucb(sample_fns, initial_stats, epsilon, delta, batch_size, top_n,
verbose=False, verbose_every=1):
# initial_stats must have n_samples, positive
n_features = len(sample_fns)
n_samples = np.array(initial_stats['n_samples'])
positives = np.array(initial_stats['positives'])
ub = np.zeros(n_samples.shape)
lb = np.zeros(n_samples.shape)
for f in np.where(n_samples == 0)[0]:
n_samples[f] += 1
positives[f] += sample_fns[f](1)
if n_features == top_n:
return range(n_features)
means = positives / n_samples
t = 1
def update_bounds(t):
sorted_means = np.argsort(means)
beta = AnchorBaseBeam.compute_beta(n_features, t, delta)
J = sorted_means[-top_n:]
not_J = sorted_means[:-top_n]
for f in not_J:
ub[f] = AnchorBaseBeam.dup_bernoulli(means[f], beta /
n_samples[f])
for f in J:
lb[f] = AnchorBaseBeam.dlow_bernoulli(means[f],
beta / n_samples[f])
ut = not_J[np.argmax(ub[not_J])]
lt = J[np.argmin(lb[J])]
return ut, lt
ut, lt = update_bounds(t)
B = ub[ut] - lb[lt]
verbose_count = 0
while B > epsilon:
verbose_count += 1
if verbose and verbose_count % verbose_every == 0:
print('Best: %d (mean:%.10f, n: %d, lb:%.4f)' %
(lt, means[lt], n_samples[lt], lb[lt]), end=' ')
print('Worst: %d (mean:%.4f, n: %d, ub:%.4f)' %
(ut, means[ut], n_samples[ut], ub[ut]), end=' ')
print('B = %.2f' % B)
n_samples[ut] += batch_size
positives[ut] += sample_fns[ut](batch_size)
means[ut] = positives[ut] / n_samples[ut]
n_samples[lt] += batch_size
positives[lt] += sample_fns[lt](batch_size)
means[lt] = positives[lt] / n_samples[lt]
t += 1
ut, lt = update_bounds(t)
B = ub[ut] - lb[lt]
sorted_means = np.argsort(means)
return sorted_means[-top_n:]
@staticmethod
def make_tuples(previous_best, state):
# alters state, computes support for new tuples
normalize_tuple = lambda x: tuple(sorted(set(x))) # noqa
all_features = range(state['n_features'])
coverage_data = state['coverage_data']
current_idx = state['current_idx']
data = state['data'][:current_idx]
labels = state['labels'][:current_idx]
if len(previous_best) == 0:
tuples = [(x, ) for x in all_features]
for x in tuples:
pres = data[:, x[0]].nonzero()[0]
# NEW
state['t_idx'][x] = set(pres)
state['t_nsamples'][x] = float(len(pres))
state['t_positives'][x] = float(labels[pres].sum())
# NEW
state['t_coverage_idx'][x] = set(
coverage_data[:, x[0]].nonzero()[0])
state['t_coverage'][x] = (
float(len(state['t_coverage_idx'][x])) /
coverage_data.shape[0])
return tuples
new_tuples = set()
for f in all_features:
for t in previous_best:
new_t = normalize_tuple(t + (f, ))
if len(new_t) != len(t) + 1:
continue
if new_t not in new_tuples:
new_tuples.add(new_t)
state['t_coverage_idx'][new_t] = (
state['t_coverage_idx'][t].intersection(
state['t_coverage_idx'][(f,)]))
state['t_coverage'][new_t] = (
float(len(state['t_coverage_idx'][new_t])) /
coverage_data.shape[0])
t_idx = np.array(list(state['t_idx'][t]))
t_data = state['data'][t_idx]
present = np.where(t_data[:, f] == 1)[0]
state['t_idx'][new_t] = set(t_idx[present])
idx_list = list(state['t_idx'][new_t])
state['t_nsamples'][new_t] = float(len(idx_list))
state['t_positives'][new_t] = np.sum(
state['labels'][idx_list])
return list(new_tuples)
@staticmethod
def get_sample_fns(sample_fn, tuples, state):
# each sample fn returns number of positives
sample_fns = []
def complete_sample_fn(t, n):
raw_data, data, labels = sample_fn(list(t), n)
current_idx = state['current_idx']
# idxs = range(state['data'].shape[0], state['data'].shape[0] + n)
idxs = range(current_idx, current_idx + n)
state['t_idx'][t].update(idxs)
state['t_nsamples'][t] += n
state['t_positives'][t] += labels.sum()
state['data'][idxs] = data
state['raw_data'][idxs] = raw_data
state['labels'][idxs] = labels
state['current_idx'] += n
if state['current_idx'] >= state['data'].shape[0] - max(1000, n):
prealloc_size = state['prealloc_size']
current_idx = data.shape[0]
state['data'] = np.vstack(
(state['data'],
np.zeros((prealloc_size, data.shape[1]), data.dtype)))
state['raw_data'] = np.vstack(
(state['raw_data'],
np.zeros((prealloc_size, raw_data.shape[1]),
raw_data.dtype)))
state['labels'] = np.hstack(
(state['labels'],
np.zeros(prealloc_size, labels.dtype)))
# This can be really slow
# state['data'] = np.vstack((state['data'], data))
# state['raw_data'] = np.vstack((state['raw_data'], raw_data))
# state['labels'] = np.hstack((state['labels'], labels))
return labels.sum()
for t in tuples:
sample_fns.append(lambda n, t=t: complete_sample_fn(t, n))
return sample_fns
@staticmethod
def get_initial_statistics(tuples, state):
stats = {
'n_samples': [],
'positives': []
}
for t in tuples:
stats['n_samples'].append(state['t_nsamples'][t])
stats['positives'].append(state['t_positives'][t])
return stats
@staticmethod
def get_anchor_from_tuple(t, state):
# TODO: This is wrong, some of the intermediate anchors may not exist.
anchor = {'feature': [], 'mean': [], 'precision': [],
'coverage': [], 'examples': [], 'all_precision': 0}
anchor['num_preds'] = state['data'].shape[0]
normalize_tuple = lambda x: tuple(sorted(set(x))) # noqa
to_remove = [x for x in t]
current_t = t
while to_remove:
best = -1
best_nsamples = -1
best_tuple = ()
for x in to_remove:
nt = set(current_t)
nt.remove(x)
nt = tuple(nt)
# nt = normalize_tuple(current_t + x)
n_samples = state['t_nsamples'][nt]
if n_samples > best_nsamples:
best_nsamples = n_samples
best = x
best_tuple = nt
to_remove.remove(best)
current_t = normalize_tuple(best_tuple + (best,))
# This is a hack, and I don't know why I would need it.
if state['t_nsamples'][current_t] == 0:
best_mean = (state['t_positives'][t] /
state['t_nsamples'][t])
else:
best_mean = (state['t_positives'][current_t] /
state['t_nsamples'][current_t])
anchor['feature'].insert(0, best)
anchor['mean'].insert(0, best_mean)
anchor['precision'].insert(0, best_mean)
anchor['coverage'].insert(0, state['t_coverage'][current_t])
raw_idx = list(state['t_idx'][current_t])
raw_data = state['raw_data'][raw_idx]
covered_true = (
state['raw_data'][raw_idx][state['labels'][raw_idx] == 1])
covered_false = (
state['raw_data'][raw_idx][state['labels'][raw_idx] == 0])
exs = {}
exs['covered'] = matrix_subset(raw_data, 10)
exs['covered_true'] = matrix_subset(covered_true, 10)
exs['covered_false'] = matrix_subset(covered_false, 10)
exs['uncovered_true'] = np.array([])
exs['uncovered_false'] = np.array([])
anchor['examples'].insert(0, exs)
current_t = best_tuple
return anchor
@staticmethod
def anchor_beam(sample_fn, delta=0.05, epsilon=0.1, batch_size=10,
min_shared_samples=0, desired_confidence=1, beam_size=1,
verbose=False, epsilon_stop=0.05, min_samples_start=0,
max_anchor_size=None, verbose_every=1,
stop_on_first=False, coverage_samples=10000):
anchor = {'feature': [], 'mean': [], 'precision': [],
'coverage': [], 'examples': [], 'all_precision': 0}
_, coverage_data, _ = sample_fn([], coverage_samples, compute_labels=False)
raw_data, data, labels = sample_fn([], max(1, min_samples_start))
mean = labels.mean()
beta = np.log(1. / delta)
lb = AnchorBaseBeam.dlow_bernoulli(mean, beta / data.shape[0])
while mean > desired_confidence and lb < desired_confidence - epsilon:
nraw_data, ndata, nlabels = sample_fn([], batch_size)
data = np.vstack((data, ndata))
raw_data = np.vstack((raw_data, nraw_data))
labels = np.hstack((labels, nlabels))
mean = labels.mean()
lb = AnchorBaseBeam.dlow_bernoulli(mean, beta / data.shape[0])
if lb > desired_confidence:
anchor['num_preds'] = data.shape[0]
anchor['all_precision'] = mean
return anchor
prealloc_size = batch_size * 10000
current_idx = data.shape[0]
data = np.vstack((data, np.zeros((prealloc_size, data.shape[1]),
data.dtype)))
raw_data = np.vstack(
(raw_data, np.zeros((prealloc_size, raw_data.shape[1]),
raw_data.dtype)))
labels = np.hstack((labels, np.zeros(prealloc_size, labels.dtype)))
n_features = data.shape[1]
state = {'t_idx': collections.defaultdict(lambda: set()),
't_nsamples': collections.defaultdict(lambda: 0.),
't_positives': collections.defaultdict(lambda: 0.),
'data': data,
'prealloc_size': prealloc_size,
'raw_data': raw_data,
'labels': labels,
'current_idx': current_idx,
'n_features': n_features,
't_coverage_idx': collections.defaultdict(lambda: set()),
't_coverage': collections.defaultdict(lambda: 0.),
'coverage_data': coverage_data
}
current_size = 1
best_of_size = {0: []}
best_coverage = -1
best_tuple = ()
t = 1
if max_anchor_size is None:
max_anchor_size = n_features
while current_size <= max_anchor_size:
tuples = AnchorBaseBeam.make_tuples(
best_of_size[current_size - 1], state)
tuples = [x for x in tuples
if state['t_coverage'][x] >= best_coverage]
if len(tuples) == 0:
break
sample_fns = AnchorBaseBeam.get_sample_fns(sample_fn, tuples,
state)
initial_stats = AnchorBaseBeam.get_initial_statistics(tuples,
state)
# print tuples, beam_size
chosen_tuples = AnchorBaseBeam.lucb(
sample_fns, initial_stats, epsilon, delta, batch_size,
min(beam_size, len(tuples)),
verbose=verbose, verbose_every=verbose_every)
best_of_size[current_size] = [tuples[x] for x in chosen_tuples]
if verbose:
print('Best of size ', current_size, ':')
# print state['data'].shape[0]
stop_this = False
for i, t in zip(chosen_tuples, best_of_size[current_size]):
# I can choose at most (beam_size - 1) tuples at each step,
# and there are at most n_feature steps
beta = np.log(1. /
(delta / (1 + (beam_size - 1) * n_features)))
# beta = np.log(1. / delta)
# if state['t_nsamples'][t] == 0:
# mean = 1
# else:
mean = state['t_positives'][t] / state['t_nsamples'][t]
lb = AnchorBaseBeam.dlow_bernoulli(
mean, beta / state['t_nsamples'][t])
ub = AnchorBaseBeam.dup_bernoulli(
mean, beta / state['t_nsamples'][t])
coverage = state['t_coverage'][t]
if verbose:
print(i, mean, lb, ub)
while ((mean >= desired_confidence and
lb < desired_confidence - epsilon_stop) or
(mean < desired_confidence and
ub >= desired_confidence + epsilon_stop)):
# print mean, lb, state['t_nsamples'][t]
sample_fns[i](batch_size)
mean = state['t_positives'][t] / state['t_nsamples'][t]
lb = AnchorBaseBeam.dlow_bernoulli(
mean, beta / state['t_nsamples'][t])
ub = AnchorBaseBeam.dup_bernoulli(
mean, beta / state['t_nsamples'][t])
if verbose:
print('%s mean = %.2f lb = %.2f ub = %.2f coverage: %.2f n: %d' % (t, mean, lb, ub, coverage, state['t_nsamples'][t]))
if mean >= desired_confidence and lb > desired_confidence - epsilon_stop:
if verbose:
print('Found eligible anchor ', t, 'Coverage:',
coverage, 'Is best?', coverage > best_coverage)
if coverage > best_coverage:
best_coverage = coverage
best_tuple = t
if best_coverage == 1 or stop_on_first:
stop_this = True
if stop_this:
break
current_size += 1
if best_tuple == ():
# Could not find an anchor, will now choose the highest precision
# amongst the top K from every round
if verbose:
print('Could not find an anchor, now doing best of each size')
tuples = []
for i in range(0, current_size):
tuples.extend(best_of_size[i])
# tuples = best_of_size[current_size - 1]
sample_fns = AnchorBaseBeam.get_sample_fns(sample_fn, tuples,
state)
initial_stats = AnchorBaseBeam.get_initial_statistics(tuples,
state)
# print tuples, beam_size
chosen_tuples = AnchorBaseBeam.lucb(
sample_fns, initial_stats, epsilon, delta, batch_size,
1, verbose=verbose)
best_tuple = tuples[chosen_tuples[0]]
return AnchorBaseBeam.get_anchor_from_tuple(best_tuple, state)