neuralnet.php/public/main.php

221 lines
5.7 KiB
PHP

<?php define('__ROOT__', dirname(dirname(__FILE__)) );
require_once __ROOT__.'/autoloader.php';
use \neuralnetwork\core\Genome;
use \neuralnetwork\core\NeuralNetwork;
use \filemanager\core\FileManager;
function behaviour($abc){
return [($abc[0] + $abc[1] - $abc[2])];
}
$train = false;
$guess = !$train;
if( $train && 'learning_process' ){
$part = 1;
echo "Welcome to neural-network.php\n";
echo "-----------------------------\n\n";
/* [1] Trying to load neural network
=========================================================*/
try{
$nn = NeuralNetwork::load('test1/test1');
echo "$part. NeuralNetwork loaded from 'test1/test1'\n"; $part++;
/* [2] Else, creates it
=========================================================*/
}catch(\Exception $e){
$nn = NeuralNetwork::create(50, 100);
$nn->setHiddenLayersCount(3); // 3 Hidden layers
$nn->setHiddenLayerNeuronsCount(3); // Composed with 3 neurons each
$nn->setInputLayerCount(3); // 3 inputs
$nn->setOutputLayerCount(1); // 1 output
$nn->setMutationThreshold(0.3); // mutation 30% each generation
$nn->setFitnessEnd(0); // Algorith is done when fitness reaches 0
echo "$part. NeuralNetwork configured\n"; $part++;
$d = [0, 0, 0]; $nn->addSample($d, behaviour($d));
$d = [0, 0, 1]; $nn->addSample($d, behaviour($d));
$d = [0, 1, 0]; $nn->addSample($d, behaviour($d));
$d = [0, 1, 1]; $nn->addSample($d, behaviour($d));
$d = [1, 0, 0]; $nn->addSample($d, behaviour($d));
$d = [1, 0, 1]; $nn->addSample($d, behaviour($d));
$d = [1, 1, 0]; $nn->addSample($d, behaviour($d));
$d = [1, 1, 1]; $nn->addSample($d, behaviour($d));
$d = [0, 0, 0]; $nn->addSample($d, behaviour($d));
$d = [0, 0, 2]; $nn->addSample($d, behaviour($d));
$d = [0, 2, 0]; $nn->addSample($d, behaviour($d));
$d = [0, 2, 2]; $nn->addSample($d, behaviour($d));
$d = [2, 0, 0]; $nn->addSample($d, behaviour($d));
$d = [2, 0, 2]; $nn->addSample($d, behaviour($d));
$d = [2, 2, 0]; $nn->addSample($d, behaviour($d));
$d = [2, 2, 2]; $nn->addSample($d, behaviour($d));
echo "$part. Samples added to NeuralNetwork\n"; $part++;
$nn->store('test1/test1', true);
echo "$part. NeuralNetwork stored to 'test1/test1'\n"; $part++;
}
/* [2] Initializing learning routine
=========================================================*/
$fitness = 0;
$max_fit = 0;
$nn->loadLearningRoutine(function($input, $output){
global $fitness;
$fitness -= abs(round($output[0]) - behaviour($input)[0]);
});
echo "$part. Learning routine initialized.\n"; $part++;
/* [3] Learning through generations and genomes
=========================================================*/
/* (1) For each generation */
$last_gnr = -1;
while( true ){
if( $nn->gnr > $last_gnr)
$start = microtime(true);
$last_gnr = $nn->gnr;
$max_fit = -1e9;
/* (2) For each genome */
while( true ){
$fitness = 0;
/* (2.1) Get current genome */
$g = $nn->getGenome();
echo "\r[x] gnm ".($nn->gnm+1)."/100 on gnr ".($nn->gnr+1)."/50 - max_fit: $max_fit ";
/* (2.2) Train genome with random samples */
for( $r = 0 ; $r < 100 ; $r++ )
$g->train([rand(0,100), rand(0,100), rand(0,100)]);
/* (2.3) Set fitness & go to next genome */
if( $fitness > $max_fit ) $max_fit = $fitness;
$g->setFitness($fitness);
if( $nn->gnm >= 100-1 )
break;
$nn->nextGenome();
}
$nn->nextGenome();
// If generation evolution, notify
if( $nn->gnr > $last_gnr)
echo "\n\t".((microtime(true)-$start))."s\n";
if( is_null($nn->gnr) || $nn->gnr == 50-1 )
break;
}
}
if( $guess && 'guessing_process' ){
$part = 1;
echo "Welcome to neural-network.php\n";
echo "-----------------------------\n\n";
/* [1] Trying to load neural network
=========================================================*/
try{
$nn = NeuralNetwork::load('test1/test1');
echo "$part. NeuralNetwork loaded from 'test1/test1'\n"; $part++;
/* [2] Else, creates it
=========================================================*/
}catch(\Exception $e){
echo "You must create/train your neural network before using it.\n";
exit();
}
/* [2] Fetch trained genome
=========================================================*/
$genome = $nn->getTrainedGenome();
$genome->setCallback(function($in, $out){
echo "callback input: ".implode(',', $in)."\n";
echo "callback output: ".round($out[0])."\n";
echo "callback result: ".implode(',', behaviour($in))."\n";
});
$genome->train([rand(0,100), rand(0,100), rand(0,100)]);
}
if( false ){
$g = new Genome(2, 3, 3, 2);
$fitness = 0;
$g->setCallback(function($input, $output){
global $fitness;
echo "callback output: ".round($output[0]).", ".round($output[1])."\n";
$result = behaviour($input);
if( $output[0] == $result[0] )
$fitness++;
if( $output[1] == $result[1] )
$fitness++;
});
echo $g->train([0, 0, 0]);
echo $g->train([0, 0, 1]);
echo $g->train([0, 1, 0]);
echo $g->train([0, 1, 1]);
echo $g->train([1, 0, 0]);
echo $g->train([1, 0, 1]);
echo $g->train([1, 1, 0]);
echo $g->train([1, 1, 1]);
echo $g->train([0, 0, 0]);
echo $g->train([0, 0, 2]);
echo $g->train([0, 2, 0]);
echo $g->train([0, 2, 2]);
echo $g->train([2, 0, 0]);
echo $g->train([2, 0, 2]);
echo $g->train([2, 2, 0]);
echo $g->train([2, 2, 2]);
echo "fitness: $fitness\n";
$g->setFitness($fitness);
echo $g->serialize();
}
// REWRITE TEST
// for( $a = 0, $al = 50 ; $a < $al ; $a++ )
// for( $b = 0, $bl = 20 ; $b < $bl ; $b++ ){
// print "genome $b/$bl on generation $a/$al \r";
// usleep(1000*10);
// }
?>