-
Notifications
You must be signed in to change notification settings - Fork 2
/
cython_functions.pyx
438 lines (341 loc) · 13.4 KB
/
cython_functions.pyx
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
435
436
437
438
#!python
#cython: profile=False, language_level=3, boundscheck=False, wraparound=False, cdivision=True
#cython --compile-args=-fopenmp --link-args=-fopenmp --force -a
## for compilation run: python setup.py build_ext --inplace
cimport cython
from cython.parallel import prange, parallel
from libc.math cimport exp
from libc.stdlib cimport rand, RAND_MAX
cimport numpy as np
import numpy as np
data_type = np.int8
ctypedef np.int8_t data_type_t
def draw_noparents_onechild(data_type_t[:,:] x, # N x D
data_type_t[:,:] sibling, # D x Lc
data_type_t[:,:] child, # N x Lc
float lbda,
float prior,
data_type_t[:,:] sampling_indicator): # N x D
cdef float p, acc_child
cdef int n, d, N = x.shape[0], D = x.shape[1]
for n in prange(N, schedule=dynamic, nogil=True):
for d in range(D):
if sampling_indicator[n,d] is True:
# compute the posterior
acc_child = lbda*score_no_parents_unified(child[n,:], x[n,:], sibling, d)
p = sigmoid(acc_child + prior)
x[n, d] = swap_metropolised_gibbs_unified(p, x[n,d])
def draw_oneparent_nochild(
data_type_t[:,:] x, # N x D
data_type_t[:,:] z_pa, # N x Lp
data_type_t[:,:] u_pa, # D x Lp
double lbda_pa,
float prior,
data_type_t[:,:] sampling_indicator): # N x D
cdef float p, acc_par
cdef int n, d, N=x.shape[0], D=x.shape[1]
for n in prange(N, schedule=dynamic, nogil=True): # parallelise
# for n in range(N):
for d in range(D):
if sampling_indicator[n,d] is True:
acc_par = lbda_pa*compute_g_alt_tilde_unified(u_pa[d,:], z_pa[n,:])
p = sigmoid(acc_par + prior)
x[n, d] = swap_metropolised_gibbs_unified(p, x[n,d])
def draw_twoparents_nochild(
data_type_t[:,:] x, # N x D
data_type_t[:,:] z_pa1, # N x Lp1
data_type_t[:,:] u_pa1, # D x Lp1
double lbda_pa1,
data_type_t[:,:] z_pa2, # N x Lp2
data_type_t[:,:] u_pa2, # D x Lp2
double lbda_pa2,
float prior,
data_type_t[:,:] sampling_indicator): # N x D
cdef float p, acc_par
cdef int n, d, N=x.shape[0], D=x.shape[1]
for n in prange(N, schedule=dynamic, nogil=True): # parallelise
for d in range(D):
if sampling_indicator[n,d] is True:
# accumulate over all parents
acc_par = lbda_pa1*compute_g_alt_tilde_unified(u_pa1[d,:], z_pa1[n,:]) +\
lbda_pa2*compute_g_alt_tilde_unified(u_pa2[d,:], z_pa2[n,:])
p = sigmoid(acc_par + prior)
x[n, d] = swap_metropolised_gibbs_unified(p, x[n,d])
def draw_oneparent_onechild(
data_type_t[:,:] x, # N x D
data_type_t[:,:] z_pa, # N x Lp
data_type_t[:,:] u_pa, # D x Lp
double lbda_pa,
data_type_t[:,:] sibling, # D x Lc
data_type_t[:,:] child, # N x Lc
float lbda,
float prior,
data_type_t[:,:] sampling_indicator): # N x D
cdef float p, acc_par, acc_child
cdef int n, d, N = x.shape[0], D=x.shape[1]
for n in prange(N, schedule=dynamic, nogil=True):
# for n in range(N):
for d in range(D):
if sampling_indicator[n,d] is True:
acc_par = lbda_pa*compute_g_alt_tilde_unified(u_pa[d,:], z_pa[n,:])
acc_child = lbda*score_no_parents_unified(child[n,:], x[n,:], sibling, d)
p = sigmoid(acc_par + acc_child + prior)
x[n, d] = swap_metropolised_gibbs_unified(p, x[n,d])
def draw_oneparent_nochild_maxdens(
data_type_t[:,:] x, # N x D
data_type_t[:,:] z_pa, # N x Lp
data_type_t[:,:] u_pa, # D x Lp
double lbda_pa,
float prior,
data_type_t[:] max_density,
data_type_t[:,:] sampling_indicator): # N x D
cdef float p, acc_par
cdef int n, d, N=x.shape[0], D=x.shape[1]
for n in range(N):
for d in range(D):
if sampling_indicator[n,d] is True:
if density_magic(x, max_density, n, d):
continue
# accumulate over all parents
acc_par = lbda_pa*compute_g_alt_tilde_unified(u_pa[d,:], z_pa[n,:])
p = sigmoid(acc_par + prior)
x[n, d] = swap_metropolised_gibbs_unified(p, x[n,d])
def draw_twoparents_nochild_maxdens(
data_type_t[:,:] x, # N x D
data_type_t[:,:] z_pa1, # N x Lp1
data_type_t[:,:] u_pa1, # D x Lp1
double lbda_pa1,
data_type_t[:,:] z_pa2, # N x Lp2
data_type_t[:,:] u_pa2, # D x Lp2
double lbda_pa2,
float prior,
data_type_t[:] max_density,
data_type_t[:,:] sampling_indicator): # N x D
cdef float p, acc_par
cdef int n, d, N=x.shape[0], D=x.shape[1]
for n in range(N):
for d in range(D):
if sampling_indicator[n,d] is True:
if density_magic(x, max_density, n, d):
continue
# accumulate over all parents
acc_par = lbda_pa1*compute_g_alt_tilde_unified(u_pa1[d,:], z_pa1[n,:]) +\
lbda_pa2*compute_g_alt_tilde_unified(u_pa2[d,:], z_pa2[n,:])
p = sigmoid(acc_par + prior)
x[n, d] = swap_metropolised_gibbs_unified(p, x[n,d])
def draw_noparents_onechild_maxdens(data_type_t[:,:] x, # N x D
data_type_t[:,:] sibling, # D x Lc
data_type_t[:,:] child, # N x Lc
float lbda,
float prior,
data_type_t[:] max_density,
data_type_t[:,:] sampling_indicator): # N x D
cdef float p, acc_child
cdef int n, d, N = x.shape[0], D = x.shape[1]
cdef bint dens_switch
for n in range(N):
for d in range(D):
if sampling_indicator[n,d] is True:
# check codes are not too dense/sparse if applic.
# the function may alter the state of the variable!
if density_magic(x, max_density, n, d):
continue
# compute the posterior
acc_child = lbda*score_no_parents_unified(child[n,:], x[n,:], sibling, d)
p = sigmoid(acc_child + prior)
x[n, d] = swap_metropolised_gibbs_unified(p, x[n,d])
def draw_oneparent_onechild_maxdens(
data_type_t[:,:] x, # N x D
data_type_t[:,:] z_pa, # N x Lp
data_type_t[:,:] u_pa, # D x Lp
double lbda_pa,
data_type_t[:,:] sibling, # D x Lc
data_type_t[:,:] child, # N x Lc
float lbda,
float prior,
data_type_t[:] max_density,
data_type_t[:,:] sampling_indicator): # N x D
cdef float p, acc_par, acc_child
cdef int n, d, N = x.shape[0], D=x.shape[1]
for n in range(N):
for d in range(D):
if sampling_indicator[n,d] is True:
if density_magic(x, max_density, n, d):
continue
acc_par = lbda_pa*compute_g_alt_tilde_unified(u_pa[d,:], z_pa[n,:])
acc_child = lbda*score_no_parents_unified(child[n,:],
x[n,:], sibling, d)
p = sigmoid(acc_par + acc_child + prior)
x[n, d] = swap_metropolised_gibbs_unified(p, x[n,d])
cpdef inline int compute_g_alt_tilde_unified(data_type_t[:] u,
data_type_t[:] z) nogil:
"""
for two vectors of same length, u and z, comput
2*np.min(1, u^T z)-1
"""
cdef int i
for i in xrange(u.shape[0]):
if u[i] == 1 and z[i] == 1:
return 1
return -1
# @cython.boundscheck(False)
# @cython.wraparound(False)
cpdef inline float score_no_parents_unified(
data_type_t[:] x, # (N x) D
data_type_t[:] z, # (N x) L
data_type_t[:,:] u, # D x L
int l) nogil: # feature index
"""
This is essentially algorithm 1 from the ICML paper
"""
cdef int L = u.shape[1]
cdef int D = u.shape[0]
cdef bint alrdy_active
cdef int score = 0
for d in range(D):
if u[d, l] != 1:
continue
alrdy_active = False
for l_prime in range(L):
if (z[l_prime] == 1 and
u[d, l_prime] == 1 and
l_prime != l):
alrdy_active = True
break
if (alrdy_active is False):
score += x[d]
return score
# @cython.boundscheck(False)
# @cython.wraparound(False)
@cython.cdivision(True)
cdef inline int swap_metropolised_gibbs_unified(float p, data_type_t x) nogil:
cdef float alpha
if x == 1:
if p <= .5:
alpha = 1
else:
alpha = (1-p)/p
else:
if p >= .5:
alpha = 1
else:
alpha = p/(1-p)
if rand()/float(RAND_MAX) < alpha:
return -x
else:
return x
cdef inline int swap_gibbs(float p, data_type_t x) nogil:
"""
swap with standard gibbs sampler. can do better sometimes(?)
can replace swap_metropolised_gibbs_unified in sampling functions.
"""
if x == 1:
if rand()/float(RAND_MAX) > p:
return -x
else:
return x
else:
if rand()/float(RAND_MAX) < p:
return -x
else:
return x
cdef inline float sigmoid(float x) nogil:
cdef float p
p = 1/(1+exp(-x))
return p
cpdef inline long compute_P_parallel(data_type_t[:,:] x,
data_type_t[:,:] u,
data_type_t[:,:] z) nogil:
""" parallelised over n (not d). """
cdef long P = 0
cdef int d, n
for n in prange(x.shape[0], schedule=dynamic, nogil=True):
# for n in range(x.shape[0]):
for d in range(x.shape[1]):
if compute_g_alt_tilde_unified(u[d,:], z[n,:]) == x[n, d]:
P += 1
return P
cpdef int no_of_ones(data_type_t[:] z) nogil:
cdef int L = z.shape[0]
cdef int acc = 0
for i in range(L):
if z[i] == 1:
acc += 1
return acc
cpdef bint max_density_checker(data_type_t[:] x, int max_density, int d) nogil:
cdef int one_count
if max_density != 0:
one_count = no_of_ones(x)
# too many ones in code
if one_count > max_density:
# if current value is one, set to zero˜
# otherwise, do not update (return True -> no update)
if x[d] == 1:
x[d] = -1
return True
# maximum no of ones in code
elif one_count == max_density:
# if current value is one, allow update
if x[d] == 1:
return False
# if current value is -1, do not update
elif x[d] == -1:
return True
return False
cpdef bint min_density_checker(data_type_t[:] x, int min_density, int d) nogil:
cdef int one_count
if min_density != 0:
one_count = no_of_ones(x)
# too many ones in code
if one_count < min_density:
# if current value is one, set to zero
# otherwise, do not update (return True -> no update)
if x[d] == -1:
x[d] = 1
return True
# maximum no of ones in code
elif one_count == min_density:
# if current value is one, allow update
if x[d] == 1:
return False
# if current value is -1, do not update
elif x[d] == -1:
return True
return False
cpdef bint density_magic(data_type_t[:,:] x,
data_type_t[:] density_conditions,
int n, int d):
"""
return true if max density is reached in either rows or columns.
if max density is violated, set x[n,d] from 1 to -1
"""
cdef bint update1, update2, update3, update4
# check whether minimum conditions are met in any dimension
update0 = min_density_checker(x[n,:], density_conditions[0], d)
update1 = min_density_checker(x[:,d], density_conditions[1], n)
# if so terminate function
if (update0 or update1):
return True
# if not, check whether maximum conditions are met
else:
update2 = max_density_checker(x[n,:], density_conditions[2], d)
update3 = max_density_checker(x[:,d], density_conditions[3], n)
return (update2 or update3)
return False
# if at least one constrained returns True (do NOT update), return True
cpdef void probabilistc_output(double[:,:] x,
double[:,:] u,
double[:,:] z,
double lbda,
int D, int N, int L):
cdef float p_dn, sgmd_lbda
"""
p_dn is the probability that every input is zero
"""
sgmd_lbda = sigmoid(lbda)
for d in range(D):
for n in range(N):
p_dn = 1
for l in range(L):
p_dn = p_dn * ( 1 - u[d,l]*z[n,l] )
x[n, d] = (sgmd_lbda * (1-p_dn) + (p_dn*(1-sgmd_lbda) ) )