-
Notifications
You must be signed in to change notification settings - Fork 0
/
approximate.py
213 lines (202 loc) · 11.9 KB
/
approximate.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
import random
import heapq
import math
import topk
def approximateShapleyInTopK(vectors, evaluationFunction, m, k, j, d, unWrapFunction, algorithm='GeneralPurpose'):
scores = [0 for x in range(d)]
attributes = [x for x in range(d)]
attributeLists = topk.preProcess(vectors, evaluationFunction)
previousSeen = {}
for mi in range(m):
permutation = attributes.copy()
random.shuffle(attributes)
currHash = 0
for position in range(len(permutation)):
prevHash = currHash
currHash = currHash | (1 << permutation[position])
if algorithm == 'Threshold':
if prevHash not in previousSeen:
prev = topk.computeTopKThreshold(vectors, attributeLists, evaluationFunction, permutation[:position], k)
prevScore = 0 if position == 0 else (1 if j in prev else 0)
previousSeen[prevHash] = prevScore
else:
prevScore = previousSeen[prevHash]
if currHash not in previousSeen:
curr = topk.computeTopKThreshold(vectors, attributeLists, evaluationFunction, permutation[:position+1], k)
currScore = 1 if j in curr else 0
previousSeen[currHash] = currScore
else:
currScore = previousSeen[currHash]
else:
if prevHash not in previousSeen:
prevTuples = topk.generateTuples(vectors, evaluationFunction, permutation, position, unWrapFunction)
prevScore = 0 if position == 0 else (1 if topk.computeInTopK(prevTuples, k, j) else 0)
previousSeen[prevHash] = prevScore
else:
prevScore = previousSeen[prevHash]
if currHash not in previousSeen:
currTuples = topk.generateTuples(vectors, evaluationFunction, permutation, position+1, unWrapFunction)
currScore = 1 if topk.computeInTopK(currTuples, k, j) else 0
previousSeen[currHash] = currScore
else:
currScore = previousSeen[currHash]
scores[permutation[position]] = scores[permutation[position]] + (currScore - prevScore)/m
return scores
def approximateShapleyNotInTopK(vectors, evaluationFunction, m, k, j, d, unWrapFunction, algorithm='GeneralPurpose'):
scores = [0 for x in range(d)]
attributes = [x for x in range(d)]
attributeLists = topk.preProcess(vectors, evaluationFunction)
previousSeen = {}
for mi in range(m):
#if mi % 100 == 0:
# print('.')
permutation = attributes.copy()
random.shuffle(attributes)
currHash = 0
for position in range(len(permutation)):
prevHash = currHash
currHash = currHash | (1 << permutation[position])
if algorithm == 'Threshold':
if prevHash not in previousSeen:
prev = topk.computeTopKThreshold(vectors, attributeLists, evaluationFunction, permutation[:position], k)
prevScore = 0 if position == 0 else (1 if not j in prev else 0)
previousSeen[prevHash] = prevScore
else:
prevScore = previousSeen[prevHash]
if currHash not in previousSeen:
curr = topk.computeTopKThreshold(vectors, attributeLists, evaluationFunction, permutation[:position+1], k)
currScore = 1 if not j in curr else 0
previousSeen[currHash] = currScore
else:
currScore = previousSeen[currHash]
else:
if prevHash not in previousSeen:
prevTuples = topk.generateTuples(vectors, evaluationFunction, permutation, position, unWrapFunction)
prevScore = 0 if position == 0 else (1 if not topk.computeInTopK(prevTuples, k, j) else 0)
previousSeen[prevHash] = prevScore
else:
prevScore = previousSeen[prevHash]
if currHash not in previousSeen:
currTuples = topk.generateTuples(vectors, evaluationFunction, permutation, position+1, unWrapFunction)
currScore = 1 if not topk.computeInTopK(currTuples, k, j) else 0
previousSeen[currHash] = currScore
else:
currScore = previousSeen[currHash]
scores[permutation[position]] = scores[permutation[position]] + (currScore - prevScore)/m
return scores
def approximateShapleyTopKLookLikeThis(vectors, evaluationFunction, m, k, d, unWrapFunction, algorithm='GeneralPurpose'):
scores = [0 for x in range(d)]
attributes = [x for x in range(d)]
attributeLists = topk.preProcess(vectors, evaluationFunction)
previousSeen = {}
initialTuples = topk.generateTuples(vectors, evaluationFunction, [x for x in range(d)], d, unWrapFunction)
initialTopK = topk.computeTopK(initialTuples, k)
setInitialTopK = set(initialTopK)
for mi in range(m):
permutation = attributes.copy()
random.shuffle(attributes)
currHash = 0
for position in range(len(permutation)):
prevHash = currHash
currHash = currHash | (1 << permutation[position])
if algorithm == 'Threshold':
if prevHash not in previousSeen:
topKPrev = topk.computeTopKThreshold(vectors, attributeLists, evaluationFunction, permutation[:position], k)
setPrevTopK = set(topKPrev)
IoUPrev = 0 if position == 0 else len(setInitialTopK.intersection(setPrevTopK))/len(setInitialTopK.union(setPrevTopK))
previousSeen[prevHash] = IoUPrev
else:
IoUPrev = previousSeen[prevHash]
if currHash not in previousSeen:
topKCurr = topk.computeTopKThreshold(vectors, attributeLists, evaluationFunction, permutation[:position+1], k)
setCurrTopK = set(topKCurr)
IoUCurr = len(setInitialTopK.intersection(setCurrTopK))/len(setInitialTopK.union(setCurrTopK))
previousSeen[currHash] = IoUCurr
else:
IoUCurr = previousSeen[currHash]
else:
if prevHash not in previousSeen:
prevTuples = topk.generateTuples(vectors, evaluationFunction, permutation, position, unWrapFunction)
topKPrev = topk.computeTopK(prevTuples, k)
setPrevTopK = set(topKPrev)
IoUPrev = 0 if position == 0 else len(setInitialTopK.intersection(setPrevTopK))/len(setInitialTopK.union(setPrevTopK))
previousSeen[prevHash] = IoUPrev
else:
IoUPrev = previousSeen[prevHash]
if currHash not in previousSeen:
currTuples = topk.generateTuples(vectors, evaluationFunction, permutation, position+1, unWrapFunction)
topKCurr = topk.computeTopK(currTuples, k)
setCurrTopK = set(topKCurr)
IoUCurr = len(setInitialTopK.intersection(setCurrTopK))/len(setInitialTopK.union(setCurrTopK))
previousSeen[currHash] = IoUCurr
else:
IoUCurr = previousSeen[currHash]
scores[permutation[position]] = scores[permutation[position]] + (IoUCurr - IoUPrev)/m
return scores
def approximateWhyInTheseTopKs(vectors, evaluationFunctions, m, k, j, d, unWrapFunction, algorithm='GeneralPurpose'):
scores = [0 for x in range(d)]
attributes = [x for x in range(d)]
attributeLists = topk.preProcess(vectors)
previousSeen = {}
initialTopKs = set()
for evaluationFunction in range(len(evaluationFunctions)):
initialTuples = topk.generateTuples(vectors, evaluationFunctions[evaluationFunction], [x for x in range(d)], d, unWrapFunction)
if topk.computeInTopK(initialTuples, k, j):
initialTopKs.add(evaluationFunction)
for mi in range(m):
permutation = attributes.copy()
random.shuffle(attributes)
currHash = 0
for position in range(len(permutation)):
prevHash = currHash
currHash = currHash | (1 << permutation[position])
if algorithm == 'Threshold':
if prevHash not in previousSeen:
prevTopKs = set()
for evaluationFunction in range(len(evaluationFunctions)):
if j in topk.computeTopKThreshold(vectors, attributeLists, evaluationFunctions[evaluationFunction], permutation[:position], k):
prevTopKs.add(evaluationFunction)
IoUPrev = 0 if position == 0 else len(initialTopKs.intersection(prevTopKs))/len(initialTopKs.union(prevTopKs))
previousSeen[prevHash] = IoUPrev
else:
IoUPrev = previousSeen[prevHash]
if currHash not in previousSeen:
currTopKs = set()
for evaluationFunction in range(len(evaluationFunctions)):
if j in topk.computeTopKThreshold(vectors, attributeLists, evaluationFunctions[evaluationFunction], permutation[:position+1], k):
currTopKs.add(evaluationFunction)
IoUCurr = len(initialTopKs.intersection(currTopKs))/len(initialTopKs.union(currTopKs))
previousSeen[currHash] = IoUCurr
else:
IoUCurr = previousSeen[currHash]
else:
if prevHash not in previousSeen:
prevTopKs = set()
for evaluationFunction in range(len(evaluationFunctions)):
prevTuples = topk.generateTuples(vectors, evaluationFunctions[evaluationFunction], permutation, position, unWrapFunction)
if topk.computeInTopK(prevTuples, k, j):
prevTopKs.add(evaluationFunction)
IoUPrev = 0 if position == 0 else len(initialTopKs.intersection(prevTopKs))/len(initialTopKs.union(prevTopKs))
previousSeen[prevHash] = IoUPrev
else:
IoUPrev = previousSeen[prevHash]
if currHash not in previousSeen:
currTopKs = set()
for evaluationFunction in range(len(evaluationFunctions)):
currTuples = topk.generateTuples(vectors, evaluationFunctions[evaluationFunction], permutation, position+1, unWrapFunction)
if topk.computeInTopK(currTuples, k, j):
currTopKs.add(evaluationFunction)
IoUCurr = len(initialTopKs.intersection(currTopKs))/len(initialTopKs.union(currTopKs))
previousSeen[currHash] = IoUCurr
else:
IoUCurr = previousSeen[currHash]
scores[permutation[position]] = scores[permutation[position]] + (IoUCurr - IoUPrev)/m
return scores
vectors = [[5,3,1],[2,4,4],[3,1,2],[4,1,3],[1,2,5]]
weights = [80,90,4]
weights2 = [90,10,5]
weights3 = [50,60,10]
print(approximateShapleyInTopK(vectors,lambda e:(sum([(weights[x]*e[x]) if e[x] is not None else 0 for x in range(len(weights))])), 5000, 2, 0, 3, None))
print(approximateShapleyNotInTopK(vectors,lambda e:(sum([(weights[x]*e[x]) if e[x] is not None else 0 for x in range(len(weights))])), 5000, 2, 2, 3, None))
print(approximateShapleyTopKLookLikeThis(vectors,lambda e:(sum([(weights[x]*e[x]) if e[x] is not None else 0 for x in range(len(weights))])), 5000, 2, 3, None))
print(approximateWhyInTheseTopKs(vectors[:3],[lambda e:(sum([(weights[x]*e[x]) if e[x] is not None else 0 for x in range(len(weights))])), lambda e:(sum([(weights2[x]*e[x]) if e[x] is not None else 0 for x in range(len(weights))])), lambda e:(sum([(weights3[x]*e[x]) if e[x] is not None else 0 for x in range(len(weights))]))], 5000, 2, 0, 3, None))