Пример #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");
}
Пример #2
0
static void test_backprop_save_load()
{
    bp net1, net2;
    int no_of_inputs=10;
    int no_of_hiddens=4;
    int no_of_outputs=3;
    int hidden_layers=3;
    int retval;
    unsigned int random_seed = 123;
    char filename[256];
    FILE * fp;

    printf("test_backprop_save_load...");

    /* create network */
    bp_init(&net1,
            no_of_inputs, no_of_hiddens,
            hidden_layers, no_of_outputs,
            &random_seed);

    sprintf(filename,"%stemp_deep.dat",DEEPLEARN_TEMP_DIRECTORY);

    /* save the first network */
    fp = fopen(filename,"wb");
    assert(fp!=0);
    bp_save(fp, &net1);
    fclose(fp);

    /* load into the second network */
    fp = fopen(filename,"rb");
    assert(fp!=0);
    bp_load(fp, &net2, &random_seed);
    fclose(fp);

    /* compare the two */
    retval = bp_compare(&net1, &net2);
    if (retval<1) {
        printf("\nretval = %d\n",retval);
    }
    assert(retval==1);

    /* free memory */
    bp_free(&net1);
    bp_free(&net2);

    printf("Ok\n");
}
Пример #3
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");
}
Пример #4
0
static void test_backprop_deep()
{
    bp net;
    bp autocoder;
    int l,itt,i;
    int no_of_inputs=10;
    int no_of_hiddens=4;
    int hidden_layers=3;
    int no_of_outputs=2;
    unsigned int random_seed = 123;

    printf("test_backprop_deep...");

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

    for (l = 0; l < hidden_layers; l++) {
        /* create an autocoder for this layer */
        bp_create_autocoder(&net,l,&autocoder);
        /* do some training */
        for (itt = 0; itt < 100; itt++) {
            /* set the inputs */
            for (i = 0; i < no_of_inputs; i++) {
                bp_set_input(&net,i,i/(float)no_of_inputs);
            }
            /* update */
            bp_pretrain(&net,&autocoder,l);
        }
        /* move the autocoder hidden weights into the main network */
        bp_update_from_autocoder(&net,&autocoder,l);
        /* delete the autocoder */
        bp_free(&autocoder);
    }

    bp_free(&net);

    printf("Ok\n");
}
Пример #5
0
static void test_backprop_inputs_from_image()
{
    bp net;
    int image_width=10;
    int image_height=10;
    int x,y,i=0;
    unsigned char * img;
    int no_of_inputs=image_width*image_height;
    int no_of_hiddens=4*4;
    int hidden_layers=2;
    int no_of_outputs=2*2;
    unsigned int random_seed = 123;

    printf("test_backprop_inputs_from_image...");

    /* create a mono image */
    img = (unsigned char*)malloc(image_width*image_height);
    for (y = 0; y < image_height; y++) {
        for (x = 0; x < image_width; x++,i++) {
            img[i] = i%256;
        }
    }

    /* create a network */
    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 inputs to zero */
    for (i = 0; i < no_of_inputs; i++) {
        (&net)->inputs[i]->value = 0;
    }

    /* insert the image into the input units */
    bp_inputs_from_image(&net, img, image_width, image_height);

    /* check that the imputs are within range */
    for (i = 0; i < no_of_inputs; i++) {
        assert((&net)->inputs[i]->value > 0.1f);
        assert((&net)->inputs[i]->value < 0.9f);
    }

    /* free the memory */
    free(img);
    bp_free(&net);

    printf("Ok\n");
}
Пример #6
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");
}
Пример #7
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");
}
Пример #8
0
static void test_backprop_init()
{
    bp net;
    int no_of_inputs=10;
    int no_of_hiddens=4;
    int hidden_layers=2;
    int no_of_outputs=2;
    unsigned int random_seed = 123;

    printf("test_backprop_init...");

    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);
    bp_free(&net);

    printf("Ok\n");
}
Пример #9
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");
}