# Training a Neural Network in PHP, Page 2

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

- Loop through the test data
- Calculate the actual output
- Calculate the error (desired output - current network output)
- Compute the delta weights backwards
- Update the weights

`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.

Page 2 of 2