Using FANN with Python

In the previous article we showed how to install the FANN artificial neural network library on Ubuntu. In this article we will use the library.

There are typically two parts in using artificial neural networks:

  • A training part, where the neural network is trained with a training dataset. This dataset is chosen in such a way that it is representative of the real cases that it will see in the running part.
  • An execution part, where the neural network is executed on a real dataset. If the neural network was trained correctly, it will now be used to give answers to input it has seen in the training dataset, but also to input it has never seen.

The example we talk about is the well-known XOR operation. Let’s say that -1 represents a false value and 1 represents a true value, then the XOR operation will give the following output based on the given input:
We will start by creating a training dataset. FANN expects a dataset to be in a specific format. The first line of this file contains three numbers. The first number tells FANN how many samples it can expect. The second number tells it how many input values there are for one sample. The third number tells FANN how many outputs there are for one sample. The rest of the file contains the samples, where the inputs are placed on one line and the corresponding output is placed on the next line. Our training dataset then looks like this:

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

We save this file as Now we can write the training part:


import libfann

connection_rate = 1
learning_rate = 0.7
num_input = 2
num_hidden = 4
num_output = 1

desired_error = 0.0001
max_iterations = 100000
iterations_between_reports = 1000

ann = libfann.neural_net()
ann.create_sparse_array(connection_rate, (num_input, num_hidden, num_output))

ann.train_on_file("", max_iterations, iterations_between_reports, desired_error)"")

The neural network is saved in the file We use this file in the execution part:


import libfann

ann = libfann.neural_net()

print[1, -1])

The output of this run is 1, which is the correct answer.

13 thoughts on “Using FANN with Python

  1. Permalink  ⋅ Reply


    September 14, 2010 at 3:23pm

    Thank you.

  2. Permalink  ⋅ Reply

    garibaldi pineda garcía

    August 29, 2011 at 3:49am

    you should probably know that notation has changed, instead of importing the library like:

    import libfann

    you do

    from pyfann import libfann

    • Permalink  ⋅ Reply


      August 16, 2012 at 7:44pm

      no, your form is not appreciated according to pep8.

      • Permalink  ⋅ Reply


        August 17, 2012 at 10:12pm

        sorry you are right

  3. Permalink  ⋅ Reply


    January 24, 2012 at 4:11pm

    Thanks. Simple, straightfoward, and effective.

  4. Permalink  ⋅ Reply

    Jonathan Hayward

    June 13, 2012 at 11:10pm

    Is greyscale output an option? In other words, when a net has been trained on values from 0 to 1, will it output a binary 0 or 1, or a decimal that could show some gray in between 0 and 1 in the case of ambiguous input?

    If output is necessarily an integer, could one train a net on values scaled up to 0 and 100, and get numbers between 0 and 100 for more ambiguous input values?


  5. Permalink  ⋅ Reply


    November 5, 2012 at 4:28pm

    Hi, Im new to this topic, are neural networks that are created by this example deterministic? Im asking because on my PC I get a bad xor value and sometimes it doesnt work properly for some examples like the pair 1,1 the outcome is 1 and should be -1 as in the file. Could this be something with my system or is this something normal?

  6. Permalink  ⋅ Reply


    April 20, 2015 at 12:44pm

    Very insightful, pyfann is great but it could use some a bit more info like this. One thing i can’t seem to find out though, is it possible to train() an ann, capturing the output in python, and having the python program itself figure out how to report the result? Training the ann “manually” works, of course, but if i want to embed this functionality in a program of my own, that really won’t do.

  7. Permalink  ⋅ Reply


    August 17, 2015 at 6:13pm

    I have paste your file on my raspberry, when i try to use (the last file), i have cute error message : ” FANN Error 3: Wrong version of configuration file, aborting read of configuration file “”. segmentation fault ”

    Can you help me?

    • Permalink  ⋅ Reply


      August 21, 2015 at 10:33am

      I have resolve ” segmentation fault ” error i need to run my .py file as run. But … ” Wrong version of configuration file ” i don’t understand that

  8. Permalink  ⋅ Reply


    July 21, 2016 at 9:09am

    hi! what if my input is in list form, and a single nueron is to accept a list, is it possible? if yes, how should I create my input data?

  9. Permalink  ⋅ Reply


    November 23, 2016 at 8:39pm

    thanks for this example :p
    I adapted a bit with fann2 but really good example !!!
    Now let’s play a bit more deeper and start to make translation scripts of videos, text, whever in values between -1 and 1 :p will be funny ! A new world open in front of me !

  10. Permalink  ⋅ Reply


    October 12, 2017 at 8:16pm

    Doesn’t work. Code seems out of date – apparently now it is “FANN2”!

    Every time I try to get into something new I end up in a forest of outdated code and broken dependencies. This is why I never get further than basic hobby stuff with this – morons are in charge, just like in the real world. I’m surprised anything gets done at all. Clearly it is, but it would happen a lot faster if code didn’t go out of date every five minutes.

    Thank you for your efforts, anyway!

Leave a Reply to tty666 Cancel reply

Your email will not be published. Name and Email fields are required.