-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
755 lines (658 loc) · 15.1 KB
/
Model.cpp
File metadata and controls
755 lines (658 loc) · 15.1 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
#include "Model.h"
using namespace std;
Model::Model() {
NI = NO = NF = NB = -1;
}
Model::Model(int idim, int odim, int fdim, int bdim, const string& type):
NI(idim), NO(odim), NF(fdim), NB(bdim), ptype(type) {
try {
model = Parameterisation::factory(NI, NO, NF, NB, ptype);
} catch(Parameterisation::BadParameterisationCreation e) {
cerr << e.what() << endl;
exit(EXIT_FAILURE);
}
Y = new Float[NO*MAX];
allocStats();
resetStats();
}
Model::Model(istream& is) {
read(is);
}
Model::~Model() {
delete model;
delete[] Y;
deallocStats();
}
void Model::read(istream& is) {
is >> ptype;
try {
model = Parameterisation::factory(is, ptype);
} catch(Parameterisation::BadParameterisationCreation e) {
cerr << e.what() << endl;
exit(EXIT_FAILURE);
}
NI = model->NI; // inputDim();
NO = model->NO; // outputDim();
NF = model->NF; // forwardStateDim();
NB = model->NB; // backwardStateDim();
Y = new Float[NO*MAX];
allocStats();
resetStats();
}
void Model::write(ostream& os) {
os << ptype << endl;
model->write(os);
}
void Model::randomize(int seed) {
//Net->initWeights(seed);
//NetF->initWeights(seed);
}
void Model::e_step(Instance* instance) {
int length = instance->length;
tree_alloc(length); // initialise tree
attachCPT(length); // attach CPTs to all clique potentials
// inject evidence
injectOut(instance->y, length); // multiply P_OFBI by outputs
injectIn(instance->u, length); // multiply P_OFBI by inputs
// propagate evidence
propagate(length);
// collect and store sufficient stats for all tables
sufficientStats(length);
//save the outputs into Y (to check the error)
saveOutput(length);
tree_dealloc(length);
}
void Model::m_step(Float att, Float prior) {
model->maximisation(P_OFBIss, P_FFIss, P_BBIss, att, prior);
resetStats();
}
void Model::predict(Instance* instance) {
int length = instance->length;
// initialise tree
tree_alloc(length);
attachCPT(length); // attach CPTs to all tables
// inject evidence
injectIn(instance->u, length); // multiply P_OFBI by inputs
// propagate evidence
propagate(length);
//save the outputs into Y
saveOutput(length);
// deallocate tree
tree_dealloc(length);
for(int t=1; t<=instance->length; ++t) {
Float pred = .0;
int arg = -1;
for(int c=0; c<NO; ++c) {
Float out_c = Y[NO*t + c];
if (out_c > pred) {
pred = out_c;
arg = c;
}
}
instance->y_pred[t] = arg;
}
}
void Model::allocStats() {
P_FFIss = new Float**[NI];
P_BBIss = new Float**[NI];
P_OFBIss = new Float***[NI];
for(int i=0; i<NI; ++i) {
P_FFIss[i] = new Float*[NF];
for(int f=0; f<NF; ++f)
P_FFIss[i][f] = new Float[NF];
P_BBIss[i] = new Float*[NB];
P_OFBIss[i] = new Float**[NB];
for(int b=0; b<NB; ++b) {
P_BBIss[i][b] = new Float[NB];
P_OFBIss[i][b] = new Float*[NF];
for(int f=0; f<NF; ++f)
P_OFBIss[i][b][f] = new Float[NO];
}
}
}
void Model::deallocStats() {
for (int i=0; i<NI; ++i) {
for (int f=0; f<NF; ++f)
delete[] P_FFIss[i][f];
for (int b=0; b<NB; ++b) {
delete[] P_BBIss[i][b];
for (int f=0; f<NF; ++f)
delete[] P_OFBIss[i][b][f];
delete[] P_OFBIss[i][b];
}
delete[] P_FFIss[i];
delete[] P_BBIss[i];
delete[] P_OFBIss[i];
}
delete[] P_FFIss;
delete[] P_BBIss;
delete[] P_OFBIss;
}
void Model::resetStats() {
int i,o,f,f1,b,b1;
for(int i=0; i<NI; ++i) {
for(int f=0; f<NF; ++f)
for(int f1=0; f1<NF; ++f1)
P_FFIss[i][f][f1] = .0;
for(int b=0; b<NB; ++b) {
for(int b1=0; b1<NB; ++b1)
P_BBIss[i][b][b1] =.0;
for(int f=0; f<NF; ++f)
for(int o=0; o<NO; ++o)
P_OFBIss[i][b][f][o] = .0;
}
}
}
int max(Float*a,int l) {
Float m=0;
int ml=-1;
for (int g=0;g<l;g++) {
if (a[g]>m) {
m=a[g];
ml=g;
}
}
return ml;
};
void Model::sufficientStats(int length) {
int t,i,f,f1,b,b1,o;
//int imax,bmax,b1max,fmax,f1max,omax;
Float* F;Float* F1;Float* B;Float *B1;Float* O;Float* I;
F=new Float[NF];
F1=new Float[NF];
B=new Float[NB];
B1=new Float[NB];
O=new Float[NO];
I=new Float[NI];
//resetStats();
for (t=1;t<=length;t++) {
memset(F,0,NF*sizeof(Float));
memset(F1,0,NF*sizeof(Float));
memset(B,0,NB*sizeof(Float));
memset(B1,0,NB*sizeof(Float));
memset(I,0,NI*sizeof(Float));
memset(O,0,NO*sizeof(Float));
for (i=0;i<NI;i++) {
for (b=0;b<NB;b++) {
for (f=0;f<NF;f++) {
for (o=0;o<NO;o++) {
// if (OFBI[t][i][b][f][o]>max) {
// max = OFBI[t][i][b][f][o];
F[f] += OFBI[t][i][b][f][o];
B[b] += OFBI[t][i][b][f][o];
I[i] += OFBI[t][i][b][f][o];
O[o] += OFBI[t][i][b][f][o];
// imax=i;fmax=f;bmax=b;omax=o;
// }
}
}
}
}
// cout << max << " " << flush;
// WARNING, POSSIBLE ERROR
// should second index be max(B,NB) and third max(F,NF)?!
P_OFBIss[max(I,NI)][max(B,NB)][max(F,NF)][max(O,NO)] += 1.0;
// cout << "r"<<flush;
for (i=0;i<NI;i++) {
for (f=0;f<NF;f++) {
for (f1=0;f1<NF;f1++) {
for (b=1;b<NB;b++) {
F1[f1] += FFBI[t][i][b][f][f1];
}
}
}
}
P_FFIss[max(I,NI)][max(F,NF)][max(F1,NF)] += 1.0;
for (i=0;i<NI;i++) {
for (b=0;b<NB;b++) {
for (b1=0;b1<NB;b1++) {
for (f=1;f<NF;f++) {
B1[b1] += BBFI[t][i][f][b][b1];
}
}
}
}
P_BBIss[max(I,NI)][max(B,NB)][max(B1,NB)] += 1.0;
}
delete[] F;
delete[] F1;
delete[] B;
delete[] B1;
delete[] O;
delete[] I;
}
void Model::tree_alloc(int length) {
int t,i,o,f,f1,b,b1;
mua=new Float[length+1];
mub=new Float[length+1];
mu1=new Float[length+1];
memset(mua,0,(length+1)*sizeof(Float));
memset(mub,0,(length+1)*sizeof(Float));
memset(mu1,0,(length+1)*sizeof(Float));
OFBI = new Float****[length+1];
FFBI = new Float****[length+1];
BBFI = new Float****[length+1];
// junction tree inference initilisation, step 1
// for each cluster and separator set, sets its corresponding
// potential to 1
for (t=0;t<=length;t++) {
OFBI[t]=new Float***[NI];
FFBI[t]=new Float***[NI];
BBFI[t]=new Float***[NI];
for (i=0;i<NI;i++) {
OFBI[t][i]=new Float**[NB];
FFBI[t][i]=new Float**[NB];
BBFI[t][i]=new Float**[NF];
for (b=0;b<NB;b++) {
OFBI[t][i][b]=new Float*[NF];
FFBI[t][i][b]=new Float*[NF];
for (f=0;f<NF;f++) {
OFBI[t][i][b][f]=new Float[NO];
for (o=0;o<NO;o++) {
OFBI[t][i][b][f][o]=1.0;
}
FFBI[t][i][b][f]=new Float[NF];
for (f1=0;f1<NF;f1++) {
FFBI[t][i][b][f][f1]=1.0;
}
}
}
for (f=0;f<NF;f++) {
BBFI[t][i][f]=new Float*[NB];
for (b=0;b<NB;b++) {
BBFI[t][i][f][b]=new Float[NB];
for (b1=0;b1<NB;b1++) {
BBFI[t][i][f][b][b1]=1.0;
}
}
}
}
}
// initialise separators
// NOTE: I don't understand why it's setting them to 0
// they should be set to 1 according to step 1 of the
// initialisation procedure
FBIa=new Float***[length+1];
FBIb=new Float***[length+1];
FB1=new Float**[length+1];
for (t=0;t<=length;t++) {
FBIa[t]=new Float**[NI];
FBIb[t]=new Float**[NI];
FB1[t]=new Float*[NB];
for (i=0;i<NI;i++) {
FBIa[t][i]=new Float*[NB];
FBIb[t][i]=new Float*[NB];
for (b=0;b<NB;b++) {
FBIa[t][i][b]=new Float[NF];
memset(FBIa[t][i][b],0,NF*sizeof(Float));
FBIb[t][i][b]=new Float[NF];
memset(FBIb[t][i][b],0,NF*sizeof(Float));
}
}
for (b=0;b<NB;b++) {
FB1[t][b] = new Float[NF];
memset(FB1[t][b],0,NF*sizeof(Float));
}
}
} // tree_alloc
void Model::tree_dealloc(int length) {
int t,i,f,b;
delete[] mua;
delete[] mub;
delete[] mu1;
for (t=0;t<=length;t++) {
for (i=0;i<NI;i++) {
for (b=0;b<NB;b++) {
for (f=0;f<NF;f++) {
delete[] OFBI[t][i][b][f];
delete[] FFBI[t][i][b][f];
}
delete[] OFBI[t][i][b];
delete[] FFBI[t][i][b];
}
for (f=0;f<NF;f++) {
for (b=0;b<NB;b++) {
delete[] BBFI[t][i][f][b];
}
delete[] BBFI[t][i][f];
}
delete[] OFBI[t][i];
delete[] FFBI[t][i];
delete[] BBFI[t][i];
}
delete[] OFBI[t];
delete[] FFBI[t];
delete[] BBFI[t];
}
delete[] OFBI;
delete[] FFBI;
delete[] BBFI;
for (t=0;t<=length;t++) {
for (i=0;i<NI;i++) {
for (b=0;b<NB;b++) {
delete[] FBIa[t][i][b];
delete[] FBIb[t][i][b];
}
delete[] FBIa[t][i];
delete[] FBIb[t][i];
}
for (b=0;b<NB;b++) {
delete[] FB1[t][b];
}
delete[] FBIa[t];
delete[] FBIb[t];
delete[] FB1[t];
}
delete[] FBIa;
delete[] FBIb;
delete[] FB1;
} // tree_dealloc
// this should correspond to the second step of the initialisation
// procedure of the junction tree for inference
void Model::attachCPT(int length) {
int t;
for (t=1;t<=length;t++) {
attachPOFBI(t);
attachPFFI(t);
attachPBBI(t);
}
attachPF();
attachPB(length);
} // attachCPT
void Model::attachPOFBI(int t) {
int i,o,f,b;
for (i=0;i<NI;i++) {
for (b=0;b<NB;b++) {
for (f=0;f<NF;f++) {
for (o=0;o<NO;o++) {
//cout << P_OFBI[i][b][f][o] << " ";
OFBI[t][i][b][f][o] *= model->p_OFBI(i, b, f, o);
}
}
}
}
//cout << endl;
} // attachPOFBI
void Model::attachPFFI(int t) {
int i,f,f1,b;
for (i=0;i<NI;i++) {
for (b=0;b<NB;b++) {
for (f=0;f<NF;f++) {
for (f1=0;f1<NF;f1++) {
//cout << P_FFI[i][f][f1] << " ";
FFBI[t][i][b][f][f1] *= model->p_FFI(i, f, f1);
//cout << FFBI[t][i][b][f][f1] << " ";
}
}
}
}
//cout << endl;
} // attachPFFI
void Model::attachPBBI(int t) {
int i,f,b,b1;
for (i=0;i<NI;i++) {
for (f=0;f<NF;f++) {
for (b=0;b<NB;b++) {
for (b1=0;b1<NB;b1++) {
BBFI[t][i][f][b][b1] *= model->p_BBI(i, b, b1);
}
}
}
}
} // attachPBBI
void Model::attachPF() {
int i,f,f1,b;
for (i=0;i<NI;i++) {
for (b=0;b<NB;b++) {
for (f=0;f<NF;f++) {
for (f1=0;f1<NF;f1++) {
FFBI[1][i][b][f][f1] *= model->P_F[f1];
}
}
}
}
} // attachPF
void Model::attachPB(int T) {
int i,f,b,b1;
for (i=0;i<NI;i++) {
for (f=0;f<NF;f++) {
for (b=0;b<NB;b++) {
for (b1=0;b1<NB;b1++) {
BBFI[T][i][f][b][b1] *= model->P_B[b1];
}
}
}
}
} // attachPB
void Model::injectIn(int* x, int length) {
int t,i,o,f,b;
for (t=1;t<=length;t++) {
for (i=0;i<NI;i++) {
for (b=0;b<NB;b++) {
for (f=0;f<NF;f++) {
for (o=0;o<NO;o++) {
if (i != x[t])
OFBI[t][i][b][f][o] = 0.0;
}
}
}
}
}
} // injectIn
void Model::injectOut(int* y, int length) {
int t,i,o,f,b;
for (t=1;t<=length;t++) {
for (i=0;i<NI;i++) {
for (b=0;b<NB;b++) {
for (f=0;f<NF;f++) {
for (o=0;o<NO;o++) {
if (o != y[t])
OFBI[t][i][b][f][o] = 0.0;
}
}
}
}
}
} // injectOut
void Model::propagate(int length) {
int t,i,f,f1,b,b1,o;
for (t=1;t<=length;t++) {
//marginalise F1FBI down to FBI
for (i=0;i<NI;i++) {
for (b=0;b<NB;b++) {
for (f=0;f<NF;f++) {
for (f1=0;f1<NF;f1++) {
FBIa[t][i][b][f] += FFBI[t][i][b][f][f1];
}
mua[t] += FBIa[t][i][b][f];
}
}
}
mua[t] = 1.0/mua[t];
//multiply OFBI by FBI
for (i=0;i<NI;i++) {
for (b=0;b<NB;b++) {
for (f=0;f<NF;f++) {
for (o=0;o<NO;o++) {
OFBI[t][i][b][f][o] *= FBIa[t][i][b][f]*mua[t];
}
}
}
}
//marginalise OFBI down to FBI
for (i=0;i<NI;i++) {
for (b=0;b<NB;b++) {
for (f=0;f<NF;f++) {
for (o=0;o<NO;o++) {
FBIb[t][i][b][f] += OFBI[t][i][b][f][o];
}
mub[t] += FBIb[t][i][b][f];
}
}
}
mub[t] = 1.0/mub[t];
//multiply BB1FI by FBI
for (i=0;i<NI;i++) {
for (f=0;f<NF;f++) {
for (b=0;b<NB;b++) {
for (b1=0;b1<NB;b1++) {
BBFI[t][i][f][b][b1] *= FBIb[t][i][b][f]*mub[t];
}
}
}
}
//marginalise BB1FI down to FB1
for (b1=0;b1<NB;b1++) {
for (f=0;f<NF;f++) {
for (i=0;i<NI;i++) {
for (b=0;b<NB;b++) {
// are we sure this isn't BBFI[t][i][f][b1][b]?
FB1[t][b1][f] += BBFI[t][i][f][b][b1];
}
}
mu1[t] += FB1[t][b1][f];
}
}
mu1[t] = 1.0/mu1[t];
//multiply F1FBI by FB1
if (t<length) {
for (i=0;i<NI;i++) {
for (b=0;b<NB;b++) {
for (f=0;f<NF;f++) {
for (f1=0;f1<NF;f1++) {
FFBI[t+1][i][b][f][f1] *= FB1[t][b][f1]*mu1[t]; //tricky: check
}
}
}
}
}
}
/*
// reset separators
for (i=0;i<NI;i++) {
for (b=0;b<NB;b++) {
memset(FBIa[i][b],0,NF*sizeof(Float));
memset(FBIb[i][b],0,NF*sizeof(Float));
}
}
for (b=0;b<NB;b++) {
memset(FB1[b],0,NF*sizeof(Float));
}
*/
for (t=1;t<=length;t++) {
// error -= log(1/mua[t])+log(1/mub[t])+log(1/mu1[t]);
mua[t]=mub[t]=mu1[t]=0;
}
Float temp=0;
// do it the other way around
// here it seems it's doing like the known message passing,
// dividing the new separator potential values by the old
// perhaps it should be done in the previous passage as well?!
for (t=1;t<=length;t++) {
//marginalise BB1FI down to FBI
for (i=0;i<NI;i++) {
for (f=0;f<NF;f++) {
for (b=0;b<NB;b++) {
temp=0;
for (b1=0;b1<NB;b1++) {
temp += BBFI[t][i][f][b][b1];
}
if (FBIb[t][i][b][f]!=0)
FBIb[t][i][b][f] = temp/FBIb[t][i][b][f];
mub[t] += FBIb[t][i][b][f];
}
}
}
mub[t] = 1.0/mub[t];
//multiply OFBI by FBI
for (i=0;i<NI;i++) {
for (b=0;b<NB;b++) {
for (f=0;f<NF;f++) {
for (o=0;o<NO;o++) {
OFBI[t][i][b][f][o] *= FBIb[t][i][b][f]*mub[t];
//cout << OFBI[t][i][b][f][o] << ' ';
}
}
}
}
//marginalise OFBI down to FBI
for (i=0;i<NI;i++) {
for (b=0;b<NB;b++) {
for (f=0;f<NF;f++) {
temp=0;
for (o=0;o<NO;o++) {
temp += OFBI[t][i][b][f][o];
}
if (FBIa[t][i][b][f]!=0)
FBIa[t][i][b][f] = temp/FBIa[t][i][b][f];
mua[t] += FBIa[t][i][b][f];
}
}
}
mua[t] = 1.0/mua[t];
//multiply F1FBI by FBI
for (i=0;i<NI;i++) {
for (b=0;b<NB;b++) {
for (f=0;f<NF;f++) {
for (f1=0;f1<NF;f1++) {
FFBI[t][i][b][f][f1] *= FBIa[t][i][b][f]*mua[t];
}
}
}
}
//marginalise F1FBI down to FB1
for (b=0;b<NB;b++) {
for (f1=0;f1<NF;f1++) {
temp=0;
for (f=0;f<NF;f++) {
for (i=0;i<NI;i++) {
temp += FFBI[t][i][b][f][f1];
}
}
// cout << temp << " " << flush;
if (FB1[t][b][f1]!=0)
FB1[t][b][f1] = temp/FB1[t][b][f1];
mu1[t] += FB1[t][b][f1];
}
}
mu1[t] = 1.0/mu1[t];
//multiply BB1FI by FB1
if (t>1) {
for (i=0;i<NI;i++) {
for (f=0;f<NF;f++) {
for (b=0;b<NB;b++) {
for (b1=0;b1<NB;b1++) {
BBFI[t-1][i][f][b][b1] *= FB1[t][b1][f]*mu1[t];
}
}
}
}
}
for (t=1;t<=length;t++) {
// error -= log(1/mua[t])+log(1/mub[t])+log(1/mu1[t]);
mua[t]=mub[t]=mu1[t]=0;
}
}
}
void Model::saveOutput(int length) {
int t,i,o,f,b;
Float tot=0;
memset(Y,0,NO*MAX*sizeof(Float));
for (t=1;t<=length;t++) {
for (i=0;i<NI;i++) {
for (b=0;b<NB;b++) {
for (f=0;f<NF;f++) {
for (o=0;o<NO;o++)
Y[NO*t+o] += OFBI[t][i][b][f][o];
}
}
}
tot=0;
for (o=0;o<NO;o++) {
tot += Y[NO*t+o];
if (Y[NO*t+o]>0) error -= log(Y[NO*t+o]);
}
for (o=0;o<NO;o++)
Y[NO*t+o] /= tot;
}
}