//Backpropagation, 25x25x8 units, binary sigmoid function network //Written by Thomas Riga, University of Genoa, Italy //thomas@magister.magi.unige.it #include #include #include #include #include #include #include #include double **input, *hidden, **output, **target, *bias, **weight_i_h, **weight_h_o, *errorsignal_hidden, *errorsignal_output; int input_array_size, hidden_array_size, output_array_size, max_patterns, bias_array_size, gaset = -2500, number_of_input_patterns, pattern, file_loaded = 0, ytemp = 0, ztemp = 0; double learning_rate, max_error_tollerance = 0.1; char filename[128]; #define IA 16807 #define IM 2147483647 #define AM (1.0 / IM) #define IQ 127773 #define IR 2836 #define NTAB 32 #define NDIV (1+(IM-1) / NTAB) #define EPS 1.2e-7 #define RNMX (1.0 - EPS) int compare_output_to_target(); void load_data(char *arg); void save_data(char *argres); void forward_pass(int pattern); void backward_pass(int pattern); void custom(); void compute_output_pattern(); void get_file_name(); float bedlam(long *idum); void learn(); void make(); void test(); void print_data(); void print_data_to_screen(); void print_data_to_file(); void output_to_screen(); int getnumber(); void change_learning_rate(); void initialize_net(); void clear_memory(); main() { cout << "backpropagation network by Thomas Riga, University of Genoa, Italy" << endl; for(;;) { char choice; cout << endl << "1. load data" << endl; cout << "2. learn from data" << endl; cout << "3. compute output pattern" << endl; cout << "4. make new data file" << endl; cout << "5. save data" << endl; cout << "6. print data" << endl; cout << "7. change learning rate" << endl; cout << "8. exit" << endl << endl; cout << "Enter your choice (1-8)"; do { choice = getch(); } while (choice != '1' && choice != '2' && choice != '3' && choice != '4' && choice != '5' && choice != '6' && choice != '7' && choice != '8'); switch(choice) { case '1': { if (file_loaded == 1) clear_memory(); get_file_name(); file_loaded = 1; load_data(filename); } break; case '2': learn(); break; case '3': compute_output_pattern(); break; case '4': make(); break; case '5': { if (file_loaded == 0) { cout << endl << "there is no data loaded into memory" << endl; break; } cout << endl << "enter a filename to save data to: "; cin >> filename; save_data(filename); } break; case '6': print_data(); break; case '7': change_learning_rate(); break; case '8': return 0; }; } } void initialize_net() { int x; input = new double * [number_of_input_patterns]; if(!input) { cout << endl << "memory problem!"; exit(1); } for(x=0; x