-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.py
434 lines (394 loc) · 17 KB
/
inference.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
"""TODO."""
from __future__ import print_function, absolute_import
import numba
from numba import jit
import numpy as np
import math
@jit(nopython=True, cache=True, nogil=True)
def gibbsthread(shardID, nshards, var_copy, weight_copy, weight, variable,
factor, fmap, vmap, factor_index, Z, cstart,
count, var_value, weight_value, sample_evidence, burnin):
"""TODO."""
# Indentify start and end variable
# print("当前位置:numbskull_extend.py line 16")
nvar = variable.shape[0]
start = (shardID * nvar) // nshards
end = ((shardID + 1) * nvar) // nshards
# TODO: give option do not store result, or just store tally
# print("当前位置:numbskull_extend.py line 20")
for var_samp in range(start, end):
if variable[var_samp]["isEvidence"] == 4:
# This variable is not owned by this machine
continue
if variable[var_samp]["isEvidence"] == 0 or sample_evidence:
v = draw_sample(var_samp, var_copy, weight_copy, weight, variable,
factor, fmap, vmap, factor_index, Z[shardID],
var_value, weight_value)
var_value[var_copy][var_samp] = v
if not burnin:
if variable[var_samp]["cardinality"] == 2:
count[cstart[var_samp]] += v
else:
count[cstart[var_samp] + v] += 1
@jit(nopython=True, cache=True, nogil=True)
def draw_sample(var_samp, var_copy, weight_copy, weight, variable, factor,
fmap, vmap, factor_index, Z, var_value, weight_value):
"""TODO."""
cardinality = variable[var_samp]["cardinality"]
for value in range(cardinality):
Z[value] = np.exp(potential(var_samp, value, var_copy, weight_copy,
weight, variable, factor, fmap,
vmap, factor_index, var_value,
weight_value))
for j in range(1, cardinality):
Z[j] += Z[j - 1]
z = np.random.rand() * Z[cardinality - 1]
return np.argmax(Z[:cardinality] >= z)
@jit(nopython=True, cache=True, nogil=True)
def potential(var_samp, value, var_copy, weight_copy, weight, variable, factor,
fmap, vmap, factor_index, var_value, weight_value):
"""TODO."""
#print('var_sample',var_samp)
p = 0.0
varval_off = value
if variable[var_samp]["dataType"] == 0:
varval_off = 0
vtf = vmap[variable[var_samp]["vtf_offset"] + varval_off]
start = vtf["factor_index_offset"]
end = start + vtf["factor_index_length"]
for k in range(start, end):
factor_id = factor_index[k]
if weight[factor[factor_id]['weightId']]['parameterize']: #如果权重需要参数化
a = weight[factor[factor_id]['weightId']]['a']
b = weight[factor[factor_id]['weightId']]['b']
x = fmap[factor[factor_id]["ftv_offset"]]['x']
theta = fmap[factor[factor_id]["ftv_offset"]]['theta']
#w = weight_value[weight_copy][factor[factor_id]["weightId"]] = pow(a,2) * (x - pow(b,2)) # w = pow(a,2)*(x-pow(b,2))
w = weight_value[weight_copy][factor[factor_id]["weightId"]] = theta * a * (x-b)
else:
w = weight_value[weight_copy][factor[factor_id]["weightId"]]
p += w * eval_factor(factor_id, var_samp, value, var_copy, variable,
factor, fmap, var_value)
return p
FACTORS = {
# Factor functions for boolean variables
"NOOP": -1,
"IMPLY_NATURAL": 0,
"OR": 1,
"EQUAL": 3,
"AND": 2,
"ISTRUE": 4,
"LINEAR": 7,
"RATIO": 8,
"LOGICAL": 9,
"IMPLY_MLN": 13,
# Factor functions for categorical variables
"AND_CAT": 12,
"OR_CAT": 14,
"EQUAL_CAT_CONST": 15,
"IMPLY_NATURAL_CAT": 16,
"IMPLY_MLN_CAT": 17,
# Factor functions for generative models for data programming.
#
# These functions accept two types of categorical variables:
#
# y \in {1, -1} corresponding to latent labels, and
# l \in {1, 0, -1} corresponding to labeling function outputs.
#
# The values of y are mapped to Numbskull variables y_index
# via {-1: 0, 1: 1}, and
# the values of l are mapped to Numbskull variables l_index
# via {-1: 0, 0: 1, 1: 2}.
# h(y) := y
"DP_GEN_CLASS_PRIOR": 18,
# h(l) := l
"DP_GEN_LF_PRIOR": 19,
# h(l) := l * l
"DP_GEN_LF_PROPENSITY": 20,
# h(y, l) := y * l
"DP_GEN_LF_ACCURACY": 21,
# h(l) := y * l * l
"DP_GEN_LF_CLASS_PROPENSITY": 22,
# l_2 fixes errors made by l_1
#
# h(y, l_1, l_2) := if l_1 == 0 and l_2 != 0: -1,
# elif l_1 == -1 * y and l_2 == y: 1,
# else: 0
"DP_GEN_DEP_FIXING": 23,
# l_2 reinforces the output of l_1
#
# h(y, l_1, l_2) := if l_1 == 0 and l_2 != 0: -1,
# elif l_1 == y and l_2 == y: 1,
# else: 0
"DP_GEN_DEP_REINFORCING": 24,
# h(l_1, l_2) := if l_1 != 0 and l_2 != 0: -1, else: 0
"DP_GEN_DEP_EXCLUSIVE": 25,
#h(l_1, l_2) := if l_1 == l_2: 1, else: 0
"DP_GEN_DEP_SIMILAR": 26,
# Factor functions for distribution
"UFO": 30,
"TEMP" : 31, #临时使用
"THETA": 32 #返回对应的theta值
}
for (key, value) in FACTORS.items():
exec("FUNC_" + key + " = " + str(value))
@jit(nopython=True, cache=True, nogil=True)
def eval_factor(factor_id, var_samp, value, var_copy, variable, factor, fmap,
var_value):
"""TODO."""
####################
# BINARY VARIABLES #
####################
fac = factor[factor_id]
ftv_start = fac["ftv_offset"]
ftv_end = ftv_start + fac["arity"]
if fac["factorFunction"] == FUNC_NOOP:
return 0
elif fac["factorFunction"] == FUNC_IMPLY_NATURAL:
for l in range(ftv_start, ftv_end):
v = value if (fmap[l]["vid"] == var_samp) else \
var_value[var_copy][fmap[l]["vid"]]
if v == 0:
# Early return if body is not satisfied
return 0
# If this point is reached, body must be true
l = ftv_end - 1
head = value if (fmap[l]["vid"] == var_samp) else \
var_value[var_copy][fmap[l]["vid"]]
if head:
return 1
return -1
elif factor[factor_id]["factorFunction"] == FUNC_OR:
for l in range(ftv_start, ftv_end):
v = value if (fmap[l]["vid"] == var_samp) else \
var_value[var_copy][fmap[l]["vid"]]
if v == 1:
return 1
return -1
elif factor[factor_id]["factorFunction"] == FUNC_EQUAL:
v = value if (fmap[ftv_start]["vid"] == var_samp) \
else var_value[var_copy][fmap[ftv_start]["vid"]]
for l in range(ftv_start + 1, ftv_end):
w = value if (fmap[l]["vid"] == var_samp) \
else var_value[var_copy][fmap[l]["vid"]]
if v != w:
return -1
return 1
elif factor[factor_id]["factorFunction"] == FUNC_AND \
or factor[factor_id]["factorFunction"] == FUNC_ISTRUE:
for l in range(ftv_start, ftv_end):
v = value if (fmap[l]["vid"] == var_samp) \
else var_value[var_copy][fmap[l]["vid"]]
if v == 0:
return -1
return 1
elif factor[factor_id]["factorFunction"] == FUNC_LINEAR:
res = 0
head = value if (fmap[ftv_end - 1]["vid"] == var_samp) \
else var_value[var_copy][fmap[ftv_end - 1]["vid"]]
for l in range(ftv_start, ftv_end - 1):
v = value if (fmap[l]["vid"] == var_samp) \
else var_value[var_copy][fmap[l]["vid"]]
if v == head:
res += 1
# This does not match Dimmwitted, but matches the eq in the paper
return res
elif factor[factor_id]["factorFunction"] == FUNC_RATIO:
res = 1
head = value if (fmap[ftv_end - 1]["vid"] == var_samp) \
else var_value[var_copy][fmap[ftv_end - 1]["vid"]]
for l in range(ftv_start, ftv_end - 1):
v = value if (fmap[l]["vid"] == var_samp) \
else var_value[var_copy][fmap[l]["vid"]]
if v == head:
res += 1
# This does not match Dimmwitted, but matches the eq in the paper
return math.log(res) # TODO: use log2?
elif factor[factor_id]["factorFunction"] == FUNC_LOGICAL:
head = value if (fmap[ftv_end - 1]["vid"] == var_samp) \
else var_value[var_copy][fmap[ftv_end - 1]["vid"]]
for l in range(ftv_start, ftv_end - 1):
v = value if (fmap[l]["vid"] == var_samp) \
else var_value[var_copy][fmap[l]["vid"]]
if v == head:
return 1
return 0
elif factor[factor_id]["factorFunction"] == FUNC_IMPLY_MLN:
for l in range(ftv_start, ftv_end - 1):
v = value if (fmap[l]["vid"] == var_samp) \
else var_value[var_copy][fmap[l]["vid"]]
if v == 0:
# Early return if body is not satisfied
return 1
# If this point is reached, body must be true
l = ftv_end - 1
head = value if (fmap[l]["vid"] == var_samp) \
else var_value[var_copy][l]
if head:
return 1
return 0
#########################
# CATEGORICAL VARIABLES #
#########################
elif factor[factor_id]["factorFunction"] == FUNC_AND_CAT \
or factor[factor_id]["factorFunction"] == FUNC_EQUAL_CAT_CONST:
for l in range(ftv_start, ftv_end):
v = value if (fmap[l]["vid"] == var_samp) \
else var_value[var_copy][fmap[l]["vid"]]
if v != fmap[l]["dense_equal_to"]:
return 0
return 1
elif factor[factor_id]["factorFunction"] == FUNC_OR_CAT:
for l in range(ftv_start, ftv_end):
v = value if (fmap[l]["vid"] == var_samp) \
else var_value[var_copy][fmap[l]["vid"]]
if v == fmap[l]["dense_equal_to"]:
return 1
return -1
elif factor[factor_id]["factorFunction"] == FUNC_IMPLY_NATURAL_CAT:
for l in range(ftv_start, ftv_end - 1):
v = value if (fmap[l]["vid"] == var_samp) \
else var_value[var_copy][fmap[l]["vid"]]
if v != fmap[l]["dense_equal_to"]:
# Early return if body is not satisfied
return 0
# If this point is reached, body must be true
l = ftv_end - 1
head = value if (fmap[l]["vid"] == var_samp) \
else var_value[var_copy][l]
if head == fmap[l]["dense_equal_to"]:
return 1
return -1
elif factor[factor_id]["factorFunction"] == FUNC_IMPLY_MLN_CAT:
for l in range(ftv_start, ftv_end - 1):
v = value if (fmap[l]["vid"] == var_samp) \
else var_value[var_copy][fmap[l]["vid"]]
if v != fmap[l]["dense_equal_to"]:
# Early return if body is not satisfied
return 1
# If this point is reached, body must be true
l = ftv_end - 1
head = value if (fmap[l]["vid"] == var_samp) \
else var_value[var_copy][l]
if head == fmap[l]["dense_equal_to"]:
return 1
return 0
#####################
# DATA PROGRAMMING #
# GENERATIVE MODELS #
#####################
elif factor[factor_id]["factorFunction"] == FUNC_DP_GEN_CLASS_PRIOR:
# NB: this doesn't make sense for categoricals
y_index = value if fmap[ftv_start]["vid"] == var_samp else \
var_value[var_copy][fmap[ftv_start]["vid"]]
return 1 if y_index == 1 else -1
elif factor[factor_id]["factorFunction"] == FUNC_DP_GEN_LF_PRIOR:
# NB: this doesn't make sense for categoricals
l_index = value if fmap[ftv_start]["vid"] == var_samp else \
var_value[var_copy][fmap[ftv_start]["vid"]]
if l_index == 2:
return -1
elif l_index == 0:
return 0
else:
return 1
elif factor[factor_id]["factorFunction"] == FUNC_DP_GEN_LF_PROPENSITY:
l_index = value if fmap[ftv_start]["vid"] == var_samp else \
var_value[var_copy][fmap[ftv_start]["vid"]]
abstain = variable[fmap[ftv_start]["vid"]]["cardinality"] - 1
return 0 if l_index == abstain else 1
elif factor[factor_id]["factorFunction"] == FUNC_DP_GEN_LF_ACCURACY:
y_index = value if fmap[ftv_start]["vid"] == var_samp else \
var_value[var_copy][fmap[ftv_start]["vid"]]
l_index = value if fmap[ftv_start + 1]["vid"] == var_samp else \
var_value[var_copy][fmap[ftv_start + 1]["vid"]]
abstain = variable[fmap[ftv_start + 1]["vid"]]["cardinality"] - 1
if l_index == abstain:
return 0
elif y_index == l_index:
return 1
else:
return -1
elif factor[factor_id]["factorFunction"] == \
FUNC_DP_GEN_LF_CLASS_PROPENSITY:
# NB: this doesn't make sense for categoricals
y_index = value if fmap[ftv_start]["vid"] == var_samp else \
var_value[var_copy][fmap[ftv_start]["vid"]]
l_index = value if fmap[ftv_start + 1]["vid"] == var_samp else \
var_value[var_copy][fmap[ftv_start + 1]["vid"]]
abstain = variable[fmap[ftv_start + 1]["vid"]]["cardinality"] - 1
if l_index == abstain:
return 0
elif y_index == 1:
return 1
else:
return -1
elif factor[factor_id]["factorFunction"] == FUNC_DP_GEN_DEP_FIXING:
# NB: this doesn't make sense for categoricals
y_index = value if fmap[ftv_start]["vid"] == var_samp else \
var_value[var_copy][fmap[ftv_start]["vid"]]
l1_index = value if fmap[ftv_start + 1]["vid"] == var_samp else \
var_value[var_copy][fmap[ftv_start + 1]["vid"]]
l2_index = value if fmap[ftv_start + 2]["vid"] == var_samp else \
var_value[var_copy][fmap[ftv_start + 2]["vid"]]
abstain = variable[fmap[ftv_start + 1]["vid"]]["cardinality"] - 1
if l1_index == abstain:
return -1 if l2_index != 1 else 0
elif l1_index == 0 and l2_index == 1 and y_index == 1:
return 1
elif l1_index == 1 and l2_index == 0 and y_index == 0:
return 1
else:
return 0
elif factor[factor_id]["factorFunction"] == FUNC_DP_GEN_DEP_REINFORCING:
# NB: this doesn't make sense for categoricals
y_index = value if fmap[ftv_start]["vid"] == var_samp else \
var_value[var_copy][fmap[ftv_start]["vid"]]
l1_index = value if fmap[ftv_start + 1]["vid"] == var_samp else \
var_value[var_copy][fmap[ftv_start + 1]["vid"]]
l2_index = value if fmap[ftv_start + 2]["vid"] == var_samp else \
var_value[var_copy][fmap[ftv_start + 2]["vid"]]
abstain = variable[fmap[ftv_start + 1]["vid"]]["cardinality"] - 1
if l1_index == abstain:
return -1 if l2_index != 1 else 0
elif l1_index == 0 and l2_index == 0 and y_index == 0:
return 1
elif l1_index == 1 and l2_index == 1 and y_index == 1:
return 1
else:
return 0
elif factor[factor_id]["factorFunction"] == FUNC_DP_GEN_DEP_EXCLUSIVE:
l1_index = value if fmap[ftv_start]["vid"] == var_samp else \
var_value[var_copy][fmap[ftv_start]["vid"]]
l2_index = value if fmap[ftv_start + 1]["vid"] == var_samp else \
var_value[var_copy][fmap[ftv_start + 1]["vid"]]
abstain = variable[fmap[ftv_start]["vid"]]["cardinality"] - 1
return 0 if l1_index == abstain or l2_index == abstain else -1
elif factor[factor_id]["factorFunction"] == FUNC_DP_GEN_DEP_SIMILAR:
l1_index = value if fmap[ftv_start]["vid"] == var_samp else \
var_value[var_copy][fmap[ftv_start]["vid"]]
l2_index = value if fmap[ftv_start + 1]["vid"] == var_samp else \
var_value[var_copy][fmap[ftv_start + 1]["vid"]]
return 1 if l1_index == l2_index else 0
###########################################
# FACTORS FOR OPTIMIZING DISTRIBUTED CODE #
###########################################
elif factor[factor_id]["factorFunction"] == FUNC_UFO:
v = value if fmap[ftv_start]["vid"] == var_samp else \
var_value[var_copy][fmap[ftv_start]["vid"]]
if v == 0:
return 0
return value if fmap[ftv_start + v - 1]["vid"] == var_samp else \
var_value[var_copy][fmap[ftv_start + v - 1]["vid"]]
elif factor[factor_id]["factorFunction"] == FUNC_TEMP:
return 1
elif factor[factor_id]["factorFunction"] == FUNC_THETA:
# print('theta', fmap[ftv_start]["theta"])
return fmap[ftv_start]["theta"]
######################
# FACTOR NOT DEFINED #
######################
else: # FUNC_UNDEFINED
print("Error: Factor Function", factor[factor_id]["factorFunction"],
"( used in factor", factor_id, ") is not implemented.")
raise NotImplementedError("Factor function is not implemented.")