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.
Really useful and straightforward. Thanks for sharing!
Just two notes, in case it can be useful to whoever reads this “howto”.
In case you get troubles installing this binding on a x86-64 system, you can get help in this link:
http://leenissen.dk/fann/forum/viewtopic.php?f=1&t=478&p=2899#p2899
And one question: wouldn’t be a better choice something like /usr/lib/ in order to place the libraries in order to be accessible from whatever dir we’re working on?
the best choice would be, instead of copying libfann.py file, to just write in your project:
from pyfann import libfann
and it works! 🙂
Thanks a lot for the tutorial, worked perfectly for me 🙂
It seems that there is a new version of FANN out there, namely 2.2.0, can I try to install it in the same manner? And if yes, do I have to uninstall FANN 2.1.0beta first, and how?
birdmw@birdmw-thinkpad:~/Desktop/final_project/fann-2.1.0/python$ sudo python setup.py install
Running SWIG before: swig -c++ -python pyfann/pyfann.i
running install
running build
running build_py
copying pyfann/libfann.py -> build/lib.linux-x86_64-2.7/pyfann
running build_ext
building ‘pyfann._libfann’ extension
x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -DSWIG_COMPILE -I../src/include -I/usr/include/python2.7 -c pyfann/pyfann_wrap.cxx -o build/temp.linux-x86_64-2.7/pyfann/pyfann_wrap.o
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++ [enabled by default]
c++ -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector –param=ssp-buffer-size=4 -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/pyfann/pyfann_wrap.o ../src/doublefann.o -o build/lib.linux-x86_64-2.7/pyfann/_libfann.so
/usr/bin/ld: ../src/doublefann.o: relocation R_X86_64_32S against `.rodata’ can not be used when making a shared object; recompile with -fPIC
../src/doublefann.o: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
error: command ‘c++’ failed with exit status 1
you have to install these packages to work properly
sudo apt-get install libfann2 libfann-dev
Matthew, I found exactly the same problems as yours. What I did, I add “-fPIC” to CPPFLAGS and LDFLAGS at the Makefile in the fann root directory and also the Makefile in the src directory. Then everything was fine.