According to Wikipedia an Artificial Neural Network (ANN) is defined as follows:
“In machine learning and cognitive science, artificial neural networks (ANNs) are a family of models inspired by biological neural networks (the central nervous systems of animals, in particular the brain) which are used to estimate or approximate functions that can depend on a large number of inputs…” – Wikipedia (Artificial Neural Networks)
Sounds cool right? Who wouldn’t want their own AI, even if it’s still not exactly the simplest thing to understand? I highly recommend at least reading through the ANN Wiki so that this article makes more sense but it’s not required to be able to follow along.
There are tons of free online classes and tutorials surrounding this topic as well as paid courses online in addition to colleges and universities. Books abound on this subject as well, just checkout Amazon!
But, where to start?
Well today I am going to pull back the curtain to show you how to actually work with ANN’s using the FANN library. FANN (Fast Artificial Neural Network) Library has been around for about 13 years and is still in development by it’s creator Steffen Nissen who works in collaboration with other programmers to maintain the the library bindings for the following languages:
The PHP FANN bindings are currently maintained by Jakub Zelenka.
You can use FANN with your language of choice but today I am going to show you how to use the library with PHP. Why PHP? Because I like PHP! 😉
I may demonstrate it’s use with Node.js, Python & C++ or C# at some point as well but today we’ll be using PHP.
Don’t worry about trying to picking a fast compiled language, just work with one you know well. FANN is written and compiled using C++ and so will operate at the machine code level, you just issue calls to it.
Additionally, FANN is released under the LGPL license so if you follow its terms you can use it commercially as well!
You can review the FANN Library on its official site: http://leenissen.dk/fann/wp/
Or the PHP version on PHP.net: http://php.net/manual/en/book.fann.php
To get started you will need a web host to host your code. The host must offer SSH terminal access and preferably the OS should be in the Debian family of Linux (Debian, Ubuntu, Mint). You can Use other Linuxes, or even Windows or Mac OS, but the instructions are different than I outline here.
My recommendation is that you create a free account with C9.io and these instructions assume that you go that route. It’s online and you can work with some of the most popular languages used today in a very functional web based IDE.
So, without further ado…
Step 1: Create an account on C9.io
Step 2: Check the email account you signed up with for an email with a link that will allow you to set your account password.
Step 3: Once your account has been created you will land on the ‘Workspaces’ view:
Step 4: Now we can create a new workspace for our Neural Network project. Click the Plus (Create new workspace).
Enter a Workspace Name and a Description.
Note: the Workspace Name needs to be lowercase and include no special chars other than an underscore.
Next select PHP as the project “template” which basically lets C9.io know what language or platform to setup for you. Then just click the Create Workspace button at the bottom of the form.
Step 5: When everything is setup, you will see this:
The bash tab at the bottom lets you issue commands to the server’s terminal. By default you will have access to Git as well as compilation tools, etc. which we will be using but I will not go into details about them in this post as I will do so in later posts as well as I am providing exact code to copy and paste if you need it so don’t worry too much about what the following does in detail if you don’t understand it. I will explain generally what each of the following commands do as we go along.
First click in the bash prompt and type the following to make a copy of the latest version of FANN so you can work with it.
git clone https://github.com/libfann/fann.git
Once that finishes you will see a new folder named fann show up in the file view on the left.
Step 6: Go back to the bash command line and change the working folder to fann:
Then you need to build the make files so you can compile the library, type the following into the bash prompt:
This shouldn’t take long.
Step 7: Once the make files are all… made 😛 you can compile everything, type the following into the bash prompt:
sudo make install
Now sit back and wait… might take a minute.
You’re doing so well!
Step 8: Okay, so now that we have the FANN Library compiled we need to leave the fann folder and go back up to the root directory of our workspace, enter the following into the bash prompt:
Now that we are at the top level of our workspace we need to download a copy of the FANN bindings for PHP, type the following into the bash prompt:
git clone --recursive https://github.com/bukka/php-fann.git
Which won’t take long. Now we want to move into that folder, type the following into the bash prompt:
Step 9: Great, now we need to to compile the extension, enter the following one at a time into the bash prompt:
sudo make install
Step 10: Only one final thing to do before we are done with the configuration.
Locate the php.ini file in the root of the folder structure on the left and open it.
Scroll down to about line 937 under the section Dynamic Extensions. Add the following:
Now save and close php.ini.
Test FANN You have completed cloning the FANN library into your workspace, making and compiling it, cloning the PHP bindings into your workspace, making and compiling it and adding it to the local php.ini file.
You can now use the FANN library to make your own neural networks!
So lets test it and make sure it’s working! Click the Green Run Project button on menu on the top of the page.
You will get a windowed browser in the IDE, click the “Popout” button so you get a nice full-size tab to preview everything in:
Now just navigate to: /php-fann/examples/logic_gates by clicking on the folders.
Once there, first run the simple_train.php to train the test ANN, the page will load and say ‘xor_float.net saved’ followed by a link to ‘Test’ the Neural Network. This step uses the .data file (which is the training file for the ANN) and trains a new ANN and saves the neural network as a .net file.
click the ‘Test’ link to run simple_test.php, if you get output like this then it’s working:
xor test (-1.000000,1.000000) -> 0.964151
In my next post I will break down what each file is actually doing and then I will show you how to create your own Artificial Neural Networks using FANN.
Until then, please leave a comment below and let me know what you like or don’t like about FANN and if you had any trouble with this tutorial.