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.
15 thoughts on “Using FANN with Python”
you should probably know that notation has changed, instead of importing the library like:
from pyfann import libfann
no, your form is not appreciated according to pep8.
sorry you are right
Thanks. Simple, straightfoward, and effective.
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?
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?
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.
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?
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
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?
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 !
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!
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!
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.