-
Notifications
You must be signed in to change notification settings - Fork 0
/
RAE.cpp
837 lines (691 loc) · 17.2 KB
/
RAE.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
#include "RAE.h"
#include "ThreadPara.h"
static void* RAELBFGS::deepThread(void* arg)
{
RAEThreadPara* threadpara = (RAEThreadPara*)arg;
threadpara->lossVal = threadpara->cRAE->_training(threadpara->g);
pthread_exit(NULL);
}
RAE::RAE(Parameter* para, WordVec* words, int RAEType)
{
vecSize = atoi(para->getPara("WordVecSize").c_str());
this->words = words;
RAETree = NULL;
x = NULL;
this->RAEType = RAEType;
this->para = para;
weights1 = MatrixLBFGS(vecSize, 2*vecSize);
weights_b1 = MatrixLBFGS(1, vecSize);
weights2 = MatrixLBFGS(2*vecSize, vecSize);
weights_b2 = MatrixLBFGS(1, 2*vecSize);
delWeight1 = MatrixLBFGS(vecSize, 2*vecSize);
delWeight1_b = MatrixLBFGS(1, vecSize);
delWeight2 = MatrixLBFGS(2*vecSize, vecSize);
delWeight2_b = MatrixLBFGS(1, 2*vecSize);
delWeight1.setZero();
delWeight1_b.setZero();
delWeight2.setZero();
delWeight2_b.setZero();
}
int RAE::getRAEWeightSize()
{
return (vecSize*vecSize*2*2 + vecSize + vecSize*2);
}
RAE::RAE(int size)
{
RAETree = NULL;
this->vecSize = size;
delWeight1 = MatrixLBFGS(vecSize, vecSize*2);
delWeight1_b = MatrixLBFGS(1, vecSize);
delWeight2 = MatrixLBFGS(vecSize*2, vecSize);
delWeight2_b = MatrixLBFGS(1, vecSize*2);
delWeight1.setZero();
delWeight1_b.setZero();
delWeight2.setZero();
delWeight2_b.setZero();
}
lbfgsfloatval_t RAE::loss()
{
Node* tmpNode = this->RAETree->getRoot();
lbfgsfloatval_t val = 0;
while(tmpNode->getNodeType() != BASED_NODE)
{
val += tmpNode->getRecError();
tmpNode = tmpNode->getLeftChildNode();
}
return val;
}
//显示参数
void RAE::showWeights()
{
cout << "W1\t" << "Row: " << weights1.rows() << "\tCol: " << weights1.cols() << endl;
cout << weights1 << endl;
cout << "B1\t" << "Row: " << weights_b1.rows() << "\tCol: " << weights_b1.cols() << endl;
cout << weights_b1 << endl;
cout << "W2\t" << "Row: " << weights2.rows() << "\tCol: " << weights2.cols() << endl;
cout << weights2 << endl;
cout << "B2\t" << "Row: " << weights_b2.rows() << "\tCol: " << weights_b2.cols() << endl;
cout << weights_b2 << endl;
}
//保存参数到文件
void RAE::logWeights(Parameter* para)
{
string filename;
if(RAEType == SL)
{
filename = para->getPara("srcRAEWeightsLogFile");
}
else
{
filename = para->getPara("tgtRAEWeightsLogFile");
}
ofstream out(filename.c_str(), ios::out);
out << "W1" << endl;
out << weights1 << endl;
out << "B1" << endl;
out << weights_b1 << endl;
out << "W2" << endl;
out << weights2 << endl;
out << "B2" << endl;
out << weights_b2 << endl;
out.close();
}
void RAE::loadWeights(Parameter* para)
{
string filename;
if(RAEType == SL)
{
filename = para->getPara("srcRAEWeightsLogFile");
}
else
{
filename = para->getPara("tgtRAEWeightsLogFile");
}
ifstream in(filename.c_str(), ios::in);
bool rae_w1 = false;
bool rae_b1 = false;
bool rae_w2 = false;
bool rae_b2 = false;
int row = 0;
string line;
while(getline(in, line))
{
if(line.find("W1") == 0)
{
row = 0;
rae_w1 = true;
continue;
}
if(line.find("W2") == 0)
{
row = 0;
rae_b1 = false;
rae_w2 = true;
continue;
}
if(line.find("B1") == 0)
{
row = 0;
rae_w1 = false;
rae_b1 = true;
continue;
}
if(line.find("B2") == 0)
{
row = 0;
rae_w2 = false;
rae_b2 = true;
continue;
}
if(rae_w1)
{
stringstream ss(line);
for(int col = 0; col < weights1.cols(); col++)
{
ss >> weights1(row, col);
}
}
if(rae_w2)
{
stringstream ss(line);
for(int col = 0; col < weights2.cols(); col++)
{
ss >> weights2(row, col);
}
}
if(rae_b2)
{
stringstream ss(line);
for(int col = 0; col < weights_b2.cols(); col++)
{
ss >> weights_b2(row, col);
}
}
if(rae_b1)
{
stringstream ss(line);
for(int col = 0; col < weights_b1.cols(); col++)
{
ss >> weights_b1(row, col);
}
}
row++;
}
}
//构建RAE树
void RAE::buildTree(string bp)
{
vector<Node*> treeNodes;
int count = 0;
stringstream ss(bp);
string tmp;
if(RAETree)
{
delete RAETree;
RAETree = NULL;
}
while(ss >> tmp)
{
if(!words->isInDict(tmp))
{
MatrixLBFGS tmpVec = MatrixLBFGS(1, vecSize);
tmpVec.setZero();
words->m_words[tmp] = tmpVec;
}
Node* newNode = new Node(BASED_NODE, count, count, tmp, words->m_words[tmp], NULL, NULL, NULL);
treeNodes.push_back(newNode);
count++;
}
count--;
if(treeNodes.size() == 1)
{
RAETree = new Tree(treeNodes[0]);
return;
}
RAETree = new Tree();
//选取Erec最小的两个based节点
vector<lbfgsfloatval_t> v_recError;
for(int i = 0; i < treeNodes.size()-1; i++)
{
RAETree->root = treeNodes[i];
RAETree->merge(treeNodes[i+1], weights1, weights_b1, weights2, weights_b2);
v_recError.push_back(RAETree->getRoot()->getRecError());
delete RAETree->root;
RAETree->root = NULL;
}
delete RAETree;
RAETree = NULL;
int minNode = 0;
lbfgsfloatval_t minRecError = v_recError[0];
for(int i = 1; i< v_recError.size(); i++)
{
if(v_recError[i] < minRecError)
{
minNode = i;
minRecError = v_recError[i];
}
}
//建立RAE树
RAETree = new Tree(treeNodes[minNode]);
RAETree->merge(treeNodes[minNode+1], weights1, weights_b1, weights2, weights_b2);
treeNodes.erase(treeNodes.begin()+minNode, treeNodes.begin()+minNode+2);
if(treeNodes.size() == 0)
{
return;
}
Tree* tmpTree = new Tree();
//添加新节点直到覆盖整个短语
while(treeNodes.size() != 1)
{
int nodePos;
if(RAETree->getRoot()->getSpan().first == 0)
{
for(int i = 0; i < treeNodes.size(); i++)
{
if(treeNodes[i]->getSpan().first == RAETree->getRoot()->getSpan().second+1)
{
nodePos = i;
}
}
}
else if(RAETree->getRoot()->getSpan().second == count)
{
for(int i = 0; i < treeNodes.size(); i++)
{
if(treeNodes[i]->getSpan().second == RAETree->getRoot()->getSpan().first-1)
{
nodePos = i;
}
}
}
else
{
int pos1, pos2;
for(int i = 0; i < treeNodes.size(); i++)
{
if(treeNodes[i]->getSpan().first == RAETree->getRoot()->getSpan().second+1)
{
pos2 = i;
}
if(treeNodes[i]->getSpan().second == RAETree->getRoot()->getSpan().first-1)
{
pos1 = i;
}
}
lbfgsfloatval_t recError;
tmpTree->root = RAETree->getRoot();
tmpTree->merge(treeNodes[pos1], weights1, weights_b1, weights2, weights_b2);
recError = tmpTree->getRoot()->getRecError();
delete tmpTree->root;
tmpTree->root = NULL;
tmpTree->root = RAETree->getRoot();
tmpTree->merge(treeNodes[pos2], weights1, weights_b1, weights2, weights_b2);
if(tmpTree->getRoot()->getRecError() < recError)
{
nodePos = pos2;
}
else
{
nodePos = pos1;
}
delete tmpTree->getRoot();
tmpTree->root = NULL;
}
RAETree->merge(treeNodes[nodePos], weights1, weights_b1, weights2, weights_b2);
treeNodes.erase(treeNodes.begin()+nodePos);
}
delete tmpTree;
tmpTree = NULL;
RAETree->merge(treeNodes[0], weights1, weights_b1, weights2, weights_b2);
treeNodes.erase(treeNodes.begin());
}
int RAE::getVecSize()
{
return vecSize;
}
RAE* RAE::copy()
{
RAE* newRAE = new RAE(this->vecSize);
newRAE->vecSize = this->vecSize;
newRAE->words = this->words;
newRAE->RAETree = NULL;
newRAE->weights1 = this->weights1;
newRAE->weights_b1 = this->weights_b1;
newRAE->weights2 = this->weights2;
newRAE->weights_b2 = this->weights_b2;
return newRAE;
}
RAE::~RAE()
{
delete RAETree;
RAETree = NULL;
trainingData.clear();
}
lbfgsfloatval_t RAE::decay()
{
lbfgsfloatval_t val = 0;
for(int row = 0; row < weights1.rows(); row++)
{
for(int col = 0; col < weights1.cols(); col++)
{
val += pow(weights1(row, col), 2)/2;
val += pow(weights2(col, row), 2)/2;
}
}
for(int col = 0; col < weights_b1.cols(); col++)
{
val += pow(weights_b1(0, col), 2)/2;
}
for(int col = 0; col < weights_b2.cols(); col++)
{
val += pow(weights_b2(0, col), 2)/2;
}
return val;
}
//递归求导
void RAE::trainRecError(Node* node, MatrixLBFGS delta_parent, int freq)
{
Node* tmpNode = this->RAETree->getRoot();
//更新w2, b2
while(tmpNode->getNodeType() != BASED_NODE)
{
MatrixLBFGS c = concatMatrix(tmpNode->getLeftChildNode()->getVector(),tmpNode->getRightChildNode()->getVector());
MatrixLBFGS cRec = concatMatrix(tmpNode->leftReconst,tmpNode->rightReconst);
for(int row = 0; row < weights2.rows(); row++)
{
lbfgsfloatval_t result = (cRec(0, row)-c(0, row)) * (1-pow(cRec(0, row), 2));
for(int col = 0; col < weights2.cols(); col++)
{
delWeight2(row, col) = delWeight2(row,col) + freq * ALPHA * result * tmpNode->getVector()(0, col);
}
delWeight2_b(0, row) = freq * ALPHA * result + delWeight2_b(0, row);
}
tmpNode = tmpNode->getLeftChildNode();
}
//仅对每一对重构中的权重求导
tmpNode = this->RAETree->getRoot();
while(tmpNode->getNodeType() != BASED_NODE)
{
MatrixLBFGS c = concatMatrix(tmpNode->getLeftChildNode()->getVector(),tmpNode->getRightChildNode()->getVector());
MatrixLBFGS cRec = concatMatrix(tmpNode->leftReconst,tmpNode->rightReconst);
MatrixLBFGS tmpDelWb = MatrixLBFGS(1, 2*vecSize);
for(int row = 0; row < weights2.rows(); row++)
{
lbfgsfloatval_t result = (cRec(0, row)-c(0, row)) * (1-pow(cRec(0, row), 2));
tmpDelWb(0, row) = ALPHA * result;
}
tmpDelWb = tmpDelWb*weights2;
recurDel(tmpNode, tmpDelWb, freq);
tmpNode = tmpNode->getLeftChildNode();
}
}
//读取训练数据
void RAE::loadTrainingData()
{
vector<string> tmpData;
trainingData.clear();
string dataFile;
string domainName;
string domainLine = para->getPara("DomainList");
bool isDev = atoi(para->getPara("IsDev").c_str());
bool isTrain = atoi(para->getPara("IsTrain").c_str());
bool isTest = atoi(para->getPara("IsTest").c_str());
stringstream ss(domainLine);
int count = 0;
while(ss >> domainName)
{
if(count == atoi( para->getPara("THREAD_NUM").c_str() ))
{
break;
}
if(isDev)
{
dataFile = para->getPara(domainName + "DevTrainFile");
}
else if(isTrain)
{
dataFile = para->getPara(domainName + "DataFile");
}
ifstream in(dataFile.c_str(), ios::in);
string line;
while(getline(in, line))
{
int order;
map<string, string> m_tmp;
vector<string> subOfLine = splitBySign(line);
if(subOfLine[0] == "mono")
{
order = 1;
}
else if(subOfLine[0] == "invert")
{
order = 0;
}
for(int i = 1; i < subOfLine.size(); i++)
{
m_tmp.insert(make_pair(subOfLine[i].substr(0, 3), subOfLine[i].substr(4, subOfLine[i].size()-4)));
}
if(RAEType == SL)
{
tmpData.push_back(m_tmp["ct1"]);
tmpData.push_back(m_tmp["ct2"]);
}
else
{
tmpData.push_back(m_tmp["et1"]);
tmpData.push_back(m_tmp["et2"]);
}
}
in.close();
count++;
}
if(isDev)
{
if(RAEType == SL)
{
dataFile = para->getPara("SourceUnlabelDevData");
}
else
{
dataFile = para->getPara("TargetUnlabelDevData");
}
}
else if(isTrain)
{
if(RAEType == SL)
{
dataFile = para->getPara("SourceUnlabelTrainingData");
}
else
{
dataFile = para->getPara("TargetUnlabelTrainingData");
}
}
string line;
ifstream in(dataFile.c_str(), ios::in);
while(getline(in, line))
{
int pos;
pair<string, string> pss;
if (line == "" || (pos = line.find("\t")) == string::npos)
{
continue;
}
pss.first = line.substr(0, pos);
pos++;
pss.second = line.substr(pos);
tmpData.push_back(pss.first);
tmpData.push_back(pss.second);
}
for(int i = 0; i < tmpData.size(); i++)
{
if(trainingData.find(tmpData[i]) == trainingData.end())
{
trainingData.insert(make_pair(tmpData[i], 1));
}
else
{
trainingData.find(tmpData[i])->second += 1;
}
}
in.close();
}
void RAE::update(lbfgsfloatval_t* g)
{
Map<MatrixLBFGS> g_Weights1(g, weights1.rows(), weights1.cols());
Map<MatrixLBFGS> g_Weights_b1(g+weights1.rows()*weights1.cols(), weights_b1.rows(), weights_b1.cols());
Map<MatrixLBFGS> g_Weights2(g+weights1.rows()*weights1.cols()+
weights_b1.rows()*weights_b1.cols(),
weights2.rows(), weights2.cols());
Map<MatrixLBFGS> g_Weights_b2(g+weights1.rows()*weights1.cols()+
weights_b1.rows()*weights_b1.cols()+
weights2.rows()*weights2.cols(),
weights_b2.rows(), weights_b2.cols());
g_Weights1 += delWeight1;
g_Weights_b1 += delWeight1_b;
g_Weights2 += delWeight2;
g_Weights_b2 += delWeight2_b;
}
void RAE::updateWeights(const lbfgsfloatval_t* x)
{
lbfgsfloatval_t* cX = const_cast<lbfgsfloatval_t*>(x);
weights1 = Map<MatrixLBFGS>(cX, vecSize, 2*vecSize);
weights_b1 = Map<MatrixLBFGS>(cX + 2*vecSize*vecSize, 1, vecSize);
weights2 = Map<MatrixLBFGS>(cX + 2*vecSize*vecSize+vecSize, 2*vecSize, vecSize);
weights_b2 = Map<MatrixLBFGS>(cX + 2*vecSize*vecSize+vecSize+2*vecSize*vecSize, 1, 2*vecSize);
delToZero();
}
void RAE::delToZero()
{
delWeight1.setZero();
delWeight1_b.setZero();
delWeight2.setZero();
delWeight2_b.setZero();
}
void RAE::training()
{
x = lbfgs_malloc(getRAEWeightSize());
Map<MatrixLBFGS>(x, getRAEWeightSize(), 1).setRandom();
lbfgs_parameter_t param;
iterTimes = atoi(para->getPara("IterationTime").c_str());
loadTrainingData();
lbfgs_parameter_init(¶m);
param.max_iterations = iterTimes;
lbfgsfloatval_t fx = 0;
int ret;
ret = lbfgs(getRAEWeightSize(), x, &fx, RAELBFGS::evaluate, RAELBFGS::progress, this, ¶m);
cout << "L-BFGS optimization terminated with status code = " << ret << endl;
cout << " fx = " << fx << endl;
updateWeights(x);
logWeights(para);
trainingData.clear();
lbfgs_free(x);
}
lbfgsfloatval_t RAE::_training(lbfgsfloatval_t* g)
{
lbfgsfloatval_t error = 0;
for(map<string, int>::iterator it = trainingData.begin(); it != trainingData.end(); it++)
{
//获取实例
buildTree(it->first);
error += loss();
MatrixLBFGS delta_parent = MatrixLBFGS(1, vecSize);
delta_parent.setZero();
//对rae求导
trainRecError(RAETree->root, delta_parent, it->second);
delete RAETree;
RAETree = NULL;
}
update(g);
return error;
}
//不包含Erec的递归求导
void RAE::recurDel(Node* n, MatrixLBFGS derivation, int freq)
{
if(n->getNodeType() == BASED_NODE)
{
return;
}
MatrixLBFGS con = concatMatrix(n->getLeftChildNode()->getVector(), n->getRightChildNode()->getVector());
for(int col = 0; col < derivation.cols(); col++)
{
derivation(0, col) *= (1-pow(n->getVector()(0, col), 2));
}
for(int row = 0; row < delWeight1.rows(); row++)
{
for(int col = 0; col < delWeight1.cols(); col++)
{
delWeight1(row, col) += freq * derivation(0, row)*con(0, col);
}
delWeight1_b(0, row) += freq * derivation(0, row);
}
MatrixLBFGS der = MatrixLBFGS(1 ,vecSize);
MatrixLBFGS tmp = derivation * weights1;
if(n->leftChild->getNodeType() != BASED_NODE)
{
for(int col = 0; col < vecSize; col++)
{
der(0, col) = tmp(0, col);
}
}
else
{
for(int col = 0; col < vecSize; col++)
{
der(0, col) = tmp(0, col+vecSize);
}
}
recurDel(n->getLeftChildNode(), der);
}
lbfgsfloatval_t RAE::_evaluate(const lbfgsfloatval_t* x, lbfgsfloatval_t* g, const int n, const lbfgsfloatval_t step)
{
lbfgsfloatval_t fx = 0;
int RAEThreadNum = atoi(para->getPara("RAEThreadNum").c_str());
RAEThreadPara* threadpara = new RAEThreadPara[RAEThreadNum];
int batchsize = trainingData.size() / RAEThreadNum;
updateWeights(x);
for(int i = 0; i < RAEThreadNum; i++)
{
threadpara[i].cRAE = this->copy();
threadpara[i].g = lbfgs_malloc(getRAEWeightSize());
for(int j = 0; j < getRAEWeightSize(); j++)
{
threadpara[i].g[j] = 0;
}
if(i == RAEThreadNum-1)
{
map<string, int>::iterator s;
s = trainingData.begin();
for(int d = 0; d < i*batchsize; d++)
{
s++;
}
threadpara[i].cRAE->trainingData.insert(s, trainingData.end());
threadpara[i].instance_num = trainingData.size()%batchsize;
}
else
{
map<string, int>::iterator s, e;
s = trainingData.begin();
e = trainingData.begin();
for(int d = 0; d < i*batchsize; d++)
{
s++;
}
for(int d = 0; d < (i+1)*batchsize; d++)
{
e++;
}
threadpara[i].cRAE->trainingData.insert(s, e);
threadpara[i].instance_num = batchsize;
}
}
pthread_t* pt = new pthread_t[RAEThreadNum];
for (int a = 0; a < RAEThreadNum; a++) pthread_create(&pt[a], NULL, RAELBFGS::deepThread, (void *)(threadpara + a));
for (int a = 0; a < RAEThreadNum; a++) pthread_join(pt[a], NULL);
for(int i = 0; i < RAEThreadNum; i++)
{
fx += threadpara[i].lossVal;
for(int elem = 0; elem < getRAEWeightSize(); elem++)
{
g[elem] += threadpara[i].g[elem];
}
}
/*
int internal_node_num = 0;
for(map<string, int>::iterator it = trainingData.begin(); it != trainingData.end(); it++)
{
internal_node_num += getInternalNode(it->first);
}*/
fx /= trainingData.size();
fx += ZETA * decay();
for(int elem = 0; elem < getRAEWeightSize(); elem++)
{
g[elem] /= trainingData.size();
}
delWeight1 += ZETA * weights1;
delWeight1_b.setZero();
delWeight2 += ZETA * weights2;
delWeight2_b.setZero();
update(g);
delete pt;
pt = NULL;
for(int i = 0; i < RAEThreadNum; i++)
{
lbfgs_free(threadpara[i].g);
delete threadpara[i].cRAE;
}
delete threadpara;
threadpara = NULL;
return fx;
}
int RAE::_progress(const lbfgsfloatval_t *x, const lbfgsfloatval_t *g, const lbfgsfloatval_t fx, const lbfgsfloatval_t xnorm, const lbfgsfloatval_t gnorm, const lbfgsfloatval_t step, int n, int k, int ls)
{
//ofstream out("./log/RAE/RAE.log", ios::app);
cout << "Iteration of RAE: " << k << endl;
cout << "Loss Value: " << fx << endl;
printf(" xnorm = %f, gnorm = %f, step = %f\n", xnorm, gnorm, step);
printf("\n");
//out.close();
return 0;
}