-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhpx-net.cpp
148 lines (123 loc) · 3.93 KB
/
hpx-net.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
/* Copyright (c) 2013 Michael LeSane
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
//TODO: find std/boost equivalents of reinvented wheels
#include "hpx-net-init.hpp"
#include "neuron.hpp"
#include "neuron_row.hpp"
#include "network.hpp"
#include "hpx-net-init.cpp"
#include "neuron.cpp"
#include "neuron_row.cpp"
#include "network.cpp"
//Initializes, trains, and backpropagates network on XOR problem based on inputs, while timing performance
int main_main(int in, int hidden_rows, int hidden_cols, int out, int its, int serial, hpx::util::high_resolution_timer t)
{
std::cout << "Initializing simulation... ";
float toffset = t.elapsed();
network n(in,hidden_rows,hidden_cols,out,1);
std::cout << "Done. "
<< "(" << (t.elapsed()-toffset) << " s)\n";
int problem_count = 4;
int problem_correct = 0;
int display_output = 1;
int success = 0;
int display_time = 0;
int i = 0;
for(i = 0; i < its; i++)
{
if(display_output) std::cout << i << " ";
int s = i%(sizeof(tests)/sizeof(tests[0]));
std::vector<float> sensor = to_vector(tests[s],sizeof(tests[s])/sizeof(float));
std::vector<float> target = to_vector(targets[s],sizeof(targets[s])/sizeof(float));
n.setSensors(sensor);
if(display_time) std::cout << "Forward Pass... ";
toffset = t.elapsed();
n.run(serial);
if(display_time) std::cout << "Done. (" << (t.elapsed()-toffset) << " s)\n";
if(display_output)
{
std::cout << "(";
for(int it = 0; it < (int)(sizeof(tests[0])/sizeof(tests[0][0])); it++)
{
std::cout << sensor[it];
if(it < (int)(sizeof(tests[0])/sizeof(tests[0][0]))-1) std::cout << " ";
}
std::cout << ") = ";
}
if(display_time) std::cout << "Waiting on results... ";
//toffset = t.elapsed();
int valid = 1;
for(int it = 0; it < (int)n.rows[n.rows.size()-1].contents.size(); it++)
{
//For "row" parallelism
//if(!serial) n.rows[n.rows.size()-1].finalize_run();
int counter = 0;
float r = n.rows[n.rows.size()-1].contents[it].get_value();
if(r < 0) r = 0;
r = round(r);
if(display_output) std::cout << r << " ";
if(round(n.rows[n.rows.size()-1].contents[it].get_value()) != targets[s][counter]) valid = 0;
else counter++;
}
if(display_time)std::cout << "Done. (" << t.elapsed() - toffset << " s total)\n";
if(display_output)
std::cout << "(";
if(display_output)
for(int it = 0; it < (int)(sizeof(targets[0])/sizeof(targets[0][0])); it++)
{
std::cout << target[it];
if(it < (int)(sizeof(targets[0])/sizeof(targets[0][0]))-1) std::cout << " ";
}
if(display_output)
std::cout << ")";
if(valid)
{
problem_correct++;
if(display_output) std::cout << "\033[32mCorrect!\033[0m \t";
}
else
{
problem_correct = 0;
if(display_output) std::cout << "\033[31mIncorrect!\033[0m\t";
}
if(!display_output && display_time) std::cout << "Backpropagating... ";
toffset = t.elapsed();
float error = 0;
error = n.correct(target,0.05,0.01,serial);
if(!display_output && display_time) std::cout << "Done. (" << (t.elapsed()-toffset) << " seconds)\n";
if(display_output)
{
std::cout << error;
std::cout << "\n";
}
if(problem_count == problem_correct)
{
success = 1;
//break;
}
}
std::cerr << i << " iterations.";
if(success) std::cerr << " Successful convergence.";
std::cerr << "\n";
return 0;
}
//Prompts for inputs and launches simulation.
int hpx_main()
{
int a,b,c,d,e,f;
std::cin >> a >> b >> c >> d >> e >> f >> FORWARD_THRESHOLD >> BACKPROP_THRESHOLD;
hpx::util::high_resolution_timer t;
for(int i = 0; i < 1; i++)
main_main(a,b,c,d,e,f,t);
std::cout << "Total Time: " << t.elapsed() << " seconds.\n";
return hpx::finalize();
}
//Initialization
int main(int argc, char* argv[])
{
init();
hpx::init(argc,argv);
}