<?php use Neural\BackpropagationTeacher; use Neural\MultilayerPerceptron; require_once '../vendor/autoload.php'; $p = new MultilayerPerceptron([4, 4, 5]); $p->generateSynapses(); $t = new BackpropagationTeacher($p); echo $t->teachKit([[0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], [0, 1, 0, 0], [1, 0, 0, 0], [1, 0, 0, 1], [0, 1, 1, 0], [1, 1, 0, 0], [0, 0, 1, 1], [1, 0, 1, 0], [0, 1, 0, 1], [0, 1, 1, 1], [1, 0, 1, 1], [1, 1, 0, 1], [1, 1, 1, 0], [1, 1, 1, 1]], [[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 1, 0, 0, 0], [0, 1, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 1, 0], [0, 0, 0, 1, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]], 0.25) . PHP_EOL; $roundElements = function (&$r) { $r = round($r); }; $test = [rand(0, 1), rand(0, 1), rand(0, 1), rand(0, 1)]; $result = $p->input($test)->output(); array_walk($result, $roundElements); echo 'Result for [' . implode(', ', $test) . ']:' . PHP_EOL; echo '[' . implode(', ', $result) . ']';
<?php use Neural\BackpropagationTeacher; use Neural\MultilayerPerceptron; require_once '../vendor/autoload.php'; $p = new MultilayerPerceptron([4, 8, 5]); $p->generateSynapses(); $t = new BackpropagationTeacher($p); $startTime = microtime(true); $epochs = $t->teachKit([[0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], [0, 1, 0, 0], [1, 0, 0, 0], [1, 0, 0, 1], [0, 1, 1, 0], [1, 1, 0, 0], [0, 0, 1, 1], [1, 0, 1, 0], [0, 1, 0, 1], [0, 1, 1, 1], [1, 0, 1, 1], [1, 1, 0, 1], [1, 1, 1, 0], [1, 1, 1, 1]], [[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 1, 0, 0, 0], [0, 1, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 1, 0], [0, 0, 0, 1, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]], 0.25) . PHP_EOL; $endTime = microtime(true); echo 'Memory peak usage: ' . round(memory_get_peak_usage() / 1024, 1) . PHP_EOL; echo 'Seconds per epoch: ' . round(($endTime - $startTime) / $epochs, 3);
<?php use Neural\BackpropagationTeacher; use Neural\MultilayerPerceptron; require_once '../vendor/autoload.php'; //Creation neural network, with 2 input-neurons, one hidden layer with 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 $p->trace(); $t = new BackpropagationTeacher($p); //Teacher with backpropagation algorithm //Teach until it learns $learningResult = $t->teachKit([[1, 0], [0, 1], [1, 1], [0, 0]], [[1], [1], [0], [0]], 0.3, 10000); 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; } $p->trace();