Open SourceCreating Neural Networks in PHP

# Creating Neural Networks in PHP

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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:

2. analyzes and processes it
3. 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)``

Where `Imin` and `Imax` represent the neural network range (-1 to 1), and `Dmin` 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.

## Training a Neural Network in PHP

Before being able to solve the problem, the artificial neural network has to learn how to solve it. Think of this network as an equation. You have added test data and the expected output, and the network has to solve the equation by finding the connection between input and output. This process is called training. In neural networks, these connections are neuron weights. A few algorithms are used for network training, but backpropagation is used most often. Backpropagation actually means backwards propagation of errors.

After initializing random weights in the network, the next steps are to:

1. Loop through the test data
2. Calculate the actual output
3. Calculate the error (desired output – current network output)
4. Compute the delta weights backwards
5. Update the weights

The process continues until all test data has been correctly classified or the algorithm has reached a stopping criterion. Usually, the programmer tries to teach the network for a maximum of three times, while the maximum number of training rounds (epochs) is 1000. Also, each learning algorithm needs an activation function. For backpropagation, the activation function is hyperbolic tangent (`tanh`)

Let’s see how to train a neural network in PHP:

``````<?php
\$max = 3;
// train the network in max 1000 epochs, with a max squared error of 0.01
while (!(\$success=\$n->train(1000, 0.01)) && \$max-->0) {
// training failed -- re-initialize the network weights
\$n->initWeights();
}

//training successful
if (\$success) {
\$epochs = \$n->getEpoch(); // get the number of epochs needed for training
}
?>``````

Mean squared error (mse) is the average of the squares of the errors, which is also known as standard deviation. The default mean squared error value usually is 0.01, which means that training is successful if the mean squared error is less than 0.01.

Before seeing the working example of an artificial neural network in PHP, it is good practice to save your neural network to a file or a SQL database. If you don’t save it, you will have to do the training every time someone executes your application. Simple tasks are learned quickly, but training takes much longer for more complex problems, and you want your users to wait as little as possible. Fortunately, there are save and load functions in the PHP class in this example:

``````<?php
\$n->save('my_network.ini');
?>``````

Note that the file extension must be .ini.

## The PHP Code for Our Neural Network

Let’s look at the PHP code of the working application that receives red, green and blue values and calculates whether the blue or red color is dominant:

``````<?php
require_once("class_neuralnetwork.php");
\$r = \$_POST['red'];
\$g = \$_POST['green'];
\$b = \$_POST['blue'];

\$n = new NeuralNetwork(4, 4, 1); //initialize the neural network
\$n->setVerbose(false);
\$n->load('my_network.ini'); // load the saved weights into the initialized neural network. This way you won't need to train the network each time the application has been executed

\$input = array(normalize(\$r),normalize(\$g),normalize(\$b));  //note that you will have to write a normalize function, depending on your needs

\$result = \$n->calculate(\$input);
If(\$result>0.5) {
// the dominant color is blue
}
else {
// the dominant color is red
}
?>``````

## Neural Network Limitations

The main limitation of neural networks is that they can solve only linearly separable problems and many problems are not linearly separable. So, non-linearly separable problems require another artificial intelligence algorithm. However, neural networks solve enough problems that require computer intelligence to earn an important place among artificial intelligence algorithms.

Subscribe to Developer Insider for top news, trends & analysis