-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrfbp.hpp
More file actions
1355 lines (1039 loc) · 36.7 KB
/
rfbp.hpp
File metadata and controls
1355 lines (1039 loc) · 36.7 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
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
#ifndef __rfbp_hpp__
#define __rfbp_hpp__
#include <rfbp.h>
template < class Mag >
double theta_node_update_approx (MagVec < Mag > m, Mag & M, const double * xi, MagVec < Mag > u, Mag & U, const Params < Mag > & params, const long int & nxi, const long int & nm)
{
#ifdef DEBUG
assert (nxi == nm);
#endif
static double maxdiff;
static double mu;
static double sigma2;
static Mag new_U;
#ifdef _OPENMP
#pragma omp barrier
#endif
new_U = U;
static std :: unique_ptr < Mag[] > h;
#ifdef _OPENMP
#pragma omp single
#endif
h.reset(new Mag[nm]);
Mag old_m_on = mag :: bar(M, new_U);
#ifdef _OPENMP
mu = 0.;
sigma2 = 0.;
#pragma omp barrier
#pragma omp for reduction (+ : mu, sigma2)
for (long int i = 0L; i < nxi; ++i)
{
h[i] = mag :: bar(m[i], u[i]);
const double hvalue = h[i].value();
mu += hvalue * xi[i];
sigma2 += (1. - hvalue * hvalue) * (xi[i] * xi[i]);
}
#else
std :: transform(m, m + nm, u,
h.get(), [](const Mag &mi, const Mag &ui)
{
return mag :: bar(mi, ui);
});
mu = std :: inner_product(h.get(), h.get() + nm, xi, 0., std :: plus < double >(), [](const Mag & hi, const double & xi_i){return hi.value() * xi_i;});
sigma2 = std :: inner_product(h.get(), h.get() + nxi, xi, 0., std :: plus < double >(), [](const Mag & hi, const double & xi_i){return (1. - hi.value() * hi.value()) * (xi_i * xi_i);});
#endif
const double dsigma2 = 2. * sigma2;
Mag new_u = mag :: merf < Mag >( mu / std :: sqrt(dsigma2) );
maxdiff = std :: abs( new_U - new_u );
#ifdef _OPENMP
#pragma omp single
#endif
new_U = mag :: damp(new_u, new_U, params.damping);
Mag new_m = old_m_on % new_U;
M = new_m;
const double g = std :: exp(- (mu * mu) / dsigma2) / std :: sqrt(M_PI * dsigma2);
const double old_m_on_value = old_m_on.value();
const double p0 = 2. * old_m_on_value * g / (1. + old_m_on_value * new_U.value());
const double pmu = p0 * (p0 + mu / sigma2);
const double psigma = p0 * (1. - mu / sigma2 - mu * p0) / dsigma2;
U = new_U;
#ifdef _OPENMP
#pragma omp for reduction (max : maxdiff)
#endif
for (long int i = 0L; i < nm; ++i)
{
const double hvalue = h[i].value();
new_u = mag :: convert < Mag >(mag :: clamp(xi[i] * (p0 + xi[i] * (hvalue * pmu + xi[i] * (1. - hvalue * hvalue) * psigma)),
-1. + epsilon, 1. - epsilon));
maxdiff = std :: max(maxdiff, std :: abs(new_u - u[i]) );
u[i] = mag :: damp(new_u, u[i], params.damping);
m[i] = h[i] % u[i];
}
return maxdiff;
}
template < class Mag >
double theta_node_update_accurate (MagVec < Mag > m, Mag & M, const double * xi, MagVec < Mag > u, Mag & U, const Params < Mag > & params, const long int & nxi, const long int & nm)
{
#ifdef DEBUG
assert (nxi == nm);
#endif
static double maxdiff;
static Mag new_U;
new_U = U;
static std :: unique_ptr < Mag[] > h;
#ifdef _OPENMP
#pragma omp single
#endif
h.reset(new Mag[nm]);
Mag old_m_on = mag :: bar(M, new_U);
#ifdef _OPENMP
static double mu;
static double sigma2;
mu = 0.;
sigma2 = 0.;
#pragma omp barrier
#pragma omp for reduction (+ : mu, sigma2)
for (long int i = 0L; i < nxi; ++i)
{
h[i] = mag :: bar(m[i], u[i]);
const double hvalue = h[i].value();
mu += hvalue * xi[i];
sigma2 += (1. - hvalue * hvalue) * (xi[i] * xi[i]);
}
#else
std :: transform(m, m + nm, u,
h.get(), [](const Mag &mi, const Mag &ui)
{
return mag :: bar(mi, ui);
});
const double mu = std :: inner_product(h.get(), h.get() + nm, xi, 0., std :: plus < double >(), [](const Mag & hi, const double &xi_i){return hi.value() * xi_i;});
const double sigma2 = std :: inner_product(h.get(), h.get() + nxi, xi, 0., std :: plus < double >(), [](const Mag & hi, const double &xi_i){return (1. - hi.value() * hi.value()) * (xi_i * xi_i);});
#endif
Mag new_u = mag :: merf < Mag >( mu / std :: sqrt(2. * sigma2));
#ifdef _OPENMP
#pragma omp single
#endif
new_U = mag :: damp(new_u, new_U, params.damping);
M = old_m_on % new_U;
U = new_U;
#ifdef _OPENMP
#pragma omp for reduction (max : maxdiff)
#endif
for (long int i = 0L; i < nm; ++i)
{
const double hvalue = h[i].value();
const double tmp = std :: sqrt( 2. * (sigma2 - (1. - hvalue * hvalue) * xi[i] * xi[i]));
new_u = mag :: erfmix(old_m_on, ((mu - hvalue * xi[i]) + xi[i]) / tmp , ((mu - hvalue * xi[i]) - xi[i]) / tmp);
maxdiff = std :: max(maxdiff, std :: abs(new_u - u[i]));
u[i] = mag :: damp(new_u, u[i], params.damping);
m[i] = h[i] % u[i];
}
return maxdiff;
}
template < class Mag >
double theta_node_update_exact (MagVec < Mag > m, Mag & M, const double * xi, MagVec < Mag > u, Mag & U, const Params < Mag > & params, const long int & nxi, const long int & nm)
{
#ifdef DEBUG
assert (nxi == nm);
#endif
static double maxdiff;
static double **leftC = nullptr,
**rightC = nullptr;
static Mag new_U;
static std :: unique_ptr < Mag[] > h;
#ifdef _OPENMP
#pragma omp barrier
#endif
#ifdef _OPENMP
#pragma omp single
{
#endif
maxdiff = 0.;
leftC = new double *[nm + 1L];
rightC = new double *[nm + 1L];
new_U = U;
h.reset(new Mag[nm]);
#ifdef _OPENMP
}
#endif
const long int z = static_cast < long int >( (nm + 1L) * .5);
Mag old_m_on = mag::bar(M, new_U);
leftC[0] = new double[nm];
std :: generate_n(leftC[0], nm, [](){return 1.;});
rightC[nm] = new double[nm];
std :: generate_n(rightC[nm], nm, [](){return 1.;});
#ifdef _OPENMP
#pragma omp barrier
#pragma omp for
#endif
for (long int i = 0L; i < nm; ++i)
{
h[i] = mag :: bar(m[i], u[i]);
leftC[i + 1L] = new double[i + 2L];
rightC[i] = new double[nm - i + 1L];
}
double hvalue = h[0].value();
leftC[1L][0] = (1. - hvalue * xi[0]) * .5;
leftC[1L][1] = (1. + hvalue * xi[0]) * .5;
for (long int i = 1L; i < nm; ++i)
{
hvalue = h[i].value();
leftC[i + 1L][0] = leftC[i][0] * (1. - hvalue * xi[i]) * .5;
for (long int j = 1L; j < i + 1L; ++j)
leftC[i + 1L][j] = leftC[i][j - 1L] * (1. + hvalue * xi[i]) * .5 + leftC[i][j] * (1. - hvalue * xi[i]) * .5;
leftC[i + 1L][i + 1L] = leftC[i][i] * (1. + hvalue * xi[i]) * .5;
}
hvalue = h[nm - 1L].value();
rightC[nm - 1L][0] = (1. - xi[nxi - 1L] * hvalue) * .5;
rightC[nm - 1L][1] = (1. + xi[nxi - 1L] * hvalue) * .5;
for (long int i = nm - 2L; i >= 0L; --i)
{
hvalue = h[i].value();
rightC[i][0] = rightC[i + 1L][0] * (1. - hvalue * xi[i]) * .5;
for (long int j = 1L; j < nm - i; ++j)
rightC[i][j] = rightC[i + 1L][j - 1] * (1. + hvalue * xi[i]) * .5 + rightC[i + 1L][j] * (1. - hvalue * xi[i]) * .5;
rightC[i][nm - i] = rightC[i + 1L][nm - i - 1L] * (1. + hvalue * xi[i]) * .5;
}
#ifdef DEBUG
assert (nm % 2L);
#endif
#ifdef _OPENMP
static double pm;
static double pp;
pm = 0.;
pp = 0.;
#pragma omp for reduction (+ : pm)
for (long int i = 0L; i < z; ++i) pm += rightC[0][i];
#pragma omp for reduction (+ : pp)
for (long int i = z ; i < nm + 1L; ++i) pp += rightC[0][i];
#else
const double pm = std :: accumulate( rightC[0], rightC[0] + z, 0. );
const double pp = std :: accumulate( rightC[0] + z, rightC[0] + nm + 1L, 0. );
#endif
Mag new_u = mag :: couple < Mag >(pp, pm);
#ifdef DEBUG
assert ( !mag :: isinf < Mag >(new_u.mag) );
#endif
#ifdef _OPENMP
#pragma omp single
#endif
new_U = mag :: damp(new_u, new_U, params.damping);
M = old_m_on % new_U;
#ifdef _OPENMP
#pragma omp single
#endif
U = new_U;
#ifdef DEBUG
assert ( !mag :: isinf < Mag >(M.mag) );
#endif
#ifdef _OPENMP
#pragma omp for reduction (max : maxdiff)
#endif
for (long int i = 0L; i < nm; ++i)
{
double pm_ = 0.;
double pz = 0.;
double pp_ = 0.;
#ifdef DEBUG
assert(xi[i] * xi[i] == 1.);
#endif
for (long int j = 0L; j < nm; ++j)
{
double p = 0.;
for (long int k = static_cast < long int >(std :: max(0L, j + i - nm + 1L));
k < static_cast < long int >(std :: min(j, i) + 1L);
++k)
p += leftC[i][k] * rightC[i + 1L][j - k];
if (j < z - 1L) pm_ += p;
else if (j == z - 1L) pz = p;
else pp_ += p;
}
const Mag mp = mag :: convert < Mag >(mag :: clamp(pp_ + xi[i] * pz - pm_, -1., 1.));
const Mag mm = mag :: convert < Mag >(mag :: clamp(pp_ - xi[i] * pz - pm_, -1., 1.));
new_u = mag :: exactmix(old_m_on, mp, mm);
maxdiff = static_cast < double >(std :: max(maxdiff, std :: abs(new_u - u[i])));
u[i] = mag :: damp(new_u, u[i], params.damping);
m[i] = h[i] % u[i];
#ifdef DEBUG
assert ( !mag :: isinf < Mag >(u[i].mag) );
#endif
}
#ifdef _OPENMP
#pragma omp single
{
#endif
for (long int i = 0L; i < nm; ++i)
{
delete[] leftC[i];
delete[] rightC[i];
}
delete[] leftC;
delete[] rightC;
#ifdef _OPENMP
}
#endif
return maxdiff;
}
template < class Mag >
double free_energy_theta(const MagVec < Mag > m, const Mag & M, const double * xi, const MagVec < Mag > u, const Mag & U, const long int & nxi, const long int & nm)
{
#ifdef DEBUG
assert (nm == nxi);
#endif
std :: unique_ptr < Mag[] > h(new Mag[nm]);
const Mag old_m_on = mag :: bar(M, U);
// #ifdef _OPENMP
// #pragma omp barrier
// #pragma omp for reduction (+ : mu, sigma)
// for (long int i = 0L; i < nxi; ++i)
// {
// h[i] = mag :: bar(m[i], u[i]);
// const double hvalue = h[i].value;
// mu += h[i] * xi[i];
// sigma += (1. - hvalue * hvalue) * (xi[i] * xi[i]);
// }
// #else
std :: transform(m, m + nm, u,
h.get(), [](const Mag &mi, const Mag &ui)
{
return mag :: bar(mi, ui);
});
const double mu = std :: inner_product(h.get(), h.get() + nm, xi, 0., std :: plus < double >(), [](const Mag & hi, const double &xi_i){return hi.value() * xi_i;});
const double sigma = std :: inner_product(h.get(), h.get() + nxi, xi, 0., std :: plus < double >(), [](const Mag & hi, const double &xi_i){return (1. - hi.value() * hi.value()) * (xi_i * xi_i);});
// #endif
const Mag b = mag :: merf < Mag >( mu / std :: sqrt( 2. * sigma ) );
double f = -mag :: log1pxy(old_m_on, b);
#ifdef DEBUG
assert( !mag :: isinf < Mag >(f) );
#endif
// #ifdef _OPENMP
// #pragma omp for reduction (+ : f)
// for (long int i = 0L; i < nm; ++i) f += mag :: log1pxy(h[i], u[i]);
// #else
f = std :: inner_product(h.get(), h.get() + nm, u, f, std :: plus < double >(), [](const Mag & hi, const Mag &ui){return mag :: log1pxy(hi, ui);});
// #endif
return f;
}
template < class Mag >
double free_energy_theta_exact(MagVec < Mag > m, const Mag & M, const double * xi, MagVec < Mag > u, const Mag & U, const long int & nm)
{
double ** leftC = nullptr;
leftC = new double*[nm];
std :: unique_ptr < Mag[] > h(new Mag[nm]);
const Mag old_m_on = mag :: bar(M, U);
const long int z = static_cast < long int >( (nm + 1L) * .5);
// #ifdef _OPENMP
// #pragma omp barrier
// #pragma omp for
// for (long int i = 0L; i < nm; ++i)
// {
// h[i] = mag :: bar(m[i], u[i]);
// leftC[i] = new double[i + 2L];
// // leftC[i] = std :: make_unique < double[] > (i + 2L);
// }
// #else
std :: transform(m, m + nm, u,
h.get(), [](const Mag &mi, const Mag &ui)
{
return mag :: bar(mi, ui);
});
for (long int i = 0L; i < nm; ++i)
leftC[i] = new double[i + 2L];
// #endif
double hvalue = h[0].value();
leftC[0][0] = (1. - hvalue * xi[0]) * .5;
leftC[0][1] = (1. + hvalue * xi[0]) * .5;
for (long int i = 1L; i < nm; ++i)
{
hvalue = h[i].value();
leftC[i][0] = leftC[i - 1L][0] * (1. - (hvalue * xi[i])) * .5;
for (long int j = 1L; j < i + 1L; ++j)
leftC[i][j] = leftC[i - 1L][j - 1L] * (1. + (hvalue * xi[i])) * .5 + leftC[i - 1L][j] * (1. - (hvalue * xi[i])) * .5;
leftC[i][i + 1L] = leftC[i - 1L][i] * (1. + (hvalue * xi[i])) * .5;
}
#ifdef DEBUG
assert (nm % 2L);
#endif
// #ifdef _OPENMP
// #pragma omp for reduction (+ : pm)
// for (long int i = 0; i < z; ++i) pm += leftC[nm - 1L][i];
// #pragma omp for reduction (+ : pp)
// for (long int i = z; i < nm + 1; ++i) pp += leftC[nm - 1L][i];
// #else
const double pm = std :: accumulate(leftC[nm - 1L], leftC[nm - 1L] + z, 0.);
const double pp = std :: accumulate(leftC[nm - 1L] + z, leftC[nm - 1L] + nm + 1, 0.);
// #endif
const Mag b= mag :: couple < Mag >(pp, pm);
double f = -mag :: log1pxy(old_m_on, b);
#ifdef DEBUG
assert ( !mag :: isinf < Mag >(f) );
#endif
// #ifdef _OPENMP
// #pragma omp for reduction (+ : f)
// for (long int i = 0; i < nm; ++i) f += mag :: log1pxy(h[i], u[i]);
// #else
f = std :: inner_product(h.get(), h.get() + nm, u, f, std :: plus < double >(), [](const Mag & h, const Mag &u){return mag :: log1pxy(h, u);});
// #endif
// #ifdef _OPENMP
// #pragma omp single
// {
// #endif
for (long int i = 0L; i < nm; ++i) delete[] leftC[i];
delete[] leftC;
// #ifdef _OPENMP
// }
// #endif
return f;
}
template < class Mag >
double m_star_update(Mag & m_j_star, Mag & m_star_j, Params < Mag > & params) //old entro_node_update
{
#ifdef _OPENMP
#pragma omp barrier
#endif
Mag old_m_j_star = mag :: bar(m_j_star, m_star_j);
Mag new_m_star_j(0.);
if (mag :: isinf < Mag >(params.r)) // to check
new_m_star_j = (old_m_j_star != 0.) ?
(mag :: copysign(params.tan_gamma, old_m_j_star.value())) :
Mag(0.) ;
else
new_m_star_j = mag :: arrow( (old_m_j_star ^ params.tan_gamma), params.r ) ^ params.tan_gamma;
const double diff = std :: abs(new_m_star_j - m_star_j);
new_m_star_j = mag :: damp(new_m_star_j, m_star_j, params.damping);
const Mag new_m_j_star = old_m_j_star % new_m_star_j;
#ifdef _OPENMP
#pragma omp barrier
#endif
m_j_star = new_m_j_star;
m_star_j = new_m_star_j;
return diff;
}
template < class Mag >
double iterate(Cavity_Message < Mag > & messages, const Patterns & patterns, Params < Mag > & params)
{
const long int size = messages.M + messages.N * messages.K;
long int i;
long int j;
long int k;
double maxdiff = 0.;
Mag z(0.);
static std :: unique_ptr < long int[] > randperm;
static std :: unique_ptr < double[] > ones;
#ifdef _OPENMP
#pragma omp single
{
#endif
randperm.reset(new long int[size]);
ones.reset(new double[messages.K]);
#ifdef _OPENMP
}
#endif
#ifdef _OPENMP
#pragma omp barrier
#pragma omp for
for (long int i = 0L; i < messages.K; ++i) ones[i] = 1L;
#else
std :: fill_n(ones.get(), messages.K, 1L);
#endif
#ifdef _OPENMP
#pragma omp for
for (long int i = 0L; i < size; ++i) randperm[i] = i;
#else
std :: iota(randperm.get(), randperm.get() + size, 0L);
#endif
#ifdef _OPENMP
#pragma omp single
{
#endif
std :: mt19937 eng;
std :: shuffle(randperm.get(), randperm.get() + size, eng);
#ifdef _OPENMP
}
#endif
#if (__cplusplus < 201700) && !(__clang_major__ > 4)
auto tnu1 = get_accuracy < Mag >(params.accuracy1);
auto tnu2 = get_accuracy < Mag >(params.accuracy2);
#else
auto tnu1 = accuracy < Mag >[params.accuracy1];
auto tnu2 = accuracy < Mag >[params.accuracy2];
#endif
for (long int a = 0L; a < size; ++a)
{
#ifdef _OPENMP
#pragma omp barrier
#endif
if (randperm[a] < messages.M)
{
z = Mag(0.);
for (k = 0L; k < messages.K; ++k)
maxdiff = std :: max(maxdiff, tnu1(messages.m_j_star[k], messages.m_in[randperm[a]][k], patterns.input[randperm[a]], messages.weights[randperm[a]][k], messages.m_no[randperm[a]][k], params, patterns.Ncol, messages.N));
maxdiff = std :: max(maxdiff, tnu2(messages.m_in[randperm[a]], messages.m_on[randperm[a]], ones.get(), messages.m_ni[randperm[a]], z, params, messages.K, messages.K));
}
else
if (!(params.r == 0. || params.tan_gamma.value() == 0.) )
{
j = static_cast < long int >(randperm[a] - messages.M);
k = static_cast < long int >(static_cast < double >(j) / messages.N);
i = static_cast < long int >(j % messages.N);
maxdiff = std::max(maxdiff, m_star_update(messages.m_j_star[k][i], messages.m_star_j[k][i], params));
}
}
return maxdiff;
}
template < class Mag >
bool converge( Cavity_Message < Mag > & messages, const Patterns & patterns, Params < Mag > & params)
{
bool ok = false;
#ifdef VERBOSE
const auto start_time = what_time_is_it_now();
#endif
for (long int it = 0L; it < params.max_iters; ++it)
{
#ifdef _OPENMP
#pragma omp barrier
#endif
const double diff = iterate(messages, patterns, params);
#ifdef VERBOSE
#ifdef _OPENMP
#pragma omp single
#endif
#ifdef _WIN32
std :: cout << "\r[it="
#else
std :: cout << "\r\x1B[K[it="
#endif
<< it << " Delta=" << diff << " lambda=" << params.damping << "]";
#endif // verbose
if (diff < params.epsil)
{
ok = true;
#ifdef VERBOSE
#ifdef _OPENMP
#pragma omp single
#endif
std :: cout << std :: endl << "ok" << std :: endl;
#endif // verbose
break;
}
}
#ifdef VERBOSE
#ifdef _OPENMP
#pragma omp single
#endif
std :: cout << ( !ok ? "\nfailed\n" : "")
<< "elapsed time = "
<< duration(start_time)
<< " milliseconds"
<< std :: endl;
#endif
return ok;
}
long int * nonbayes_test (long int ** const sign_m_j_star, const Patterns & patterns, const long int & K)
{
long int * predicted_labels = new long int[patterns.Nrow];
#ifdef _OPENMP
#pragma omp barrier
#pragma omp for
#endif
for (long int i = 0L; i < patterns.Nrow; ++i)
{
const double s = std :: accumulate(sign_m_j_star, sign_m_j_star + K,
0., [&](const double & val, const long int * wk)
{
const double trsf0 = static_cast < double >(std :: inner_product( wk, wk + patterns.Ncol, patterns.input[i], 0., std :: plus < double >(), [](const long int & wki, const double & pi) { return wki * pi; }));
const double s2 = static_cast < double >(std :: inner_product( wk, wk + patterns.Ncol, patterns.input[i], 0., std :: plus < double >(), [](const long int & wki, const double & pi) { return (1L - wki * wki) * (pi * pi); }));
return val + std :: erf( trsf0 / std :: sqrt(2. * s2) );
});
predicted_labels[i] = static_cast < long int >(sign(s));
}
return predicted_labels;
}
template < class Mag >
long int error_test(Cavity_Message < Mag > & messages, const Patterns & patterns)
{
long int ** bin_weights = messages.get_weights();
long int * lbls = nonbayes_test(bin_weights, patterns, messages.K);
#ifdef _OPENMP
static long int t;
t = 0.;
#pragma omp barrier
#pragma omp for reduction (+ : t)
for (long int i = 0L; i < patterns.Nrow; ++i)
t += static_cast < long int >( lbls[i] != patterns.output[i] );
#else
const long int t = std :: inner_product(lbls, lbls + patterns.Nrow, patterns.output, 0L, std :: plus < long int >(), [](const long int &lbli, const long int &oi){ return static_cast < long int >( lbli != oi );});
#endif
#ifdef _OPENMP
#pragma omp single
#endif
delete[] lbls;
return t;
}
template < class Mag >
double free_energy(const Cavity_Message < Mag > & messages, const Patterns & patterns, const Params < Mag > & params)
{
static double f;
f = 0.;
Mag z(0.);
static std :: unique_ptr < double[] > ones;
#ifdef _OPENMP
#pragma omp single
#endif
ones.reset(new double[messages.K]);
std :: unique_ptr < Mag[] > v(new Mag[messages.M]);
#ifdef _OPENMP
#pragma omp barrier
#pragma omp for
for (long int i = 0L; i < messages.K; ++i) ones[i] = 1.;
#else
std :: fill_n(ones.get(), messages.K, 1.);
#endif
#ifdef _OPENMP
#pragma omp for reduction (+ : f) collapse(2)
#endif
for (long int i = 0L; i < messages.M; ++i)
for (long int j = 0L; j < messages.K; ++j)
f += free_energy_theta(messages.m_j_star[j], messages.m_in[i][j], patterns.input[i], messages.weights[i][j], messages.m_no[i][j], patterns.Ncol, messages.N);
#ifdef _OPENMP
#pragma omp for reduction (+ : f)
#endif
for (long int i = 0L; i < messages.M; ++i)
f += free_energy_theta_exact(messages.m_in[i], messages.m_on[i], ones.get(), messages.m_ni[i], z, messages.K);
#ifdef _OPENMP
#pragma omp for reduction (+ : f) collapse(2)
#endif
for (long int i = 0L; i < messages.K; ++i)
for (long int j = 0L; j < messages.N; ++j)
{
for (long int k = 0L; k < messages.M; ++k) v[k] = messages.weights[k][i][j];
f -= mag :: logZ(messages.m_star_j[i][j], v.get(), messages.M);
f -= log2_over_2;
f += mag :: log1pxy(params.tan_gamma, -params.tan_gamma) * .5;
Mag old_m_j_star = mag :: bar(messages.m_j_star[i][j], messages.m_star_j[i][j]);
f += mag :: log1pxy(old_m_j_star, messages.m_star_j[i][j]);
f += mag :: mcrossentropy( mag :: arrow(old_m_j_star ^ params.tan_gamma, params.r + 1.),
old_m_j_star ^ params.tan_gamma);
}
return f / (messages.N * messages.K);
}
#ifdef STATS
template < class Mag >
double compute_S (const Cavity_Message < Mag > & messages, const Params < Mag > & params)
{
static double S;
S = 0.;
#ifdef _OPENMP
#pragma omp barrier
#pragma omp for reduction (+ : S) collapse(2)
#endif
for (long int i = 0L; i < messages.K; ++i)
for (long int j = 0L; j < messages.N; ++j)
{
Mag old_m_j_star = mag :: bar(messages.m_star_j[i][j], messages.m_j_star[i][j]);
S += ( ( old_m_j_star ^ mag :: arrow( (old_m_j_star ^ params.tan_gamma), params.r ) ) % params.tan_gamma ).value();
}
return S / (messages.N * messages.K);
}
template < class Mag >
double compute_q_bar (const Cavity_Message < Mag > & messages, const Params < Mag > & params)
{
static double q_bar;
q_bar = 0.;
#ifdef _OPENMP
#pragma omp barrier
// #pragma omp declare reduction (sum_mags : Mag : omp_out = omp_out + omp_in ) initializer(omp_priv=0)
// #pragma omp for reduction (sum_mags : q_bar) collapse(2)
#pragma omp for reduction (+ : q_bar) collapse(2)
#endif
for (long int i = 0L; i < messages.K; ++i)
for (long int j = 0L; j < messages.N; ++j)
{
Mag old_m_j_star = mag :: bar(messages.m_j_star[i][j], messages.m_star_j[i][j]);
Mag mx = mag :: arrow( old_m_j_star ^ params.tan_gamma, params.r + 1.);
q_bar += (mx ^ mx).value();
}
return q_bar / (messages.N * messages.K);
}
template < class Mag >
double compute_q (const Cavity_Message < Mag > & messages, const long int & nm_j_star, const long int & nm_j_star_col)
{
static double q;
q = 0.;
#ifdef _OPENMP
#pragma omp barrier
#pragma omp for collapse (2) reduction (+ : q)
#endif
for (long int i = 0L; i < nm_j_star; ++i)
for (long int j = 0L; j < nm_j_star_col; ++j)
{
const double value = messages.m_j_star[i][j].value();
q += value * value;
}
return q / (messages.N * messages.K);
}
template < class Mag >
void mags_symmetry (const Cavity_Message < Mag > & messages, double * overlaps)
{
static double * qs;
qs = new double[messages.K];
#ifdef _OPENMP
#pragma omp barrier
#pragma omp for
for (long int i = 0L; i < messages.K; ++i) qs[i] = 0.;
#else
std :: fill_n(qs, messages.K, 0.);
#endif
#ifdef _OPENMP
#pragma omp for reduction(+: qs[:messages.K])
#endif
for (long int it = 0L; it < messages.K * messages.N; ++it)
{
std :: ldiv_t dv = std :: div(it, messages.N);
const double value = messages.m_j_star[dv.quot][dv.rem].value();
qs[dv.quot] += value * value;
}
#ifdef _OPENMP
#pragma omp for
#endif
for (long int i = 0L; i < messages.K; ++i) qs[i] = std :: sqrt(qs[i]);
#ifdef _OPENMP
#pragma omp for
#endif
for (long int i = 0L; i < messages.K; ++i) overlaps[ i * messages.K + i ] = 1.;
#ifdef _OPENMP
#pragma omp for
#endif
for (long int k1 = 0L; k1 < messages.K; ++k1)
for (long int k2 = k1 + 1L; k2 < messages.K; ++k2)
{
const double s = std :: inner_product(messages.m_j_star[k1], messages.m_j_star[k1] + messages.N,
messages.m_j_star[k2],
0., std :: plus < double >(),
[](const Mag & a, const Mag & b)
{
return a.value() * b.value();
}) / (qs[k1] * qs[k2]);
overlaps[k1*messages.K + k2] = s;
overlaps[k2*messages.K + k1] = s;
}
#ifdef _OPENMP
#pragma omp single
#endif
delete[] qs;
return; /* qs/N; */
}
#endif // STATS
#if (__cplusplus < 201700) && !(__clang_major__ > 4)
template < class Mag, typename std :: enable_if < std :: is_same < Mag, MagP64 > :: value > :: type * >
void set_outfields (const Cavity_Message < Mag > & message, const long int * output, const double & beta)
{
const double t = std :: tanh(beta * .5);
#ifdef _OPENMP
#pragma omp for
#endif
for (long int i = 0L; i < message.M; ++i)
message.m_on[i] = MagP64(output[i] * t);
}
template < class Mag, typename std :: enable_if < std :: is_same < Mag, MagT64 > :: value > :: type * >
void set_outfields (const Cavity_Message < Mag > & message, const long int * output, const double & beta)
{
const double t = std :: tanh(beta * .5);
#ifdef _OPENMP
#pragma omp for
#endif
for (long int i = 0L; i < message.M; ++i)
message.m_on[i] = MagT64(std :: atanh(output[i] * t));
}
template < class Mag, typename std :: enable_if < std :: is_same < Mag, MagP64 > :: value > :: type * >
theta_function < Mag > get_accuracy ( const std :: string & acc )
{
if ( acc == "accurate" ) return theta_node_update_accurate;
else if ( acc == "exact" ) return theta_node_update_exact;
else if ( acc == "none" ) return theta_node_update_accurate;
else
{
error_accuracy(acc);
return theta_node_update_exact;
}
}
template < class Mag, typename std :: enable_if < std :: is_same < Mag, MagT64 > :: value > :: type * >
theta_function < Mag > get_accuracy ( const std :: string & acc )
{
if ( acc == "accurate" ) return theta_node_update_accurate;
else if ( acc == "exact" ) return theta_node_update_exact;
else if ( acc == "none" ) return theta_node_update_accurate;
else
{
error_accuracy(acc);
return theta_node_update_exact;
}
}