neuralnet.php/build/neuralnetwork/core/NeuralNetworkManager.php

115 lines
3.9 KiB
PHP

<?php
namespace neuralnetwork\core;
use filemanager\core\FileManager;
class NeuralNetworkCore implements \Serializable{
/************************************************
**** GLOBAL ATTRIBUTES ****
************************************************/
private $maxGnr; // Maximum generation iterations
private $maxGnm; // Maximum genomes per generation
private $kptGnm; // Number of genomes kept for each generation
private $mutThr; // Mutation threshold
private $fitEnd; // Fitness range to end process
private $numHid; // Number of hidden layer(s)
private $numNeu; // Number of neurons for each hidden layer
private $max; // max values for input and output neurons
private $storage; // path to storage
/************************************************
**** LOCAL ATTRIBUTES ****
************************************************/
private $gnr; // Current generation index
private $gmns; // Current generation's genomes
private $gnm; // Current genome index
private $fit; // Current fitness
}
/************************************************
**** USE CASE ****
************************************************/
$use_case = false;
if( $use_case ){
/* (1) Instanciate and configure neural network
---------------------------------------------------------*/
/* (1) Initialize neural network to try 100 genomes over 50 generations (max) */
$neunet = NeuralNetwork::create(100, 50);
/* (2) Specifies that a fitness greater than 0.89 will stop the neural network */
$neunet->setFitnessEnd(0.89);
/* (3) Specifies that it will keep the 2 best genomes of each generation */
$neunet->setKeptGenomes(2);
/* (4) Specifies that for each mutation, it must be at maximum 30% different */
$neunet->setMutationThreshold(0.4);
/* (5) Specifies the number of hidden layers */
$neunet->setHiddenLayersCount(2);
/* (6) Specifies the number of neurons per hidden layer */
$neunet->setHiddenLayerNeuronsCount(4);
/* (2) Let's learn
---------------------------------------------------------*/
/* (1) Set dataset sample maximum values */
$neunet->setMaxValues([2,2,7], [10, 10]);
// input output
/* (2) Setting our example dataset */
$neunet->addSample([1.9, 2, 5.3], [6, 0]);
$neunet->addSample([1.2, 1.2, 0.1], [0, 2]);
$neunet->addSample([0, 0, 0], [5, 5]);
/* (3) Launch learning routine with callback function */
$neunet->learn(function($ctx){ // callback with @ctx the current context
echo 'generation '. $ctx['generation'] .'/'. $ctx['generations'] ."\n";
echo 'genome '. $ctx['genome'] .'/'. $ctx['genomes'] ."\n";
echo 'fitness: '. $ctx['fitness'] .'/'. $ctx['fitness_end']. "\n";
});
/* (3) What to do next ?
---------------------------------------------------------*/
/* (1) Store your data */
$neunet->store('/some/path/to/storage');
/* (2) [HIDDEN] will create those 2 files */
'/some/path/to/storage.nn'; // containing neural network configuration
'/somt/path/to/storage.ln'; // containing neural network learnt values & weights
'/some/path/to/storage.ex'; // containing neural network samples
/* (4) And next ?
---------------------------------------------------------*/
/* (1) Load your stored neural network */
$trainednn = NeuralNetwork::load('/some/path/to/storage');
/* (2) Use it to guess output, with well-formed input */
$guessed = $trainednn->guess([1.2, 0.9, 6.1]);
/* (3) And correct it if needed*/
echo "1: ". $guessed[0] ."\n";
echo "2: ". $guessed[2] ."\n";
echo "is it correct ?\n";
// .. some code to manage and read correct output if it was wrong
$trainednn->addSample([1.2, 0.9, 6.1], [9, 0]);
// You can now relaunch a new neural network's learning
// or You can just add a few generations to evolve your neural network
}
?>