Creating Neural Networks in PHP
An artificial neural network (or ANN) is an algorithm used in artificial intelligence to simulate human thinking. The network works similarly to the human brain: it is comprised of neurons that communicate with each other and provide valuable outputs. Although just a model -- and not even close to human thinking -- artificial neural networks have been used in prediction, classification, and decision-support systems, as well as in optical character recognition and many other applications.
Artificial neural networks are developed mostly in high-level programming languages such as C or C++, but you can implement neural networks in PHP as well, which is perhaps the most convenient way of using artificial intelligence in Web applications. In this article, I will explain how to set up one of the most common neural network topologies, multi-layer perception, and create your first neural network in PHP using a PHP neural network class.
Similar to the human thought process, a neural network:
- receives some input (your data)
- analyzes and processes it
- provides an output value (i.e. the result of the calculation)
That's why the topology in this example (multi-layer perception) has three layers:
- Input layer
- hidden layer
- output layer
Each layer has a certain number of neurons, depending on your needs. Every neuron is connected to all neurons in the next layer. The neurons process the given task by adjusting output (i.e. weight coefficients between them). Of course, before they can be applied to a practical use case, neural networks have to learn the task. But, before everything, you have to prepare your data for the network.
Neural Network Input in PHP -- Preparing Data
Because neural networks are complex mathematical models, you can't send just any data type to input neurons. The data must be normalized before the network can used it. This means that the data should be scaled to the range of -1 to 1. Unfortunately, there is no normalization function in PHP, so you will have to do it yourself, but I'll give you the formula:
I = Imin + (Imax-Imin)*(D-Dmin)/(Dmax-Dmin)
Imax represent the neural network range (-1 to 1), and
Dmax are data minimum and maximum value.
After normalizing the data, you have to choose the number of input neurons. For example, if you have RGB colors and you want to determine if red or blue is a dominant color, you would have four input neurons (three neurons for holding red, green and blue values, and the fourth is bias -- usually equaling 1). Here is the PHP code for this calculation:
<?php require_once("class_neuralnetwork.php"); $n = new NeuralNetwork(4, 4, 1); // the number of neurons in each layer of the network -- 4 input, 4 hidden and 1 output neurons $n->setVerbose(false); // do not display error messages //test data // First array is input data, and the second is the expected output value (1 means blue and 0 means red) $n->addTestData( array (0, 0, 255, 1), array (1)); $n->addTestData( array (0, 0, 192, 1), array (1)); $n->addTestData( array (208, 0, 49, 1), array (0)); $n->addTestData( array ( 228, 105, 116, 1), array (0)); $n->addTestData( array (128, 80, 255, 1), array (1)); $n->addTestData( array ( 248, 80, 68, 1), array (0)); ?>
There is only one output neuron because you have only two possible results. For more complex problems, you can use more than one neuron as the network output, thus having many combinations of 0s and 1s as possible outputs.
Originally published on http://www.developer.com.
Page 1 of 2