Exemple #1
0
<?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";
}
Exemple #2
0
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 
}
Exemple #3
0
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 
}
 public function __construct($opts)
 {
     parent::__construct($opts);
 }
Exemple #5
0
    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();
    }
Exemple #6
0
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();
}
		<style type='text/css'>
			body { padding:0px; margin:0px; background: #e8e8e8; font-family: "Open Sans", Helvetica, Arial, sans; }
			.container { margin: 30px auto; padding:20px; max-width: 900px; background: #f8f8f8; border:2px solid #a8a8a8; border-radius: 10px; -moz-border-radius:10px; -webkit-border-radius:10px; -o-border-radius:10px;}
			.container > h1:first-child { margin-top:0px; }
			.result { padding:10px; background: #f0f0f0; margin: 0 auto; max-width:650px; }
			h1, h2 { padding:0px; margin:40px 0px 0px; }
			p { margin: 0px 0px 20px; }
		</style>
	</head>
	<body>
		<div class='container'>
		<?php 
require_once "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;
$i = 0;
echo "<h1>Learning the XOR function</h1>";
// train the network in max 1000 epochs, with a max squared error of 0.01
while (!($success = $n->train(1000, 0.01)) && ++$i < $max) {
    echo "Round {$i}: No success...<br />";
}