Using FANN and its Python bindings to construct neural networks

来源:互联网 发布:吊杆控制台编程 编辑:程序博客网 时间:2024/05/22 14:01

FANN stands for Fast Artificial Neural Network, which is a neural network library, originally intended only for C. However, due to the boost of the terrific script language – Python, the demand to use a FANN Python binding arises, which makes a perfect combination of the speed of C and elegance of Python.

The FANN Python Binding — pyfann, seems a bit out of date. The conventional way “python setup.py install” cannot do the right thing with VS 2010 or later. So after experienced a lot of difficulties during the configuration of pyfann, I decided to record all these procedures down to help other guys who also suffers from this pain..

Below is my method to build pyfann on Windows 7, which is independent of “setup.py”.

=============================================
Step 1 – Download FANN 2.1.0, swig 2.0.8, Python 3.2, VS 2010
=============================================

Well, you know how to do it.

============================
Step 2 – Adapting FANN for Python 3.x
============================

Up to today (Jun 23, 2013), FANN is still not compatible with Python 3.x. However, this issue can be settled by the lovely tool 2to3.py. So you should type the following command in the cmd window and enter (you know what ‘<>’ means).

cd <Python-installation-path>/Tools/Scripts
python 2to3.py -wWno <FANN-installation-path>/python3 <FANN-installation-path>/python

And also remember to copy the non-python files to the newly created python3 folder.

============================
Step 3 – Parse the Swig interface file “pyfann.i”
============================

This is done by

cd <FANN-installation-path>/python3/pyfann
swig -c++ -python pyfann.i

This will generate a file named “pyfann_wrap.cxx”.

============================
Step 4 – Build _libfann.pyd in VS 2010
============================

This is done by
(1) Create a new project, use the AppWizard to select an empty DLL project.
(2) Add pyfann_wrap.cxx, doublefann.c to the source folder of the project, add doublefann.h to the header folder of the project.
(3) Switch to RELEASE configuration.
(4) Select the settings for the entire project and go to “C++:Preprocessor”. Add the Python include directories under “Additional include directories”.
(5) Define the symbol __WIN32__ and FANN_DLL_EXPORTS under preprocessor options.
(6) Finally, select the settings for the entire project and go to “Link Options”. Add Python library file to your link libraries. For example “python32.lib”. Also, set the name of the output file to match the name of your Python module, ie. _libfann.pyd.
(7) Build your project.

===========================================
Step 5 – Have a try wth the generated FANN Python binding
===========================================

Modify the “simple_train.py” under <FANN-installation-path>/python3/examples. And then run.

import osimport syssys.path.insert(0, os.path.normpath(os.path.join(sys.path[0], '../pyfann')))import libfann as pyfannconnection_rate = 1learning_rate = 0.7num_input = 2num_neurons_hidden = 4num_output = 1desired_error = 0.0001max_iterations = 100000iterations_between_reports = 1000ann = pyfann.neural_net()ann.create_sparse_array(connection_rate, (num_input, num_neurons_hidden, num_output))ann.set_learning_rate(learning_rate)ann.set_activation_function_output(pyfann.SIGMOID_SYMMETRIC_STEPWISE)ann.train_on_file("../../examples/xor.data", max_iterations, iterations_between_reports, desired_error)

Enjoy!