January 20, 2021
Hot Topics:

Training a Neural Network in PHP, Page 2

  • By Voja Janjic
  • Send Email »
  • More Articles »

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:

$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

//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:


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:

$r = $_POST['red'];
$g = $_POST['green'];
$b = $_POST['blue'];

$n = new NeuralNetwork(4, 4, 1); //initialize the neural network
$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.

Originally published on https://www.developer.com.

Page 2 of 2

This article was originally published on December 1, 2011

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date