我在网上找到了一些用PHP完成的神经网络的源代码。你可以在这里找到:http://pastebin.com/MaqFXkWW
我认为这将是完美的决定你在画布上画的东西是否已经画过了。所以我开始构建一个概念证明,并构建这个neural_trainer.php
脚本:
<?
require_once ("class_neuralnetwork.php");
$pattern = $_POST['data'];
$n = new NeuralNetwork(90000, 90000, 1);
$n->setVerbose(false);
$n->addTestData($pattern, array (0));
$max = 9;
while (!($success = $n->train(1000, 0.01)) && $max -- > 0) {
echo "Nothing found...<hr />";
}
if ($success) {
$epochs = $n->getEpoch();
echo "Success in $epochs training rounds!<hr />";
}
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";
}
这是javascript,它发布到neural_trainer.php
//canvas1 is 300x300
var img1Data = ctx1.getImageData(0,0,canvas1.width,canvas1.height);
$.ajax({
url: 'neural_training.php',
data: { data: img },
type: 'post',
success: function(data) {
console.log(data);
}
});
但它一直抛出Allowed memory size of x bytes exhausted (tried to allocate x bytes)
,即使我把它放在class_neuralnetwork.php
的顶部ini_set("memory_limit","340M");
ini_set('max_execution_time', 1000);
可供PHP使用的内存太小。要么通过memory_limit
配置指令增加PHP可用的内存,要么重写脚本,使其消耗更少的内存。