-
Notifications
You must be signed in to change notification settings - Fork 3
/
BattRAE.cpp
1269 lines (1114 loc) · 39.7 KB
/
BattRAE.cpp
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
#include "BattRAE.h"
BattRAE::BattRAE(Parameter* para, Vocabulary* vocab){
this -> para = para;
this -> vocab = vocab;
this -> save_times = 0;
this -> best_model = -1;
this -> best_score = -10000000;
//{ allocate the memory
__LBFGSALLOC__(x, get_x_size());
Map<VectorLBFGS>(x, get_x_size()).setRandom();
//}
this -> loadnet();
}
BattRAE::~BattRAE(){ __LBFGSFREE__(x);}
int BattRAE::get_src_dim(){
static int dim = atoi(para -> get_para("[src_dim]").c_str());
return dim;
}
int BattRAE::get_tgt_dim(){
static int dim = atoi(para -> get_para("[tgt_dim]").c_str());
return dim;
}
int BattRAE::get_att_dim(){
static int dim = atoi(para -> get_para("[att_dim]").c_str());
return dim;
}
int BattRAE::get_sem_dim(){
static int dim = atoi(para -> get_para("[sem_dim]").c_str());
return dim;
}
int BattRAE::get_src_size(){
static int dim = vocab -> get_source_size() * get_src_dim();
return dim;
}
int BattRAE::get_tgt_size(){
static int dim = vocab -> get_target_size() * get_tgt_dim();
return dim;
}
int BattRAE::get_vocab_size(){
static int dim = get_src_size() + get_tgt_size();
return dim;
}
int BattRAE::get_src_rae_size(){
int dim = get_src_dim();
return dim * 2 * dim + dim + 2 * dim * dim + 2 * dim;
}
int BattRAE::get_tgt_rae_size(){
int dim = get_tgt_dim();
return dim * 2 * dim + dim + 2 * dim * dim + 2 * dim;
}
int BattRAE::get_att_size(){
int src_dim = get_src_dim();
int tgt_dim = get_tgt_dim();
int att_dim = get_att_dim();
return att_dim * src_dim + att_dim * tgt_dim + att_dim;
}
int BattRAE::get_sem_size(){
int src_dim = get_src_dim();
int tgt_dim = get_tgt_dim();
int sem_dim = get_sem_dim();
return sem_dim * src_dim + sem_dim * tgt_dim + sem_dim + sem_dim * sem_dim + 1;
}
int BattRAE::get_wb_size(){
return get_src_rae_size()
+ get_tgt_rae_size()
+ get_att_size()
+ get_sem_size()
;
}
int BattRAE::get_x_size(){
return get_vocab_size() // vocabulary size
+ get_wb_size() // weight&bias size
;
}
void BattRAE::initnet(){
// default initialization of word embeddings
Map<VectorLBFGS> m_src_words(x, get_src_size());
m_src_words.setRandom();
int src_dim = get_src_dim();
for(size_t i = 0; i < (size_t) vocab -> get_source_size(); ++ i){
m_src_words.segment(i * src_dim, src_dim) /=
m_src_words.segment(i * src_dim, src_dim).norm();
}
Map<VectorLBFGS> m_tgt_words(x + get_src_size(), get_tgt_size());
m_tgt_words.setRandom();
int tgt_dim = get_tgt_dim();
for(size_t i = 0; i < (size_t) vocab -> get_target_size(); ++ i){
m_tgt_words.segment(i * tgt_dim, tgt_dim) /=
m_tgt_words.segment(i * tgt_dim, tgt_dim).norm();
}
{
string init_src_net_file = this -> para -> get_para("[init_src_net]");
ifstream is(init_src_net_file.c_str());
if(!is){
__FILE_MSG__(
"failed to read source net:\t" << init_src_net_file << endl
);
}else{
cout << "#Starting loading source net" << endl;
//{ init word embedding with pre-trained word embedding :)
string line; getline(is, line); vector<string> v_str = split_str(line, SPACE);
int _dim = atoi(v_str[1].c_str());
if(_dim != src_dim){
__FILE_MSG__(
"man, the word dimension of init net should be the same of configuration"
);
exit(1);
}
//}
while(getline(is, line)){
if("" == (line = strip_str(line))) continue;
v_str = split_str(line, SPACE);
string word = v_str[0];
// src?
if(vocab->word2id.find(SRC+word) != vocab->word2id.end()){
long id = vocab -> get_id(word, true);
for(size_t i = 1; i < v_str.size(); ++ i){
x[id * src_dim + i - 1] = atof(v_str[i].c_str());
}
}
}
is.close();
}
}
{
string init_tgt_net_file = this -> para -> get_para("[init_tgt_net]");
fstream is(init_tgt_net_file.c_str());
if(!is){
__FILE_MSG__(
"failed to read target net:\t" << init_tgt_net_file << endl
);
}else{
cout << "#Starting loading target net" << endl;
//{ init word embedding with pre-trained word embedding :)
string line; getline(is, line); vector<string> v_str = split_str(line, SPACE);
int _dim = atoi(v_str[1].c_str());
if(_dim != tgt_dim){
__FILE_MSG__(
"man, the word dimension of init net should be the same of configuration"
);
exit(1);
}
//}
while(getline(is, line)){
if("" == (line = strip_str(line))) continue;
v_str = split_str(line, SPACE);
string word = v_str[0];
// tgt?
if(vocab->word2id.find(TGT+word) != vocab->word2id.end()){
long id = vocab -> get_id(word, false);
for(size_t i = 1; i < v_str.size(); ++ i){
x[get_src_size() + (id - vocab -> get_source_size()) * tgt_dim + i - 1]
= atof(v_str[i].c_str());
}
}
}
is.close();
}
}
initWB();
}
void BattRAE::initWB(){
std::default_random_engine generator(time(NULL));
std::normal_distribution<double> distribution(0.0, 0.01);
for(int i = get_vocab_size(); i < get_x_size(); ++ i){
x[i] = distribution(generator);
}
{
int src_dim = get_src_dim();
__DEFINE_WBS__(x + get_vocab_size(), src_dim);
m_B_1.setZero(); m_B_2.setZero();
}
{
int tgt_dim = get_tgt_dim();
__DEFINE_WBS__(x + get_vocab_size() + get_src_rae_size(), tgt_dim);
m_B_1.setZero(); m_B_2.setZero();
}
{
int src_dim = get_src_dim();
int tgt_dim = get_tgt_dim();
int att_dim = get_att_dim();
int sem_dim = get_sem_dim();
__DEFINE_ATT_WBS__(x + get_vocab_size() + get_src_rae_size() + get_tgt_rae_size(),
src_dim, tgt_dim, att_dim, sem_dim);
m_Aw_b.setZero(); m_Sw_b.setZero(); m_Sb.setZero();
}
}
void BattRAE::savenet(bool is_best, bool inc_times){
if (inc_times){
++ save_times;
}
string prefix = "";
if (is_best) prefix = "best_";
string save_vocab = prefix + para -> get_para("[save_net_vocab]");
if (inc_times){
save_vocab += "_" + num2str(save_times);
}
int src_dim = get_src_dim();
int tgt_dim = get_tgt_dim();
int src_num = vocab -> get_source_size();
ofstream os(save_vocab.c_str());
//{ meta information
os << vocab -> word2id.size() << SPACE << src_dim << SPACE << tgt_dim << endl;
//}
//{ each word representation
Map<VectorLBFGS> m_words(x, get_vocab_size());
for(long i = 0; i < (long)vocab -> id2word.size(); ++ i){
string word = vocab -> get_word(i);
os << word << SPACE;
if(i < src_num){
os << m_words.segment((i - 0) * src_dim, src_dim).transpose() << endl;
}else{
os << m_words.segment(src_num * src_dim + (i - src_num) * tgt_dim, tgt_dim).transpose() << endl;
}
}
//}
os.close();
string save_net = prefix + para -> get_para("[save_net]");
if(inc_times){
save_net += "_" + num2str(save_times);
}
os.open(save_net.c_str());
os << Map<VectorLBFGS>(x, get_x_size()).transpose() << endl;
os.close();
}
void BattRAE::loadnet(string sfile){
string save_net = sfile;
if(sfile == "")
save_net = para -> get_para("[save_net]");
ifstream in(save_net.c_str());
if(!in){
__FILE_MSG__(
"failed to read:\t" << "\"" << save_net << "\""
);
return ;
}
string line = "";
stringstream ss;
getline(in, line);
ss << line;
copy(
istream_iterator<lbfgsfloatval_t>(ss),
istream_iterator<lbfgsfloatval_t>(),
x
);
if(getline(in, line)){
__FILE_MSG__(
"bad file format:\t" << endl <<
"file:\t" << "\"" << save_net << "\"" << endl <<
"line:\t" << "\"" << line << "\""
);
exit(1);
}
ss.clear(), ss.str("");
in.close();
}
// TODO: includ pretrain wrt RAE alone?
void BattRAE::train(){
// convert training file
cout << "#Converting the training file" << endl;
trainset.clear();
this -> file_lines =
vocab -> convert_train_file(
para -> get_para("[train_file]"), trainset);
cout << "#Finishing training file conversion" << endl;
// TrainIng
initnet();
_train_lbfgs();
}
void BattRAE::_train_lbfgs(){
// batching training
int echotimes = atoi(para -> get_para("[iter_num]").c_str());
// prepare the training parameter
lbfgs_parameter_t lbfgs_para;
lbfgs_parameter_init(&lbfgs_para);
lbfgs_para.max_iterations = echotimes;
lbfgsfloatval_t fx = 0.0;
int ret = lbfgs(get_x_size(), x, &fx, _evaluate, _progress, this, &lbfgs_para);
printf("L-BFGS optimization terminated with status code = %d\n", ret);
printf(" fx = %f\n", fx);
cout << "best model:\t" << best_model << endl;
cout << "best score:\t" << best_score << endl;
}
lbfgsfloatval_t BattRAE::bilattional_semantic(
int src_dim, // source word & phrase & sentence dimension
int tgt_dim, // target word & phrase & sentence dimension
int att_dim, // the attentional space dimension
int sem_dim, // the semantic space dimension
int src_word_num, // the source side word number in the vocabulary
int src_vocab_size, // the source side vocabulary size
int total_vocab_size, // the total vocabulary size
int src_rae_size, // the source side rae parameter size
int tgt_rae_size, // the target side rae parameter size
lbfgsfloatval_t* theta, // the whole parameters
lbfgsfloatval_t* grand, // the whole gradients
lbfgsfloatval_t alpha, // the alpha for loss balance from RAE & SEM
string src_instance, // the source side instance
string tgt_instance, // the target side instance
BiattSemValue* bsv // a structure of the required statistics
){
// 0. We can split the parameters first
lbfgsfloatval_t* theta_src_word = theta;
lbfgsfloatval_t* theta_tgt_word = theta + src_vocab_size;
lbfgsfloatval_t* theta_src_rae = theta + total_vocab_size;
lbfgsfloatval_t* theta_tgt_rae = theta_src_rae + src_rae_size;
lbfgsfloatval_t* theta_att_sem = theta_tgt_rae + tgt_rae_size;
lbfgsfloatval_t* grand_src_word = grand;
lbfgsfloatval_t* grand_tgt_word = grand + src_vocab_size;
lbfgsfloatval_t* grand_src_rae = grand + total_vocab_size;
lbfgsfloatval_t* grand_tgt_rae = grand_src_rae + src_rae_size;
// 1. We need build the tree structure from source side and target side respectively
Tree* src_tree = new Tree(src_instance
, src_dim
, 0
, bsv -> is_tree
, alpha
, theta_src_word
, theta_src_rae
, grand_src_word
, grand_src_rae);
Tree* tgt_tree = new Tree(tgt_instance
, tgt_dim
, src_word_num
, bsv -> is_tree
, alpha
, theta_tgt_word
, theta_tgt_rae
, grand_tgt_word
, grand_tgt_rae);
// 2. Construct the RAE representation matrix
int src_node_num = src_tree -> nodes.size();
int tgt_node_num = tgt_tree -> nodes.size();
// 2.1 Allocate memory for BiattSemValue
// The tree structure means the training processure
if(bsv -> is_tree){
__LBFGSALLOC__(bsv->theta_src_att_mat, src_node_num * att_dim); // S * n
__LBFGSALLOC__(bsv->theta_tgt_att_mat, tgt_node_num * att_dim); // T * n
__LBFGSALLOC__(bsv->grand_src_att_mat, src_node_num * att_dim); // S * n
__LBFGSALLOC__(bsv->grand_tgt_att_mat, tgt_node_num * att_dim); // T * n
__LBFGSALLOC__(bsv->grand_src_att_score, src_node_num * src_node_num); // S * S
__LBFGSALLOC__(bsv->grand_tgt_att_score, tgt_node_num * tgt_node_num); // T * T
__LBFGSALLOC__(bsv->theta_src_att_score, src_node_num * 1); // S * 1
__LBFGSALLOC__(bsv->theta_tgt_att_score, tgt_node_num * 1); // T * 1
__LBFGSALLOC__(bsv->theta_src_rae_mat, src_node_num * src_dim); // S * n1
__LBFGSALLOC__(bsv->theta_tgt_rae_mat, tgt_node_num * tgt_dim); // T * n2
__LBFGSALLOC__(bsv->grand_src_sem_rep, sem_dim * 1); // ns * 1
__LBFGSALLOC__(bsv->grand_tgt_sem_rep, sem_dim * 1); // nt * 1
__LBFGSALLOC__(bsv->theta_src_sem_rep, sem_dim * 1); // ns * 1
__LBFGSALLOC__(bsv->theta_tgt_sem_rep, sem_dim * 1); // nt * 1
__LBFGSALLOC__(bsv->theta_src_inst_representation, src_dim * 1); // n1 * 1
__LBFGSALLOC__(bsv->theta_tgt_inst_representation, tgt_dim * 1); // n2 * 1
__LBFGSALLOC__(bsv->grand_v_score, 1); // 1 * 1
__LBFGSALLOC__(bsv->grand_biattention_matrix, src_node_num * tgt_node_num);
}
__LBFGSALLOC__(bsv->biattention_matrix, src_node_num * tgt_node_num);
MatrixLBFGS src_rae_mat(src_node_num, src_dim);
MatrixLBFGS tgt_rae_mat(tgt_node_num, tgt_dim);
for(int i = 0; i < src_node_num; ++ i){
src_rae_mat.row(i) = Map<VectorLBFGS>(src_tree->nodes[i]->v_vector, src_dim).transpose();
}
for(int j = 0; j < tgt_node_num; ++ j){
tgt_rae_mat.row(j) = Map<VectorLBFGS>(tgt_tree->nodes[j]->v_vector, tgt_dim).transpose();
}
// 3. Transform the representation into the attentional space
__DEFINE_ATT_WBS__(theta_att_sem, src_dim, tgt_dim, att_dim, sem_dim);
// 3.1 w*x + b
MatrixLBFGS src_att_mat = src_rae_mat * m_Aw_s.transpose() + m_Aw_b.transpose().colwise().replicate(src_node_num);
MatrixLBFGS tgt_att_mat = tgt_rae_mat * m_Aw_t.transpose() + m_Aw_b.transpose().colwise().replicate(tgt_node_num);
// 3.2 f(z) <= tanh
// 3-for gradient
// We need the `gradient` of the src_att_mat as well as that of the tgt_att_mat
src_att_mat = (src_att_mat.array().exp() - (-1 * src_att_mat).array().exp()).array()
/ (src_att_mat.array().exp() + (-1 * src_att_mat).array().exp()).array();
tgt_att_mat = (tgt_att_mat.array().exp() - (-1 * tgt_att_mat).array().exp()).array()
/ (tgt_att_mat.array().exp() + (-1 * tgt_att_mat).array().exp()).array();
// 3.3 f'(z)
if(bsv->is_tree){
Map<MatrixLBFGS> grand_src_att_mat(bsv->grand_src_att_mat, src_node_num, att_dim);
Map<MatrixLBFGS> grand_tgt_att_mat(bsv->grand_tgt_att_mat, tgt_node_num, att_dim);
grand_src_att_mat = 1.0 - src_att_mat.array().square();
grand_tgt_att_mat = 1.0 - tgt_att_mat.array().square();
}
// 4. calculate the attention matrix & bi-attentional
// 4.1 S x n * n * T
MatrixLBFGS bi_att_mat = src_att_mat * tgt_att_mat.transpose();
bi_att_mat = 1.0 / (1.0 + (-1.0 * bi_att_mat).array().exp());
Map<MatrixLBFGS>(bsv->biattention_matrix, src_node_num, tgt_node_num) = bi_att_mat;
// 4.2 S, T
VectorLBFGS src_att_score = bi_att_mat.rowwise().sum();
VectorLBFGS tgt_att_score = bi_att_mat.colwise().sum();
src_att_score /= (1.0 * tgt_node_num);
tgt_att_score /= (1.0 * src_node_num);
// 4.3 SoftMax
// 4-for gradient
// We need calculate the `gradient` of softMax in this scoring
// We also need preserve the `src&tgt_att_mat` for the attention derivation
src_att_score = (src_att_score.array() - src_att_score.maxCoeff()).array().exp();
src_att_score /= src_att_score.sum();
tgt_att_score = (tgt_att_score.array() - tgt_att_score.maxCoeff()).array().exp();
tgt_att_score /= tgt_att_score.sum();
// 4.4 SoftMax'
if(bsv->is_tree){
MatrixLBFGS src_att_score_diag = src_att_score.asDiagonal() * 1.0;
Map<MatrixLBFGS> grand_src_att_score(bsv->grand_src_att_score, src_node_num, src_node_num);
grand_src_att_score = src_att_score_diag - 1.0 * (src_att_score * src_att_score.transpose());
MatrixLBFGS tgt_att_score_diag = tgt_att_score.asDiagonal() * 1.0;
Map<MatrixLBFGS> grand_tgt_att_score(bsv->grand_tgt_att_score, tgt_node_num, tgt_node_num);
grand_tgt_att_score = tgt_att_score_diag - 1.0 * (tgt_att_score * tgt_att_score.transpose());
Map<MatrixLBFGS> grand_biattention_matrix(bsv->grand_biattention_matrix, src_node_num, tgt_node_num);
grand_biattention_matrix = bi_att_mat.array() * (1.0 - bi_att_mat.array());
}
// 5. Generate the attentioned source&target representation
// 5-for gradient
// We need the `src_att_score` and `tgt_att_score` for prop into the matrix
// We also need the `src_rae_mat` and `tgt_rae_mat` for prop into the attention scorer layer
VectorLBFGS src_inst_representation = src_rae_mat.transpose() * src_att_score;
VectorLBFGS tgt_inst_representation = tgt_rae_mat.transpose() * tgt_att_score;
// 5.1 preserving representation
if(bsv->is_tree){
Map<VectorLBFGS> theta_src_inst_representation(bsv->theta_src_inst_representation, src_dim * 1);
theta_src_inst_representation = src_inst_representation;
Map<VectorLBFGS> theta_tgt_inst_representation(bsv->theta_tgt_inst_representation, tgt_dim * 1);
theta_tgt_inst_representation = tgt_inst_representation;
}
// 6. Transform the representation into the semantic space
// 6.1 w*x + b
VectorLBFGS src_sem_rep = m_Sw_s * src_inst_representation + m_Sw_b;
VectorLBFGS tgt_sem_rep = m_Sw_t * tgt_inst_representation + m_Sw_b;
// 6.2 f(z) <= tanh
// 6-for gradient
// We need the `gradient` of src_sem_rep and tgt_sem_rep for backprop
src_sem_rep = (src_sem_rep.array().exp() - (-1 * src_sem_rep).array().exp()).array()
/ (src_sem_rep.array().exp() + (-1 * src_sem_rep).array().exp()).array();
tgt_sem_rep = (tgt_sem_rep.array().exp() - (-1 * tgt_sem_rep).array().exp()).array()
/ (tgt_sem_rep.array().exp() + (-1 * tgt_sem_rep).array().exp()).array();
// 6.3 f'(z)
if(bsv->is_tree){
Map<VectorLBFGS> grand_src_sem_rep(bsv->grand_src_sem_rep, sem_dim);
grand_src_sem_rep = 1.0 - src_sem_rep.array().square();
Map<VectorLBFGS> grand_tgt_sem_rep(bsv->grand_tgt_sem_rep, sem_dim);
grand_tgt_sem_rep = 1.0 - tgt_sem_rep.array().square();
}
// 7. Calculate the semantic score
// 7-for gradient
// We need both `src_sem_rep` and `tgt_sem_rep` to obtain the source side and target side gradient
// 7.1 I considered for several times, now i decide to convert this score with the tanh translation
VectorLBFGS v_score = src_sem_rep.transpose() * m_Sw * tgt_sem_rep + m_Sb;
// only the training stage, we want to tanh the scoring
// this is to control the scale of the learned score
if(bsv->is_tree){
v_score = (v_score.array().exp() - (-1 * v_score).array().exp()).array()
/ (v_score.array().exp() + (-1 * v_score).array().exp()).array();
}
lbfgsfloatval_t score = v_score(0);
if(bsv->is_tree){
Map<VectorLBFGS> grand_v_score(bsv->grand_v_score, 1);
grand_v_score = 1.0 - v_score.array().square();
}
// 8. Preserving the internal variable for gradient training
bsv->semScore = score;
bsv->src_tree = src_tree;
bsv->tgt_tree = tgt_tree;
if(bsv->is_tree){
Map<MatrixLBFGS> theta_src_att_mat(bsv->theta_src_att_mat, src_node_num, att_dim);
theta_src_att_mat = src_att_mat;
Map<MatrixLBFGS> theta_tgt_att_mat(bsv->theta_tgt_att_mat, tgt_node_num, att_dim);
theta_tgt_att_mat = tgt_att_mat;
Map<VectorLBFGS> theta_src_att_score(bsv->theta_src_att_score, src_node_num);
theta_src_att_score = src_att_score;
Map<VectorLBFGS> theta_tgt_att_score(bsv->theta_tgt_att_score, tgt_node_num);
theta_tgt_att_score = tgt_att_score;
Map<MatrixLBFGS> theta_src_rae_mat(bsv->theta_src_rae_mat, src_node_num, src_dim);
theta_src_rae_mat = src_rae_mat;
Map<MatrixLBFGS> theta_tgt_rae_mat(bsv->theta_tgt_rae_mat, tgt_node_num, tgt_dim);
theta_tgt_rae_mat = tgt_rae_mat;
Map<VectorLBFGS> theta_src_sem_rep(bsv->theta_src_sem_rep, sem_dim);
theta_src_sem_rep = src_sem_rep;
Map<VectorLBFGS> theta_tgt_sem_rep(bsv->theta_tgt_sem_rep, sem_dim);
theta_tgt_sem_rep = tgt_sem_rep;
}
return score;
}
void BattRAE::bilattional_semantic_backprop(
int src_dim, // source word & phrase & sentence dimension
int tgt_dim, // target word & phrase & sentence dimension
int att_dim, // the attentional space dimension
int sem_dim, // the semantic space dimension
int src_word_num, // the source side word number in the vocabulary
int src_vocab_size, // the source side vocabulary size
int total_vocab_size, // the total vocabulary size
int src_rae_size, // the source side rae parameter size
int tgt_rae_size, // the target side rae parameter size
lbfgsfloatval_t* theta, // the whole parameters
lbfgsfloatval_t* grand, // the whole gradients
lbfgsfloatval_t flag, // positive or negative
BiattSemValue* bsv // bilingual attentional semantic values
){
// 0. We can split the parameters first
lbfgsfloatval_t* theta_src_rae = theta + total_vocab_size;
lbfgsfloatval_t* theta_tgt_rae = theta_src_rae + src_rae_size;
lbfgsfloatval_t* theta_att_sem = theta_tgt_rae + tgt_rae_size;
lbfgsfloatval_t* grand_src_rae = grand + total_vocab_size;
lbfgsfloatval_t* grand_tgt_rae = grand_src_rae + src_rae_size;
lbfgsfloatval_t* grand_att_sem = grand_tgt_rae + tgt_rae_size;
int src_node_num = bsv->src_tree->nodes.size();
int tgt_node_num = bsv->tgt_tree->nodes.size();
// 1. We can extract the internal value inside the `bsv`
Map<MatrixLBFGS> theta_src_att_mat(bsv->theta_src_att_mat, src_node_num, att_dim);
Map<MatrixLBFGS> theta_tgt_att_mat(bsv->theta_tgt_att_mat, tgt_node_num, att_dim);
Map<VectorLBFGS> theta_src_att_score(bsv->theta_src_att_score, src_node_num);
Map<VectorLBFGS> theta_tgt_att_score(bsv->theta_tgt_att_score, tgt_node_num);
Map<MatrixLBFGS> theta_src_rae_mat(bsv->theta_src_rae_mat, src_node_num, src_dim);
Map<MatrixLBFGS> theta_tgt_rae_mat(bsv->theta_tgt_rae_mat, tgt_node_num, tgt_dim);
Map<VectorLBFGS> theta_src_sem_rep(bsv->theta_src_sem_rep, sem_dim);
Map<VectorLBFGS> theta_tgt_sem_rep(bsv->theta_tgt_sem_rep, sem_dim);
Map<VectorLBFGS> theta_src_inst_representation(bsv->theta_src_inst_representation, src_dim);
Map<VectorLBFGS> theta_tgt_inst_representation(bsv->theta_tgt_inst_representation, tgt_dim);
Map<MatrixLBFGS> grand_src_att_mat(bsv->grand_src_att_mat, src_node_num, att_dim);
Map<MatrixLBFGS> grand_tgt_att_mat(bsv->grand_tgt_att_mat, tgt_node_num, att_dim);
Map<MatrixLBFGS> grand_src_att_score(bsv->grand_src_att_score, src_node_num, src_node_num);
Map<MatrixLBFGS> grand_tgt_att_score(bsv->grand_tgt_att_score, tgt_node_num, tgt_node_num);
Map<VectorLBFGS> grand_src_sem_rep(bsv->grand_src_sem_rep, sem_dim);
Map<VectorLBFGS> grand_tgt_sem_rep(bsv->grand_tgt_sem_rep, sem_dim);
Map<MatrixLBFGS> grand_biattention_matrix(bsv->grand_biattention_matrix, src_node_num, tgt_node_num);
Map<VectorLBFGS> grand_v_score(bsv->grand_v_score, 1);
// 2. backprop from the score into the semantic values
__DEFINE_ATT_DWBS__(grand_att_sem, src_dim, tgt_dim, att_dim, sem_dim);
__DEFINE_ATT_WBS__(theta_att_sem, src_dim, tgt_dim, att_dim, sem_dim);
VectorLBFGS sem_err = grand_v_score * flag;
m_DSb += sem_err;
m_DSw += (theta_src_sem_rep * theta_tgt_sem_rep.transpose()) * sem_err(0);
VectorLBFGS err_src_sem_rep = ((m_Sw * theta_tgt_sem_rep) * sem_err(0)).array() * grand_src_sem_rep.array();
VectorLBFGS err_tgt_sem_rep = ((theta_src_sem_rep.transpose() * m_Sw).transpose() * sem_err(0)).array() * grand_tgt_sem_rep.array();
// 3. backprop from the commen semantic space to the rae space
m_DSw_s += err_src_sem_rep * theta_src_inst_representation.transpose();
m_DSw_t += err_tgt_sem_rep * theta_tgt_inst_representation.transpose();
m_DSw_b += err_src_sem_rep + err_tgt_sem_rep;
VectorLBFGS err_src_inst_representation = m_Sw_s.transpose() * err_src_sem_rep;
VectorLBFGS err_tgt_inst_representation = m_Sw_t.transpose() * err_tgt_sem_rep;
// 4. backprop from the rae space into the rae tree
MatrixLBFGS err_src_rae_mat = theta_src_att_score * err_src_inst_representation.transpose();
for(int i = 0; i < src_node_num; ++ i){
Map<VectorLBFGS>(bsv->src_tree->nodes[i]->v_cerror, src_dim) +=
Map<MatrixLBFGS>(bsv->src_tree->nodes[i]->v_deriva, src_dim, src_dim) * err_src_rae_mat.row(i).transpose();
}
MatrixLBFGS err_tgt_rae_mat = theta_tgt_att_score * err_tgt_inst_representation.transpose();
for(int j = 0; j < tgt_node_num; ++ j){
Map<VectorLBFGS>(bsv->tgt_tree->nodes[j]->v_cerror, tgt_dim) +=
Map<MatrixLBFGS>(bsv->tgt_tree->nodes[j]->v_deriva, tgt_dim, tgt_dim) * err_tgt_rae_mat.row(j).transpose();
}
VectorLBFGS err_src_att_score = grand_src_att_score * (theta_src_rae_mat * err_src_inst_representation);
VectorLBFGS err_tgt_att_score = grand_tgt_att_score * (theta_tgt_rae_mat * err_tgt_inst_representation);
// 5. backprop from rae space into the attention space
MatrixLBFGS err_src_att_score_mat = (err_src_att_score * 1.0 / tgt_node_num).rowwise().replicate(tgt_node_num);
MatrixLBFGS err_tgt_att_score_mat = (err_tgt_att_score * 1.0 / src_node_num).transpose().colwise().replicate(src_node_num);
MatrixLBFGS err_bi_att_mat = grand_biattention_matrix.array() * (err_src_att_score_mat + err_tgt_att_score_mat).array();
MatrixLBFGS err_src_att_mat = (err_bi_att_mat * theta_tgt_att_mat).array() * grand_src_att_mat.array();
MatrixLBFGS err_tgt_att_mat = (err_bi_att_mat.transpose() * theta_src_att_mat).array() * grand_tgt_att_mat.array();
// 6. backprop from the attention space into the rae space
m_DAw_s += err_src_att_mat.transpose() * theta_src_rae_mat;
m_DAw_t += err_tgt_att_mat.transpose() * theta_tgt_rae_mat;
m_DAw_b += err_src_att_mat.colwise().sum() + err_tgt_att_mat.colwise().sum();
err_src_rae_mat = err_src_att_mat * m_Aw_s;
for(int i = 0; i < src_node_num; ++ i){
Map<VectorLBFGS>(bsv->src_tree->nodes[i]->v_cerror, src_dim) +=
Map<MatrixLBFGS>(bsv->src_tree->nodes[i]->v_deriva, src_dim, src_dim) * err_src_rae_mat.row(i).transpose();
}
err_tgt_rae_mat = err_tgt_att_mat * m_Aw_t;
for(int j = 0; j < tgt_node_num; ++ j){
Map<VectorLBFGS>(bsv->tgt_tree->nodes[j]->v_cerror, tgt_dim) +=
Map<MatrixLBFGS>(bsv->tgt_tree->nodes[j]->v_deriva, tgt_dim, tgt_dim) * err_tgt_rae_mat.row(j).transpose();
}
// 7. backprop from the rae space into the words
bsv->src_tree->backprop();
bsv->tgt_tree->backprop();
}
void BattRAE::train_a_instance(string instance,
int src_dim,
int tgt_dim,
int att_dim,
int sem_dim,
int total_vocab_size,
int src_vocab_size,
int tgt_vocab_size,
int src_rae_size,
int tgt_rae_size,
int src_word_num,
int tgt_word_num,
lbfgsfloatval_t alpha,
lbfgsfloatval_t& error,
long& ins_num,
lbfgsfloatval_t margin,
lbfgsfloatval_t& correct_sem_score,
lbfgsfloatval_t& incorrect_source_score,
lbfgsfloatval_t& incorrect_target_score,
lbfgsfloatval_t* theta,
lbfgsfloatval_t* grand){
vector<string> v_str = split_str(instance, MOSES_SEP);
if(v_str.size() != 4){
__FILE_MSG__(
"bad file format:\t" << "\"" << instance << "\""
);
exit(1);
}
string correct_source = v_str[0]; // the bilingual source phrase
string correct_target = v_str[1]; // the bilingual target phrase
string incorrect_source = v_str[2]; // the negative source phrase
string incorrect_target = v_str[3]; // the negative target phrase
// correct source
Tree* src_tree = new Tree(correct_source
, src_dim
, 0
, false
, 0.100
, theta
, theta + total_vocab_size
, grand
, grand);
correct_source = src_tree -> print_tree();
delete src_tree;
// correct target
Tree* tgt_tree = new Tree(correct_target
, tgt_dim
, src_word_num
, false
, 0.100
, theta + src_vocab_size
, theta + total_vocab_size + src_rae_size
, grand
, grand);
correct_target = tgt_tree -> print_tree();
delete tgt_tree;
// incorrect source
incorrect_source = replace_word(correct_source, incorrect_source);
// incorrect target
incorrect_target = replace_word(correct_target, incorrect_target);
// correct source Vs. correct target phrases
BiattSemValue* correct_bsv = new BiattSemValue(true);
bilattional_semantic(
src_dim,
tgt_dim,
att_dim,
sem_dim,
src_word_num,
src_vocab_size,
total_vocab_size,
src_rae_size,
tgt_rae_size,
theta,
grand,
alpha,
correct_source,
correct_target,
correct_bsv
);
lbfgsfloatval_t scorer = correct_bsv->semScore;
// correct source Vs. incorrect target phrase
BiattSemValue* incorrect_target_bsv = new BiattSemValue(true);
bilattional_semantic(
src_dim,
tgt_dim,
att_dim,
sem_dim,
src_word_num,
src_vocab_size,
total_vocab_size,
src_rae_size,
tgt_rae_size,
theta,
grand,
alpha,
correct_source,
incorrect_target,
incorrect_target_bsv
);
// incorrect source Vs. correct target phrase
BiattSemValue* incorrect_source_bsv = new BiattSemValue(true);
bilattional_semantic(
src_dim,
tgt_dim,
att_dim,
sem_dim,
src_word_num,
src_vocab_size,
total_vocab_size,
src_rae_size,
tgt_rae_size,
theta,
grand,
alpha,
incorrect_source,
correct_target,
incorrect_source_bsv
);
// max-margin training
// we want maximium the correcting score, but minimizing the incorrect score
// We need minimize the target objective, so the objective should be
// max{0, 1 - correct + incorrect}
// Thus, minimize upside, minimize incorrect, while maximize correct!
// 1.0 correct_bsv Vs incorrect_target_bsv
lbfgsfloatval_t _error = 0.0;
_error = margin - correct_bsv->semScore + incorrect_target_bsv->semScore;
if(_error > 0){
error += _error * (1.0 - alpha);
error += correct_bsv->src_tree->rfValue;
error += correct_bsv->tgt_tree->rfValue;
for(size_t i = 0; i < incorrect_target_bsv->src_tree->nodes.size(); ++i){
const Node* cur_node = incorrect_target_bsv->src_tree->nodes[i];
Map<VectorLBFGS>(cur_node->v_perror, cur_node->dim*2).setZero();
Map<VectorLBFGS>(cur_node->v_oerror, cur_node->dim*2).setZero();
}
for(size_t i = 0; i < incorrect_target_bsv->tgt_tree->nodes.size(); ++i){
const Node* cur_node = incorrect_target_bsv->tgt_tree->nodes[i];
Map<VectorLBFGS>(cur_node->v_perror, cur_node->dim*2).setZero();
Map<VectorLBFGS>(cur_node->v_oerror, cur_node->dim*2).setZero();
}
// correct ones
bilattional_semantic_backprop(
src_dim,
tgt_dim,
att_dim,
sem_dim,
src_word_num,
src_vocab_size,
total_vocab_size,
src_rae_size,
tgt_rae_size,
theta,
grand,
-1.0 * (1.0 - alpha),
correct_bsv
);
// incorrect target ones
bilattional_semantic_backprop(
src_dim,
tgt_dim,
att_dim,
sem_dim,
src_word_num,
src_vocab_size,
total_vocab_size,
src_rae_size,
tgt_rae_size,
theta,
grand,
1.0 * (1.0 - alpha),
incorrect_target_bsv
);
}
// correct source Vs. correct target phrases
// We need calcute this again, because the gradient is dirty upside
if(correct_bsv != NULL) delete correct_bsv;
correct_bsv = new BiattSemValue(true);
bilattional_semantic(
src_dim,
tgt_dim,
att_dim,
sem_dim,
src_word_num,
src_vocab_size,
total_vocab_size,
src_rae_size,
tgt_rae_size,
theta,
grand,
alpha,
correct_source,
correct_target,
correct_bsv
);
if(fabs(scorer - correct_bsv->semScore) > 1e-6) {
__FILE_MSG__(
"Two computation is inconsistent:\t" << "\"" << scorer << "\" Vs. \"" << correct_bsv->semScore << "\""
);
exit(1);
}
// 2.0 correct_bsv Vs incorrect_source_bsv
_error = margin - correct_bsv->semScore + incorrect_source_bsv->semScore;
if(_error > 0){
error += _error * (1.0 - alpha);
error += correct_bsv->src_tree->rfValue;
error += correct_bsv->tgt_tree->rfValue;
for(size_t i = 0; i < incorrect_source_bsv->src_tree->nodes.size(); ++i){
const Node* cur_node = incorrect_source_bsv->src_tree->nodes[i];
Map<VectorLBFGS>(cur_node->v_perror, cur_node->dim*2).setZero();
Map<VectorLBFGS>(cur_node->v_oerror, cur_node->dim*2).setZero();
}
for(size_t i = 0; i < incorrect_source_bsv->tgt_tree->nodes.size(); ++i){
const Node* cur_node = incorrect_source_bsv->tgt_tree->nodes[i];
Map<VectorLBFGS>(cur_node->v_perror, cur_node->dim*2).setZero();
Map<VectorLBFGS>(cur_node->v_oerror, cur_node->dim*2).setZero();
}
// correct ones
bilattional_semantic_backprop(
src_dim,
tgt_dim,
att_dim,
sem_dim,
src_word_num,
src_vocab_size,
total_vocab_size,
src_rae_size,
tgt_rae_size,
theta,
grand,
-1.0 * (1.0 - alpha),
correct_bsv
);
// incorrect target ones
bilattional_semantic_backprop(
src_dim,
tgt_dim,
att_dim,
sem_dim,
src_word_num,
src_vocab_size,
total_vocab_size,
src_rae_size,
tgt_rae_size,
theta,
grand,
1.0 * (1.0 - alpha),
incorrect_source_bsv
);
}
// collect the semantic score
correct_sem_score += correct_bsv->semScore;
incorrect_source_score += incorrect_source_bsv->semScore;
incorrect_target_score += incorrect_target_bsv->semScore;
ins_num += 1;
// free the memory
if(correct_bsv != NULL) delete correct_bsv;
if(incorrect_source_bsv != NULL) delete incorrect_source_bsv;
if(incorrect_target_bsv != NULL) delete incorrect_target_bsv;
}
lbfgsfloatval_t BattRAE::test_a_instance(string src, string tgt, lbfgsfloatval_t* x){
lbfgsfloatval_t* gTmp = NULL;
__LBFGSALLOC__(gTmp, get_x_size());
BiattSemValue* bsv = new BiattSemValue(false);
lbfgsfloatval_t score = bilattional_semantic(
get_src_dim(),
get_tgt_dim(),
get_att_dim(),
get_sem_dim(),
vocab->get_source_size(),
get_src_size(),
get_vocab_size(),
get_src_rae_size(),
get_tgt_rae_size(),
x,
gTmp,
0.0,
src,
tgt,
bsv
);
__LBFGSFREE__(gTmp);
if(bsv != NULL) delete bsv;
return score;
}
void BattRAE::test(){
string tst_file = para->get_para("[test_file]");
ifstream in(tst_file.c_str());
if(!in){
__FILE_MSG__(
"failed to read:\t" << "\"" << tst_file << "\""
);
exit(1);
}
vector<string> segs = split_str(tst_file, "/");
ofstream os((segs[segs.size()-1] + ".battrae").c_str());
string line = "";
vector<string> v_str;
cout << "#Starting procesing test file" << endl;
long count = 0;
while(getline(in, line)){
if((line = strip_str(line)) == "") continue;
v_str = split_str(line, MOSES_SEP);
if(v_str.size() < 2){
__FILE_MSG__(
"bad file format" << "\t\"" << line << "\""