I found some source code online for a neural network done in PHP. You can find it here: http://pastebin.com/MaqFXkWW
I thought this would be perfect for deciding if something you've drawn in canvas has been drawn before. So I set out to build a proof of concept and build this neural_trainer.php
script:
<?
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).")
";
}
And this is the javascript, which posts to 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);
}
});
But it keeps throwing Allowed memory size of x bytes exhausted (tried to allocate x bytes)
eventhough I put this at the top of class_neuralnetwork.php
ini_set("memory_limit","340M"); ini_set('max_execution_time', 1000);
The memory available to PHP is too small. Either increase the memory available to PHP by means of the memory_limit
configuration directive or rewrite the script such that it consumes less memory.