PHP code example of stephenlmoshea / neuralnetwork

1. Go to this page and download the library: Download stephenlmoshea/neuralnetwork library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

stephenlmoshea / neuralnetwork example snippets




use neuralnetwork\Network\FeedForward;
use neuralnetwork\Activation\Sigmoid;
use neuralnetwork\Train\Backpropagation;

//Create network with 2 input nodes, 2 hidden nodes, and 1 output node
//and set activation function
$network = new FeedForward([2, 2, 1], new Sigmoid());

//Define learning rate and momentum parameters for backpropagation algorithm
$ann = new Backpropagation($network, 0.7, 0.3);

//Provide XOR training data
$trainingSet = [
                    [0,0,0],
                    [0,1,1],
                    [1,0,1],
                    [1,1,0]
                ];

//Keep training the neural network until it converges
do {
    $ann->initialise();
    $result = $ann->train($trainingSet);
} while (!$result);

//Present [0,0] as network inputs and get the network output
$network->activate([0, 0]);
$outputs = $network->getOutputs();
echo $outputs[0]."\n";

//Present [0,1] as network inputs and get the network output
$network->activate([0, 1]);
$outputs = $network->getOutputs();
echo $outputs[0]."\n";

//Present [1,0] as network inputs and get the network output 
$network->activate([1, 0]);
$outputs = $network->getOutputs();
echo $outputs[0]."\n";

//Present [1,1] as network inputs and get the network output
$network->activate([1, 1]);
$outputs = $network->getOutputs();
echo $outputs[0]."\n";



use neuralnetwork\Network\FeedForward;
use neuralnetwork\Activation\HyperbolicTangent;
use neuralnetwork\Train\Backpropagation;

//Create network with 2 input nodes, 2 hidden nodes, and 1 output node
//and set activation function
$network = new FeedForward([2, 2, 1], new HyperbolicTangent());

//Define learning rate and momentum parameters for backpropagation algorithm
$ann = new Backpropagation($network, 0.7, 0.3, 0.001);

//Provide XOR training data
$trainingSet = [
                    [-1,-1,-1],
                    [-1,1,1],
                    [1,-1,1],
                    [1,1,-1]
                ];

//Keep training the neural network until it converges
do {
    $ann->initialise();
    $result = $ann->train($trainingSet);
} while (!$result);

//Present [-1,-1] as network inputs and get the network output
$network->activate([-1, -1]);
$outputs = $network->getOutputs();
echo $outputs[0]."\n";

//Present [-1,1] as network inputs and get the network output
$network->activate([-1, 1]);
$outputs = $network->getOutputs();
echo $outputs[0]."\n";

//Present [1,-1] as network inputs and get the network output 
$network->activate([1, -1]);
$outputs = $network->getOutputs();
echo $outputs[0]."\n";

//Present [1,1] as network inputs and get the network output
$network->activate([1, 1]);
$outputs = $network->getOutputs();
echo $outputs[0]."\n";



use neuralnetwork\Network\FeedForward;
use neuralnetwork\Activation\Sigmoid;
use neuralnetwork\Train\Backpropagation;

//Create network with 2 input nodes, 2 hidden nodes, and 1 output node
//and set activation function
$network = new FeedForward([2, 2, 1], new Sigmoid());

//Define learning rate and momentum parameters for backpropagation algorithm
$ann = new Backpropagation($network, 0.7, 0.3);

//Provide XOR training data
$trainingSet = [
                    [0,0,0],
                    [0,1,1],
                    [1,0,1],
                    [1,1,0]
                ];

//Keep training the neural network until it converges
do {
    $ann->initialise();
    $result = $ann->train($trainingSet);
} while (!$result);

$network->save('./network.txt');



use neuralnetwork\Network\FeedForward;
use neuralnetwork\Activation\Sigmoid;
use neuralnetwork\Train\Backpropagation;

$network->load('./network.txt');

//Present [0,0] as network inputs and get the network output
$network->activate([0, 0]);
$outputs = $network->getOutputs();
echo $outputs[0]."\n";

//Present [0,1] as network inputs and get the network output
$network->activate([0, 1]);
$outputs = $network->getOutputs();
echo $outputs[0]."\n";

//Present [1,0] as network inputs and get the network output 
$network->activate([1, 0]);
$outputs = $network->getOutputs();
echo $outputs[0]."\n";

//Present [1,1] as network inputs and get the network output
$network->activate([1, 1]);
$outputs = $network->getOutputs();
echo $outputs[0]."\n";