-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
337 lines (297 loc) · 11.3 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
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
#include <iostream>
#include <limits>
#include <cmath>
//#include <string>
#include "main.h"
#include <chrono>
#include "grapher.h"
#include <thread>
using std::cout;
using std::endl;
//neuron info for each neuron
neuron_info *input_neuron_info;
neuron_info *output_neuron_info;
neuron_info *hidden_neuron_info;
//network graph as adjacency matrix
axon_info **network_connection_info;
//activation function
double (*activate) (double, double, double);
neuron_info* get_input_neuron_info(){
return input_neuron_info;
}
neuron_info* get_output_neuron_info(){
return output_neuron_info;
}
neuron_info* get_hidden_neuron_info(){
return hidden_neuron_info;
}
axon_info** get_network_connection_info(){
return network_connection_info;
}
/*
* Neuron info
*/
neuron_info::neuron_info() {
queue_pointer = 0;
exist = false;
//queue_start = 0;
//queue_end = OUTPUT_MEMORY_SIZE-2;
bias=0;
}
//pushes the next output into the output_memory
void neuron_info::enqueue(double val) {
queue_pointer = (queue_pointer+1)%OUTPUT_MEMORY_SIZE;
output_memory[queue_pointer] = val;
}
//returns the last output inserted
double neuron_info::last_output() {
return output_memory[queue_pointer];
}
void neuron_info::printInfo() {
cout << "Exists: " << exist << endl
<< "Max Activation: " << max_activation << endl
<< "bias: " << bias << endl
<< "Queue: " << endl;
//cout << output_memory[queue_start] << endl;
for (int i = 0; i < OUTPUT_MEMORY_SIZE; i++ ) {
cout << output_memory[(i + queue_pointer)%OUTPUT_MEMORY_SIZE] << endl;
}
}
axon_info::axon_info(){
exist = false;
weight = 0;
axon_length = 0;
queue_pointer = 0;
}
//pushes the next output into the axon queue
void axon_info::enqueue(double val) {
queue_pointer = (queue_pointer + 1)%axon_length;
axon_throughput_queue[queue_pointer] = val * weight;
}
void axon_info::printInfo() {
cout << "Exists: " << exist << endl
<< "Weight: " << weight << endl
<< "Axon Length: " << axon_length << endl
<< "Axon Queue: " << endl;
for (int i = 0; i < axon_length; i++) {
cout << "\t" << axon_throughput_queue[(i + queue_pointer)%axon_length] << endl;
}
}
//initializes the connections between existing neurones
void init_Network() {
//TODO : make generation random
network_connection_info = new axon_info *[INPUT_NEURONS + MAX_HIDDEN_NEURONS];
for (int x = 0; x < INPUT_NEURONS + MAX_HIDDEN_NEURONS; x++) {
network_connection_info[x] = new axon_info[OUTPUT_NEURONS + MAX_HIDDEN_NEURONS];
for (int y = 0; y < OUTPUT_NEURONS + MAX_HIDDEN_NEURONS; y++) {
if (y < OUTPUT_NEURONS) {
if (x >= INPUT_NEURONS) {
init_Axon(x, y);
}
} else {
init_Axon(x, y);
}
}
}
}
int axons = 0;
//help method
void init_Axon(int x, int y) {
srand((unsigned) std::chrono::high_resolution_clock::now().time_since_epoch().count());
//manipulating the exist will alter the network structure
//-> should be manipulated by a fitting algorithm
network_connection_info[x][y].exist =
((((double) random()) / RAND_MAX) * MAX_HIDDEN_NEURONS * (1 / CONNECTIVITY)) < 1;
if (network_connection_info[x][y].exist) {
//following should only be set if exist = true
//initializes network with random weights (between -1 and 1)
network_connection_info[x][y].weight = (((double) random()) / RAND_MAX) * 2 - 1;
//initializes axon with random length (= queue length)
network_connection_info[x][y].axon_length = (int) (1 + random() % MAX_AXON_LENGTH);
//initializes queue; axon_length is the initial looked at destination synapsis
network_connection_info[x][y].axon_throughput_queue =
new double[network_connection_info[x][y].axon_length];
network_connection_info[x][y].axon_throughput_queue[0] = 0;
axons++;
}
}
//deletes the connection between neurones
void del_Network(){
for (int i = 0; i < INPUT_NEURONS + MAX_HIDDEN_NEURONS; ++i) {
delete[] network_connection_info[i];
}
delete[] network_connection_info;
}
//initialize neuron info
void init_neuron_info() {
//TODO : make generation random
input_neuron_info = new neuron_info[INPUT_NEURONS];
for (int i = 0; i < INPUT_NEURONS; i++) {
input_neuron_info[i].exist = true;
input_neuron_info[i].max_activation = 1;//std::numeric_limits<double>::max();
for(int j = 0; j < OUTPUT_MEMORY_SIZE; j++){
input_neuron_info[i].output_memory[j] = 0;
}
}
output_neuron_info = new neuron_info[OUTPUT_NEURONS];
for (int i = 0; i < OUTPUT_NEURONS; i++) {
output_neuron_info[i].exist = true;
output_neuron_info[i].max_activation = 1;//std::numeric_limits<double>::max();
for(int j = 0; j < OUTPUT_MEMORY_SIZE; j++){
output_neuron_info[i].output_memory[j] = 0;
}
}
hidden_neuron_info = new neuron_info[MAX_HIDDEN_NEURONS];
for (int i = 0; i < MAX_HIDDEN_NEURONS; i++) {
hidden_neuron_info[i].exist = true;
hidden_neuron_info[i].max_activation = 1;//std::numeric_limits<double>::max();
for(int j = 0; j < OUTPUT_MEMORY_SIZE; j++){
hidden_neuron_info[i].output_memory[j] = 0;
}
}
}
//deletes all neurones
void del_neuron_info() {
delete[] input_neuron_info;
delete[] output_neuron_info;
delete[] hidden_neuron_info;
}
/*
* feed forward
*/
//simulates the next iteration of the network
void tick() {
//step 1: learn if there is something to learn (update all departing axons weights
// dependent on output_memory)
learn();
//step 2: collect new input -> push result in output_memory
double buffer = 0;
for (int y = 0; y < OUTPUT_NEURONS; y++) {
buffer = 0;
if (output_neuron_info[y].exist) {
for (int x = 0; x < INPUT_NEURONS + MAX_HIDDEN_NEURONS; x++) {
if (network_connection_info[x][y].exist) {
buffer += network_connection_info[x][y].axon_throughput_queue[
(network_connection_info[x][y].queue_pointer + 1) %
network_connection_info[x][y].axon_length];
}
}
output_neuron_info[y].enqueue(
activate(buffer, output_neuron_info[y].max_activation, hidden_neuron_info[y].bias));
}
}
for (int y = OUTPUT_NEURONS; y < OUTPUT_NEURONS + MAX_HIDDEN_NEURONS; y++) {
buffer = 0;
if (hidden_neuron_info[y - OUTPUT_NEURONS].exist) {
for (int x = 0; x < INPUT_NEURONS + MAX_HIDDEN_NEURONS; x++) {
if (network_connection_info[x][y].exist) {
buffer += network_connection_info[x][y].axon_throughput_queue[
(network_connection_info[x][y].queue_pointer + 1) %
network_connection_info[x][y].axon_length];
}
}
hidden_neuron_info[y - OUTPUT_NEURONS].enqueue(
activate(buffer, hidden_neuron_info[y - OUTPUT_NEURONS].max_activation,
hidden_neuron_info[y - OUTPUT_NEURONS].bias));
}
}
//step 3: push step 2s result in all departing axons
for (int x = 0; x < INPUT_NEURONS; x++) {
for (int y = 0; y < MAX_HIDDEN_NEURONS + OUTPUT_NEURONS; y++) {
if (network_connection_info[x][y].exist) {
network_connection_info[x][y].enqueue(input_neuron_info[x].last_output());
}
}
}
for (int x = INPUT_NEURONS; x < INPUT_NEURONS + MAX_HIDDEN_NEURONS; x++) {
for (int y = 0; y < MAX_HIDDEN_NEURONS + OUTPUT_NEURONS; y++) {
if (network_connection_info[x][y].exist) {
network_connection_info[x][y].enqueue(hidden_neuron_info[x - INPUT_NEURONS].last_output());
}
}
}
}
int feedback = 0; //should only be -1, 0 or 1, altering that should have an effect similiar to
//slightly good or bad when below 1/-1
void helpLearn(int x, double total_activation) {
double percentage_change = total_activation / (OUTPUT_MEMORY_SIZE);
for (int y = 0; y < OUTPUT_NEURONS + MAX_HIDDEN_NEURONS; y++) {
if (feedback < 0) {
network_connection_info[x][y].weight += network_connection_info[x][y].weight * percentage_change * feedback;
} else {
network_connection_info[x][y].weight += (1 - network_connection_info[x][y].weight) * percentage_change * feedback;
}
}
}
double expected_average_degree = CONNECTIVITY; //how many incoming axons a neuron has on average
void learn() {
if (feedback != 0) {
for (int x = 0; x < INPUT_NEURONS; x++) {
double total_activation = 0;
for (int i = 0; i < OUTPUT_MEMORY_SIZE; i++) {
total_activation += input_neuron_info[x].output_memory[i];
}
helpLearn(x, total_activation);
}
for (int x = 0; x < MAX_HIDDEN_NEURONS; x++) {
double total_activation = 0;
for (int i = 0; i < OUTPUT_MEMORY_SIZE; i++) {
total_activation += hidden_neuron_info[x].output_memory[i];
}
helpLearn(x + INPUT_NEURONS, total_activation);
}
}
}
double relu_tanh(double in, double maxActivation, double bias) {
if (in > 0 && in < maxActivation) {
//return 1;
return std::min(in + tanh(in + bias), maxActivation) == maxActivation ? maxActivation : tanh(
in / expected_average_degree);
} else {
//std::cout << "ree? " << in << std::endl;
return 0;
}
}
void print_adjacency(){
for(int i = 0; i < INPUT_NEURONS + MAX_HIDDEN_NEURONS; i++){
for(int j = 0; j < OUTPUT_NEURONS + MAX_HIDDEN_NEURONS; j++){
cout << network_connection_info[i][j].exist << " ";
}
cout << endl;
}
}
/*
* MAIN
*/
int main(int argc, char *argv[]) {
//set activation function
activate = relu_tanh;
cout << "initializing neurons\n";
init_neuron_info();
cout << "done\n";
cout << "initializing network\n";
init_Network();
cout << "done\n";
cout << "total connections: " << axons << endl;
print_adjacency();
std::thread window (createWindow);
input_neuron_info[0].output_memory[input_neuron_info[0].queue_pointer] = 1;
std::chrono::high_resolution_clock::time_point ts1 = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 20; i++) {
//input_neuron_info[(int)((random()/(float)RAND_MAX)*INPUT_NEURONS)].output_memory[0] = random()/(float)RAND_MAX;
for (int a = 0; a < OUTPUT_NEURONS; a++) {
cout << i << " Output: " << output_neuron_info[a].last_output() << endl;
}
std::this_thread::sleep_for(std::chrono::duration<float>(1));
tick();
input_neuron_info[0].output_memory[input_neuron_info[0].queue_pointer] = 0;
}
std::chrono::high_resolution_clock::time_point ts2 = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> time_span = std::chrono::duration_cast<std::chrono::duration<double>>(ts2 - ts1);
cout << "tick time: " << time_span.count()/20 << endl;
window.join();
cout << "window closed" << endl;
del_neuron_info();
del_Network();
return 0;
}