-
Notifications
You must be signed in to change notification settings - Fork 0
/
dn_aux.c
494 lines (445 loc) · 14.4 KB
/
dn_aux.c
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
///implimentations
#include "deepnet.h"
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <pthread.h>
void print_usage( void ){
fprintf( stderr, "Usage: deepnet [mode] [# data] [size] [neurons.brain] [visualize] [rotate]\n \
\tmode: [t/a] either a 't' to specify training mode or 'a' to analyze\n \
\t# data: number of data for training or analysis\n \
\tsize: the length of the largest data (including solution flag and length header if training).\n \
\tneurons.brain: file to dump neurons to after training or load neurons for analysis\n \
\tvisualize: [0 to # data] opengl visualization of training (slower). 0 to disable\n \
\trotate: [0 or 1] rotate the visualization in 3 dimentions\n" );
}
int check_args( char* argv[] ){
//check mode: if its not a single char or its not the char t or a
if( strlen( argv[1] ) != 1 || !( argv[1][0] == 't' || argv[1][0] == 'a' ) ){
fprintf( stderr, "Invalid mode: options are 't' to train or 'a' to analyze\n\n" );
print_usage();
return 1;
}
//check # data: if it is a number greater than 0
for( unsigned int c = 0; c < strlen( argv[2] ); c++ ){
if( !isdigit( argv[2][c] ) ){
fprintf( stderr, "# data parameter must be a number greater than 0\n\n" );
print_usage();
return 1;
}
}
if( strtol( argv[2], NULL, 10 ) < 1 ){
fprintf( stderr, "# data must be greater than 0\n\n" );
print_usage();
return 1;
}
//check size: if it is a number greater than 0
for( unsigned int c = 0; c < strlen( argv[3] ); c++ ){
if( !isdigit( argv[3][c] ) ){
fprintf( stderr, "size parameter must be a number greater than 0\n\n" );
print_usage();
return 1;
}
}
if( strtol( argv[3], NULL, 10 ) < 1 ){
fprintf( stderr, "size must be greater than 0\n\n" );
print_usage();
return 1;
}
//check file: make sure filename is less than 50 chars long
if( strlen( argv[4] ) > 49 ){
fprintf( stderr, "File name cannot exceed 50 characters\n\n" );
print_usage();
return 1;
}
//visualize
for( unsigned int c = 0; c < strlen( argv[5] ); c++ ){
if( !isdigit( argv[5][c] ) ){
fprintf( stderr, "visualize parameter must be a number greater than 0 and less than # data\n\n" );
print_usage();
return 1;
}
}
if( strtol( argv[5], NULL, 10 ) < 0 || strtol( argv[5], NULL, 10 ) > strtol( argv[2], NULL, 10 ) ){
fprintf( stderr, "Invalid number: must be 0 or greater and less then # data\n\n" );
print_usage();
return 1;
}
if( sizeof(argv[6])/sizeof(long) != 1 ){
fprintf( stderr, "Invalid choice: must be 0 or 1\n\n");
print_usage();
return 1;
}
else{
if( !(argv[6][0]=='1' || argv[6][0]=='0') ){
fprintf( stderr, "Invalid choice: must be 0 or 1\n\n");
print_usage();
return 1;
}
}
return 0;
}
void get_input( char input[], int size ){
fgets( input, size+2, stdin);
}
//char data extraction : TODO : raw binary extraction
void extract_data( TData d, char input_buffer[], int size ){
d->data = malloc( sizeof( double ) * (size - 1 ) );
for( int bit = 0; bit < size - 1; bit++){
if( input_buffer[bit] == '1' )
d->data[bit] = 1.0;
else
d->data[bit] = 0.0;
}
if( input_buffer[size-1] == '1' )
d->solution = 1.0;
else
d->solution = 0.0;
}
void init_syn2( double syn0[], int size ){
srand((unsigned) time(NULL));
for( int cell = 0; cell < size; cell++ ){
syn0[cell] = (double)rand()/RAND_MAX*2.0-1.0;
}
}
void init_syn1( double** syn1, int rows, int cols ){
srand((unsigned) time(NULL));
for( int row = 0; row < rows; row++ ){
for( int col = 0; col < cols; col++ ){
syn1[row][col] = (double)rand()/RAND_MAX*2.0-1.0;
}
}
}
int train_mode(Options o, SynStore s){
Opengl gl = malloc( sizeof( Opengl_s ) );
if( o->visualize > 0 )
init_opengl( gl );
int num_prims = 4*(o->size-1)*6;
double primatives[num_prims];
generate_colors( o, primatives );
for( int data = 0; data < o->numdata; data++ ){
#ifdef DEBUG
printf("##########################\n#Training data number: %d #\n##########################\n", data);
printf("===SYNAPSES===\n");
printf("Syn0:\n");
for(int row = 0; row < o->size - 1; row++){
for(int col = 0; col < 4; col++){
printf("%f ",s->synapse0[row][col]);
}
printf("\n");
}
printf("\nSyn1:\n");
for(int row = 0; row < 4; row++){
for(int col = 0; col < 4; col++){
printf("%f ",s->synapse1[row][col]);
}
printf("\n");
}
printf("\nSyn2:\n");
for(int cell = 0; cell < 4; cell++){
printf("%f ",s->synapse2[cell]);
}
printf("\n=============\n\n");
#endif
//create train data input buffer based on largest size
char input_buffer[o->size+1];
//zero out the buffer
for( int cell = 0; cell < o->size; cell++)
input_buffer[ cell ] = '0';
//get the input from stdin
get_input( input_buffer, o->size );
#ifdef DEBUG
printf("INPUT: ");
for(int cell = 0; cell < o->size-1; cell++){
printf("%c, ",input_buffer[cell]);
}
#endif
//extract data length, solution flag, and data
TData d = malloc( sizeof( TData_s ) );
extract_data( d, input_buffer, o->size );
#ifdef DEBUG
printf("[%f]\n",d->solution);
#endif
//##Layers###
//
double* L0 = d->data;
double L1[4] = {0};
double L2[4] = {0};
double L3 = 0;
//
//###########
//begin training
//FORWARD PROPEGATION
//L1
vm( o->size-1, 4, L0, s->synapse0, L1 );
sigmoid_vector( 4, L1, 0 );
//L2
vm( 4, 4, L1, s->synapse1, L2 );
sigmoid_vector( 4, L2, 0 );
//L3
L3 = sigmoid( vv( L2, s->synapse2, 4 ), 0 );
#ifdef DEBUG
printf("LAYERS:\n\tlayer0: ");
for( int cell = 0; cell < o->size - 1; cell++ ){
printf("%f ", L0[cell]);
}
printf("\n");
printf("\tlayer1: ");
for( int cell = 0; cell < 4; cell++ ){
printf("%f ", L1[cell]);
}
printf("\n");
printf("\tlayer2: ");
for( int cell = 0; cell < 4; cell++ ){
printf("%f ", L2[cell]);
}
printf("\n");
printf("\tlayer3: %f\n", L3);
#endif
//ERROR
double L3_error = d->solution - L3;
#ifdef DEBUG
printf("ERROR:\n\tlayer3: %f\n",L3_error);
#endif
//backpropegation
double L3_delta = L3_error * sigmoid( L3, 1 );
#ifdef DEBUG
printf("\t\tlayer3 delta: %f\n",L3_delta);
#endif
double L2_error[4] = {0};
#ifdef DEBUG
printf("\tlayer2: ");
#endif
for( int cell = 0; cell < 4; cell++ ){
L2_error[cell] = s->synapse2[cell] * L3_delta;
#ifdef DEBUG
printf("%f ",L2_error[cell]);
#endif
}
#ifdef DEBUG
printf("\n\t\tlayer2 delta: ");
#endif
double L2_delta[4] = {0};
for( int cell = 0; cell < 4; cell++ ){
L2_delta[cell] = L2_error[cell] * sigmoid( L2[cell], 1 );
#ifdef DEBUG
printf("%f ",L2_delta[cell]);
#endif
}
#ifdef DEBUG
printf("\n\tlayer1: ");
#endif
double L1_error[4] = {0};
vm( 4, 4, L2_delta, s->synapse1, L1_error );
#ifdef DEBUG
for( int cell = 0; cell < 4; cell++ ){
printf("%f ", L1_error[cell]);
}
printf("\n\t\tlayer1 delta: ");
#endif
double L1_delta[4] = {0};
for( int cell = 0; cell < 4; cell++ ){
L1_delta[cell] = L1_error[cell] * sigmoid( L1[cell], 1 );
#ifdef DEBUG
printf("%f ",L1_delta[cell]);
#endif
}
#ifdef DEBUG
printf("\n");
#endif
//UPDATE synapses
//syn2
for( int cell = 0; cell < 4; cell++ ){
s->synapse2[cell] += L2[cell] * L3_delta;
}
//syn1
for( int row = 0; row < 4; row++ ){
for( int col = 0; col < 4; col++ ){
s->synapse1[row][col] += L1[row] * L2_delta[col];
}
}
//syn0
for( int row = 0; row < o->size-1; row++ ){
for( int col = 0; col < 4; col++){
s->synapse0[row][col] += L0[row] * L1_delta[col];
}
}
//prepare for opengl
generate_primatives( s, o, primatives );
#ifdef DEBUG
printf("\nPrimatives\n");
for(int i = 0; i<num_prims; i+=6){
printf("%f %f %f %f %f %f\n",primatives[i],primatives[i+1],primatives[i+2], \
primatives[i+3],primatives[i+4],primatives[i+5]);
}
#endif
int rotate_ammount = 0;
if( o->visualize > 0 ){
if( o->rotate )
rotate_ammount = data;
if( (data % o->visualize) == 0 )
render_primatives( primatives, gl, num_prims, rotate_ammount );
}
//cleanup
free(d->data);
free(d);
#ifndef DEBUG
printf( "\033[2J" );
fflush( stdout );
printf( "\033[%d;%dH", 1, 0 );
//progress bar
printf("[");
int bars = ((double)data/o->numdata*100);
for( int bar = 0; bar < bars; bar++ )
printf("=");
for( int space = 0; space < 100 - bars; space++ )
printf(" ");
printf("]\n");
printf("%f%%\n",(double)data/o->numdata*100);
printf("%d of %d\n",data,o->numdata);
#endif
}
free( gl );
return 0;
}
int analyze_mode(Options o, SynStore s){
printf("\n===SYNAPSES===\n");
printf("Syn0:\n");
for(int row = 0; row < o->size; row++){
for(int col = 0; col < 4; col++){
printf("%f ",s->synapse0[row][col]);
}
printf("\n");
}
printf("\nSyn1:\n");
for(int row = 0; row < 4; row++){
for(int col = 0; col < 4; col++){
printf("%f ",s->synapse1[row][col]);
}
printf("\n");
}
printf("\nSyn2:\n");
for(int cell = 0; cell < 4; cell++){
printf("%f ",s->synapse2[cell]);
}
printf("\n=============\n\n");
for( int data = 0; data < o->numdata; data++ ){
printf("##########################\n#Analysis data number: %d #\n##########################\n", data);
//create train data input buffer based on largest size
char input_buffer[o->size+1];
//zero out the buffer
for( int cell = 0; cell < o->size; cell++)
input_buffer[ cell ] = '0';
//get the input from stdin
get_input( input_buffer, o->size );
//extract
double data[o->size];
printf("INPUT: ");
for(int cell = 0; cell < o->size; cell++){
printf("%c ",input_buffer[cell]);
if( input_buffer[cell] == '1' )
data[cell] = 1.0;
else
data[cell] = 0.0;
}
//FORWARD PROPEGATION
//##Layers###
//
double L1[4] = {0};
double L2[4] = {0};
double L3 = 0;
//
//###########
//begin training
//FORWARD PROPEGATION
//L1
vm( o->size, 4, data, s->synapse0, L1 );
sigmoid_vector( 4, L1, 0 );
//L2
vm( 4, 4, L1, s->synapse1, L2 );
sigmoid_vector( 4, L2, 0 );
//L3
L3 = sigmoid( vv( L2, s->synapse2, 4 ), 0 );
printf("\nLAYERS:\n\tlayer0: ");
for( int cell = 0; cell < o->size; cell++ ){
printf("%f ", data[cell]);
}
printf("\n");
printf("\tlayer1: ");
for( int cell = 0; cell < 4; cell++ ){
printf("%f ", L1[cell]);
}
printf("\n");
printf("\tlayer2: ");
for( int cell = 0; cell < 4; cell++ ){
printf("%f ", L2[cell]);
}
printf("\n");
printf("\tlayer3: %f\n", L3);
printf("RESULT: %f%%\n", L3*100);
}
return 0;
}
void export_brain( SynStore s, Options o ){
FILE* fp = fopen( o->file, "w" );
//write synapse0
for( int row = 0; row < o->size-1; row++ ){
for( int col = 0; col < 4; col++ ){
fprintf(fp, "%f\n",s->synapse0[row][col]);
}
}
//syn1
for( int row = 0; row < 4; row++ ){
for( int col = 0; col < 4; col++ ){
fprintf(fp, "%f\n",s->synapse1[row][col]);
}
}
//syn2
for( int cell = 0; cell < 4; cell++ ){
fprintf(fp,"%f\n",s->synapse2[cell]);
}
fclose(fp);
}
void import_brain( SynStore s, Options o ){
FILE* fp = fopen( o->file, "r" );
char tempbuf[11] = {0};
//read in synapse0
for( int row = 0; row < o->size; row++ ){
for( int col =0; col < 4; col++ ){
fgets( tempbuf, 11, fp );
s->synapse0[row][col] = atof(tempbuf);
}
}
//read in synapse1
for( int row = 0; row < 4; row++ ){
for( int col =0; col < 4; col++ ){
fgets( tempbuf, 11, fp );
s->synapse1[row][col] = atof(tempbuf);
}
}
//read in synapse2
for( int cell = 0; cell < 4; cell++ ){
fgets( tempbuf, 11, fp );
s->synapse2[cell] = atof(tempbuf);
}
printf("Synapses loaded\n");
fclose(fp);
}
void generate_primatives( SynStore s, Options o, double primatives[] ){
//synapse 0 x, synapse 1 y, synapse 2 z
//number of primatives = grid size of synapse 0 * 3
//fill
for( int prim = 0; prim < ( 4 * (o->size-1) ); prim++ ){
primatives[prim*6] = prim_norm(s->synapse0[prim%(o->size-1)][prim/(o->size-1)]);
primatives[prim*6+1] = prim_norm(s->synapse1[(prim%(4*4))%4][(prim%(4*4))/4]);
primatives[prim*6+2] = prim_norm(s->synapse2[prim%4]);
}
}
void generate_colors( Options o, double primatives[] ){
srand((unsigned) time(NULL));
for( int prim = 0; prim < ( 4 * (o->size-1) ); prim++ ){
primatives[prim*6+3] = (double)rand()/RAND_MAX;
primatives[prim*6+4] = (double)rand()/RAND_MAX;
primatives[prim*6+5] = (double)rand()/RAND_MAX;
}
}