-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_CI_BP.py
1379 lines (1191 loc) · 74.9 KB
/
utils_CI_BP.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#This file allows to simulate BP (or variants like Circular BP and Fractional BP) for probability distributions with pairwise factors and binary variables.
# from jax import jit #I removed that as it is not speeding up the computations
# import jax.numpy as np #I removed that as it is not speeding up the computations
# import numpy as onp
import numpy as np
import copy
import itertools
import matplotlib.pyplot as plt
from operator import xor
from graph_generator import get_all_oriented_edges, get_w_matrix
from itertools import repeat
from utils_alpha_obj import *
import time
from utils_basic_functions import sig
def F(x, w):
print("function F is deprecated: use F_w (or F_f) instead")
return F_w(x, w)
#@jit
def F_w(x, w):
"""
Compute F_x = F(x, w)
Previously called F
MAYBE INSTEAD I CAN TAKE PRE-BUILT FUNCTIONS (E.G. SIGMOID) HERE: https://github.com/google/jax/blob/master/jax/experimental/stax.py (it could be faster)
Note that we have: F(x) = 2 * artanh( (2*w_ij-1) * tanh(x/2) )
"""
# print("x = {}, w = {}".format(x, w))
# print("min(x) = {}, max(x) = {}".format(np.min(x), np.max(x)))
# print("min(w) = {}, max(w) = {}".format(np.min(w), np.max(w)))
# print("F: x.shape = {}, w.shape = {}".format(x.shape, w.shape))
# print("F: x.type = {}, w.type = {}".format(type(x), type(w)))
exp_x = np.exp(x)
# print("min(exp_x) = {}, max(exp_x) = {}".format(np.min(exp_x), np.max(exp_x)))
# print("denominator = \n{}".format((1-w)*exp_x + w))
# print("min(denominator) = {}, max(denominator) = {}".format(np.min((1-w)*exp_x + w), np.max((1-w)*exp_x + w)))
# print("F_w with log = {}, {}".format(np.min(np.log((w*exp_x + 1 - w) / ((1-w)*exp_x + w))), np.max(np.log((w*exp_x + 1 - w) / ((1-w)*exp_x + w)))))
# print("F_w with arctanh = {}, {}".format(np.min(2 * np.arctanh( (2*w-1) * np.tanh(x/2) )), np.max(2 * np.arctanh( (2*w-1) * np.tanh(x/2) ))))
return np.log((w*exp_x + 1 - w) / ((1-w)*exp_x + w))
# return 2 * np.arctanh( (2*w-1) * np.tanh(x/2) ) #same thing as above, though it is around 40% slower
def F_w_power(x, w, p):
"""
Compute F_x = F(x, factor^p), where factor = [[w,1-w],[1-w,w]]
"""
return F_f(x, np.array([[w, 1-w],[1-w, w]])**p)
#return F_w(x, transf_w_power(w, p)) #slightly more intense computationnally (I checked)
def dF_w(x, w):
"""
Compute the derivative (w.r.t. x) of F(x, w)
dF/dx = (2w-1) * (1 - tanh(x/2)^2) / (1 - (2w-1)^2 * tanh(x/2)^2)
I checked the formula (+ by approximating numerically dF_w(x,w) ~ (F_w(x+dx,w) - F_w(x,w) ) / dx --> ok
"""
sqr_tanh_xdiv2 = np.tanh(x/2)**2
return (2*w-1) * (1 - sqr_tanh_xdiv2) / (1 - (2*w-1)**2 * sqr_tanh_xdiv2)
def F_f(x, factor):
"""
Compute F_x = F(x, factor)
MAYBE INSTEAD I CAN TAKE PRE-BUILT FUNCTIONS (E.G. SIGMOID) HERE: https://github.com/google/jax/blob/master/jax/experimental/stax.py (it could be faster)
"""
exp_x = np.exp(x)
F_x = np.log((factor[1,1]*exp_x + factor[0,1]) / (factor[1,0]*exp_x + factor[0,0]))
return F_x
# try:
# F_x = np.log((factor[1,1]*exp_x + factor[0,1]) / (factor[1,0]*exp_x + factor[0,0]))
# return F_x
# except RuntimeWarning:
# print(x, factor)
def F_f_power(x, factor, p):
"""
Compute F_x = F(x, factor^p)
"""
return F_f(x, factor**p)
def F_w_approx_tanh(x, w):
"""
Linearizes F_w(x) ~ 2 * (2*w-1) * tanh(x/2)
That is the same as true F but without the arctanh: indeed, F(x) = 2 * arctanh( (2*w-1) * tanh(x/2))
(the approximation is really good for w<0.7, and even for w~0.7 it starts to fail only for x>2)
"""
return 2 * (2*w-1) * np.tanh(x/2)
def F_w_approx_linear(x, w):
"""
Linearizes F_w(x) ~ (2*w-1) * x
"""
return (2*w-1) * x
def F_w_approx(x, w):
"""
Approximation of F_x = F(x, w) ~ a + b.p (where p=sig(x) is the probability as x is the log-odds)
"""
eps_1 = w - 0.5
eps_2 = -(w - 0.5)
eps_3 = -(w - 0.5)
eps_4 = w - 0.5
p = 1 / (1 + np.exp(-x)) #p = sig(x)
#Approx where bounds are fitted but the approx around x=0 is bad (because the arbitrary sigmoid function is fitted based on the values at -inf and +inf). Arbitrary sigmoid function which is designed to fit well the curve for x=-inf and +inf. But this function often doesn't fit well for x close to 0
# low = np.log((0.5+eps_2) / (0.5+eps_1))
# high = np.log((0.5+eps_4) / (0.5+eps_3))
# return low + (high-low)*p
#Approx which fits well for low x but not well for the bounds (except if all eps_i are small). Based on the Taylor expansion for low eps_i. This function often doesn't fit well for strong |x| (unless all |eps_i|<0.05 for instance)
return 2*(eps_2-eps_1) + p*2*(eps_4-eps_2-eps_3+eps_1) #= - 4*eps_1 + p*8*eps_1
def F_f_approx(x, f):
"""
Approximation of F_x = F(x, factor)
"""
eps_1 = f[0,0]-0.5
eps_2 = f[0,1]-0.5
eps_3 = f[1,0]-0.5
eps_4 = f[1,1]-0.5
p = 1 / (1 + np.exp(-x)) #p = sig(x)
#Approx where bounds are fitted but the approx around x=0 is bad (because the arbitrary sigmoid function is fitted based on the values at -inf and +inf)
# low = np.log((0.5+eps_2) / (0.5+eps_1))
# high = np.log((0.5+eps_4) / (0.5+eps_3))
# return low + (high-low)*p
#Approx which fits well for low x but not well for the bounds (except if all eps_i are small)
return 2*(eps_2-eps_1) + p*2*(eps_4-eps_2-eps_3+eps_1)
def transf_w_power(w, p):
"""
p = 1/beta
Get tanh(J/beta) from tanh(J)
(2*w-1 = tanh(J) vs 2*w_new-1 = tanh(J/beta))
tanh(x) = 2*sig(2*x)-1 thus w = sig(2*J), respectively w_new = sig(2*J/beta)
--> w_new = sig(2/beta * J) with J = sig^{-1}(w) / 2 with sig^{-1}(x) = log(x/(1-x))
--> w_new = sig(1/beta * log(w/(1-w)))
"""
return sig(p * np.log(w/(1-w))) #if returns 0 or 1, possible approximation of sig(x): 1-exp(-x) if x->+inf, and exp(x) if x->-inf (but I don't know if the result would be different from 0, resp. 1 ...
# def transf_w_power(w, p):
# """
# Finding the w_new such that w_new / (1-w_new) = ( w / (1-w) )^p
# """
# ratio = w / (1-w)
# ratio_power = ratio**p
# # print("ratio_power = {}".format(ratio_power))
# w_power = ratio_power / (1 + ratio_power)
# print("w_power = {}".format(w_power))
# print("min(w_power) = {}, max(w_power) = {}".format(np.min(w_power), np.max(w_power)))
# if np.max(ratio_power) == np.inf:
# print("Numerical problem!")
# w_power_2 = sig(p * np.log(w/(1-w))) #2nd method to compute w_power (--> better)
# print("Alternative computation: {}".format(w_power_2))
# print("checking on one example: for w = {} and p = {}: {} vs {}".format(w[0,1], p[0,1], w_power[0,1], w_power_2[0,1]))
# return w_power
# class Graph:
# def __init__(self, graph):
# super().__init__()
# # # self.nodes = np.unique(list(graph.keys())) #with numpy
# # self.nodes = set(list(itertools.chain.from_iterable(graph.keys()))) #with jax.numpy
# self.n = len(self.nodes)
# # # self.edges = []
# # # for (i, j) in graph.keys():
# # # self.edges.append((i, j))
# # #better (?) to have a dictionnary of all possible pairs, with True if connection
# # self.neighbors = {}
# # for i in self.nodes:
# # self.neighbors[i] = [j for j in self.nodes if (((i,j) in graph) or ((j,i) in graph))]
def transpose(M):
"""
Only inverts the first two dimensions: the shape goes from (i1,i2,i3,...,in) to (i2,i1,i3,...,in)
"""
if len(M.shape) == 2:
return M.T
elif len(M.shape) == 3:
return M.transpose((1,0,2))
else:
raise NotImplemented
class Network:
def __init__(self, graph, M_ext,
alpha=None, w=None, w_input=None,
damping=0, keep_history_beliefs=False, keep_history_messages=False,
which_CI='CI',
parallel_CI=True, parallel_Mext=True,
niter=100
):
"""
damping=0 corresponds to BP/CI : M_new = (1-damping)*F(M_old) + damping*M_old
Note that damping=1-dt if we write M_new = M_old + dt*(F(M_old) - M_old) i.e. dM/dt = - M_old + F(M_old) (tau=1/dt)
graph is undirected (has type Graph) but has information about the directionality ('up' or 'down' associated to each undirected edge (node1,node2), i.e. (node2,node1) does not exist in graph)
input 'alpha': One can include uniform alpha, different alpha_c and alpha_d, non-uniform dict_alpha_impaired (of type {edge:alpha} or {edge:(alpha_c,alpha_d)} (with all edges for which alpha is impaired - if an edge isn't indicated in the dictionnary but exists in the graph then it means alpha=1 for this edge)
parallel_CI indicates whether updates in CI are made in parallel or not (M_ij^new = F(M_ij^old) for all (i,j))
---> M is a matrix of size n_nodes*n_nodes
parallel_CI = True makes the function 5 times faster for small networks (probably more for bigger networks) by using matrices M_ij
parallel_Mext indicates whether updates in CI are made in parallel on the examples (same graph but different M_ext)
---> M is a matrix of size n_nodes*n_nodes*n_examples
parallel_Mext can be True only if parallel_CI is True
"""
super().__init__()
assert not(parallel_Mext == True and parallel_CI == False) #parallel_Mext can be True only if parallel_CI is True
#with_factors is inferred directly based on graph
with_factors = 'factor' in graph.edges[list(graph.edges)[0]].keys() #if False, weights
alpha.check()
self.graph = graph
self.damping = damping
self.parallel_CI = parallel_CI
self.keep_history_beliefs = keep_history_beliefs
self.keep_history_messages = keep_history_messages
self.parallel_Mext = parallel_Mext
#Initiate the external messages
if self.parallel_Mext == False:
ex_Mext = list(M_ext.values())[0] #M_ext[list(M_ext.keys())[0]]
else:
self.n_examples = len(M_ext)
# print("n_examples = len(M_ext) = {}".format(n_examples))
ex_Mext = list(M_ext[0].values())[0] #M_ext is a list of dictionnaries: M_ext = [{node: [M_ext_ex[node][t] for all t]} for M_ext_ex in M_ext]
self.constant_Mext = isinstance(ex_Mext, int) or isinstance(ex_Mext, np.int64) or isinstance(ex_Mext, float)
assert not(self.constant_Mext == False and self.parallel_Mext == True) #TODO: implement (if possible) - probably requires to add an extra dimensions to all matrices
if self.constant_Mext: #M_ext is constant over the whole simulation (M_ext = {node: M_ext_node})
# print("M_ext is constant")
self.T = max(len(self.graph) * 2, niter) #80) #this is arbitrary - it is possible that BP/CI does not have time to fully converge with that number of iterations
if self.parallel_Mext == False:
self.M_ext = {node: M_ext.get(node, 0) for node in self.graph.nodes} #self.M_ext = {node: [M_ext.get(node, 0)]*self.T for node in self.graph.nodes}
else:
self.M_ext = {node: [Mext_ex.get(node, 0) for Mext_ex in M_ext] for node in self.graph.nodes}
#introduce a bit of noise at the beginning
# # np.random.seed()
# np.random.seed(0) #always the same noise, for the simulation to be reproducible (and functions using Network like diff_CI_true to be reproducible)
# T_perturb = 4
# for key in self.M_ext.keys():
# self.M_ext[key][:T_perturb] = self.M_ext[key][:T_perturb] + np.random.normal(scale=0.6, size=T_perturb)
else: #M_ext varies with time (M_ext = {node: [M_ext_node[t] for t in range(T)]}, or M_ext = [{node: [M_ext_ex[node][t] for t in range(T)]} for M_ext_ex in M_ext])
# print("M_ext varies with time")
self.T = len(M_ext[list(M_ext.keys())[0]])
self.M_ext = {node : M_ext.get(node, [0]*self.T) for node in self.graph.nodes} #because M_ext only has keys corresponding to stimulated nodes only (if for instance n_stimulated_nodes=1)
# print("self.M_ext created", self.M_ext)
#change M_ext using K_nodes
# print("alpha = {}".format(alpha))
if 'power' in which_CI and ((alpha.get('K_nodes') is not None) or (alpha.get('K_nodes_vector') is not None)):
if alpha.get('K_nodes') is not None:
K_nodes_dict = alpha.get('K_nodes')
else:
K_nodes_dict = dict(zip(list(graph.nodes), alpha.get('K_nodes_vector')))
# print("K_nodes_dict", K_nodes_dict)
# print("Mext", self.M_ext)
self.M_ext = {node: val / K_nodes_dict[node] for node, val in self.M_ext.items()} #Be careful: by modifying M_ext, M_ext is not the true M_ext anymore. As a consequence, if it's used only to compute the beliefs (by running CI or another algorithm) then it's fine. On the contrary, if it's recovered (Network.M_ext) or used for something else (Network() instantiated and then used for CI,CIpower,...etc), then I should change the code and take the powers into account only when updating the beliefs (with M_ext)
if w_input is not None: #multiplying M_ext by the input weights
assert type(w_input) not in [list, int, float] #not list but np.array (to be able to do the operation below)
if type(w_input) != dict: #w_input is a vector
w_input = dict(zip(list(graph.nodes), w_input))
self.M_ext = {node: np.array(val) * w_input[node] for node, val in self.M_ext.items()} #self.M_ext = {node: list(np.array(val) * w_input[node]) for node, val in self.M_ext.items()}
if self.parallel_CI: #change M_ext[node][t] into M_ext[t][node] in order to catch M_ext[t] easily
assert list(self.M_ext.keys()) == list(self.graph.nodes)
if self.constant_Mext == False:
self.M_ext = np.array([np.array([self.M_ext[node][t] for node in self.M_ext.keys()]) for t in range(self.T)]) #{t: np.array([self.M_ext[node][t] for node in self.M_ext.keys()]) for t in range(len(self.M_ext[list(self.M_ext.keys())[0]]))}
# print("M_ext.shape = {}".format(self.M_ext.shape))
else:
M_ext_t = np.array(list(self.M_ext.values())) #np.array([self.M_ext[node][0] for node in self.M_ext.keys()]) #with t=0 (because constant M_ext)
self.M_ext = M_ext_t #{t: M_ext_t for t in range(len(self.M_ext[list(self.M_ext.keys())[0]]))}
# print("M_ext.shape = {} (the number of examples should be the last number)".format(self.M_ext.shape))
if self.parallel_Mext == True:
assert self.M_ext.shape[-1] == self.n_examples
# print("created M_ext", self.M_ext)
#Define the connections weights (if w != None, then it represents the weights of the graph: it's not a multiplicative coefficient)
self.with_factors = with_factors
list_models_change_weights = ['CIpower', 'CIpower_approx_tanh', 'full_CIpower', 'full_CIpower_approx_tanh']
if self.parallel_CI == False:
assert not(which_CI in list_models_change_weights and alpha.get('K_edges_matrix') is not None) #K_edges should be != None (but K_edges_matrix should be None)
if self.with_factors == False:
if w is None:
self.w = {}
for node1, node2, d in graph.edges(data=True):
w_edge = d['weight']
if which_CI in list_models_change_weights and alpha.get('K_edges') is not None:
K_edges = alpha.get('K_edges')
w_edge = transf_w_power(w_edge, 1 / K_edges[node1, node2]) #Be careful: by setting self.w this way, self.w are not the true weights. As a consequence, if it's used only to compute the beliefs (by running CI or another algorithm) then it's fine. On the contrary, if it's recovered (Network.w) or used for something else (Network() instantiated and then used for CI,CIpower,...etc), then I should change the code and take the powers into account only when updating the messages
self.w[node1, node2] = w_edge
self.w[node2 ,node1] = w_edge
else:
assert which_CI not in list_models_change_weights #not implemented (do the transformation)
self.w = {}
if w.get('K_edges') is not None:
w_unoriented = w.get('K_edges')
assert list(w_unoriented) == list(graph.edges)
for node1, node2 in graph.edges:
w_edge = w_unoriented[node1, node2]
self.w[node1, node2] = w_edge
self.w[node2, node1] = w_edge
elif w.get('dict_alpha_impaired') is not None:
w_oriented = w.get('dict_alpha_impaired')
assert list(w_oriented) == list(get_all_oriented_edges(graph.edges))
self.w = w_oriented
else:
if w is None:
self.factor = {}
for node1, node2, d in graph.edges(data=True):
factor = d['factor']
if which_CI in list_models_change_weights and alpha.get('K_edges') is not None:
K_edges = alpha.get('K_edges')
factor = factor**(1 / K_edges[node1, node2]) #Be careful: by setting self.factor this way, self.factor are not the true factors. As a consequence, if it's used only to compute the beliefs (by running CI or another algorithm) then it's fine. On the contrary, if it's recovered (Network.factor) or used for something else (Network() instantiated and then used for CI,CIpower,...etc), then I should change the code and take the powers into account only when updating the messages
self.factor[node1, node2] = factor
self.factor[node2, node1] = factor.T
else:
raise NotImplemented
else: #parallel_CI == True:
assert not('power' in which_CI and alpha.get('K_edges') is not None) #K_edges_matrix should be != None (but K_edges should be None)
if self.with_factors == False:
#create the matrix of weights
if w is None:
self.w = get_w_matrix(graph) #, check_infinite_weights='power' in which_CI and not(('CIpower_approx' in which_CI and 'CIpower_approx_tanh' not in which_CI))) #checking only for CIpower models (for which 2*w-1 = tanh(beta*alpha*J_ij) = tanh(beta*alpha*arctanh(2*wgraph_ij-1)) which will create numerical problems if arctanh = inf --> in fact there are also numerical mistakes for w=0 or 1 with CIpower_approx models, but at another point: beliefs become nan or inf
if which_CI in list_models_change_weights and alpha.get('K_edges_matrix') is not None:
K_edges_matrix = alpha.get('K_edges_matrix')
# print("before transformation")
# print(self.w)
self.w = transf_w_power(self.w, 1 / K_edges_matrix)
# print("min(w_eff) = {}, max(w_eff) = {}".format(np.min(self.w), np.max(self.w)))
# print("after transformation with K_edges_matrix = {}".format(K_edges_matrix))
# print(self.w)
# self.w = np.zeros((len(graph.nodes), len(graph.nodes))) + 0.5 #default value: 0.5
# node_to_inode = dict(zip(list(graph.nodes), range(len(graph.nodes))))
# print("alpha = {}".format(alpha))
# if 'power' in which_CI and alpha.get('K_edges_matrix') is not None:
# # K_edges = alpha.get('K_edges')
# K_edges_matrix = alpha.get('K_edges_matrix')
# for node1, node2, d in graph.edges(data=True):
# w_edge = d['weight']
# i_node1 = node_to_inode[node1] #list(graph.nodes).index(node1)
# i_node2 = node_to_inode[node2]
# if 'power' in which_CI and alpha.get('K_edges_matrix') is not None:
# w_edge = transf_w_power(w_edge, 1 / K_edges_matrix[i_node1, i_node2]) #Be careful: by setting self.w this way, self.w are not the true weights. As a consequence, if it's used only to compute the beliefs (by running CI or another algorithm) then it's fine. On the contrary, if it's recovered (Network.w) or used for something else (Network() instantiated and then used for CI,CIpower,...etc), then I should change the code and take the powers into account only when updating the messages
# self.w[i_node1, i_node2] = w_edge
# self.w[i_node2, i_node1] = w_edge
else:
self.w = w.get('alpha_matrix')
if which_CI in list_models_change_weights and alpha.get('K_edges_matrix') is not None:
K_edges_matrix = alpha.get('K_edges_matrix')
self.w = transf_w_power(self.w, 1 / K_edges_matrix)
# print("self.w_eff = {}".format(self.w))
if self.parallel_Mext:
# print("before (self.w)", self.w.shape, type(self.w))
self.w = self.w[..., np.newaxis] #expand the dimension
# print("after (self.w)", self.w.shape, type(self.w))
else:
if self.parallel_Mext:
raise NotImplemented #TODO: check that it's ok
#create the matrix of factors
if w is None:
self.factor = np.zeros((2, 2, len(graph.nodes), len(graph.nodes))) + 1 #default value: [[1,1],[1,1]]
node_to_inode = dict(zip(list(graph.nodes), range(len(graph.nodes))))
for node1, node2, d in graph.edges(data=True):
factor = d['factor']
if which_CI in list_models_change_weights and alpha.get('K_edges_matrix') is not None:
K_edges_matrix = alpha.get('K_edges_matrix')
factor = factor**(1 / K_edges_matrix[i_node1, i_node2]) #Be careful: by setting self.factor this way, self.factor are not the true factors. As a consequence, if it's used only to compute the beliefs (by running CI or another algorithm) then it's fine. On the contrary, if it's recovered (Network.factor) or used for something else (Network() instantiated and then used for CI,CIpower,...etc), then I should change the code and take the powers into account only when updating the messages
i_node1 = node_to_inode[node1]
i_node2 = node_to_inode[node2]
self.factor[i_node1, i_node2] = factor
self.factor[i_node2, i_node1] = factor.T
else:
raise NotImplemented
if self.parallel_Mext:
self.factor = self.factor[..., np.newaxis] #expands the dimension
#Define the alpha (for each each edge). Convention: to compute M_ij, alpha[i,j] is considered (not alpha[j,i])
#i.e. M_ij = F(B_i - alpha_ij M_ji)
# print("dict_alpha_impaired", dict_alpha_impaired)
#Set self.temporal_alpha
if which_CI != 'CIbeliefs':
self.temporal_alpha = alpha.is_temporal(self.graph)
else: #which_CI == 'CIbeliefs':
self.temporal_alpha = False
assert self.temporal_alpha == False
# print("self.temporal_alpha", self.temporal_alpha)
# print("self.alpha", self.alpha)
# print(self.parallel_CI, self.temporal_alpha)
#Transform alpha into a dictionnary {alpha[i,j] for all oriented edges (i,j)}
if self.parallel_CI == False:
if which_CI != 'CIbeliefs':
self.alpha = alpha.get_alpha_dict(self.graph)
# print("self.alpha for a given node = ")
# if type(self.alpha[list(self.alpha)[0]]) == int:
# print(self.alpha)
# plt.plot(self.alpha[list(self.alpha)[0]])
# plt.show()
else: #which_CI == 'CIbeliefs':
self.alpha = alpha.get('dict_alpha_impaired')
else: #if self.parallel_CI:
assert which_CI != 'CIbeliefs'
if self.temporal_alpha:
print("TODO: find a faster way (inside call to Network)")
self.alpha = alpha.get_alpha_dict(self.graph)
#change alpha[i,j][t] into alpha[t][i,j] (in order to catch alpha[t] quickly)
self.alpha = {t: np.array([self.alpha[key][t] for key in self.alpha.keys()])
for t in range(self.alpha[list(self.alpha.keys())[0]])}
else:
self.alpha = alpha.to_matrix(graph)
if self.parallel_Mext:
# print("before (self.alpha)", self.alpha.shape, type(self.alpha))
self.alpha = self.alpha[..., np.newaxis] #expand the dimension
# print("after (self.alpha)", self.alpha.shape, type(self.alpha))
# print("self.alpha = {}".format(self.alpha))
# print("self.alpha: from {} to {}".format(np.min(self.alpha), np.max(self.alpha))) #only if self.alpha is a matrix (i.e., sef.parallel_CI = True)
#define K_node (for each node) - only needed for CIpower_approx models (because M_ij = 1/gamma_j * F(B_i - gamma_i / K_ij * M_ji)
if ('CIpower_approx' in which_CI and 'CIpower_approx_tanh' not in which_CI) or (which_CI == 'rate_network'): #CIpower_approx_tanh = CIpower with F ~ F_w_approx / CIpower_approx (resp. CIpower_approx_approx_tanh) is CIpower_approx model (resp. its approximation)
assert self.parallel_CI == True
assert alpha.get('K_nodes') is None
self.K_nodes = alpha.get('K_nodes_vector', np.array([1.]))
if self.K_nodes is None: #happens for instance for which_alpha = 'undirected'
self.K_nodes = np.array([1.])
# print("self.K_nodes = {}".format(self.K_nodes))
# print("before (self.K_nodes)", self.K_nodes.shape, type(self.K_nodes))
self.K_nodes = self.K_nodes[..., np.newaxis]
if self.parallel_Mext:
self.K_nodes = self.K_nodes[..., np.newaxis]
# print("after (self.K_nodes)", self.K_nodes.shape, type(self.K_nodes))
#Initiate the messages
if which_CI != 'CIbeliefs':
if self.parallel_CI == False:
self.M = dict(zip(list(get_all_oriented_edges(graph)), repeat(0)))
else: #parallel CI
if self.parallel_Mext == False:
self.M = np.zeros((len(graph.nodes), len(graph.nodes)))
else:
self.M = np.zeros((len(graph.nodes), len(graph.nodes), self.n_examples))
#Initiate B_history (history of the beliefs)
if self.keep_history_beliefs:
self.B_history = {}
for node in self.graph.nodes:
self.B_history[node] = []
if self.keep_history_beliefs and self.parallel_CI:
self.B_history = []
#Initiate M_history (history of the messages)
if self.keep_history_messages:
self.M_history = {}
for edge in self.graph.edges:
self.M_history[edge] = []
if self.keep_history_messages and self.parallel_CI:
self.M_history = []
#Initiate the beliefs
# self.B = {i: 0 for i in self.graph.nodes}
if self.parallel_CI == False:
self.neighbors = {i: self.get_neighbors(i) for i in self.graph.nodes} #{i: [j for j in self.graph.nodes if (j, i) in self.graph] for i in ....} #compute it once for all
def get_neighbors(self, node):
return list(self.graph.neighbors(node)) #ok because graph is undirected; otherwise use nx.all_neighbors(node) #self.graph.neighbors[node]
def get_Mext(self, t):
if self.constant_Mext:
return self.M_ext
else:
return self.M_ext[t]
def step_message_passing_CI(self, t):
"""
Non-vectorized message-passing (for vectorized version, see step_message_passing_CI_matrix)
Careful with the convention: M_ij = F_ij(B_i - alpha_ij M_ji)
i.e. alpha_ij appears in the computation of M_ij, but is in front of the term M_ji
In CI we consider the matrix alpha to be potentially non-symmetric --> without any constraint ("directed") / decomposed as a product of nodal term and edge term ("directed ratio": alpha_ij = Knode_i / Kedge_ij) / symmmetry contraint ("undirected": alpha = 1 / K_edge_ij) / only as a nodal term ("nodal": alpha_ij = Knode_i)
"""
# M_old = M.copy()
if self.keep_history_beliefs == False:
sum_M = self.compute_beliefs(t-1)
else:
sum_M = {node: val[-1] for node, val in self.B_history.items()}
# print("sum_M = {}".format(sum_M))
#Copy self.M
M_old = copy.copy(self.M) #shallow/deep copy? --> shallow, but OK: one can modify one without modifying the other - equivalent to self.M.copy()
if self.temporal_alpha: #alpha varies with time
if self.with_factors == False:
for (i,j) in self.M:
self.M[i, j] = (1-self.damping) * F_w(sum_M[i] - self.alpha[i, j][t] * M_old[j, i], self.w[i, j]) + self.damping * M_old[i, j]
#self.M[i, j] = F_w(sum_M[i] - self.alpha[i, j][t] * M_old[j, i], self.w[i, j])
else:
for (i,j) in self.M:
#self.M[i, j] = F_f(sum_M[i] - self.alpha[i, j][t] * M_old[j, i], self.factor[i, j])
self.M[i, j] = (1-self.damping) * F_f(sum_M[i] - self.alpha[i, j][t] * M_old[j, i], self.factor[i, j]) + self.damping * M_old[i, j]
else: #default
if self.with_factors == False:
# for (i,j) in self.M:
# #self.M[i, j] = F_w(sum_M[i] - self.alpha[i, j] * M_old[j, i], self.w[i, j])
# self.M[i, j] = (1-self.damping) * F_w(sum_M[i] - self.alpha[i, j] * M_old[j, i], self.w[i, j]) + self.damping * M_old[i, j]
#compute_message = lambda edge: F_w(sum_M[edge[0]] - self.alpha[edge] * M_old[edge[1], edge[0]], self.w[edge])
compute_message = lambda edge: (1-self.damping) * F_w(sum_M[edge[0]] - self.alpha[edge] * M_old[edge[1], edge[0]], self.w[edge]) + self.damping * M_old[edge[0], edge[1]]
self.M = dict(zip(self.M.keys(), map(compute_message, self.M.keys())))
else:
for (i,j) in self.M:
#self.M[i, j] = F_f(sum_M[i] - self.alpha[i, j] * M_old[j, i], self.factor[i, j])
self.M[i, j] = (1-self.damping) * F_f(sum_M[i] - self.alpha[i, j] * M_old[j, i], self.factor[i, j]) + self.damping * M_old[i, j]
# print({key: np.array(val).item() for key, val in self.M.items()})
del M_old
# print(self.M)
if self.keep_history_beliefs:
B_current = self.compute_beliefs(t)
for node in self.graph.nodes:
self.B_history[node].append(B_current[node])
if self.keep_history_messages:
for edge in self.graph.edges:
self.M_history[edge].append(self.M[edge])
def step_message_passing_CI_matrix(self, t):
"""
Vectorized message-passing
Easy vectorization = using matrices(alternatively one could use dataframes) for {M, B, alpha, W}. In this case the update equation is very simple: M_new = F(B-alpha*M^T, W)
It's probably faster than step_message_passing, but it won't automatically be faster: indeed, we will be doing operations on N_nodes*N_nodes instead of N_edges, i.e. on non-existent edges as well (for which the result will be 0)
TODO: What would be even better would be to vectorize on all edges (by using Mooij's PhD thesis?) if it's possible... (TODO = think about it)
Careful with the convention: M_ij = F(B_i - alpha_ij M_ji)
i.e. alpha_ij appears in the computation of M_ij, but is in front of the term M_ji
"""
if self.keep_history_beliefs == False:
sum_M = self.compute_beliefs_matrix(t-1)
else:
sum_M = self.B_history[-1]
# print("before (sum_M)", sum_M.shape, type(sum_M))
sum_M = sum_M[:, np.newaxis] #sum_M.reshape((-1,1))
# print("after (sum_M)", sum_M.shape, type(sum_M))
if self.temporal_alpha: #alpha varies with time
if self.with_factors == False:
dM = F_w(sum_M - self.alpha[t] * transpose(self.M), self.w)
else:
dM = F_f(sum_M - self.alpha[t] * transpose(self.M), self.factor)
else: #default
if self.with_factors == False:
# print("sum_M.shape = {}, self.alpha.shape = {}, self.M.shape = {}, transpose(self.M).shape = {}, self.w.shape = {}".format(sum_M.shape, self.alpha.shape, self.M.shape, transpose(self.M).shape, self.w.shape))
dM = F_w(sum_M - self.alpha * transpose(self.M), self.w)
else:
dM = F_f(sum_M - self.alpha * transpose(self.M), self.factor)
# print("dM.shape = {}".format(dM.shape))
self.M = (1-self.damping) * dM + self.damping * self.M #damping
# print({key: np.array(val).item() for key, val in self.M.items()})
# print(self.M)
# sys.exit()
if self.keep_history_beliefs:
B_current = self.compute_beliefs_matrix(t)
# print("B_current = {}".format(B_current))
self.B_history.append(B_current)
if self.keep_history_messages:
self.M_history.append(copy.copy(self.M)) #shallow copy but ok
def step_message_passing_CIpower_matrix(self, t):
"""
Extension of Fractional BP
Like step_message_passing_CI_matrix but with CIpower instead of CI
CIpower: M_ij = 1/alpha_ji . F_ij(B_i - alpha_ij . M_ji) where alpha_ij = K_i / K_ij, F is applied to f_ij**1/K_edges, and M_ext is replaced by 1/K_nodes * M_ext
CInew: M_ij = 1/alpha_ji . F_ij(B_i - alpha_ij . M_ji) where alpha_ij = K_i / K_ij (we still use step_message_passing_CIpower_matrix, with unchanged M_ext and w_ij)
"""
if self.keep_history_beliefs == False:
sum_M = self.compute_beliefs_matrix(t-1)
else:
sum_M = self.B_history[-1]
# print("before (sum_M)", sum_M.shape, type(sum_M))
sum_M = sum_M[:, np.newaxis]
# print("after (sum_M)", sum_M.shape, type(sum_M))
if self.temporal_alpha: #alpha varies with time
if self.with_factors == False:
dM = 1 / transpose(self.alpha[t]) * F_w(sum_M - self.alpha[t] * transpose(self.M), self.w)
else:
dM = 1 / transpose(self.alpha[t]) * F_f(sum_M - self.alpha[t] * transpose(self.M), self.factor)
else: #default
if self.with_factors == False:
dM = 1 / transpose(self.alpha) * F_w(sum_M - self.alpha * transpose(self.M), self.w)
else:
dM = 1 / transpose(self.alpha) * F_f(sum_M - self.alpha * transpose(self.M), self.factor)
self.M = (1-self.damping) * dM + self.damping * self.M #damping #before: (1-self.damping*self.alpha.T) (resp. (1-self.damping*self.alpha[t].T)) and no 1/self.alpha.T in dM ---> but it was a mistake)
# print({key: np.array(val).item() for key, val in self.M.items()})
# print(self.M)
if self.keep_history_beliefs:
B_current = self.compute_beliefs_matrix(t)
# print("B_current = {}".format(B_current))
self.B_history.append(B_current)
if self.keep_history_messages:
self.M_history.append(copy.copy(self.M)) #shallow copy but ok
def step_message_passing_CIpower_approx_tanh_matrix(self, t):
"""
Approximation of CIpower (= Fractional BP's extension)
CIpower_approx_tanh: M_ij = 1/alpha_ji . F_w_approx(B_i - alpha_ij . M_ji) where alpha_ij = K_i / K_ij, F_w_approx is applied to f_ij**1/K_edges, and M_ext is replaced by 1/K_nodes * M_ext
CIpower: M_ij = 1/alpha_ji . F_ij(B_i - alpha_ij . M_ji) where alpha_ij = K_i / K_ij, F is applied to f_ij**1/K_edges, and M_ext is replaced by 1/K_nodes * M_ext
"""
if self.keep_history_beliefs == False:
sum_M = self.compute_beliefs_matrix(t-1)
else:
sum_M = self.B_history[-1]
sum_M = sum_M[:, np.newaxis]
if self.temporal_alpha: #alpha varies with time
if self.with_factors == False:
dM = 1 / transpose(self.alpha[t]) * F_w_approx_tanh(sum_M - self.alpha[t] * transpose(self.M), self.w)
else:
dM = 1 / transpose(self.alpha[t]) * F_f_approx_tanh(sum_M - self.alpha[t] * transpose(self.M), self.factor)
else: #default
if self.with_factors == False:
dM = 1 / transpose(self.alpha) * F_w_approx_tanh(sum_M - self.alpha * transpose(self.M), self.w)
else:
dM = 1 / transpose(self.alpha) * F_f_approx_tanh(sum_M - self.alpha * transpose(self.M), self.factor)
self.M = (1-self.damping) * dM + self.damping * self.M
if self.keep_history_beliefs:
B_current = self.compute_beliefs_matrix(t)
self.B_history.append(B_current)
if self.keep_history_messages:
self.M_history.append(copy.copy(self.M)) #shallow copy but ok
def step_message_passing_full_CIpower_matrix(self, t):
"""
full_CIpower: M_ij = 1/alpha_ji . F_ij(B_i), F is applied to f_ij**1/K_edges, and M_ext is replaced by 1/K_nodes * M_ext
CIpower: M_ij = 1/alpha_ji . F_ij(B_i - alpha_ij . M_ji) where alpha_ij = K_i / K_ij, F is applied to f_ij**1/K_edges, and M_ext is replaced by 1/K_nodes * M_ext
There is no K_edges (in fact the model is by definition CIpower with K_edges = + inf)
"""
if self.keep_history_beliefs == False:
sum_M = self.compute_beliefs_matrix(t-1)
else:
sum_M = self.B_history[-1]
sum_M = sum_M[:, np.newaxis]
if self.temporal_alpha: #alpha varies with time
if self.with_factors == False:
dM = 1 / transpose(self.alpha[t]) * F_w(sum_M, self.w)
else:
dM = 1 / transpose(self.alpha[t]) * F_f(sum_M, self.factor)
else: #default
if self.with_factors == False:
dM = 1 / transpose(self.alpha) * F_w(sum_M, self.w)
else:
dM = 1 / transpose(self.alpha) * F_f(sum_M, self.factor)
self.M = (1-self.damping) * dM + self.damping * self.M
if self.keep_history_beliefs:
B_current = self.compute_beliefs_matrix(t)
self.B_history.append(B_current)
if self.keep_history_messages:
self.M_history.append(copy.copy(self.M)) #shallow copy but ok
def step_message_passing_full_CIpower_approx_tanh_matrix(self, t):
"""
full_CIpower_approx_tanh: M_ij = 1/alpha_ji . F_w_approx(B_i) where alpha_ij = K_i / K_ij, F_w_approx is applied to f_ij**1/K_edges, and M_ext is replaced by 1/K_nodes * M_ext
CIpower_approx_tanh: M_ij = 1/alpha_ji . F_w_approx(B_i - alpha_ij . M_ji) where alpha_ij = K_i / K_ij, F_w_approx is applied to f_ij**1/K_edges, and M_ext is replaced by 1/K_nodes * M_ext
"""
if self.keep_history_beliefs == False:
sum_M = self.compute_beliefs_matrix(t-1)
else:
sum_M = self.B_history[-1]
sum_M = sum_M[:, np.newaxis]
if self.temporal_alpha: #alpha varies with time
if self.with_factors == False:
dM = 1 / transpose(self.alpha[t]) * F_w_approx_tanh(sum_M, self.w)
else:
dM = 1 / transpose(self.alpha[t]) * F_f_approx_tanh(sum_M, self.factor)
else: #default
if self.with_factors == False:
dM = 1 / transpose(self.alpha) * F_w_approx_tanh(sum_M, self.w)
else:
dM = 1 / transpose(self.alpha) * F_f_approx_tanh(sum_M, self.factor)
self.M = (1-self.damping) * dM + self.damping * self.M
if self.keep_history_beliefs:
B_current = self.compute_beliefs_matrix(t)
self.B_history.append(B_current)
if self.keep_history_messages:
self.M_history.append(copy.copy(self.M)) #shallow copy but ok
def step_message_passing_CIpower_approx_matrix(self, t):
"""
Extension of CI in the case where alpha_ij in CI is a symmetrical matrix
Approximation of CIpower (= extension of Fractional BP)
CIpower_approx: M_ij = 1/K_j . F_ij(B_i - alpha_ij . M_ji) where alpha_ij = K_i / K_ij, F is applied to f_ij, and M_ext is replaced by 1/K_nodes * M_ext
CIpower: M_ij = 1/alpha_ji . F_ij(B_i - alpha_ij . M_ji) where alpha_ij = K_i / K_ij, F is applied to f_ij**1/K_edges, and M_ext is replaced by 1/K_nodes * M_ext
"""
if self.keep_history_beliefs == False:
sum_M = self.compute_beliefs_matrix(t-1)
else:
sum_M = self.B_history[-1]
sum_M = sum_M[:, np.newaxis]
assert self.temporal_alpha == False
if self.with_factors == False:
# print("F_w(sum_M - self.alpha * transpose(self.M), self.w).shape = {}".format(F_w(sum_M - self.alpha * transpose(self.M), self.w).shape))
# print("self.K_nodes.shape = {}".format(self.K_nodes.shape))
# dM = 1 / self.K_nodes * F_w(sum_M - self.alpha * transpose(self.M), self.w) #does indeed 1/K_j (= not 1/K_i) ---> actually wrong!! (it would be the case if self.K_nodes was a 1d-array, but here it is 2d or even 3d because of [:,np.newaxis])
dM = 1 / transpose(self.K_nodes) * F_w(sum_M - self.alpha * transpose(self.M), self.w)
else:
# dM = 1 / self.K_nodes * F_f(sum_M - self.alpha * transpose(self.M), self.factor) #does indeed 1/K_j (= not 1/K_i) ---> actually wrong!!
dM = 1 / transpose(self.K_nodes) * F_f(sum_M - self.alpha * transpose(self.M), self.factor)
self.M = (1-self.damping) * dM + self.damping * self.M #damping #before: (1-self.damping*self.alpha.T) (resp. (1-self.damping*self.alpha[t].T)) and no 1/self.alpha.T in dM ---> but it was a mistake)
# print({key: np.array(val).item() for key, val in self.M.items()})
# print(self.M)
if self.keep_history_beliefs:
B_current = self.compute_beliefs_matrix(t)
# print("B_current", B_current)
self.B_history.append(B_current)
if self.keep_history_messages:
self.M_history.append(copy.copy(self.M)) #shallow copy but ok
def step_message_passing_CIpower_approx_approx_tanh_matrix(self, t):
"""
Approximation of CIpower_approx (which is the extension of CI) with F = F_w ~ F_w_approx_tanh (without the arctanh)
CIpower_approx_approx_tanh: M_ij = 1/K_j . F_w_approx_tanh(B_i - alpha_ij . M_ji) where F_w_approx_tanh is F without the arctanh (just the tanh), alpha_ij = K_i / K_ij, F_w_approx_tanh is applied to f_ij, and M_ext is replaced by 1/K_nodes * M_ext
CIpower_approx: M_ij = 1/K_j . F_ij(B_i - alpha_ij . M_ji) where alpha_ij = K_i / K_ij, F is applied to f_ij, and M_ext is replaced by 1/K_nodes * M_ext
"""
if self.keep_history_beliefs == False:
sum_M = self.compute_beliefs_matrix(t-1)
else:
sum_M = self.B_history[-1]
sum_M = sum_M[:, np.newaxis]
assert self.temporal_alpha == False
if self.with_factors == False:
dM = 1 / transpose(self.K_nodes) * F_w_approx_tanh(sum_M - self.alpha * transpose(self.M), self.w)
else:
dM = 1 / transpose(self.K_nodes) * F_f_approx_tanh(sum_M - self.alpha * transpose(self.M), self.factor)
self.M = (1-self.damping) * dM + self.damping * self.M
if self.keep_history_beliefs:
B_current = self.compute_beliefs_matrix(t)
self.B_history.append(B_current)
if self.keep_history_messages:
self.M_history.append(copy.copy(self.M)) #shallow copy but ok
def step_message_passing_full_CIpower_approx_matrix(self, t):
"""
full_CIpower_approx: M_ij = 1/K_j . F_ij(B_j) where F is applied to f_ij, and M_ext is replaced by 1/K_nodes * M_ext
CIpower_approx: M_ij = 1/K_j . F_ij(B_i - alpha_ij . M_ji) where alpha_ij = K_i / K_ij, F is applied to f_ij, and M_ext is replaced by 1/K_nodes * M_ext
There is no K_edges (in fact the model is by definition CIpower_approx with K_edges = + inf)
"""
if self.keep_history_beliefs == False:
sum_M = self.compute_beliefs_matrix(t-1)
else:
sum_M = self.B_history[-1]
sum_M = sum_M[:, np.newaxis]
assert self.temporal_alpha == False
if self.with_factors == False:
dM = 1 / transpose(self.K_nodes) * F_w(sum_M, self.w)
else:
dM = 1 / transpose(self.K_nodes) * F_f(sum_M, self.factor)
self.M = (1-self.damping) * dM + self.damping * self.M
if self.keep_history_beliefs:
B_current = self.compute_beliefs_matrix(t)
self.B_history.append(B_current)
if self.keep_history_messages:
self.M_history.append(copy.copy(self.M)) #shallow copy but ok
def step_message_passing_full_CIpower_approx_approx_tanh_matrix(self, t):
"""
Approximation of full_CIpower_approx (which is the extension of CI) with F = F_w ~ F_w_approx_tanh (without the arctanh)
full_CIpower_approx_approx_tanh: M_ij = 1/K_j . F_w_approx_tanh(B_i) where F_w_approx_tanh is F without the arctanh (just the tanh), F_w_approx_tanh is applied to f_ij, and M_ext is replaced by 1/K_nodes * M_ext
CIpower_approx_approx_tanh: M_ij = 1/K_j . F_w_approx_tanh(B_i - alpha_ij . M_ji) where F_w_approx_tanh is F without the arctanh (just the tanh), alpha_ij = K_i / K_ij, F_w_approx_tanh is applied to f_ij, and M_ext is replaced by 1/K_nodes * M_ext
full_CIpower_approx: M_ij = 1/K_j . F_ij(B_j) where F is applied to f_ij, and M_ext is replaced by 1/K_nodes * M_ext
There is no K_edges (in fact the model is by definition CIpower_approx_approx_tanh with K_edges = + inf)
"""
if self.keep_history_beliefs == False:
sum_M = self.compute_beliefs_matrix(t-1)
else:
sum_M = self.B_history[-1]
sum_M = sum_M[:, np.newaxis]
assert self.temporal_alpha == False
if self.with_factors == False:
dM = 1 / transpose(self.K_nodes) * F_w_approx_tanh(sum_M, self.w)
else:
dM = 1 / transpose(self.K_nodes) * F_f_approx_tanh(sum_M, self.factor)
self.M = (1-self.damping) * dM + self.damping * self.M
if self.keep_history_beliefs:
B_current = self.compute_beliefs_matrix(t)
self.B_history.append(B_current)
if self.keep_history_messages:
self.M_history.append(copy.copy(self.M)) #shallow copy but ok
def step_message_passing_CIbeliefs2(self, t):
"""
M_ij(t+1) = F_ij(B_i(t) - alpha_ij B_j(t-1))
Question: should we use B_j(t+1) = sum_i F_ij(B_i(t) - alpha_ij B_j(t-1)), or M_ij(t+1) = F_ij(B_i(t) - alpha_ij B_j(t-1))?
I implemented the 2nd version
keep_history_beliefs needs to be True (the history of the beliefs is needed in the equation - actually the last belief)
"""
assert self.damping == 0 #TODO: think of damping != 0 in this model --> one would need to compute M_old, right?
sum_M = {node: self.B_history[node][-1] for node in self.graph.nodes} #self.compute_beliefs(t-1)
sum_M_previous = {node: self.B_history[node][-2] for node in self.graph.nodes}
if self.temporal_alpha: #alpha varies with time
if self.with_factors == False:
for (i,j) in self.M:
self.M[i, j] = F_w(sum_M[i] - self.alpha[i, j][t] * sum_M_previous[j], self.w[i, j])
else:
for (i,j) in self.M:
self.M[i, j] = F_f(sum_M[i] - self.alpha[i, j][t] * sum_M_previous[j], self.factor[i, j])
else: #default
if self.with_factors == False:
# for (i,j) in self.M:
# self.M[i, j] = F_w(sum_M[i] - self.alpha[i, j] * sum_M_previous[j], self.w[i, j])
compute_message = lambda edge: F_w(sum_M[edge[0]] - self.alpha[edge] * sum_M_previous[edge[1]], self.w[edge])
self.M = dict(zip(self.M.keys(), map(compute_message, self.M.keys())))
else:
for (i,j) in self.M:
self.M[i, j] = F_f(sum_M[i] - self.alpha[i, j] * sum_M_previous[j], self.factor[i, j])
# print(self.M)
B_current = self.compute_beliefs(t)
for node in self.graph.nodes:
self.B_history[node].append(B_current[node])
# print("t = {}, len(B_history) = {}".format(t, len(self.B_history[list(self.graph.nodes)[0]])))
if self.keep_history_messages:
for edge in self.graph.edges:
self.M_history[edge].append(self.M[edge])
#@jit
def step_message_passing_CIbeliefs(self, t):
"""
Uses the following approximation:
$$B_j^t = \sum\limits_{i \in N(j)} F_{ij}(B_i^{t-1}) - \sum\limits_{k=2}^{k_{max}} \alpha_{j,k} B_j^{t-k}$$
(alpha are associated to nodes j, and not to edges as in CI)
keep_history_beliefs needs to be True (the history of the beliefs is needed in the equation)
Here alpha are in the opposite order: alpha_kmax, alpha_kmax-1, ..., alpha_3, alpha_2
TODO: Try to write it in a vectorized way
"""
assert self.damping == 0 #TODO: think of damping != 0 in this model --> B(t+1) = (1-damping)*F(B(t)) + damping*B(t) for instance?
if self.temporal_alpha: #alpha varies with time
print("not implemented")
sys.exit()
else: #default
if self.with_factors == False:
for j in self.B_history:
# print("alpha[j]", self.alpha[j])
F_wij_t_minus_1 = lambda i: F_w(self.B_history[i][t-1], self.w[i,j])
neighbors_contrib = np.sum(list(map(F_wij_t_minus_1 , self.neighbors[j]))) #np.sum([F_w(self.B_history[i][t-1], self.w[i,j]) for i in self.neighbors[j]])
self_inhib = np.dot(self.alpha[j][max(len(self.alpha[j])-len(self.B_history[j][:-1]),0):],
np.array(self.B_history[j])[-len(self.alpha[j]) - 1: -1]) #self.alpha[j][-len(self.B_history[j][:-1]):] (without the "len(self.alpha[j])") does not work: for instance [1,2,3][-0:] = [1,2,3] and not [] as we would like #added the np.array(list) in order to use jax.numpy (the input must be of type array in function dot)
# print("neighbors_contrib", neighbors_contrib)
# print("self_inhib", self_inhib)
# print("self.M_ext[j][t-1]", self.M_ext[j][t-1])
self.B_history[j].append(neighbors_contrib - self_inhib + self.M_ext[j][t-1]) #self.alpha[j] = ... , alpha_{j,3}, alpha_{j,2}. #self.alpha[j][-len(self.B_history[j]):] because at the beginning of the simulation the list self.alpha[j] has a bigger size than self.B_history[j]
else:
for j in self.B_history:
self.B_history[j].append(
np.sum([F_f(self.B_history[i][t-1], self.factor[i,j]) for i in self.neighbors[j]]) -
np.dot(self.alpha[j][-len(self.B_history[j][:-1]):],
np.array(self.B_history[j])[-len(self.alpha[j]) - 1: -1]) +
self.M_ext[j][t-1]
) #added the np.array(list) in order to use jax.numpy (the input must be of type array in function dot)
def step_message_passing_CIapprox(self, t):
"""
M_ij = F_w_approx(B_i - alpha_ij.M_ji, w_ij) where F_w_approx is an "arbitrary" function trying to fit F as a+b.p where p = sig(x) is the probability
"""
sum_M = self.compute_beliefs(t-1)
#copy self.M
M_old = copy.copy(self.M) #shallow/deep copy?
if self.temporal_alpha: #alpha varies with time
if self.with_factors == False:
for (i,j) in self.M:
#self.M[i, j] = F_w_approx(sum_M[i] - self.alpha[i, j][t] * M_old[j, i], self.w[i, j])
self.M[i, j] = (1-self.damping) * F_w_approx(sum_M[i] - self.alpha[i, j][t] * M_old[j, i], self.w[i, j]) + self.damping * M_old[i, j]
else:
for (i,j) in self.M:
#self.M[i, j] = F_f_approx(sum_M[i] - self.alpha[i, j][t] * M_old[j, i], self.factor[i, j])
self.M[i, j] = (1-self.damping) * F_f_approx(sum_M[i] - self.alpha[i, j][t] * M_old[j, i], self.factor[i, j]) + self.damping * M_old[i, j]
else: #default
if self.with_factors == False:
for (i,j) in self.M:
#self.M[i, j] = F_w_approx(sum_M[i] - self.alpha[i, j] * M_old[j, i], self.w[i, j])
self.M[i, j] = (1-self.damping) * F_w_approx(sum_M[i] - self.alpha[i, j] * M_old[j, i], self.w[i, j]) + self.damping * M_old[i, j]
else:
for (i,j) in self.M:
#self.M[i, j] = F_f_approx(sum_M[i] - self.alpha[i, j] * M_old[j, i], self.factor[i, j])
self.M[i, j] = (1-self.damping) * F_f_approx(sum_M[i] - self.alpha[i, j] * M_old[j, i], self.factor[i, j]) + self.damping * M_old[i, j]
del M_old
if self.keep_history_beliefs:
B_current = self.compute_beliefs(t)
for node in self.graph.nodes:
self.B_history[node].append(B_current[node])
if self.keep_history_messages:
for edge in self.graph.edges:
self.M_history[edge].append(self.M[edge])
def step_message_passing_CIapprox_tanh_matrix(self, t):
"""
M_ij = F_w_approx_tanh(B_i - alpha_ij.M_ji, w_ij) where F_w_approx_tanh is F without the arctanh (just the tanh)
"linearized" CI (using the linearization artanh(x)~x --> same equation as Srdjan's work, with a tanh)
To be more precise, we use artanh(J*tanh(x))~J*tanh(x)
So the "linearized" CI is not fully fully linearized but has a tanh
"""
if self.keep_history_beliefs == False:
sum_M = self.compute_beliefs_matrix(t-1)
else:
sum_M = self.B_history[-1]
sum_M = sum_M[:, np.newaxis] #sum_M.reshape((-1,1))
if self.temporal_alpha: #alpha varies with time
if self.with_factors == False:
dM = F_w_approx_tanh(sum_M - self.alpha[t] * transpose(self.M), self.w)
else:
dM = F_f_approx_tanh(sum_M - self.alpha[t] * transpose(self.M), self.factor)
else: #default
if self.with_factors == False:
dM = F_w_approx_tanh(sum_M - self.alpha * transpose(self.M), self.w)
else:
dM = F_f_approx_tanh(sum_M - self.alpha * transpose(self.M), self.factor)
self.M = (1-self.damping) * dM + self.damping * self.M #damping
if self.keep_history_beliefs:
B_current = self.compute_beliefs_matrix(t)
self.B_history.append(B_current)
if self.keep_history_messages:
self.M_history.append(copy.copy(self.M)) #shallow copy but ok
def step_message_passing_CIapprox_tanh(self, t):
"""
M_ij = F_w_approx_tanh(B_i - alpha_ij.M_ji, w_ij) where F_w_approx_tanh is F without the arctanh (just the tanh)
"linearized" CI (using the linearization artanh(x)~x --> same equation as Srdjan's work, with a tanh)
To be more precise, we use artanh(J*tanh(x))~J*tanh(x)
So the "linearized" CI is not fully fully linearized but has a tanh
"""
sum_M = self.compute_beliefs(t-1)
#copy self.M
M_old = copy.copy(self.M) #shallow/deep copy?
if self.temporal_alpha: #alpha varies with time
if self.with_factors == False:
for (i,j) in self.M:
self.M[i, j] = (1-self.damping) * F_w_approx_tanh(sum_M[i] - self.alpha[i, j][t] * M_old[j, i], self.w[i, j]) + self.damping * M_old[i, j]
# self.M[i, j] = F_w_approx_tanh(sum_M[i] - self.alpha[i, j][t] * M_old[j, i], self.w[i, j])
else:
for (i,j) in self.M: