neuralnet.php/public/main.php

197 lines
5.2 KiB
PHP
Raw Normal View History

<?php define('__ROOT__', dirname(dirname(__FILE__)) );
require_once __ROOT__.'/autoloader.php';
use \neuralnetwork\core\Genome;
use \neuralnetwork\core\NeuralNetwork;
use \filemanager\core\FileManager;
2016-10-29 13:47:32 +00:00
function behaviourtest1($in){ return [$in[0] + $in[1] - $in[2]]; }
function behaviourtest2($in){ return [ 2*pow($in[0], 2) - 5*$in[1] + 8*$in[2]]; }
2016-10-29 13:47:32 +00:00
$train = true;
$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{
2016-10-29 13:47:32 +00:00
$nn = NeuralNetwork::load('test2/test2');
echo "$part. NeuralNetwork loaded from 'test2/test2'\n"; $part++;
/* [2] Else, creates it
=========================================================*/
}catch(\Exception $e){
2016-10-29 13:47:32 +00:00
$nn = NeuralNetwork::create(50, 500);
$nn->setHiddenLayersCount(3); // 3 Hidden layers
2016-10-29 13:47:32 +00:00
$nn->setHiddenLayerNeuronsCount(4); // Composed with 3 neurons each
$nn->setInputLayerCount(3); // 3 inputs
$nn->setOutputLayerCount(1); // 1 output
$nn->setMutationThreshold(0.3); // mutation 30% each generation
2016-10-29 13:47:32 +00:00
$nn->setFitnessEnd(-1.5); // Algorithm is done when fitness reaches 0
$nn->setAntiRegression(true); // That repeats a generation while its fitness is lower than the previous one
echo "$part. NeuralNetwork configured\n"; $part++;
2016-10-29 13:47:32 +00:00
$d = [0, 0, 0]; $nn->addSample($d, behaviourtest2($d));
$d = [0, 0, 1]; $nn->addSample($d, behaviourtest2($d));
$d = [0, 1, 0]; $nn->addSample($d, behaviourtest2($d));
$d = [0, 1, 1]; $nn->addSample($d, behaviourtest2($d));
$d = [1, 0, 0]; $nn->addSample($d, behaviourtest2($d));
$d = [1, 0, 1]; $nn->addSample($d, behaviourtest2($d));
$d = [1, 1, 0]; $nn->addSample($d, behaviourtest2($d));
$d = [1, 1, 1]; $nn->addSample($d, behaviourtest2($d));
$d = [0, 0, 0]; $nn->addSample($d, behaviourtest2($d));
$d = [0, 0, 2]; $nn->addSample($d, behaviourtest2($d));
$d = [0, 2, 0]; $nn->addSample($d, behaviourtest2($d));
$d = [0, 2, 2]; $nn->addSample($d, behaviourtest2($d));
$d = [2, 0, 0]; $nn->addSample($d, behaviourtest2($d));
$d = [2, 0, 2]; $nn->addSample($d, behaviourtest2($d));
$d = [2, 2, 0]; $nn->addSample($d, behaviourtest2($d));
$d = [2, 2, 2]; $nn->addSample($d, behaviourtest2($d));
echo "$part. Samples added to NeuralNetwork\n"; $part++;
2016-10-29 13:47:32 +00:00
$nn->store('test2/test2', true);
echo "$part. NeuralNetwork stored to 'test2/test2'\n"; $part++;
}
/* [2] Initializing learning routine
=========================================================*/
2016-10-29 13:47:32 +00:00
$defaultMT = 0.3;
$fitness = 0;
$max_fit = 0;
2016-10-27 19:28:09 +00:00
$nn->loadLearningRoutine(function($input, $output){
global $fitness;
2016-10-29 13:47:32 +00:00
$fitness -= abs($output[0] - behaviourtest2($input)[0]);
});
echo "$part. Learning routine initialized.\n"; $part++;
/* [3] Learning through generations and genomes
=========================================================*/
/* (1) For each generation */
$last_gnr = -1;
2016-10-29 13:47:32 +00:00
$gen_repeat = 0;
while( true ){
if( $nn->gnr > $last_gnr)
$start = microtime(true);
$last_gnr = $nn->gnr;
$max_fit = -1e9;
2016-10-29 13:47:32 +00:00
$min_fit = 100;
2016-10-27 19:28:09 +00:00
/* (2) For each genome */
while( true ){
$fitness = 0;
/* (2.1) Get current genome */
$g = $nn->getGenome();
2016-10-29 13:47:32 +00:00
echo "\r[x] gnm ".($nn->gnm+1)."/500 on gnr ".($nn->gnr+1)."/50 - x".($gen_repeat+1)." - fit[$min_fit;$max_fit] ";
/* (2.2) Train genome with random samples */
for( $r = 0 ; $r < 100 ; $r++ )
2016-10-29 13:47:32 +00:00
$g->train([rand(0,10), rand(0,10), rand(0,10)]);
/* (2.3) Set fitness & go to next genome */
if( $fitness > $max_fit ) $max_fit = $fitness;
2016-10-29 13:47:32 +00:00
if( $fitness < $min_fit ) $min_fit = $fitness;
$g->setFitness($fitness);
2016-10-29 13:47:32 +00:00
if( $nn->gnm >= 500-1 )
break;
$nn->nextGenome();
}
$nn->nextGenome();
2016-10-29 13:47:32 +00:00
// If generation evolution, notify
2016-10-29 13:47:32 +00:00
if( $nn->gnr > $last_gnr){
echo "\n\t".((microtime(true)-$start))."s\n";
2016-10-29 13:47:32 +00:00
$gen_repeat = 0;
}else $gen_repeat++;
if( is_null($nn->gnr) || $nn->gnr == 50-1 )
break;
2016-10-27 19:28:09 +00:00
}
}
2016-10-29 13:47:32 +00:00
if( $guess && 'guessing_process' ){
$part = 1;
echo "Welcome to neural-network.php\n";
echo "-----------------------------\n\n";
/* [1] Trying to load neural network
=========================================================*/
try{
2016-10-29 13:47:32 +00:00
$nn = NeuralNetwork::load('test2/test2');
echo "$part. NeuralNetwork loaded from 'test2/test2'\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";
2016-10-29 13:47:32 +00:00
echo "callback output: ".$out[0]."\n";
echo "callback result: ".implode(',', behaviourtest2($in))."\n";
});
2016-10-29 13:47:32 +00:00
$genome->train([rand(0,10), rand(0,10), rand(0,10)]);
}
// 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);
// }
?>