static void test_backprop() { bp net; int no_of_inputs=10; int no_of_hiddens=4; int hidden_layers=2; int no_of_outputs=5; int i,l; unsigned int random_seed = 123; printf("test_backprop..."); bp_init(&net, no_of_inputs, no_of_hiddens, hidden_layers, no_of_outputs, &random_seed); assert((&net)->inputs!=0); assert((&net)->hiddens!=0); assert((&net)->outputs!=0); /* set some inputs */ for (i = 0; i < no_of_inputs; i++) { bp_set_input(&net, i, i/(float)no_of_inputs); (&net)->inputs[i]->BPerror = 999; } for (l = 0; l < hidden_layers; l++) { for (i = 0; i < bp_hiddens_in_layer(&net,l); i++) { (&net)->hiddens[l][i]->BPerror = 999; } } /* set some target outputs */ for (i = 0; i < no_of_outputs; i++) { (&net)->outputs[i]->BPerror = 999; bp_set_output(&net, i, i/(float)no_of_inputs); } /* feed forward */ bp_feed_forward(&net); bp_backprop(&net,0); /* check for non-zero backprop error values */ for (i = 0; i < no_of_inputs; i++) { assert((&net)->inputs[i]->BPerror != 999); } for (l = 0; l < hidden_layers; l++) { for (i = 0; i < bp_hiddens_in_layer(&net,l); i++) { assert((&net)->hiddens[l][i]->BPerror != 999); } } for (i = 0; i < no_of_outputs; i++) { assert((&net)->outputs[i]->BPerror != 999); } bp_free(&net); printf("Ok\n"); }
static void test_backprop_autocoder() { bp autocoder; int itt,i,j; int no_of_inputs=10; int no_of_hiddens=4; int no_of_outputs=10; unsigned int random_seed = 123; float tot; printf("test_backprop_autocoder..."); /* create the autocoder */ bp_init(&autocoder, no_of_inputs, no_of_hiddens,1, no_of_outputs, &random_seed); autocoder.learningRate = 0.5f; /* run the autocoder for some itterations */ for (itt = 0; itt < 100; itt++) { /* set the inputs */ for (i = 0; i < no_of_inputs; i++) { bp_set_input(&autocoder,i,0.25f + (i*0.5f/(float)no_of_inputs)); bp_set_output(&autocoder,i,0.75f - (i*0.5f/(float)no_of_inputs)); } /* update */ bp_update(&autocoder,0); } for (i = 0; i < no_of_hiddens; i++) { /* check that some errors have been back-propogated */ assert((&autocoder)->hiddens[0][i]->BPerror != 0); /* check that weights have changed */ tot = 0; for (j = 0; j < no_of_inputs; j++) { assert((&autocoder)->hiddens[0][i]->lastWeightChange[j]!=0); tot += fabs((&autocoder)->hiddens[0][i]->lastWeightChange[j]); } /* total weight change */ assert(tot > 0.00001f); } bp_free(&autocoder); printf("Ok\n"); }
/** * @brief Update an autocoder neural net * @param net Backprop neural net object */ static void bp_update_autocoder(bp * net) { int i; /* number of input and output units should be the same */ assert(net->NoOfInputs == net->NoOfOutputs); /* set the target outputs to be the same as the inputs */ for (i = 0; i < net->NoOfInputs; i++) { bp_set_output(net,i,net->inputs[i]->value); } /* run the autocoder */ bp_update(net,0); }
static void test_backprop_training() { bp * net; int no_of_inputs=2; int no_of_hiddens=2; int hidden_layers=1; int no_of_outputs=1; int itt,example; unsigned int random_seed = 123; float state_TRUE = 0.8f; float state_FALSE = 0.2f; printf("test_backprop_training..."); net = (bp*)malloc(sizeof(bp)); bp_init(net, no_of_inputs, no_of_hiddens, hidden_layers, no_of_outputs, &random_seed); assert(net->inputs!=0); assert(net->hiddens!=0); assert(net->outputs!=0); /* training */ example=0; for (itt = 0; itt < 500000; itt++, example++) { if (example>=4) example=0; /* select an example from the XOR truth table */ switch(example) { case 0: { bp_set_input(net, 0, state_FALSE); bp_set_input(net, 1, state_FALSE); bp_set_output(net, 0, state_FALSE); break; } case 1: { bp_set_input(net, 0, state_TRUE); bp_set_input(net, 1, state_FALSE); bp_set_output(net, 0, state_TRUE); break; } case 2: { bp_set_input(net, 0, state_FALSE); bp_set_input(net, 1, state_TRUE); bp_set_output(net, 0, state_TRUE); break; } case 3: { bp_set_input(net, 0, state_TRUE); bp_set_input(net, 1, state_TRUE); bp_set_output(net, 0, state_FALSE); break; } } /* train on the example */ bp_update(net,0); } bp_set_input(net, 0, state_FALSE); bp_set_input(net, 1, state_FALSE); bp_feed_forward(net); if (bp_get_output(net, 0) >= 0.5f) { printf("\n%.5f\n",bp_get_output(net, 0)); } assert(bp_get_output(net, 0) < 0.5f); bp_set_input(net, 0, state_FALSE); bp_set_input(net, 1, state_TRUE); bp_feed_forward(net); if (bp_get_output(net, 0) <= 0.5f) { printf("\n%.5f\n",bp_get_output(net, 0)); } assert(bp_get_output(net, 0) > 0.5f); bp_set_input(net, 0, state_TRUE); bp_set_input(net, 1, state_FALSE); bp_feed_forward(net); if (bp_get_output(net, 0) <= 0.5f) { printf("\n%.5f\n",bp_get_output(net, 0)); } assert(bp_get_output(net, 0) > 0.5f); bp_set_input(net, 0, state_FALSE); bp_set_input(net, 1, state_FALSE); bp_feed_forward(net); if (bp_get_output(net, 0) >= 0.5f) { printf("\n%.5f\n",bp_get_output(net, 0)); } assert(bp_get_output(net, 0) < 0.5f); bp_free(net); free(net); printf("Ok\n"); }