1. Go to this page and download the library: Download a1essandro/neural-network library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
a1essandro / neural-network example snippets
use Neural\BackpropagationTeacher;
use Neural\MultilayerPerceptron;
ith 2 neurons and one output neuron:
$p = new MultilayerPerceptron([2, 2, 1]); //You may add more hidden layers or neurons to layers: [2, 3, 2, 1]
$p->generateSynapses(); //automatically add synapses
$t = new BackpropagationTeacher($p); //Teacher with backpropagation algorithm
//Teach until it learns
$learningResult = $t->teachKit(
[[1, 0], [0, 1], [1, 1], [0, 0]], //kit for learning
[[1], [1], [0], [0]], //appropriate expectations
0.3, //error
10000 //max iterations
);
if ($learningResult != -1) {
echo '1,0: ' . round($p->input([1, 0])->output()[0]) . PHP_EOL;
echo '0,1: ' . round($p->input([0, 1])->output()[0]) . PHP_EOL;
echo '0,0: ' . round($p->input([0, 0])->output()[0]) . PHP_EOL;
echo '1,1: ' . round($p->input([1, 1])->output()[0]) . PHP_EOL;
}
/* Result:
1,0: 1
0,1: 1
0,0: 0
1,1: 0
*/
$p = new MultilayerPerceptron([2, 2, 1]);
//Equivalent to:
$p = new MultilayerPerceptron();
$p->addLayer(new Layer())->toLastLayer()
->addNode(new Input())
->addNode(new Input())
->addNode(new Bias());
$p->addLayer(new Layer())->toLastLayer()
->addNode(new Neuron())
->addNode(new Neuron())
->addNode(new Bias());
$p->addLayer(new Layer())->toLastLayer()
->addNode(new Neuron());
//Do not forget to add synapses:
$p->generateSynapses();
//Or you may direct the process:
$neuronFilter = function($node) {
return $node instanceof Neuron;
};
$secondLayerNeuron = iterator_to_array($p->getLayers()[1]->getNodes($neuronFilter))[0];
$input = iterator_to_array($p->getLayers()[0]->getNodes())[0];
$secondLayerNeuron->addSynapse(new Synapse($input));
//and so on...
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.