This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.cpp
218 lines (192 loc) · 6.98 KB
/
test.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
// Copyright PinaPL
//
// test.cpp
// PinaPL
//
#include "test.h"
// Auxiliary functions needed to test
// Convert a letter into a legitimate input
Eigen::VectorXd letterToInput(char letter) {
Eigen::VectorXd input(7);
switch (letter) {
case 'B':
input << 1, 0, 0, 0, 0, 0, 0;
break;
case 'T':
input << 0, 1, 0, 0, 0, 0, 0;
break;
case 'P':
input << 0, 0, 1, 0, 0, 0, 0;
break;
case 'S':
input << 0, 0, 0, 1, 0, 0, 0;
break;
case 'X':
input << 0, 0, 0, 0, 1, 0, 0;
break;
case 'V':
input << 0, 0, 0, 0, 0, 1, 0;
break;
case 'E':
input << 0, 0, 0, 0, 0, 0, 1;
break;
}
return input;
}
// Convert an output filtered into a letter (useful to debug)
char inputToLetter(Eigen::VectorXd vector) {
Eigen::VectorXd B(7);
B << 1, 0, 0, 0, 0, 0, 0;
Eigen::VectorXd T(7);
T << 0, 1, 0, 0, 0, 0, 0;
Eigen::VectorXd P(7);
P << 0, 0, 1, 0, 0, 0, 0;
Eigen::VectorXd S(7);
S << 0, 0, 0, 1, 0, 0, 0;
Eigen::VectorXd X(7);
X << 0, 0, 0, 0, 1, 0, 0;
Eigen::VectorXd V(7);
V << 0, 0, 0, 0, 0, 1, 0;
Eigen::VectorXd E(7);
E << 0, 0, 0, 0, 0, 0, 1;
char letter = 0;
if (vector == B) letter = 'B';
else if (vector == T) letter = 'T';
else if (vector == P) letter = 'P';
else if (vector == S) letter = 'S';
else if (vector == X) letter = 'X';
else if (vector == V) letter = 'V';
else if (vector == E) letter = 'E';
else letter = '*';
return letter;
}
// Apply a threshold : if value > 0.3 then value =1, else value = 0
std::vector<Eigen::VectorXd> applyThreshold(std::vector<Eigen::VectorXd> outputs) {
for (size_t i = 0; i < outputs.size(); i++) {
outputs.at(i) = outputs.at(i).unaryExpr(&threshold);
}
return(outputs);
}
// Used to evaluate the preditcted transitions by the network and the expected transition
// Simple reber grammar
long compareSimple(std::vector<Eigen::VectorXd> outputs, std::vector<Eigen::VectorXd> expectedOutputs) {
long score = 0;
Eigen::VectorXd diff;
bool transitionPredicted = true;
// for each VectorXd
for (unsigned long i = 0; i < outputs.size(); i++) {
// We compare the state predicted and the next state
diff = outputs.at(i) - expectedOutputs.at(i);
transitionPredicted = true;
for (unsigned long j = 0; j < diff.size(); j++) {
// if one of the coordinates is <0 there is a transition not predicted
if (diff(j) < 0)
transitionPredicted = false;
}
// If we did not found any error, we score
if (transitionPredicted)
score += 1;
}
// Checks if we preditected ALL the transitions
return (long)(score == outputs.size());
}
// Same thing for the double reber grammar
long compareDouble(std::vector<Eigen::VectorXd> outputs, std::vector<Eigen::VectorXd> expectedOutputs) {
long score = 0;
Eigen::VectorXd diff;
bool transitionPredicted = true;
// We compare the last state predicted and the first transition
diff = outputs.at(outputs.size() - 2) - expectedOutputs.at(outputs.size() - 2);
for (unsigned long j = 0; j < diff.size(); j++) {
// if one of the coordinates is <0 there is a transition not predicted
if (fabs(diff(j)) > 0.1) {
transitionPredicted = false;
}
}
// If we did not found any error, we score
if (transitionPredicted)
score = 1;
return score;
}
// Learn a grammar
void grammarLearn(bool symmetrical) {
unsigned long inputSize = 7;
unsigned long outputSize = 7;
unsigned long layerSize = 30;
unsigned long batchCount = 2000;
unsigned long batchSize = 50;
Network network = Network(inputSize, layerSize, outputSize);
std::ifstream file(openFile(symmetrical));
std::string inputString;
// random offset in data set
unsigned long offset = arc4random_uniform(10000);
for (unsigned long i = 0; i < offset; i++)
std::getline(file, inputString);
// std::cout << "===== Beginnning of Learning =====" << '\n';
for (unsigned long batch = 0; batch < batchCount; batch++) {
// std::cout << "batch no"<< batch;
std::cout << batch;
long remainingWords = batchSize;
while (std::getline(file, inputString) && remainingWords > 0) {
//std::cout << inputString << std::endl;
for (unsigned long i = 0; i < inputString.length() - 1; i++) {
network.propagate(letterToInput(inputString.at(i)));
network.backpropagate(letterToInput(inputString.at(i + 1)));
//printVector(letterToInput(inputString.at(i)));
//printVector(network.getOutputs());
}
//printVector(letterToInput(inputString.back()));
//std::cout << std::endl;
network.updateWeigths();
remainingWords -= 1;
}
if (symmetrical)
grammarTestDouble(network, 1000);
else
grammarTestSimple(network, 1000);
}
}
void grammarTestSimple(Network network, unsigned long wordCount) {
std::ifstream file("reber_test_1M.txt");
std::string inputString;
long score = 0;
long remainingWords = wordCount;
unsigned long offset = arc4random_uniform(10000);
for (unsigned long i = 0; i < offset; i++)
std::getline(file, inputString);
while (std::getline(file, inputString) && remainingWords > 0) {
std::vector<Eigen::VectorXd> allOutputs = std::vector<Eigen::VectorXd>();
std::vector<Eigen::VectorXd> expectedOutputs = std::vector<Eigen::VectorXd>();
for (unsigned long i = 0; i < inputString.length() - 1; ++i) {
network.propagate(letterToInput(inputString.at(i)));
allOutputs.push_back(network.getOutputs());
expectedOutputs.push_back(letterToInput(inputString.at(i + 1)));
}
score += compareSimple(applyThreshold(allOutputs), expectedOutputs);
remainingWords -= 1;
}
double scorePercent = ((double)100.0 * score / wordCount);
std::cout << "," <<scorePercent << '\n';
}
void grammarTestDouble(Network network, unsigned long wordCount) {
std::ifstream file("symmetrical_reber_test_1M.txt");
std::string inputString;
long score = 0;
long remainingWords = wordCount;
unsigned long offset = arc4random_uniform(10000);
for (unsigned long i = 0; i < offset; i++)
std::getline(file, inputString);
while (std::getline(file, inputString) && remainingWords > 0) {
std::vector<Eigen::VectorXd> allOutputs = std::vector<Eigen::VectorXd>();
std::vector<Eigen::VectorXd> expectedOutputs = std::vector<Eigen::VectorXd>();
for (unsigned long i = 0; i < inputString.length() - 1; ++i) {
network.propagate(letterToInput(inputString.at(i)));
allOutputs.push_back(network.getOutputs());
expectedOutputs.push_back(letterToInput(inputString.at(i + 1)));
}
score += compareDouble(applyThreshold(allOutputs), expectedOutputs);
remainingWords -= 1;
}
double scorePercent = ((double)100.0 * score / wordCount);
std::cout << "," <<scorePercent << '\n';
}