#pragma warning (disable:4786) #include "ai nnet.hpp" #include #include Back_Layer::Back_Layer (int nodes,int ninputs,bool first) { Create (nodes,ninputs,first); } void Back_Layer::Create (int nodes,int ninputs,bool first) { int i,j; double temp; next = NULL; prev = NULL; num_nodes = nodes; next = prev = NULL; thenodes = (Back_Node *) calloc (sizeof (Back_Node),num_nodes); num_inputs = ninputs; inputs = (double *) calloc (sizeof (double),num_inputs); outputs = (double *) calloc (sizeof (double),num_nodes); errors = (double *) calloc (sizeof (double),num_nodes); for (i=0;inum_inputs)); for (i=0;iinputs[i] = outputs[i]; } } void Back_Layer::propagate_error (void) { int i,j; assert (next); for (i=0;inum_nodes;j++) { errors[i] += (next->errors[j]*next->thenodes[j].weights[i]); } errors[i] *= (outputs[i] * (1.0-outputs[i])); } } void Back_Network::correct (bool finetune) { compute_error (finetune); propagate_error (); adjust_weights (); } void Back_Network::adjust_weights (void) { int i,j,k; double tochange; for (i=1;i=0;i--) { thelayers[i].propagate_error (); } } void Back_Network::compute_error (bool finetune) { int i; for (i=0;i 0.5) wanted_output[i] = 1.0; else wanted_output[i] = 0.0; } correct (finetune); } else { temp = get_answer (); for (i=0;i 0.5) wanted_output[i] = 1.0; else wanted_output[i] = 0.0; } else { if ((rand ()&0x01)) wanted_output[i] = 1.0; else wanted_output[i] = 0.0; } } correct (finetune); } } Back_Network::Back_Network (int layers,int *num_nodes,double neta,double nalpha) { int i; eta = neta; alpha = nalpha; num_inputs = num_nodes[0]; num_outputs = num_nodes[layers-1]; input = (double *) calloc (sizeof (double),num_inputs); output = (double *) calloc (sizeof (double),num_outputs); wanted_output = (double *) calloc (sizeof (double),num_outputs); num_layers = layers; thelayers = (Back_Layer *) calloc (sizeof (Back_Layer),layers); thelayers[0].Create (num_nodes[0],num_nodes[0],true); thelayers[0].next = &(thelayers[1]); for (i=1;i