-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIterativeSetQuery.py
More file actions
313 lines (255 loc) · 7.39 KB
/
IterativeSetQuery.py
File metadata and controls
313 lines (255 loc) · 7.39 KB
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
"""An iterative algorithm for Set Query.
Author: Maxwell Pang (maxwell.amao@gmail.com)
"""
import math
import random
import numpy as np
import time
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
DEBUGGING = False
def debug_print(*args, **kwargs):
if DEBUGGING:
print(*args, **kwargs)
def getH(B):
"""Returns a hash function used in creating phi.
Args:
B: number of buckets.
Returns:
A pair-wise independent hash function mapping integers to range(B).
"""
print("B is %s" % B)
a = random.randint(0, B - 1) * 2 + 1
b = random.randint(0, B - 1)
return lambda x: (a * x + b) % B
def testH():
"""Tests a hash function used in creating phi.
Args:
None.
Returns:
None.
"""
h = getH(2 ** 10)
results = [0] * 1024
for i in range(2 ** 10):
results[h(i)] += 1
total = 0
for i in results:
if i >= 2:
total += 1
assert total <= 2
hprime = getH(2 ** 10)
collisions = 0
for i in range(2 ** 10):
if h(i) == hprime(i):
collisions += 1
assert collisions <= 3
def getSigma(n):
"""Returns a hash function used in creating phi.
Args:
n: length of x.
Returns:
A hash function mapping range(n) to 1 or -1.
"""
k = random.randint(0, n - 1)
return lambda x : (hash(str((x + k) % n)) % 2) * 2 - 1
def testSigma():
"""Tests a hash function used in creating phi.
Args:
None.
Returns:
None.
"""
sigma = getSigma(2 ** 10)
numOnes = 0
for i in range(2 ** 10):
if (sigma(i) == 1):
numOnes += 1
assert numOnes > 2**9 - 64
assert numOnes < 2**9 + 64
# Not used, invalid syntax
def createPhi(h, sigma, n, B):
H = [h(i) for i in range(n)]
G = [sigma(i) for i in range(n)]
phi = np.zeros((n, B))
phi[np.arrange(n), H] = 1
phi *= G
return phi
class Measurement:
def __init__(self, m):
self.y = np.array([0] * m)
self.S = set()
class IterativeSetQuery:
"""Creates a class to set up and solve compressed sensing."""
def __init__(self, eps, k, n):
"""Initializes the parameters.
Args:
eps: A variable relating to error bound.
k: The number of non-zero indices.
n: The length of x.
Returns:
Nothing.
"""
self.c = 20
self.gamma = 1./600
self.blist = []
self.num_blocks = int(math.log(k, 10))
for i in range(1, self.num_blocks + 1):
eps *= 10
self.blist.append(int(self.c * k / eps) + 1)
self.m = 0
for i in self.blist:
self.m += i
self.hlist = [] # Keeps track of all the hash functions
self.sigmalist = [] # Keeps track of all the other hash functions
self.philist = [] # Keeps track of all the arrays made by hash function pairs
for i in range(self.num_blocks):
h = getH(self.blist[i]) # Creates a hash function
self.hlist.append(h)
sigma = getSigma(n)
self.sigmalist.append(sigma)
def measure(self, x, start_block=0):
"""Creates y from x.
Args:
x: A k-sparse vector represented by a dict.
Returns:
The product of phi and x.
"""
y = []
for j in range(start_block, self.num_blocks):
t = [0] * self.blist[j]
for i in x:
t[self.hlist[j](i)] += x[i] * self.sigmalist[j](i)
y.extend(t)
return np.array(y)
def new_measurement(self):
return Measurement(m=self.m)
def update(self, measurement, val, pos):
"""Updates y from a stream of values and positions.
Args:
measurement: the current y and S
val: the value at an index in x
pos: the position of the index in x
Returns:
An updated measurement.
"""
measurement.S.add(pos)
l = 0
for j in range(0, self.num_blocks):
measurement.y[l + self.hlist[j](pos)] += val * self.sigmalist[j](pos)
l += self.blist[j]
return measurement
def query(self, measurement):
"""Finds x from y and the list of non-zero indices.
Args:
y: The product of phi and x.
S: The list of non-zero indices.
Returns:
An approximation for x based on y and S.
"""
y = measurement.y
S = set(measurement.S)
xprime = {}
print("blist=%s" % self.blist)
for j in range(self.num_blocks):
counter = {} # Counts number of collisions for each bucket
hprime = self.hlist[j] # Hash function for this iteration, should be fixed with above
B = self.blist[j]
xhat = {}
removable = []
for i in S:
hashv = hprime(i)
if hashv in counter:
counter[hashv] = False # Has appeared before, means collision
else:
counter[hashv] = True # Appears for the first time
for i in S:
hashv = hprime(i)
if counter.get(hashv, False): # Checks if the bucket doesn't have collisions
xprime[i] = xhat[i] = y[hprime(i)] * self.sigmalist[j](i)
removable.append(i)
for i in removable:
S.remove(i)
y = y[B:] - self.measure(xhat, start_block=j+1)
if len(S) != 0:
print("Uh oh, S is %s" % S)
return xprime
def createRep(self, S):
Sp = list(S)
Sp.sort()
debug_print("Sp is %s" % Sp)
phiArr = []
for i in range(self.m):
phiArr.append([0] * len(Sp))
debug_print("The number of blocks is %s" % self.num_blocks)
debug_print("Phi array looks like %s" % phiArr)
l = 0
for i in range(self.num_blocks):
for j in range(len(Sp)):
s_j = Sp[j]
hashv = self.hlist[i](s_j)
debug_print("%s, %s" % (j, l + hashv))
phiArr[l + hashv][j] = self.sigmalist[i](s_j)
l += self.blist[i]
return phiArr
# How can I make it so that S is both used in constructing x but also in retrieving x; does it have to appear both in the function and class??
def createX(n, S):
"""Creates an x.
Args:
n: The length of x.
S: The list of non-zero indices.
Returns:
A k-sparse vector of length n.
"""
return {i: 1. for i in S}
def makePerm(n, k):
"""Returns a random list of k integers from 0 to n - 1.
Args:
n: The range to choose from.
k: The length of the list.
Returns:
A random list of k integers from 0 to n - 1.
"""
result = np.random.permutation(n)
return set(result[:k])
n = 10000000
k = 1024
S = makePerm(n, k)
x = createX(n, S)
debug_print("X = %s\nS = %s" % (x, S))
isq = IterativeSetQuery(eps=.1, k=k, n=n)
me = isq.new_measurement()
totaltime = 0.
for i in x:
tic = time.perf_counter()
me = isq.update(me, val=x[i], pos=i)
toc = time.perf_counter()
totaltime += toc - tic
print("Average update time = %s us" % (totaltime / k * 1e6))
debug_print("Y = %s" % me.y)
tic = time.perf_counter()
xprime = isq.query(me)
toc = time.perf_counter()
debug_print("Approximate of X = %s" % xprime)
if xprime == x:
print("We succeeded!")
else:
print("We failed.")
print("query time = %s us" % ((toc - tic) * 1e6))
def graph(isq):
phirep = isq.createRep(S)
debug_print("Phi array looks like %s" % phirep)
data = np.array(phirep)
cmap = plt.cm.bwr
norm = plt.Normalize(vmin=data.min(), vmax=data.max())
image = cmap(norm(data))
plt.imsave('isqphi.png', image)
# Notes for next time
# Plot phi, results after each execution
# Try half 1, half -1 instead of all 1
# S could just have intersection with some of the non-zero indices
# Check edge cases
# Measure update time and space
# Make separate documents for inputs and outputs
# Plot the naive implementation and this implementation on a graph about efficiency
# Try to recover the inputs of training data using a neural network