Using FANN with Python

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.

Installing FANN with Python bindings on Ubuntu

fann

The Fast Artificial Neural Network Library (FANN) is a neural network library, which implements multilayer artificial neural networks in C with support for both fully connected and sparsely connected networks. It has a Python binding that allows you to use its functionality from within Python, but with the bits that need speed implemented in C.

We are going to install the FANN library on Ubuntu and install the Python binding. Get and unzip the library:

wget http://downloads.sourceforge.net/project/fann/fann/2.1.0beta/fann-2.1.0beta.zip
sudo apt-get install unzip
unzip fann-2.1.0beta.zip

Configure, make and install the library:

cd fann-2.1.0/
sudo apt-get install gcc make
./configure
make
sudo make install

Install the Python bindings:

cd python/
sudo apt-get install g++ python-dev swig
sudo python setup.py install

The Python files are now located in a build directory. Copy them to a place where you can use them, e.g. your home directory:

cd build/lib.linux-i686-2.6/pyfann/
cp libfann.py ~
cp _libfann.so ~

And finally test that Python can now work with the library, start up Python and type:

import libfann
print dir(libfann)

This should print out all the functions of the library.