Click here to Skip to main content
15,881,898 members
Articles / Artificial Intelligence

Artificial Neural Networks made easy with the FANN library

Rate me:
Please Sign up or sign in to vote.
4.93/5 (46 votes)
28 Aug 2013CPOL24 min read 194.2K   10.6K   206  
Neural networks are typically associated with specialised applications, developed only by select groups of experts. This misconception has had a highly negative effect on its popularity. Hopefully, the FANN library will help fill this gap.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Getting Started</TITLE
><link href="../style.css" rel="stylesheet" type="text/css"><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Fast Artificial Neural Network Library"
HREF="index.html"><LINK
REL="UP"
TITLE="Introduction"
HREF="c13.html"><LINK
REL="PREVIOUS"
TITLE="Installation"
HREF="x26.html"><LINK
REL="NEXT"
TITLE="Getting Help"
HREF="x100.html"></HEAD
><BODY
CLASS="section"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Fast Artificial Neural Network Library</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="x26.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 1. Introduction</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="x100.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="intro.start"
>1.3. Getting Started</A
></H1
><P
>&#13;        An ANN is normally run in two different modes, a training mode and an execution mode. Although it is
        possible to do this in the same program, using different programs is recommended.
      </P
><P
>&#13;        There are several reasons to why it is usually a good idea to write the training and execution in two
	different programs, but the most obvious is the fact that a typical ANN system is only trained once, while it
	is executed many times.
      </P
><DIV
CLASS="section"
><H2
CLASS="section"
><A
NAME="intro.start.train"
>1.3.1. Training</A
></H2
><P
>&#13;	  The following is a simple program which trains an ANN with a data set and then saves the ANN to a file. 
	</P
><DIV
CLASS="example"
><A
NAME="example.simple_train"
></A
><P
><B
>Example 1-1. Simple training example</B
></P
><PRE
CLASS="programlisting"
>&#13;
#include "fann.h"

int main()
{
        const float connection_rate = 1;
        const float learning_rate = 0.7;
        const unsigned int num_input = 2;
        const unsigned int num_output = 1;
        const unsigned int num_layers = 3;
        const unsigned int num_neurons_hidden = 4;
        const float desired_error = 0.0001;
        const unsigned int max_iterations = 500000;
        const unsigned int iterations_between_reports = 1000;

        struct fann *ann = fann_create(connection_rate, learning_rate, num_layers,
                num_input, num_neurons_hidden, num_output);
        
        fann_train_on_file(ann, "xor.data", max_iterations,
                iterations_between_reports, desired_error);
        
        fann_save(ann, "xor_float.net");
        
        fann_destroy(ann);

        return 0;
}

          </PRE
></DIV
><P
>&#13;	  The file xor.data, used to train the xor function:
	  <PRE
CLASS="literallayout"
>&#13;4 2 1
0 0
0
0 1
1
1 0
1
1 1
0
	  </PRE
> The first line consists of three numbers: The first is the number of training pairs in the file, the second is the number of inputs and
	  the third is the number of outputs. The rest of the file is the actual training data, consisting of one line with inputs, one with outputs etc.
	</P
><P
>&#13;	  This example introduces several fundamental functions, namely <A
HREF="r258.html"
><CODE
CLASS="function"
>fann_create</CODE
></A
>,
	  <A
HREF="r806.html"
><CODE
CLASS="function"
>fann_train_on_file</CODE
></A
>,
	  <A
HREF="r474.html"
><CODE
CLASS="function"
>fann_save</CODE
></A
>, and <A
HREF="r361.html"
><CODE
CLASS="function"
>fann_destroy</CODE
></A
>.
	</P
></DIV
><DIV
CLASS="section"
><H2
CLASS="section"
><A
NAME="intro.start.execution"
>1.3.2. Execution</A
></H2
><P
>&#13;	  The following example shows a simple program which executes a single input on the ANN. The program introduces two new functions
	  (<A
HREF="r519.html"
><CODE
CLASS="function"
>fann_create_from_file</CODE
></A
> and
	  <A
HREF="r376.html"
><CODE
CLASS="function"
>fann_run</CODE
></A
>) which were not used in the training procedure, as well as the <SPAN
CLASS="type"
>fann_type</SPAN
>
	  type.
	</P
><DIV
CLASS="example"
><A
NAME="example.simple_exec"
></A
><P
><B
>Example 1-2. Simple execution example</B
></P
><PRE
CLASS="programlisting"
>&#13;
#include &#60;stdio.h&#62;
#include "floatfann.h"

int main()
{
        fann_type *calc_out;
        fann_type input[2];

        struct fann *ann = fann_create_from_file("xor_float.net");
        
        input[0] = 0;
        input[1] = 1;
        calc_out = fann_run(ann, input);

        printf("xor test (%f,%f) -&#62; %f\n",
                input[0], input[1], *calc_out);
        
        fann_destroy(ann);
        return 0;
}

          </PRE
></DIV
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="x26.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="x100.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Installation</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c13.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Getting Help</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Publisher
Poland Poland
Software Developer's Journal (formerly Software 2.0) is a magazine for professional programmers and developers publishing news from the software world and practical articles presenting very interesting ready programming solutions. To read more

Comments and Discussions