-
Notifications
You must be signed in to change notification settings - Fork 0
/
SentenceTree.cpp
674 lines (571 loc) · 27.5 KB
/
SentenceTree.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
#include "SentenceTree.h"
#include "MatrixOperations.h"
#include<string>
#include<cstdio>
#include<iostream>
#include<fstream>
using namespace std;
#define WORD_SIZE_VEC 30
/**
* Contains methods which interact with a given sentence's tree.
*/
double distanceBetweenTwoVectors(vector<double> vec1, vector<double> vec2, vector<vector<double>> sentimentWeightMatrix, vector<double> product, vector<double> biasSentimentMatrix) {
double score = 0;
/* for(int i = 0; i < vec1.size(); i++) {
score = score + (vec1[i] - vec2[i])*(vec1[i] - vec2[i]);
} */
vector<double> afterTanh = applyTanhElementWise(product);
vector<double> softmaxResult = softmax(addTwoVectors(biasSentimentMatrix, matrixMultplicationWithVector(sentimentWeightMatrix, afterTanh)));
if (softmaxResult[0] - softmaxResult[1] > 0) return softmaxResult[0] - softmaxResult[1];
else softmaxResult[1] - softmaxResult[0];
return score;
}
// Given a sentence, construct its semantic tree by combining the words using the tanh rule.
Tree* constructTreeForASentence(string sentence, vector<vector<double>> weights, vector<vector<double>> sentimentWeightScore,
Vocabulary *vocab, vector<double> biasSentimentMatrix, vector<double> biasWeights) {
vector<string> words = getWordsFromSentence(sentence);
vector<Node> nodes = retrieveWordRepresentation(words, vocab);
vector<pair<int,int>> pairedElem;
vector<Tree*> trees;
Tree *t;
// Convert the nodes into trees with height 1.
for(int i = 0; i < nodes.size();i++) {
t = new Tree(nodes[i]);
if (i < words.size()) {
t->setWord(words[i]);
}
trees.push_back(t);
}
int numberOfWords = trees.size();
// Merge all the small trees into only one tree.
while (numberOfWords > 1) {
double score = 0;
double maxScore = 0;
vector<double> concatenation = concatenateTwoVectors(trees[0]->getRootRepresentation(),
trees[1]->getRootRepresentation());
vector<double> product = matrixMultplicationWithVector(weights, concatenation);
product = addTwoVectors(product, biasWeights);
vector<double> afterTanh = applyTanhElementWise(product);
maxScore = distanceBetweenTwoVectors(trees[0]->getRootRepresentation(), trees[1]->getRootRepresentation(), sentimentWeightScore, product, biasSentimentMatrix);
int left = 0;
int right = 1;
for(int i = 1; i < trees.size(); i++) {
concatenation = concatenateTwoVectors(trees[i-1]->getRootRepresentation(),
trees[i]->getRootRepresentation());
product = matrixMultplicationWithVector(weights, concatenation);
if (isnan(product[i])) {
cout<<"The product has nan elements. This was found in constructTreeForASentence() method."<<endl;
cout<<sentence<<endl;
exit(0);
}
product = addTwoVectors(product, biasWeights);
afterTanh = applyTanhElementWise(product);
//. score = distanceBetweenTwoVectors(trees[i-1]->getRootRepresentation(),
// trees[i]->getRootRepresentation());
score = distanceBetweenTwoVectors(trees[i-1]->getRootRepresentation(),
trees[i]->getRootRepresentation(), sentimentWeightScore, product, biasSentimentMatrix);
// Check if the new node would hove a higer score. IF it has, change the variables
// left and right to point to the positions of the two nodes.
if (score > maxScore) {
maxScore = score;
left = i-1;
right = i;
}
}
// Recompute the value of the new node.
concatenation = concatenateTwoVectors(trees[left]->getRootRepresentation(),
trees[right]->getRootRepresentation());
product = matrixMultplicationWithVector(weights, concatenation);
product = addTwoVectors(product, biasWeights);
afterTanh = applyTanhElementWise(product);
for(int i = 0;i < afterTanh.size(); i++) {
if (isnan(afterTanh[i])) {
cout<<"NAN is located in constructTreeForASentence() method, after applying tanh"<<endl;
printElementsOfVector(product);
//afterTanh[i] = 0;
exit(0);
}
}
// Create new node which has as children the 2 nodes, and as the root the computed
// score.
Tree *merged = new Tree(afterTanh);
merged->setScore(addTwoVectors(softmax(matrixMultplicationWithVector(sentimentWeightScore, afterTanh)), biasSentimentMatrix));
merged->setLeftTree(trees[left]);
merged->setRightTree(trees[right]);
// Replace the 2 nodes by the new computed parent: add the root of the new node into the
// vector trees (on the left position and shift the other nodes by one position to left).
trees[left] = merged;
for(int i = right + 1; i < trees.size();i++) {
// delete()
trees[i-1] = trees[i];
}
trees.pop_back();
pairedElem.push_back(make_pair(left,right));
numberOfWords--;
}
// delete(t);
return trees[0];
}
// Create a container of size 2 (number of classes) containing in both positions a specific value, d.
vector<double> createTemporaryNodeRepresentation(int d) {
vector<double> wordRep;
for(int i = 0;i < 2; i++) {
wordRep.push_back(d);
}
return wordRep;
}
// Find an element in an int array and return its index.
int findFirstOccurenceOfElement(int a[], int elem, int length) {
for (int i = 0; i < length; i++) {
if (a[i] == elem) return i;
}
return -1;
}
// Find the 2 positions, i and j, in the array for which a[i]=a[j]=elem
pair<int,int> findElementInArray(int a[], int length, int elem) {
pair<int,int> result = make_pair(-1,-1);
int c = 0;
for( int i = 0;i < length; i++) {
if (a[i] == elem && c == 0) {
result.first = i;
c++;
} else if (a[i] == elem && c == 1) {
result.second = i;
c++;
break;
}
}
return result;
}
// Assign the labels of to the tree in a post-order way.
string assignRightLabels(Tree* t, vector<string> words, Dictionary* dictionary, SentimentLabels* sentimentLabels, int &numberOfLeaves) {
if (t == nullptr) {
cout<<"I am null"<<endl;
return "";
}
// The current node is a leaf, so we compute its score by searching the word in the sentiment labels map.
if (t->getLeftTree() == nullptr && t->getRightTree() == nullptr) {
long long phraseIndex = dictionary->getPhraseIndex(words[numberOfLeaves]);
double score = sentimentLabels->getSentimentScore(phraseIndex);
if (phraseIndex == -1) cout<<words[numberOfLeaves]<<" not found in the dictionary while constructing the target tree***"<<endl;
if (score >= 0.5) {
// case when the word is positive.
vector<double> root = t->getRootRepresentation();
root[0] = 0;
root[1] = 1;
t->setRoot(root);
t->setWord(words[numberOfLeaves]);
} else {
// case when the word is negative.
vector<double> root = t->getRootRepresentation();
root[0] = 1;
root[1] = 0;
t->setRoot(root);
t->setWord(words[numberOfLeaves]);
}
numberOfLeaves++;
return words[numberOfLeaves - 1];
}
// The current node is an inner node, compute both left and right trees and then compute the value for the tree;
string left = assignRightLabels(t->getLeftTree(), words, dictionary, sentimentLabels, numberOfLeaves);
string right = assignRightLabels(t->getRightTree(), words, dictionary, sentimentLabels, numberOfLeaves);
string partialPhrase = left + " " + right;
long long phraseIndex = dictionary->getPhraseIndex(partialPhrase);
if (phraseIndex == -1) cout<<partialPhrase<<" not found in the dictionary"<<endl;
double score = sentimentLabels->getSentimentScore(phraseIndex);
if (score >= 0.5) {
// case when the word is positive.
vector<double> root = t->getRootRepresentation();
root[0] = 0;
root[1] = 1;
t->setRoot(root);
} else {
// case when the word is negative.
vector<double> root = t->getRootRepresentation();
root[0] = 1;
root[1] = 0;
t->setRoot(root);
}
return partialPhrase;
}
// Update the given tree by merging the given branch to the tree.
void updateTree(Tree* t, vector<int> branch) {
Tree* temp = t;
int s = branch.size() - 2;
while (s >= 0) {
int value = branch[s];
if (temp->getLeftTree() == nullptr) {
Node* node = new Node(createTemporaryNodeRepresentation(value));
Tree* newTree= new Tree(*node);
temp->setLeftTree(newTree);
delete(node);
temp = temp->getLeftTree();
} else {
Tree * leftChild = temp->getLeftTree();
vector<double> leftChildRepresentation = leftChild->getRootRepresentation();
if (leftChildRepresentation[0] == value) {
temp = temp->getLeftTree();
} else {
Tree * rightChild = temp->getRightTree();
if (rightChild == nullptr) {
Node* node = new Node(createTemporaryNodeRepresentation(value));
Tree* newTree= new Tree(*node);
temp->setRightTree(newTree);
temp = temp->getRightTree();
delete(node);
} else {
vector<double> rightChildRepresentation = rightChild->getRootRepresentation();
if (rightChildRepresentation[0] == value) {
temp = temp->getRightTree();
} else cout<<"Error in the construction of the parse tree."<<endl;
}
}
}
s--;
}
}
// Create tree such that the leaves have indices in ascending order
Tree* constructTree(int a[], int length, int numberOfLeaves) {
int indexRoot = findFirstOccurenceOfElement(a, 0, length);
indexRoot++;
Node* temp = new Node(createTemporaryNodeRepresentation(indexRoot));
Tree* parent = new Tree(*temp);
delete(temp);
vector<vector<int>> listOfBranches;
for (int i = 0; i < numberOfLeaves; i++) {
int j = i;
vector<int> tempList;
while (j <= length) {
tempList.push_back(j + 1);
if (a[j] == 0) break;
j = a[j] - 1;
}
updateTree(parent, tempList);
//parent->inOrderTraversal();
}
return parent;
}
// Given a string of digits and | symbols, return the target tree represented by that string.
Tree* constructTargetTree(string treeText, string sentence, Dictionary* dictionary, SentimentLabels* sentimentLabels) {
vector<string> words = getWordsFromSentence(sentence);
// Find the number of nodes in the tree and also retrieve the positions from treeText.
int numberOfNodes = 0;
for (char & c: treeText) {
if (c == '|') numberOfNodes++;
}
numberOfNodes++;
int positions[numberOfNodes] ;
int number = 0;
int k = 0;
for (char & c: treeText) {
if (c != '|') number = number * 10 + c - '0';
else {
positions[k] = number;
k++;
number = 0;
}
}
positions[k] = number; // add the last int in the array.
// Create tree by using temporary values for the inner nodes. Start by finding the root (0) in the array.
int indexRoot = findFirstOccurenceOfElement(positions, 0, numberOfNodes);
// Increase the index of the root by one since the array should be indexed from 1.
indexRoot++;
Tree* root = constructTree(positions, numberOfNodes, words.size());
//cout<<"Index of the root: "<<indexRoot<<endl;
int nr = 0;
string x = assignRightLabels(root, words, dictionary, sentimentLabels, nr);
if (root->getLeftTree() == nullptr) cout<<"XOXO"<<endl;
return root;
}
RNNParam* backprop(Tree * targetTree, Tree * computedTree, vector<vector<double>> weightScoresMatrix, vector<vector<double>> weightsMatrix, vector<double> parentError,
vector<double> biasSentiment) {
// Find the transpose matrixes.
vector<vector<double>> weightScoresMatrixTranspose = getTransposeMatrix(weightScoresMatrix);
vector<vector<double>> weightsMatrixTranspose = getTransposeMatrix(weightsMatrix);
RNNParam* rnnParam = new RNNParam();
vector<double> targetRootRepresentation;
if ((targetTree == nullptr && computedTree != nullptr) || (targetTree != nullptr && computedTree == nullptr)) {
cout<<"copacei difereti"<<endl;
return rnnParam;
} else {
targetRootRepresentation = targetTree->getRootRepresentation();
}
vector<double> zeros25 = getZeros(WORD_SIZE_VEC);
vector<vector<double>> resultSentimentMatrix;
resultSentimentMatrix.push_back(zeros25);
resultSentimentMatrix.push_back(zeros25);
if ((targetRootRepresentation[0] == 1 && targetRootRepresentation[1] == 1) ||
(targetRootRepresentation[0] == 0 && targetRootRepresentation[1] == 0)) {
cout<<"Tree is constructed badly i backprop"<<endl;
exit(0);
}
// Retrieve the current node vector.
vector<double> currentNodeVectorRepresentation = computedTree->getRootRepresentation();
// Compute predictions of the current node.
// vector<double> softmaxResult = softmax(addTwoVectors(biasSentiment, matrixMultplicationWithVector(weightScoresMatrix, currentNodeVectorRepresentation)));
vector<double> softmaxResult = computedTree->getScore();
// Subtract the actual predictions from the softmax.
vector<double> difference = softmaxResult;
double totalError = 0;
if (targetRootRepresentation[0] == 1) {
totalError += log((double)difference[0]);
difference[0] = difference[0] - 1;
}
if (targetRootRepresentation[1] == 1) {
totalError += log((double)difference[1]);
difference[1] = difference[1] - 1;
}
vector<double> biasSentimentMatrix = difference;
rnnParam->updateBiasSentimentMatrix(biasSentimentMatrix);
vector<double> biasWeightsMatrix;
// Compute the error for the Sentiment Weight Matrix at this node.
for(int i = 0; i < difference.size(); i++) {
for(int j = 0; j < currentNodeVectorRepresentation.size(); j++) {
resultSentimentMatrix[i][j] = (difference[i])*currentNodeVectorRepresentation[j];
}
}
rnnParam->updateSentimentWeightsMatrix(resultSentimentMatrix);
vector<double> deltaScore = matrixMultplicationWithVector(weightScoresMatrixTranspose, difference);
for(int i = 0;i < deltaScore.size(); i++) {
if (isnan((double)deltaScore[i])) {
cout<<"NAN is located when computing first delta score"<<endl;
printElementsOfVector(deltaScore);
cout<<endl<<endl;
printElementsOfVector(difference);
cout<<endl<<endl;
printElementsOfVector(targetRootRepresentation);
cout<<endl<<endl;
printElementsOfMatrix(weightScoresMatrix);
cout<<endl<<endl;
exit(0);
}
}
// Add parent error.
deltaScore = addTwoVectors(deltaScore, parentError);
// Compute f' (which in this case is tanh').
vector<double> afterTanhDeriv = getTanhDerivativeFunction(currentNodeVectorRepresentation);
for(int i = 0;i < afterTanhDeriv.size(); i++) {
if (isnan((double)afterTanhDeriv[i])) {
cout<<"NAN is located when computing tanh deruvatuve"<<endl;
printElementsOfVector(afterTanhDeriv);
cout<<endl<<endl;
printElementsOfVector(currentNodeVectorRepresentation);
cout<<endl<<endl;
exit(0);
}
}
// deltaScore = getVectorHadamardProduct(deltaScore, afterTanhDeriv);
for(int i = 0;i < deltaScore.size(); i++) {
if (currentNodeVectorRepresentation[i] <= 0) deltaScore[i] = 0;
}
for(int i = 0;i < deltaScore.size(); i++) {
if (isnan((double)deltaScore[i])) {
cout<<"NAN is located when computing delta score"<<endl;
printElementsOfVector(deltaScore);
cout<<endl<<endl;
printElementsOfVector(afterTanhDeriv);
cout<<endl<<endl;
exit(0);
}
}
vector<double> deltaTotal = deltaScore;
// This means the current node is a leaf, return the error only for this node.
if (computedTree->getLeftTree() == nullptr) {
rnnParam->updateVocabError(computedTree->getWord(), deltaScore);
return rnnParam;
}
biasWeightsMatrix = deltaScore;
rnnParam->updateBiasWeightMatrix(biasWeightsMatrix);
vector<double> newParentError = matrixMultplicationWithVector(weightsMatrixTranspose, deltaScore);
// Compute error for children. This means the node is not a leaf.
// Retrieve the vector representations of the two children.
vector<double> leftChild = computedTree->getLeftTree()->getRootRepresentation();
vector<double> rightChild = computedTree->getRightTree()->getRootRepresentation();
// Concatenate the two children.
vector<double> mergedChildren = concatenateTwoVectors(leftChild, rightChild);
// Transpose the total error already obtained at this node.
vector<vector<double>> deltaTransposed = transposeRowVector(deltaScore);
// Compute the total error at this node and obtain the error matrices.
vector<vector<double>> deltaMatrix = multiplyMatrices(deltaTransposed, mergedChildren);
rnnParam->updateWeightsMatrix(deltaMatrix);
for(int i = 0;i < newParentError.size(); i++) {
if (mergedChildren[i] <= 0) newParentError[i] = 0;
}
vector<double> leftChildError;
vector<double> rightChildError;
for (int i = 0; i < newParentError.size(); i++) {
if (i < newParentError.size()/2) leftChildError.push_back(newParentError[i]);
else rightChildError.push_back(newParentError[i]);
}
RNNParam* leftRNNParam = backprop(targetTree->getLeftTree(), computedTree->getLeftTree(), weightScoresMatrix, weightsMatrix, leftChildError, biasSentiment);
RNNParam* rightRNNParam = backprop(targetTree->getRightTree(), computedTree->getRightTree(), weightScoresMatrix, weightsMatrix, rightChildError, biasSentiment);
rnnParam->updateVocabError(leftRNNParam->getVocabError(), rightRNNParam->getVocabError());
rnnParam->updateBiasSentimentMatrix(leftRNNParam->getBiasSentimentMatrix());
rnnParam->updateBiasSentimentMatrix(rightRNNParam->getBiasSentimentMatrix());
rnnParam->updateBiasWeightMatrix(leftRNNParam->getBiasWeightMatrix());
rnnParam->updateBiasWeightMatrix(rightRNNParam->getBiasWeightMatrix());
rnnParam->updateWeightsMatrix(leftRNNParam->getWeightsMatrix());
rnnParam->updateWeightsMatrix(rightRNNParam->getWeightsMatrix());
rnnParam->updateSentimentWeightsMatrix(leftRNNParam->getSentimentWeightsMatrix());
rnnParam->updateSentimentWeightsMatrix(rightRNNParam->getSentimentWeightsMatrix());
rnnParam->updateTotalError(totalError);
rnnParam->updateTotalError(leftRNNParam->getTotalError());
rnnParam->updateTotalError(rightRNNParam->getTotalError());
delete(leftRNNParam);
delete(rightRNNParam);
return rnnParam;
}
// Assign the labels of to the tree in a post-order way.
void assignParsingTreeLabels(Tree* t, vector<string> words, Vocabulary* vocab, int &numberOfLeaves,
vector<vector<double>> weightScoresMatrix, vector<vector<double>> weightsMatrix, vector<double> biasSentimentMatrix, vector<double> biasWeights) {
if (t == nullptr) {
cout<<"I am null"<<endl;
return;
}
// The current node is a leaf, so we compute its score by searching the word in the sentiment labels map.
if (t->getLeftTree() == nullptr && t->getRightTree() == nullptr) {
vector<double> leafRepresentation = vocab->getWordRepresentation(words[numberOfLeaves]);
if (leafRepresentation.empty()) {
cout<<words[numberOfLeaves]<<" not found in the dictionary."<<endl;
// vocab->addNewWord(words[numberOfLeaves]);
leafRepresentation = vocab->getWordRepresentation("UNKNOWN");
// cout<<"HUUUGE PROBLEM IN VOCABULARY"<<endl;
}
for(int i = 0;i < leafRepresentation.size(); i++) {
if (isnan(leafRepresentation[i])) {
cout<<"NAN is located in assignParsingTreeLabels() method, in assigning the leaf a value."<<endl;
printElementsOfVector(leafRepresentation);
exit(0);
}
}
vector<double> root = leafRepresentation;
t->setRoot(root);
t->setWord(words[numberOfLeaves]);
t->setScore(softmax(addTwoVectors(matrixMultplicationWithVector(weightScoresMatrix, root), biasSentimentMatrix)));
numberOfLeaves++;
return;
}
if ((t->getLeftTree() != nullptr && t->getRightTree() == nullptr) ||
(t->getLeftTree() == nullptr && t->getRightTree() != nullptr)) {
cout<<"The tree is not binary."<<endl;
exit(0);
}
// The current node is an inner node, compute both left and right trees and then compute the value for the tree;
if (t->getLeftTree() != nullptr && t->getRightTree() != nullptr) {
assignParsingTreeLabels(t->getLeftTree(), words, vocab, numberOfLeaves, weightScoresMatrix, weightsMatrix, biasSentimentMatrix, biasWeights);
assignParsingTreeLabels(t->getRightTree(), words, vocab, numberOfLeaves, weightScoresMatrix, weightsMatrix, biasSentimentMatrix, biasWeights);
vector<double> concatenation = concatenateTwoVectors(t->getLeftTree()->getRootRepresentation(),
t->getRightTree()->getRootRepresentation());
vector<double> product = matrixMultplicationWithVector(weightsMatrix, concatenation);
for(int i = 0; i < product.size(); i++) {
if (isnan(product[i])) {
cout<<"The product has nan elements. This was found in assignParsingTreeLabels() method."<<endl;
printElementsOfVector(product);
cout<<endl;
printElementsOfVector(concatenation);
cout<<endl;
printElementsOfMatrix(weightsMatrix);
cout<<endl;
printElementsOfMatrix(weightScoresMatrix);
cout<<words[0] + words[1] + words[2]<<endl;
exit(0);
}
}
product = addTwoVectors(product, biasWeights);
for(int i = 0; i < product.size(); i++) {
if (product[i] <= 0) product[i] = 0;
}
// vector<double> afterTanh = applyTanhElementWise(product);
vector<double> afterTanh = product;
for(int i = 0;i < afterTanh.size(); i++) {
if (isnan(afterTanh[i])) {
cout<<"NAN is located in assignParsingTreeLabels() method, after applying tanh"<<endl;
printElementsOfVector(product);
exit(0);
}
}
// Set score of the current node.
t->setScore(softmax(addTwoVectors(matrixMultplicationWithVector(weightScoresMatrix, afterTanh), biasSentimentMatrix)));
// Set the new node value.
t->setRoot(afterTanh);
}
}
// Read from the PreprocessedDatasetSentences.txt. Map each tree representation with its line position in the file.
unordered_map<long long, string> readParsedTrees() {
// ifstream input("PreprocessedDatasetSentences.txt");
ifstream input("stanfordSentimentTreebank/STree.txt");
string line;
unordered_map<long long, string> result;
long long counter = 0;
while(getline(input, line)) {
counter++;
result.insert(make_pair(counter, line));
}
return result;
}
void checkVectorRepresentationHaveSize25(Tree* t) {
if (t == nullptr) return;
if (t->getRootRepresentation().size() != WORD_SIZE_VEC) cout<<"!!!!!!!! "<<t->getRootRepresentation().size();
checkVectorRepresentationHaveSize25(t->getLeftTree());
checkVectorRepresentationHaveSize25(t->getRightTree());
}
// Use the parsing tring in the forward propogation.
Tree* useParserForCreatingTheTree(string treeText, string sentence, Vocabulary* vocab,
vector<vector<double>> weightScoresMatrix, vector<vector<double>> weightsMatrix, vector<double> biasSentimentMatrix, vector<double> biasWeights) {
vector<string> words = getWordsFromSentence(sentence);
// Find the number of nodes in the tree and also retrieve the positions from treeText.
int numberOfNodes = 0;
for (char & c: treeText) {
if (c == '|') numberOfNodes++;
}
numberOfNodes++;
int positions[numberOfNodes] ;
int number = 0;
int k = 0;
for (char & c: treeText) {
if (c != '|') number = number * 10 + c - '0';
else {
positions[k] = number;
k++;
number = 0;
}
}
positions[k] = number; // add the last int in the array.
// Create tree by using temporary values for the inner nodes. Start by finding the root (0) in the array.
int indexRoot = findFirstOccurenceOfElement(positions, 0, numberOfNodes);
// Increase the index of the root by one since the array should be indexed from 1.
indexRoot++;
if (2 * words.size() - 1 == indexRoot) {
Tree* root = constructTree(positions, numberOfNodes, words.size());
int nr = 0;
// Compute the values of the inner nodes and leaves of the tree.
assignParsingTreeLabels(root, words, vocab, nr, weightScoresMatrix, weightsMatrix, biasSentimentMatrix, biasWeights);
if (root->getLeftTree() == nullptr) {
cout<<"The root of the parse tree has left child null => not binary tree."<<endl;
exit(0);
}
//checkVectorRepresentationHaveSize25(root);
return root;
} else return constructTreeForASentence(sentence, weightsMatrix, weightScoresMatrix, vocab, biasSentimentMatrix, biasWeights);
}
// Get error for validation.
double computeErrorForTree(Tree* computedTree, Tree* targetTree, vector<vector<double>> weightScoresMatrix, vector<double> biasWeights) {
if (computedTree == nullptr || targetTree == nullptr) return 0;
double totalError = 0;
double left = computeErrorForTree(computedTree->getLeftTree(), targetTree->getLeftTree(), weightScoresMatrix, biasWeights);
double right = computeErrorForTree(computedTree->getRightTree(), targetTree->getRightTree(), weightScoresMatrix, biasWeights);
// Compute predictions of the current node.
vector<double> softmaxResult = softmax(addTwoVectors(biasWeights, matrixMultplicationWithVector(weightScoresMatrix, computedTree->getRootRepresentation())));
if (targetTree->getRootRepresentation()[0] == 1) totalError += log(softmaxResult[0]);
if (targetTree->getRootRepresentation()[1] == 1) totalError += log(softmaxResult[1]);
totalError = totalError + left + right;
return totalError;
}
long long getTotalNumberOfInnerNodesCorrectlyPredictted(Tree* targetTree, Tree* computedTree) {
if (targetTree == nullptr || computedTree == nullptr) return 0;
long long leftSide = getTotalNumberOfInnerNodesCorrectlyPredictted(targetTree->getLeftTree(), computedTree->getLeftTree());
long long rightSide = getTotalNumberOfInnerNodesCorrectlyPredictted(targetTree->getRightTree(), computedTree->getRightTree());
if (targetTree->getRootRepresentation()[0] == 1 && computedTree->getScore()[0] > computedTree->getScore()[1]) return 1 + leftSide + rightSide;
if (targetTree->getRootRepresentation()[1] == 1 && computedTree->getScore()[0] < computedTree->getScore()[1]) return 1 + leftSide + rightSide;
return 0 + leftSide + rightSide;
}