Brainy
Neural network implementation in PHP, packaged for Composer., (*1)
Based heavily on the Tremani neural network by Edward Akerboom but stripped down, tidied up and packaged for Composer., (*2)
Installation
Install Brainy via Composer like this:, (*3)
composer require lambdacasserole/brainy
Or alternatively, if you're using the PHAR (make sure the php.exe
executable is in your PATH):, (*4)
php composer.phar require lambdacasserole/brainy
Usage
Create a new neural network instance like this:, (*5)
// Create a new neural network with 3 input neurons, one layer of 4 hidden neurons, and 1 output neuron.
$network = new NeuralNetwork(3, 4, 1);
Add training data to your new network thusly:, (*6)
// Add training data to the network. In this case, we want the network to learn the 'XOR' function.
$network->addTrainingData([-1, -1, 1], [-1]);
$network->addTrainingData([-1, 1, 1], [1]);
$network->addTrainingData([1, -1, 1], [1]);
$network->addTrainingData([1, 1, 1], [-1]);
Then begin training:, (*7)
// Train in a maximum of 1000 epochs to a maximum error rate of 0.01.
$success = $network->train(1000, 0.01);
Now put it to work:, (*8)
$output = $network->calculate([-1, -1, 1]); // Gives [-1].
$output = $network->calculate([-1, 1, 1]); // Gives [1].
Compatibility
Uses new array syntax and splats, so won't work on any PHP versions earlier than 5.6., (*9)
Further Reading
The original repository contains more comprehensive documentation, though it may need adjusting slightly due to modifications made to it in this version., (*10)