a1essandro/neural-network

Artificial neural network

v0.1.2 2017-01-15 19:37 UTC

This package is not auto-updated.

Last update: 2024-04-13 17:19:02 UTC


README

Build Status Coverage Status Code Climate Latest Stable Version Latest Unstable Version Total Downloads License

Language choice:

English Russian

Requirements

This package is only supported on PHP 5.5 and above.

Installation

Method #1(recommended): Composer package

See more getcomposer.org.

Execute command

composer require a1essandro/neural-network ^0.1.0

Or add line to composer.json

"require": {
    ...
    "require a1essandro/neural-network": "^0.1.0"
},

Method #2: Clone repository

Execute command

git clone https://github.com/A1essandro/neural-network

Usage

Common

XOR example:

use Neural\BackpropagationTeacher;
use Neural\MultilayerPerceptron;

require_once '../vendor/autoload.php';

//Creation neural network, with 2 input-neurons, one hidden layer with 2 neurons and one output neuron:
$p = new MultilayerPerceptron([2, 2, 1]); //You may add more hidden layers or neurons to layers: [2, 3, 2, 1]
$p->generateSynapses(); //automatically add synapses

$t = new BackpropagationTeacher($p); //Teacher with backpropagation algorithm

//Teach until it learns
$learningResult = $t->teachKit(
    [[1, 0], [0, 1], [1, 1], [0, 0]], //kit for learning
    [[1], [1], [0], [0]], //appropriate expectations 
    0.3, //error
    10000 //max iterations
);

if ($learningResult != -1) {
    echo '1,0: ' . round($p->input([1, 0])->output()[0]) . PHP_EOL;
    echo '0,1: ' . round($p->input([0, 1])->output()[0]) . PHP_EOL;
    echo '0,0: ' . round($p->input([0, 0])->output()[0]) . PHP_EOL;
    echo '1,1: ' . round($p->input([1, 1])->output()[0]) . PHP_EOL;
}

/* Result:
1,0: 1
0,1: 1
0,0: 0
1,1: 0
*/

Manually configuration of network

$p = new MultilayerPerceptron([2, 2, 1]);

//Equivalent to:

$p = new MultilayerPerceptron();
$p->addLayer(new Layer())->toLastLayer()
    ->addNode(new Input())
    ->addNode(new Input())
    ->addNode(new Bias());
$p->addLayer(new Layer())->toLastLayer()
    ->addNode(new Neuron())
    ->addNode(new Neuron())
    ->addNode(new Bias());
$p->addLayer(new Layer())->toLastLayer()
    ->addNode(new Neuron());

//Do not forget to add synapses:

$p->generateSynapses();

//Or you may direct the process:

$neuronFilter = function($node) {
    return $node instanceof Neuron;
};

$secondLayerNeuron = iterator_to_array($p->getLayers()[1]->getNodes($neuronFilter))[0];
$input = iterator_to_array($p->getLayers()[0]->getNodes())[0];
$secondLayerNeuron->addSynapse(new Synapse($input));

//and so on...

Specification

Network

Interface implementation of INetwork is a container comprising nodes (INode) interconnected by synapses (Synapse).

Layers

Interface implementations of ILayer are formal groups of INode in a LayeredNetwork.

Nodes

INode - neurons, input-neurons etc.

Synapses

Synapse - is a connection between two nodes (INode). Synapse gets output (call output()) of neuron-transmitter and convert the value via its weight. Result value gets neuron-reciever (it call output() of ISynapse).

Contribute

Contributions to the package are always welcome!

License

The code base is licensed under the MIT license.