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 } ?>