So it turns out that I am the worlds premiere neural network developer working with PHP as my language of choice… trust me that sounds way more glamorous than it actually is! ๐Ÿ˜›

I feel confident in saying this though because I literally wrote all the examples of neural networks in PHP.

I also added the bulk of the user contributed comments about the FANN extension over on PHP.net.

I’ve also written a bunch of PHP Neural Network Tutorials and more are on they way! ๐Ÿ™‚

Basically, I’ve worked very hard to establish my credibility so that when I speak on the subject I do so with a modicum of authority… hopefully. ๐Ÿ˜›

Anyway, I was contacted by a reader about my OCR Neural Network this past weekend, seems they were having some difficulty in getting FANN to work on their machine:

I have tried to run this project, but i am not able to resolve the error produce by fann library. please guide me with it how to resolve it. ~Abhijit Gutal

When in doubt ask for help!

I replied to Mr. Abhijit though I got no reply.

No worries though, I’ve been meaning to re-address the issue of setting up a FANN development environment since Amazon bought C9 so now seems like a good time to do an update!

What we’ll cover in today’s post:

  • How to download and install Virtualization Software for free.
  • I walk you through the basics of how to build a Virtual Machine and install Linux on it.
  • How to install Git, PHP, FANN & the PHP extension.
  • How to test everything is setup correctly.
  • What to do next.

All that in just one post!

So… Ready to get started creating your own neural networks using PHP?

Virtualization Software

Note: If you are using Linux you can skip this. We’re interested in Virtualization if we’re developing from a Windows or Mac. It is possible to install FANN on Windows using PECL and sometimes it works… but no bodyย  has had the time to sit down and figure out which versions of FANN have trouble on which versions of Windows in correlation to PHP version… So… ultimately if you are on a Mac or on Windows… just follow these instructions, if you are already on Linux… you can skip the virtualization step.

Essentially what Virtualization Software does is allow you to allocate some of your system resources to be used by a second operating system that runs “virtually” on your computer.

More simply, you can run a “simulated” or “emulated” computer, on your computer which results in a second (less powerful) virtual computer.

Some companies gain significant security benefits and reduced hardware downtime by employing “locked images” which are run virtually on server’s which are then remotely accessed via cheep “thin client” machines with a high fungibility… but that’s outside the scope of our discussion today. ๐Ÿ˜‰

In any case, you can use whatever virtualization solution you prefer however I recommend that hobbyists and developers who are just starting out download and Install VirtualBox (not a sponsor).

Download the version for the OS you are running.

After the download is complete, Run the Setup application:

The Mac install will be slightly different but it will have the same result.

All the default settings are fine… not sure why like 3 “Next >” buttons, a “Yes” and an “Install” button are required but I guess they want to make sure you really want to install it. ๐Ÿ˜›

Unshown are a few security confirmation screens where you are required to confirm “yes/install”.

If you navigate the Minotaur’s labyrinth properly the Oracle rewards you with a “blessed” install. ๐Ÿ˜›

Uncheck the “Start Application” checkbox at the end, we’ll come back to VirtualBox.

 

Linux

Until the issues with theย  PECL builds are worked out, Linux really is your best option for working with FANN and PHP right now.

There are many “flavors” of Linux however the one I am going to recommend today is Mint Linux (not a sponsor).

Why I choose Mint?

Mainly, because it’s safe to recommend. It’s very novice friendly and “gingerly” handles the more complicated “linuxy” stuff for you if you want it to.

Many Linux “purists” will argue that a “distro” like Mint is “dumbed down” or “too flashy”… or whatever other reason they feel umbrage is warranted.

I wont begrudge them their opinion… I would argue though that one of the best parts of Linux is it’s “extensibility”.

Anyone can “build their own OS” using Linux and so over the years people have created highly customized versions with specialized interfaces and tools to do everything from multimedia production to hacking!

So what’s wrong with building a “new user friendly” version of Linux?

Its still Linux and everyone has to start learning somewhere!

Giving somebody a tool that makes learning easier is kind thing to do!

In any case, Mint comes with everything you would expect from a modern “desktop environment” right out of the box requiring less initial setup and configuration from a “vanilla” install to become “productive”.

So, because Mint is relatively easy for non-technical users to get comfortable with fairly quickly, I tend to recommend it as a “first” Linux for people to try/use (even over its progenitor Ubuntu)… and since I recommend Mint, I also tend to use it simply because if/when people ask me those “how do I solve/fix/do …”ย  sorts of questions… it’s easier for me to reference the same screens they are looking at and possibly even reproduce the error.

The Mint UI (Cinnamon) sits pleasantly “between” the Windows and OS X use experiences in my opinion.

This facilitates “floating” between the three OS’s and feels quite natural to me.

With all that in mind, feel free to substitute your own flavor preference of Linux. ๐Ÿ˜›

Download Mint Linux and I recommend the 64 bit Cinnamon edition and that is what I will use in projects going forward.

Build a Virtual Machine

With our favorite Linux downloaded we’re ready to return to VirtualBox and setup our virtual machine.

Open VirtualBox by locating it in the Start Menu or by using the desktop icon.

Click New.

Next we need to Name the virtual machine as well as configure the OS Type & Version.

I called mine JoysANN-Dev (change to whatever you want). I left the Folder as the default location. I set the Type to Linux and set the Version to Other Linux(64-bit):

We next need to allocate some RAM for the virtual machine to use while running.

According the Mint Developers: Mint runs okay with 1GB but they recommend 2 GB for comfortable usage.

I don’t intend to do heavy graphic editing using the virtual machine at this time but we may do some visual ANN development eventually and that extra 1 GB might come in handy!

I have 8 GB available on the host computer so I think I can spare an extra gigabyte just in case so I configure the VM to use 2048 MB which is 2 GB.

Next we’re going to configure the virtual hard drive and allocate it some space.

Select Create a virtual hard disk now.

Select VDI (VirtualBox Disk Image).

Pick either Dynamically allocated or Fixed size.

I went with dynamic allocation because I have the space to spare and I don’t care to hassle with resizing partitions in the middle of a project because I allocated too little space.

If you are in a storage constrained environment select Fixed size.

According the Mint Developers: Mint requires 15GB of disk space (20GB recommended).

Since I selected dynamic allocation I’m not worried about accounting for every file my project will need to store but I set the initial size to 15 GB because Mint will refuse to install with less even if the Virtual Machine is configured to allocate space as needed. ๐Ÿ˜‰

I left the File Location as the auto populated default.

Once you click Create your new Virtual Machine will be created and then you will return to the VirtualBox default view.

Your VM will be in the list on the left. With it selected, click the Settings button… (you could also just click the [Optical Drive] Empty field under storage but I want to at least show the settings menu)

Click Storage on the leftย  then select the Empty virtual optical drive. Click the Disc Load button.

A menu will appear.

Notice that I’ve previously created a VM using the Mint ISO file so it remembers it so you can use it again.

For the first time though you will need to select: Choose Virtual Optical Disk File…

Then navigate to where you downloaded the Mint .iso file. On windows this is probably where it was downloaded (copy & paste):

C:\Users\%Username%\Downloads\linuxmint-19.1-cinnamon-64bit.iso

Click Open.

We can leave everything else as default for now click OK to exit Settings.

 

Install Mint

From the default screen in VirtualBox, click Start.

Just wait until you get this screen. Run the Install Linux Mint application.

Select a language you understand.

Do the same with the keyboard.

 

Skip installing 3rd party apps at this time , click Continue.

 

This next screen may look scary when it says Erase disk and install Linux Mint followed by a warning.

However this only refers to the Virtual hard drive for the VM, not your whole drive… though if you are not using virtualization software and instead burned the .iso to a disc (or bootable thumbdrive) and are actually installing this on your computer or are simply not following these instructions… well… that’s not my fault! ๐Ÿ˜›

Click Install Now

Click Continue when it asks you to confirm.

 

Set your location so the virtual computer’s chronometer accurately reflects the time and then click Continue.

 

Next we need to setup administrative login credentials.

 

Once you proceed from the credentials screen you should see this screen.

 

Congratulations!ย  You are installing Mint Linux on your first Virtual Machine. ๐Ÿ™‚

 

Now just wait for the instillation to finish and then select Restart Now.

Once you see this screen, wait like 5 seconds and Press Enter.

VirtualBox will “auto eject” the .iso disc from the virtual optical drive we added in the settings screen.

The virtual machine will “boot” and you should see a sign in screen… you do remember the password you entered right? ๐Ÿ˜›

 

Installing Our Software

 

Close the welcome dialog and click here to open a terminal.

Now install PHP by typing this command:

sudo apt install php7.2-dev

Linux will ask you for your password, type it and press enter.

Created with GIMP

It will think for a second, then ask if you want to install Do you want to continue ? [Y/n]

Answer y and press enter.

Created with GIMP

 

Now install Git by typing this command:

sudo apt install git
Created with GIMP

Linux should still remember your password and not require you to re-authenticate. Instead it will move right to asking Do you want to continue ? [Y/n]

Answer y and press enter.

Created with GIMP

 

Now install cmake by typing this command:

sudo apt install cmake

Linux should still remember your password and not require you to re-authenticate. Instead it will move right to asking Do you want to continue ? [Y/n]

Answer y and press enter.

Created with GIMP

 

Now Install FANN by typing this command:

sudo apt-get install libfann-dev

Linux should still remember your password and not require you to re-authenticate. Instead it will move right to asking Do you want to continue ? [Y/n]

Answer y and press enter.

Next we need to recursively clone the github repo:

git clone --recursive https://github.com/bukka/php-fann.git

 

Then move into the cloned php-fann directory and compile the extension.

cd php-fann
phpize

 

Next type:

./configure --with-fann

 

Now type:

make

 

Now type:

sudo make install

 

We’ve now installed everything but we still need to let PHP know it’s there.

First we need to find the location of the ‘Loaded Configuration File’ so type:

php -i | grep 'Loaded Configuration File'

Whatever the path returned is the location you are looking for.

 

Open that file with nano for editing by typing:

sudo nano /etc/php7.2/cli/php.ini

Page Down ~56 times to reach the end of the extensions section and add this line:

extension=fann.so

Press Ctrl+X then type Y to confirm save and exit, press enter without changing the name.

We’ve installed everything. Reboot your virtual machine.

 

Testing FANN

(AFTER REBOOTING)

Open a terminal and change directories:

cd php-fann/examples/logic_gates

 

Now we can train an ANN to do the XOR operation using simple_train.php and test it with simple_test.php

php simple_train.php
php simple_test.php

Your output should look similar to the image above. If you don’t get any errors then FANN and PHP are working perfectly!

 

What to do Now?

Try running the other examples and review the some of the code.

You can also review these posts which cover the examples:

Getting Started with Neural Networks Series

Pathfinding From Scratch Using A Neural Network Series

Lets Teach AI How To Read Using PHP Series (Optical Character Recognition)

Machine Learning from a Database Series

 

With that, I’m going to end this epically long post by saying I hope this helps!

If you appreciate guides like this and would like to say thanks, please support me over on Patreon for as little as $1 a month, thank you!

But, if all you can do is like, share, comment and subscribeโ€ฆ well, thatโ€™s cool too! ๐Ÿ˜‰

Much Love,

~Joy