Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将XOR神经网络修改为其他类型的神经网络_Php_Html_Machine Learning_Neural Network - Fatal编程技术网

Php 将XOR神经网络修改为其他类型的神经网络

Php 将XOR神经网络修改为其他类型的神经网络,php,html,machine-learning,neural-network,Php,Html,Machine Learning,Neural Network,我下载了一个用PHP编写的神经网络程序。它是用于异或门的。我想把它修改成一个适用于其他数据集的程序。是否可以只更改训练数据集 我改变了这部分。我使用数据集(1,2,1)-(3)、(1,4,1)-(5)、(2,2,1)-(4)、(2,4,1)-6代替XOR门的训练数据集 代码以前是有效的,但仅仅通过更改训练数据集,它对我不起作用。为每个数据集生成的输出为1。你能帮帮我吗 <?php require_once ("class_neuralnetwork.php"); // C

我下载了一个用PHP编写的神经网络程序。它是用于异或门的。我想把它修改成一个适用于其他数据集的程序。是否可以只更改训练数据集

我改变了这部分。我使用数据集(1,2,1)-(3)、(1,4,1)-(5)、(2,2,1)-(4)、(2,4,1)-6代替XOR门的训练数据集

代码以前是有效的,但仅仅通过更改训练数据集,它对我不起作用。为每个数据集生成的输出为1。你能帮帮我吗

        <?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;

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 />";
}

// print a message if the network was succesfully trained
if ($success) {
    $epochs = $n->getEpoch();
    echo "Success in $epochs training rounds!<br />";
}

echo "<h2>Result</h2>";
echo "<div class='result'>";
// 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]);
    echo "<div>Testset $i; ";
    echo "expected output = (".implode(", ", $n->trainOutput[$i]).") ";
    echo "output from neural network = (".implode(", ", $output).")\n</div>";
}
echo "</div>";
//echo "<h2>Internal network state</h2>";
//$n->showWeights($force=true);

// Now, play around with some of the network's parameters a bit, to see how it 
// influences the result
$learningRates = array(0.1, 0.25, 0.5, 0.75, 1);
$momentum = array(0.2, 0.4, 0.6, 0.8, 1);
$rounds = array(100, 500, 1000, 2000);
$errors = array(0.1, 0.05, 0.01, 0.001);

echo "<h1>Playing around...</h1>";
echo "<p>The following is to show how changing the momentum & learning rate, 
in combination with the number of rounds and the maximum allowable error, can 
lead to wildly differing results. To obtain the best results for your 
situation, play around with these numbers until you find the one that works
best for you.</p>";
echo "<p>The values displayed here are chosen randomly, so you can reload 
the page to see another set of values...</p>";

for ($j=0; $j<10; $j++) {
    // no time-outs
    set_time_limit(0);

    $lr = $learningRates[array_rand($learningRates)];
    $m = $momentum[array_rand($momentum)];
    $r = $rounds[array_rand($rounds)];
    $e = $errors[array_rand($errors)];
    echo "<h2>Learning rate $lr, momentum $m @ ($r rounds, max sq. error $e)</h2>";
    $n->clear();
    $n->setLearningRate($lr);
    $n->setMomentum($m);
    $i = 0;
    while (!($success = $n->train($r, $e)) && ++$i<$max) {
        echo "Round $i: No success...<br />";
        flush();
    }

    // print a message if the network was succesfully trained
    if ($success) {
        $epochs = $n->getEpoch();
        echo "Success in $epochs training rounds!<br />";

        echo "<div class='result'>";
        for ($i = 0; $i < count($n->trainInputs); $i ++) {
            $output = $n->calculate($n->trainInputs[$i]);
            echo "<div>Testset $i; ";
            echo "expected output = (".implode(", ", $n->trainOutput[$i]).") ";
            echo "output from neural network = (".implode(", ", $output).")\n</div>";
        }
        echo "</div>";
    }
}
?>
        </div>
    </body>
</html>

如果没有看到神经网络实现的代码,就很难给出明确的答案。但是看起来这个实现可能使用了一个
tanh
激活函数,它将神经元输出限制在范围(-1,1)内。此外,该实现似乎没有使用隐式偏差输入,这就是为什么XOR函数的示例使用第三个输入,该输入在所有情况下都显式设置为1

所以基本的问题是,所有的目标输出都超出了激活函数的范围。你需要重新构建你的人际网络,但你如何做到这一点将取决于你正在努力实现的目标。从你的问题来看,不清楚你是在训练分类器还是在插值函数

如果四个不同的输出(3、5、4和6)表示类,则应定义具有四个输出的网络,并定义所需的输出值,如下所示:

$n = new NeuralNetwork(3, 4, 4);

$n->addTestData(array (1, 2, 1), array ( 1, -1, -1, -1));
$n->addTestData(array (1, 4, 1), array (-1,  1, -1, -1));
$n->addTestData(array (2, 2, 1), array (-1, -1,  1, -1));
$n->addTestData(array (2, 4, 1), array (-1, -1, -1,  1));
请注意,您的示例可能需要4个以上的隐藏节点


如果您尝试进行函数插值,那么您将只保留单个输出神经元,但需要将目标输出值调整到
tanh
函数的范围内。

谢谢您的回答。以防您想知道代码在哪里