
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 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.