This article is about to help you on implementing an Artificial Neural Network by using FANN Library developed by Steffen Nissen. It supports more than 20+ programming languages (http://leenissen.dk/fann/wp/language-bindings/) including Delphi and C++ Builder. You can reach full information and documentation here and you can download FANN source files from their web page ((http://leenissen.dk).
1: Download FANN Library package from Nissen’s official web page (http://leenissen.dk). This package includes binaries, cmake files, datasets, examples, source codes in the src folder, and some examples.
2: Create a new C++ Builder Application. Add a Panel (Panel1) that has 32 pixels in height and two Buttons (TEST, TRAIN) into it. Align Panel to the bottom of Form1. Add a Memo (Memo1) into the form and make it a client. This will be our visual output place
3: Create FANN_Test folder and save all project files with “FANN_” prefix into this folder as listed below;
4: Now we need source files to compile with our application. Unzip the package, copy “src” folder from this folder into this FANN_Test folder. Make your RADS/C++ Builder/Delphi IDE size smaller and drag the src folder from FANN_Test to Project Manager on the “FANN_Project1.exe”. When IDE asks “Would you like to add the selected files to the project …” confirm with Yes.
5: We need to include a folder of FANN for headers. From the IDE menu go to Project -> Options ->C++ Compiler->Directories and Conditionals->Include File Search path. Add here “src/include” folder which is in our FANN_Test folder
6: Now you can add a header into our FANN_Unit1.cpp. Below #include “FANN_Unit1.h” line add this #include “floatfann.h”.
If you do all steps now we implemented FANN library into our project with its original sources without any tweaks or changes on the original files. That means FANN source files and headers are very friendly because of their native C codes.
7: Now let’s test if all is fine. Build all projects from Projects -> Build All Projects menu. If Success that means all is fine. if there are errors please check the steps above.
Go to Form view and double click to TRAIN button and add below ,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
/* --- TRAINING FANN (from the simple_train.c) --- */ const unsigned int num_input = 2; const unsigned int num_output = 1; const unsigned int num_layers = 3; const unsigned int num_neurons_hidden = 3; const float desired_error = (const float) 0.001; const unsigned int max_epochs = 500000; const unsigned int epochs_between_reports = 1000; struct fann *ann = fann_create_standard(num_layers, num_input, num_neurons_hidden, num_output); fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC); fann_set_activation_function_output(ann, FANN_SIGMOID_SYMMETRIC); fann_train_on_file(ann, "xor.data", max_epochs, epochs_between_reports, desired_error); fann_save(ann, "xor_float.net"); fann_destroy(ann); Memo1->Lines->LoadFromFile("xor_float.net"); Memo1->Lines->Add("Training done on xor_float.net file as above"); |
If you want to compile for other compilers don’t forget to add include search path “src/include” to its project options as we do above. Please check examples, other commands, and options of FANN Library to understand its mechanism.
Double click to TEST button and add below;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/* ---- TESTING FANN (from the simple_test.c) ----*/ char s[255]; fann_type *calc_out; fann_type input[2]; struct fann *ann = fann_create_from_file("xor_float.net"); input[0] = -1; input[1] = 1; calc_out = fann_run(ann, input); Memo1->Lines->Add(""); Memo1->Lines->Add("Testing ... "); sprintf(s, "xor test (%f,%f) -> %f\n", input[0], input[1], calc_out[0]); Memo1->Lines->Add(s); Memo1->Lines->Add("Testing Done ! "); fann_destroy(ann); |
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition