function test(NeuralNetwork $neuralNetwork) { $trainingDatas = prepareTrainingDatas(); foreach (NUMBERS as $number) { ?> <div class="displays"><?php ?> <div class="display"><?php foreach ($number as $v) { ?> <div class="display-pixel" style="opacity: <?php echo ($v + 1) / 2; ?> "></div><?php } ?> </div><?php ?> <div class="display"><?php foreach ($neuralNetwork->calculate($number) as $v) { ?> <div class="display-pixel" style="opacity: <?php echo ($v + 1) / 2; ?> "></div><?php } ?> </div><?php ?> </div><?php } ?> <div class="graphs"> <div class="graphNetwork" data-data="<?php echo htmlspecialchars(json_encode($neuralNetwork->toArray()), ENT_QUOTES, 'UTF-8'); ?> "></div> </div> <script>draw()</script> <?php }
<?php require_once "../core/class_neuralnetwork.php"; // Create a new neural network with 3 input neurons, // 4 hidden neurons, and 1 output neuron $n = new NeuralNetwork(3, 4, 1); $n->setVerbose(false); // Add test-data to the network. In this case, // we want the network to learn the 'XOR'-function $n->addTestData(array(-1, -1, 1), array(-1)); $n->addTestData(array(-1, 1, 1), array(1)); $n->addTestData(array(1, -1, 1), array(1)); $n->addTestData(array(1, 1, 1), array(-1)); // we try training the network for at most $max times $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) { echo "Nothing found...<hr />"; } // print a message if the network was succesfully trained if ($success) { $epochs = $n->getEpoch(); echo "Success in {$epochs} training rounds!<hr />"; } // in any case, we print the output of the neural network for ($i = 0; $i < count($n->trainInputs); $i++) { $output = $n->calculate($n->trainInputs[$i]); print "<br />Testset {$i}; "; print "expected output = (" . implode(", ", $n->trainOutput[$i]) . ") "; print "output from neural network = (" . implode(", ", $output) . ")\n"; }
function test(NeuralNetwork $neuralNetwork, $withError = true) { $accuracy = []; $errors = []; foreach ($this->allTestData as $testData) { $result = $neuralNetwork->calculate($testData->getData()); $resultBool = 0; if ($result[0] < 0 && $result[1] > 0 && $testData->getExpectedResult() == [-1, 1]) { $resultBool = 1; } if ($result[0] > 0 && $result[1] < 0 && $testData->getExpectedResult() == [1, -1]) { $resultBool = 1; } $accuracy[] = $resultBool; $errors[] = $neuralNetwork->getError($result, $testData->getExpectedResult()); } echo '<div>Skuteczność <b>' . avg($accuracy) . '</b></div>'; echo '<div>Błąd <b>' . avg($errors) . '</b></div>'; // echo '<pre>'.print_r($neuralNetwork->toArray(), true).'</pre>'; ?> <div class="graphs"> <?php if ($withError) { ?> <div class="graphError" data-data="<?php echo htmlspecialchars(json_encode($neuralNetwork->getHistoricErrors())); ?> "></div> <?php } ?> <div class="graphNetwork" data-data="<?php echo htmlspecialchars(json_encode($neuralNetwork->toArray()), ENT_QUOTES, 'UTF-8'); ?> "></div> </div> <script>draw()</script> <?php flush(); ob_flush(); }
function test(NeuralNetwork $neuralNetwork) { ?> <div style="position: relative; width: 1000px; height: 100px; background: #eee; border-radius: 5px; margin: 20px 0px 20px;"><?php for ($i = 0; $i < 6.5; $i += 0.1) { $result = $neuralNetwork->calculate([$i])[0]; $left = $i * 100; $height = $result + 1; $height *= 50; ?> <div style="position: absolute; left: <?php echo $left; ?> px; width: 10px; bottom: 0px; height: <?php echo $height; ?> px; background: green; opacity: 0.5;"></div><?php } for ($i = 0; $i < 6.5; $i += 0.1) { $result = sin($i); $left = $i * 100; $height = $result + 1; $height *= 50; ?> <div style="position: absolute; left: <?php echo $left; ?> px; width: 10px; bottom: 0px; height: <?php echo $height; ?> px; background: red; opacity:0.5;"></div><?php } ?> </div><?php }
function test(NeuralNetwork $neuralNetwork) { $correctResult = []; for ($x = MIN; $x <= MAX; $x += STEP) { for ($y = MIN; $y <= MAX; $y += STEP) { $result = func($x, $y); if (is_infinite($result)) { continue; } $correctResult[] = [$x, $y, func($x, $y)]; } } $networkResult = []; for ($x = MIN; $x <= MAX; $x += STEP) { for ($y = MIN; $y <= MAX; $y += STEP) { $networkResult[] = [$x, $y, $neuralNetwork->calculate([$x, $y])[0]]; } } // echo '<pre>'.print_r($neuralNetwork->toArray(), true).'</pre>'; ?> <div class="graphs"> <div class="graph3d" data-data="<?php echo json_encode($correctResult); ?> "></div> <div class="graph3d" data-data="<?php echo json_encode($networkResult); ?> "></div> <div class="graphNetwork" data-data="<?php echo htmlspecialchars(json_encode($neuralNetwork->toArray()), ENT_QUOTES, 'UTF-8'); ?> "></div> </div> <script>draw()</script> <?php flush(); ob_flush(); }