Neural networks: Calculating simple additions

Source: Internet
Author: User
Tags fann windows dll files

Here's the complete code:

//

FANN_TEST.C:

FANN Test

//

Download the Fann library to the following Web site:

Fast Artificial Neural Network Library (Fann)

http://leenissen.dk/fann/

Download links: Download FANN---> C Source Code and Windows DLL files

//


#include ". /fann-2.0.0/src/include/doublefann.h "


#ifdef _DEBUG

#pragma comment (lib, "...). /fann-2.0.0/microsoftwindowsdll/bin/fanndoublemtd.lib ")

#else

#pragma comment (lib, "...). /fann-2.0.0/microsoftwindowsdll/bin/fanndoublemt.lib ")

#endif


Training:

Additive neural network

c = a+b;

void Train ()

{

const unsigned int num_input = 2; Number of items entered

const unsigned int num_output = 1; Number of output items

const unsigned int num_layers = 3;

const unsigned int num_neurons_hidden = 3;

const FLOAT Desired_error = (const float) 0.00000001;

const unsigned int max_epochs = 500000; Maximum execution times

const unsigned int epochs_between_reports = 10000; Reporting frequency


struct Fann *ann;


int Num = 200;

float Mf = num*3.f;

int i;

Double A, B, C;


FILE *FP;

Fopen_s (&FP, "Add.fann", "w");

fprintf_s (FP, "%d 2 1\n", Num);


Generate Training Files

For (I=1 i<=num; i++) {

Generates 2 numbers, required between (0,1)

A = I/MF;

b = (i+1)/mf;

c = a+b; Requirements between (0,1)


The input is written to the training file

fprintf_s (FP, "%lf%lf\n%lf\n", A, B, c);

}

Fclose (FP);


Sample Training

Ann = Fann_create_standard (num_layers, Num_input, Num_neurons_hidden, num_output);


Fann_set_activation_function_hidden (Ann, Fann_linear);

Fann_set_activation_function_output (Ann, Fann_linear);


Fann_train_on_file (Ann, "Add.fann", Max_epochs, Epochs_between_reports, Desired_error);

Fann_save (Ann, "Add.fann.net");

Fann_destroy (ANN);

}

Perform:

Test

void exec (double A, double b)

{

struct Fann *ann;


Fann_type *calc_out;

Fann_type input[2];

Ann = Fann_create_from_file ("add.fann.net");


Input[0] = A;

INPUT[1] = b;


Calc_out = Fann_run (ann, input);

Fann_destroy (ANN);

printf ("A=%f\nb=%f\nc=%f\n expected c=%f\n\n", input[0], input[1], calc_out[0], input[0]+input[1]);

}


//

Main program

//

int main ()

{

The following method only needs to be called once, then comment out

Train ();


EXEC (0.354,0.58934);

EXEC (0.21469,0.3914968);

EXEC (0.130,0.44);

EXEC ( -0.3654,0.58455);

EXEC (0.365420,-0.95);


return 0;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.