gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:56:39 27 4
gpt4 key购买 nike

我下载了一个用 PHP 编写的神经网络程序。它用于异或门。我想将其修改为适用于其他数据集的程序。是否可以通过更改训练数据集来实现?

我改变了这部分。我使用了数据集(1,2,1)-(3),(1,4,1)-(5),(2,2,1)-(4),(2,4,1)-6而不是异或门的训练数据集。

该代码以前可以工作,但仅更改训练数据集,它对我来说不起作用。每个数据集生成的输出均为 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 个以上的隐藏节点。<​​/p>

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

关于php - 将 XOR 神经网络修改为其他类型的神经网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25195205/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com