Beispiel #1
0
/**
* @brief Update the neural net during training
* @param net Backprop neural net object
*/
void bp_update(bp * net, int current_hidden_layer)
{
    bp_dropouts(net);
    bp_feed_forward(net);
    bp_backprop(net, current_hidden_layer);
    bp_learn(net, current_hidden_layer);
    bp_clear_dropouts(net);
}
Beispiel #2
0
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");
}
Beispiel #3
0
static void test_backprop_feed_forward()
{
    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_feed_forward...");

    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);
    }
    /* clear the outputs */
    for (i = 0; i < no_of_outputs; i++) {
        (&net)->outputs[i]->value = 999;
    }

    /* feed forward */
    bp_feed_forward(&net);

    /* check for non-zero outputs */
    for (i = 0; i < no_of_outputs; i++) {
        assert((&net)->outputs[i]->value != 999);
    }

    bp_free(&net);

    printf("Ok\n");
}
Beispiel #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");
}