-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
258 lines (194 loc) · 7.39 KB
/
main.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
#include <cstdio>
#include <unistd.h>
#include <cstring>
#include <chrono>
#include "colored_output.h"
#include "Network.h"
#include "Perceptron.cpp"
#include "Layer.cpp"
#define _1 0.9
#define _0 0.1
const int SAMPLES_COUNT = 20;
float const learn_data[SAMPLES_COUNT][11] = {
{ _0, _0, _0,
_0, _0, _0, // empty
_0, _0, _0, _0 /*vert*/, _0 /*horiz*/},
{ _0, _0, _0,
_0, _1, _0, // point
_0, _0, _0, _0 /*vert*/, _0 /*horiz*/},
{ _1, _1, _1,
_0, _0, _0, // h-line 1
_0, _0, _0, _0 /*vert*/, _1 /*horiz*/},
{ _0, _0, _0,
_1, _1, _1, // h-line 2
_0, _0, _0, _0 /*vert*/, _1 /*horiz*/},
{ _0, _0, _0,
_0, _0, _0, // h-line 3
_1, _1, _1, _0 /*vert*/, _1 /*horiz*/},
{ _1, _0, _0,
_1, _0, _0, // v-line 1
_1, _0, _0, _1 /*vert*/, _0 /*horiz*/},
{ _0, _1, _0,
_0, _1, _0, // v-line 2
_0, _1, _0, _1 /*vert*/, _0 /*horiz*/},
{ _0, _0, _1,
_0, _0, _1, // v-line 3
_0, _0, _1, _1 /*vert*/, _0 /*horiz*/},
{ _1, _1, _1,
_0, _0, _1, // corner 1
_0, _0, _1, _1 /*vert*/, _1 /*horiz*/},
{ _1, _0, _0,
_1, _0, _0, // corner 2
_1, _1, _1, _1 /*vert*/, _1 /*horiz*/},
{ _1, _1, _1,
_1, _0, _0, // corner 3
_1, _0, _0, _1 /*vert*/, _1 /*horiz*/},
{ _0, _0, _1,
_0, _0, _1, // corner 4
_1, _1, _1, _1 /*vert*/, _1 /*horiz*/},
{ _1, _1, _1,
_0, _1, _0, // T
_0, _1, _0, _1 /*vert*/, _1 /*horiz*/},
{ _0, _1, _0,
_0, _1, _0, // T upside
_1, _1, _1, _1 /*vert*/, _1 /*horiz*/},
{ _0, _0, _1,
_1, _1, _1, // T turned right
_0, _0, _1, _1 /*vert*/, _1 /*horiz*/},
{ _1, _0, _0,
_1, _1, _1, // T turned left
_1, _0, _0, _1 /*vert*/, _1 /*horiz*/},
{ _0, _1, _0,
_1, _1, _1, // cross
_0, _1, _0, _1 /*vert*/, _1 /*horiz*/},
{ _0, _1, _0,
_0, _1, _0, // short v-line
_0, _0, _0, _1 /*vert*/, _0 /*horiz*/},
{ _0, _0, _0,
_1, _1, _0, // short h-line
_0, _0, _0, _0 /*vert*/, _1 /*horiz*/},
{ _0, _1, _1,
_0, _0, _0, // short h-line
_0, _0, _0, _0 /*vert*/, _1 /*horiz*/},
};
const int TESTS_COUNT = 4;
float const test_data[TESTS_COUNT][9] = {
{ _0, _0, _0,
_0, _0, _0, // empty
_0, _0, _0},
{ _0, _0, _0,
_0, _1, _0, // dot
_0, _0, _0},
{ _0, _0, _1,
_0, _0, _1, // short v-line
_0, _0, _0},
{ _1, _1, _0,
_0, _0, _0, // short h-line
_0, _0, _0},
};
void print_results(float vert_value, float horz_value){
pf(" Results: %.2f %.2f ", vert_value, horz_value);
pf(vert_value > 0.5 ? _BOLD "✓ vertical " _RST: "");
pf(horz_value > 0.5 ? _BOLD "✓ horisontal " _RST: "");
pf("\n");
}
int time_ms(){
std::chrono::milliseconds ms = std::chrono::duration_cast< std::chrono::milliseconds >(
std::chrono::system_clock::now().time_since_epoch()
);
return ms.count();
}
int main(int argc, char * argv [])
{
pf("Usage: %s [activation-function] [seed] [epoches] \n", argv[0]);
//Perceptron::learning_rate = 0.007f;
activation_function_index = 0;
int train_count = 0;
int start_timestamp_ms = time_ms();
int seed = time(NULL);
int total_epoches = 3000;
if(argc > 1){
int arg_value = std::stoi(argv[1]);
if(arg_value < 0 || arg_value >= ACTIVATION_BUNDLES_COUNT){
pf_red("Error: wrong activation function (%d). Exit\n", arg_value);
exit(1);
}
activation_function_index = arg_value;
}
if(argc > 2){
seed = std::stoi(argv[2]);
}
if(argc > 3){
total_epoches = std::stoi(argv[3]);
}
// initialize random generator with seed
srand(seed);
Network net("net1");
net.createLayer("input", 9); // 9 pixes input
net.createLayer("lay1", 5);
net.createLayer("lay2", 3);
net.createLayer("out", 2); // two neurons at the output
//net.loadWeights("weights.txt");
FILE * file_errors_by_sample = fopen("plot1.data", "w");
FILE * file_errors_summary = fopen("plot2.data", "w");
// learn cycle
for(int epoch = 0; epoch < total_epoches; epoch++){
//usleep(1);
float epoch_out_err_max = 0;
for(int sample = 0; sample < SAMPLES_COUNT; sample ++){
//PRINT_ON = epoch > total_epoches - 10; // print only last 10 epoches
PRINT_ON = (epoch + 1) % 1000 == 0; // print every 1000-th epoch
pf_green("\nepoch %d sample #%d\n", epoch, sample);
for(int k=0; k < 9; k++){ // print sample square
if((k) % 3 == 0) pf("\n");
pf("%s ", learn_data[sample][k] > 0.5 ? "◼" : "◻");
}
for(int k=0; k < 9; k++){ // fill input layer with sample
net.setInputValue(k, learn_data[sample][k]);
}
// provide signal through the network
net.forward();
print_results(net.outLayer()->perceptrons[0]->result, net.outLayer()->perceptrons[1]->result);
net.printState();
// lear the sample
float data [2] = {learn_data[sample][9], learn_data[sample][10]};
net.learn(data, 2);
train_count++;
pf("error sum: " _RED "%+.3f " _RST " outerr:" _YELLOW " %.3f" _RST "\n", net.errorSum(), net.outLayer()->errorSum());
// save to file
char buf [100];
sprintf(buf, "%f %f\n", net.errorSum(), net.outLayer()->errorSum());
fputs(buf, file_errors_summary); // save to summary plot file
sprintf(buf, "%f ", net.outLayer()->errorAbsSum());
fputs(buf, file_errors_by_sample); // save to by_sample plot file
epoch_out_err_max = std::max(epoch_out_err_max, net.outLayer()->errorAbsSum());
}
fputs("\n", file_errors_by_sample);
pf("epoch_out_err_max: " _BG_BLUE " %.3f " _RST " seed: %d\n", epoch_out_err_max, seed);
}
fclose(file_errors_summary);
fclose(file_errors_by_sample);
PRINT_ON = 1;
pf_bold("SEED: %d\n", seed);
// run tests
for(int tindex = 0; tindex < TESTS_COUNT; tindex ++)
{
pf_green("\ntest #%d", tindex);
for(int k=0; k < 9; k++){ // print test square
if((k) % 3 == 0) pf("\n");
pf("%s ", test_data[tindex][k] > 0.5 ? "◼" : "◻");
}
for(int k=0; k < 9; k++){ // fill input layer with sample
net.setInputValue(k, test_data[tindex][k]);
}
net.forward();
print_results(net.outLayer()->perceptrons[0]->result, net.outLayer()->perceptrons[1]->result);
net.printState();
}
net.saveWeights("weights.txt");
pf("Used Activation function: " _BG_RED " %s " _RST, activation_bundles[activation_function_index].name);
pf(" LR: " _BG_YELL " %g " _RST "\n", Perceptron::learning_rate);
pf("train count: %d\n", train_count);
pf("train count / ms: %d\n", train_count / (time_ms() - start_timestamp_ms));
pf("exec time: %g sec\n", (float)(time_ms() - start_timestamp_ms)/1000);
}