forked from chenyuhsu/learnedsketch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.py
302 lines (255 loc) · 10.4 KB
/
sketch.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
import numpy as np
def compute_avg_loss(counts, y, y_buckets):
""" Compute the loss of a sketch.
Args:
counts: estimated counts in each bucket, float - [num_buckets]
y: true counts of each item, float - [num_items]
y_bueckets: item -> bucket mapping - [num_items]
Returns:
Estimation error
"""
assert np.sum(counts) == np.sum(y), 'counts do not have all the flows!'
assert len(y) == len(y_buckets)
if len(y) == 0:
return 0 # avoid division of 0
loss = 0
for i in range(len(y)):
loss += np.abs(y[i] - counts[y_buckets[i]]) * y[i]
return loss / np.sum(y)
def random_hash(y, n_buckets):
""" Sketch with a random hash
Args:
y: true counts of each item, float - [num_items]
n_buckets: number of buckets
Returns
counts: estimated counts in each bucket, float - [num_buckets]
loss: estimation error
y_bueckets: item -> bucket mapping - [num_items]
"""
counts = np.zeros(n_buckets)
y_buckets = np.random.choice(np.arange(n_buckets), size=len(y))
for i in range(len(y)):
counts[y_buckets[i]] += y[i]
loss = compute_avg_loss(counts, y, y_buckets)
return counts, loss, y_buckets
def count_min(y, n_buckets, n_hash):
""" Count-Min
Args:
y: true counts of each item, float - [num_items]
n_buckets: number of buckets
n_hash: number of hash functions
Returns:
Estimation error
"""
if len(y) == 0:
return 0 # avoid division of 0
counts_all = np.zeros((n_hash, n_buckets))
y_buckets_all = np.zeros((n_hash, len(y)), dtype=int)
for i in range(n_hash):
counts, _, y_buckets = random_hash(y, n_buckets)
counts_all[i] = counts
y_buckets_all[i] = y_buckets
loss = 0
for i in range(len(y)):
y_est = np.min([counts_all[k, y_buckets_all[k, i]] for k in range(n_hash)])
loss += np.abs(y[i] - y_est) * y[i]
return loss / np.sum(y)
def cutoff_countmin(y, n_buckets, b_cutoff, n_hashes):
""" Learned Count-Min
Args:
y: true counts of each item (sorted, largest first), float - [num_items]
n_buckets: number of total buckets
b_cutoff: number of unique buckets
n_hash: number of hash functions
Returns:
loss_avg: estimation error
space: space usage in bytes
"""
assert b_cutoff <= n_buckets, 'bucket cutoff cannot be greater than n_buckets'
counts = np.zeros(n_buckets)
if len(y) == 0:
return 0 # avoid division of 0
y_buckets = []
for i in range(b_cutoff):
if i >= len(y):
break # more unique buckets than # flows
counts[i] += y[i] # unique bucket for each flow
y_buckets.append(i)
loss_cf = compute_avg_loss(counts[:b_cutoff], y[:b_cutoff], y_buckets) # loss = 0
loss_cm = count_min(y[b_cutoff:], n_buckets - b_cutoff, n_hashes)
loss_avg = (loss_cf * np.sum(y[:b_cutoff]) + loss_cm * np.sum(y[b_cutoff:])) / np.sum(y)
print('\tloss_cf %.2f\tloss_rd %.2f\tloss_avg %.2f' % (loss_cf, loss_cm, loss_avg))
space = b_cutoff * 4 * 2 + (n_buckets - b_cutoff) * n_hashes * 4
return loss_avg, space
def cutoff_countmin_wscore(y, scores, score_cutoff, n_cm_buckets, n_hashes):
""" Learned Count-Min (use predicted scores to identify heavy hitters)
Args:
y: true counts of each item (sorted, largest first), float - [num_items]
scores: predicted scores of each item - [num_items]
score_cutoff: threshold for heavy hitters
n_cm_buckets: number of buckets of Count-Min
n_hashes: number of hash functions
Returns:
loss_avg: estimation error
space: space usage in bytes
"""
if len(y) == 0:
return 0 # avoid division of 0
y_ccm = y[scores > score_cutoff]
y_cm = y[scores <= score_cutoff]
loss_cf = 0 # put y_ccm into cutoff buckets, no loss
loss_cm = count_min(y_cm, n_cm_buckets, n_hashes)
assert len(y_ccm) + len(y_cm) == len(y)
loss_avg = (loss_cf * np.sum(y_ccm) + loss_cm * np.sum(y_cm)) / np.sum(y)
print('\tloss_cf %.2f\tloss_rd %.2f\tloss_avg %.2f' % (loss_cf, loss_cm, loss_avg))
space = len(y_ccm) * 4 * 2 + n_cm_buckets * n_hashes * 4
return loss_avg, space
def cutoff_lookup(x, y, n_cm_buckets, n_hashes, d_lookup, y_cutoff, sketch='CountMin'):
""" Learned Count-Min (use predicted scores to identify heavy hitters)
Args:
x: feature of each item - [num_items]
y: true counts of each item, float - [num_items]
n_cm_buckets: number of buckets of Count-Min
n_hashes: number of hash functions
d_lookup: x[i] -> y[i] look up table
y_cutoff: threshold for heavy hitters
sketch: type of sketch (CountMin or CountSketch)
Returns:
loss_avg: estimation error
space: space usage in bytes
"""
if len(y) == 0:
return 0 # avoid division of 0
y_ccm = []
y_cm = []
for i in range(len(y)):
if x[i] in d_lookup:
if d_lookup[x[i]] > y_cutoff:
y_ccm.append(y[i])
else:
y_cm.append(y[i])
else:
y_cm.append(y[i])
loss_cf = 0 # put y_ccm into cutoff buckets, no loss
if sketch == 'CountMin':
loss_cm = count_min(y_cm, n_cm_buckets, n_hashes)
elif sketch == 'CountSketch':
loss_cm = count_sketch(y_cm, n_cm_buckets, n_hashes)
else:
assert False, "unknown sketch type"
assert len(y_ccm) + len(y_cm) == len(y)
loss_avg = (loss_cf * np.sum(y_ccm) + loss_cm * np.sum(y_cm)) / np.sum(y)
print('\tloss_cf %.2f\tloss_rd %.2f\tloss_avg %.2f' % (loss_cf, loss_cm, loss_avg))
print('\t# uniq', len(y_ccm), '# cm', len(y_cm))
space = len(y_ccm) * 4 * 2 + n_cm_buckets * n_hashes * 4
return loss_avg, space
def random_hash_with_sign(y, n_buckets):
""" Assign items in y into n_buckets, randomly pick a sign for each item
Args:
y: true counts of each item, float - [num_items]
n_buckets: number of buckets
Returns
counts: estimated counts in each bucket, float - [num_buckets]
loss: estimation error
y_bueckets: item -> bucket mapping - [num_items]
"""
counts = np.zeros(n_buckets)
y_buckets = np.random.choice(np.arange(n_buckets), size=len(y))
y_signs = np.random.choice([-1, 1], size=len(y))
for i in range(len(y)):
counts[y_buckets[i]] += (y[i] * y_signs[i])
return counts, y_buckets, y_signs
def count_sketch(y, n_buckets, n_hash):
""" Count-Sketch
Args:
y: true counts of each item, float - [num_items]
n_buckets: number of buckets
n_hash: number of hash functions
Returns:
Estimation error
"""
if len(y) == 0:
return 0 # avoid division of 0
counts_all = np.zeros((n_hash, n_buckets))
y_buckets_all = np.zeros((n_hash, len(y)), dtype=int)
y_signs_all = np.zeros((n_hash, len(y)), dtype=int)
for i in range(n_hash):
counts, y_buckets, y_signs = random_hash_with_sign(y, n_buckets)
counts_all[i] = counts
y_buckets_all[i] = y_buckets
y_signs_all[i] = y_signs
loss = 0
for i in range(len(y)):
y_est = np.median(
[y_signs_all[k, i] * counts_all[k, y_buckets_all[k, i]] for k in range(n_hash)])
loss += np.abs(y[i] - y_est) * y[i]
return loss / np.sum(y)
def cutoff_countsketch(y, n_buckets, b_cutoff, n_hashes):
""" Learned Count-Sketch
Args:
y: true counts of each item (sorted, largest first), float - [num_items]
n_buckets: number of total buckets
b_cutoff: number of unique buckets
n_hash: number of hash functions
Returns:
loss_avg: estimation error
space: space usage in bytes
"""
assert b_cutoff <= n_buckets, 'bucket cutoff cannot be greater than n_buckets'
counts = np.zeros(n_buckets)
if len(y) == 0:
return 0 # avoid division of 0
y_buckets = []
for i in range(b_cutoff):
if i >= len(y):
break # more unique buckets than # flows
counts[i] += y[i] # unique bucket for each flow
y_buckets.append(i)
loss_cf = compute_avg_loss(counts[:b_cutoff], y[:b_cutoff], y_buckets) # loss = 0
loss_cs = count_sketch(y[b_cutoff:], n_buckets - b_cutoff, n_hashes)
loss_avg = (loss_cf * np.sum(y[:b_cutoff]) + loss_cs * np.sum(y[b_cutoff:])) / np.sum(y)
print('\tloss_cf %.2f\tloss_rd %.2f\tloss_avg %.2f' % (loss_cf, loss_cs, loss_avg))
space = b_cutoff * 4 * 2 + (n_buckets - b_cutoff) * n_hashes * 4
return loss_avg, space
def cutoff_countsketch_wscore(y, scores, score_cutoff, n_cs_buckets, n_hashes):
""" Learned Count-Sketch (use predicted scores to identify heavy hitters)
Args:
y: true counts of each item (sorted, largest first), float - [num_items]
scores: predicted scores of each item - [num_items]
score_cutoff: threshold for heavy hitters
n_cs_buckets: number of buckets of Count-Sketch
n_hashes: number of hash functions
Returns:
loss_avg: estimation error
space: space usage in bytes
"""
if len(y) == 0:
return 0 # avoid division of 0
y_ccs = y[scores > score_cutoff]
y_cs = y[scores <= score_cutoff]
loss_cf = 0 # put y_ccs into cutoff buckets, no loss
loss_cs = count_sketch(y_cs, n_cs_buckets, n_hashes)
assert len(y_ccs) + len(y_cs) == len(y)
loss_avg = (loss_cf * np.sum(y_ccs) + loss_cs * np.sum(y_cs)) / np.sum(y)
print('\tloss_cf %.2f\tloss_rd %.2f\tloss_avg %.2f' % (loss_cf, loss_cs, loss_avg))
space = len(y_ccs) * 4 * 2 + n_cs_buckets * n_hashes * 4
return loss_avg, space
def order_y_wkey(y, results, key, n_examples=0):
""" Order items based on the scores in results """
print('loading results from %s' % results)
results = np.load(results)
pred_prob = results[key].squeeze()
if n_examples:
pred_prob = pred_prob[:n_examples]
idx = np.argsort(pred_prob)[::-1]
assert len(idx) == len(y)
return y[idx], pred_prob[idx]
def order_y_wkey_list(y, results_list, key):
""" Order items based on the scores in results """
pred_prob = np.array([])
for results in results_list:
results = np.load(results)
pred_prob = np.concatenate((pred_prob, results[key].squeeze()))
idx = np.argsort(pred_prob)[::-1]
assert len(idx) == len(y)
return y[idx], pred_prob[idx]