Using FANN and its Python bindings to construct neural networks

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 os
import sys
sys.path.insert(0, os.path.normpath(os.path.join(sys.path[0], '../pyfann')))
import libfann as pyfann

connection_rate = 1
learning_rate = 0.7
num_input = 2
num_neurons_hidden = 4
num_output = 1

desired_error = 0.0001
max_iterations = 100000
iterations_between_reports = 1000

ann = 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!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值