Esempio n. 1
0
static void test_backprop_update()
{
    bp net;
    int no_of_inputs=10;
    int no_of_hiddens=4;
    int hidden_layers=2;
    int no_of_outputs=5;
    int i;
    unsigned int random_seed = 123;

    printf("test_backprop_update...");

    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);
    }

    for (i = 0; i < 100; i++) {
        bp_update(&net,0);
    }

    bp_free(&net);

    printf("Ok\n");
}
Esempio n. 2
0
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");
}
Esempio n. 3
0
/**
* @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);
}
Esempio n. 4
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");
}