项目作者: A1essandro

项目描述 :
Multilayer Perceptron, Kohonen Network, etc.
高级语言: PHP
项目地址: git://github.com/A1essandro/neural-network.git
创建时间: 2016-04-11T19:23:29Z
项目社区:https://github.com/A1essandro/neural-network

开源协议:MIT License

下载


neural-network

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

See more getcomposer.org.

Execute command

  1. composer require a1essandro/neural-network ^0.1.0

Or add line to composer.json

  1. "require": {
  2. ...
  3. "require a1essandro/neural-network": "^0.1.0"
  4. },

Method #2: Clone repository

Execute command

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

Usage

Common

XOR example:

  1. use Neural\BackpropagationTeacher;
  2. use Neural\MultilayerPerceptron;
  3. require_once '../vendor/autoload.php';
  4. //Creation neural network, with 2 input-neurons, one hidden layer with 2 neurons and one output neuron:
  5. $p = new MultilayerPerceptron([2, 2, 1]); //You may add more hidden layers or neurons to layers: [2, 3, 2, 1]
  6. $p->generateSynapses(); //automatically add synapses
  7. $t = new BackpropagationTeacher($p); //Teacher with backpropagation algorithm
  8. //Teach until it learns
  9. $learningResult = $t->teachKit(
  10. [[1, 0], [0, 1], [1, 1], [0, 0]], //kit for learning
  11. [[1], [1], [0], [0]], //appropriate expectations
  12. 0.3, //error
  13. 10000 //max iterations
  14. );
  15. if ($learningResult != -1) {
  16. echo '1,0: ' . round($p->input([1, 0])->output()[0]) . PHP_EOL;
  17. echo '0,1: ' . round($p->input([0, 1])->output()[0]) . PHP_EOL;
  18. echo '0,0: ' . round($p->input([0, 0])->output()[0]) . PHP_EOL;
  19. echo '1,1: ' . round($p->input([1, 1])->output()[0]) . PHP_EOL;
  20. }
  21. /* Result:
  22. 1,0: 1
  23. 0,1: 1
  24. 0,0: 0
  25. 1,1: 0
  26. */

Manually configuration of network

  1. $p = new MultilayerPerceptron([2, 2, 1]);
  2. //Equivalent to:
  3. $p = new MultilayerPerceptron();
  4. $p->addLayer(new Layer())->toLastLayer()
  5. ->addNode(new Input())
  6. ->addNode(new Input())
  7. ->addNode(new Bias());
  8. $p->addLayer(new Layer())->toLastLayer()
  9. ->addNode(new Neuron())
  10. ->addNode(new Neuron())
  11. ->addNode(new Bias());
  12. $p->addLayer(new Layer())->toLastLayer()
  13. ->addNode(new Neuron());
  14. //Do not forget to add synapses:
  15. $p->generateSynapses();
  16. //Or you may direct the process:
  17. $neuronFilter = function($node) {
  18. return $node instanceof Neuron;
  19. };
  20. $secondLayerNeuron = iterator_to_array($p->getLayers()[1]->getNodes($neuronFilter))[0];
  21. $input = iterator_to_array($p->getLayers()[0]->getNodes())[0];
  22. $secondLayerNeuron->addSynapse(new Synapse($input));
  23. //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.