neural-network
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:
xor
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
1
1 1
-1

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

#!/usr/bin/python

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.set_learning_rate(learning_rate)
ann.set_activation_function_output(libfann.SIGMOID_SYMMETRIC_STEPWISE)

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

ann.save("xor.net")

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

#!/usr/bin/python

import libfann

ann = libfann.neural_net()
ann.create_from_file("xor.net")

print ann.run([1, -1])

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

Using FANN with Python
Tagged on:         

15 thoughts on “Using FANN with Python

  • 2011-08-29 at 03:49
    Permalink

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

    import libfann

    you do

    from pyfann import libfann

    Reply
    • 2012-08-16 at 19:44
      Permalink

      no, your form is not appreciated according to pep8.

      Reply
  • 2012-01-24 at 16:11
    Permalink

    Thanks. Simple, straightfoward, and effective.

    Reply
  • 2012-06-13 at 23:10
    Permalink

    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?

    Thanks,

    Reply
  • 2012-11-05 at 16:28
    Permalink

    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?

    Reply
  • 2015-04-20 at 12:44
    Permalink

    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.

    Reply
  • 2015-08-17 at 18:13
    Permalink

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

    Can you help me?

    Reply
    • 2015-08-21 at 10:33
      Permalink

      Hi,
      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

      Reply
  • 2016-07-21 at 09:09
    Permalink

    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?

    Reply
  • 2016-11-23 at 20:39
    Permalink

    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 !

    Reply
  • 2017-10-12 at 20:16
    Permalink

    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!

    Reply
  • 2018-10-24 at 00:27
    Permalink

    How to install this using pip? I tried:
    pip install fann2

    And got message in Python 2.7:
    Exception: Couldn’t find FANN source libs!

    Reply
  • 2020-06-15 at 16:21
    Permalink

    It works on opensuse 15.0. Installed libfann2 using yast and made small modification to the given code. Instead “import libfann” I wrote “from fann2 import libfann”. If you check your library (pip2 list), it is installed as fann2 1.1.2 in my case.

    Reply

Leave a Reply

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