Fast Artificial Neural Network Library

Getting Started

For general help information about FANN, please go the main FANN Help page.

Russian translation available here.

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.

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.


The following is a simple program which trains an ANN with a data set and then saves the ANN to a file.

Simple training example

#include "fann.h"

int main()
    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, "", max_epochs,
        epochs_between_reports, desired_error);

    fann_save(ann, "");


    return 0;

The file, used to train the xor function

4 2 1
-1 -1
-1 1
1 -1
1 1

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.

This example introduces several fundamental functions, namely fann_create_standard, fann_train_on_file, fann_save, and fann_destroy.


The following example shows a simple program which executes a single input on the ANN. The program introduces two new functions (fann_create_from_file and fann_run) which were not used in the training procedure, as well as the fann_type type.

Simple execution example

#include <stdio.h>
#include "floatfann.h"

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

    struct fann *ann = fann_create_from_file("");

    input[0] = -1;
    input[1] = 1;
    calc_out = fann_run(ann, input);

    printf("xor test (%f,%f) -> %f\n", input[0], input[1], calc_out[0]);

    return 0;

Getting Help

If after reading the documentation you are still having problems, or have a question that is not covered in the documentation, please consult the FANN forum.


  1. Ahmed Ahmed
    April 7, 2012    

    Well done.
    Simple and neat :)

  2. Tar ina Tar ina
    January 11, 2013    

    I would like use fann code for input/output regression
    using incremental standard backpropagation
    I use 1 and 3 inputs and 1 output (target)
    I want help about incremental neural network backpropagation

  3. Xtrim-A Xtrim-A
    March 25, 2013    

    Cool new look!!!
    I would like to see more examples (different ways of implementation) in documentation page, most possibly by the FANN users who are using it for various applications and using different languages such as c++ and python. Thant will help newbies like me to quickly understand how to use/code FANN more efficiently.

  4. HoBot HoBot
    September 16, 2013    

    Translations of this article in Russian contain gross errors. Please remove the link to it! Better understand the right article poorly than well understood wrong.

  5. john john
    November 11, 2013    

    can you please explain how to build the program under ubuntu?


    • john john
      November 12, 2013    

      After copying all the fann libraries from /user/lib to /usr/local/lib my linking issues went away!

  6. baraut baraut
    November 23, 2013    

    If you don’t have any linking issue you should be able build it just like any other program. Are you getting any FANN specific errors while building it?

  7. June 26, 2014    

    When i try to compile this program on ubuntu i get the following error :

    main.c:(.text+0x6e): undefined reference to `fann_create’
    main.c:(.text+0x90): undefined reference to `fann_train_on_file’
    main.c:(.text+0xa1): undefined reference to `fann_save’
    main.c:(.text+0xad): undefined reference to `fann_destroy’
    collect2: ld returned 1 exit status

    I’m guessing this is because while compiling you need to link with the FANN libraries, could you please tell how that is done, or point me to a source which tells that?


    • Ichigo-Roku Ichigo-Roku
      October 16, 2014    

      You have to link the library with -lfann

    • David David
      December 3, 2014    

      Hey! you can compile this program on ubuntu, running the command:

      “gcc main.c -lfann -lm -o main”

      • sainarai sainarai
        July 30, 2015    

        “gcc main.c -lfann -lm -o main”
        Thank you!

  8. October 20, 2014    

    The results are just wrong:

    Max epochs 500000. Desired error: 0.0010000000.
    Epochs 1. Current error: 0.2501462102. Bit fail 4.
    Epochs 23. Current error: 0.0009238253. Bit fail 0.

    xor test (-1.000000,1.000000) -> 80547008.000000

    this is the generated file:

    why ?

    • June 6, 2015    

      They use a polar notation of the boolean inputs; i.e. 1 -> 1, 0 -> -1. See the input file
      However the output line,
      xor test(-1.0000,1.000) -> HUGE_NUMBER seems like a mystery to me as well.

    • Chinmay Jain Chinmay Jain
      June 30, 2015    

      I am getting the same error. Did you find a solution to it?

  9. Alan Alan
    January 16, 2015    

    Can we built cascade forward neural networks using FANN library?

  10. Shreyas Karnic Shreyas Karnic
    September 3, 2015    

    Can anyone help me with the code or flow as to how the fannj library should be used to run FANN in java, starting from processing the data till running the file. Im totally new to java and finding it difficult to use the classes described under fannj.

No Pings Yet

  1. Artificial Neural Networks on July 11, 2012 at 2:24 pm

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Comments