PHPackages                             stephenlmoshea/neuralnetwork - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. stephenlmoshea/neuralnetwork

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

stephenlmoshea/neuralnetwork
============================

Artificial neural network for PHP

0.0.2(5y ago)134MITPHPPHP &gt;=5.3.0CI failing

Since Dec 16Pushed 5y ago2 watchersCompare

[ Source](https://github.com/stephenlmoshea/neuralnetwork)[ Packagist](https://packagist.org/packages/stephenlmoshea/neuralnetwork)[ RSS](/packages/stephenlmoshea-neuralnetwork/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (1)Dependencies (2)Versions (4)Used By (0)

Neural network
==============

[](#neural-network)

Artificial neural network for PHP. Features online backpropagtion learning using gradient descent, momentum, the sigmoid and hyperbolic tangent activation function.

About
-----

[](#about)

The library allows you to build and train multi-layer neural networks. You first define the structure for the network. The number of input, output, layers and hidden nodes. The network is then constructed. Interconnection strengths are represented using an adjacency matrix and initialised to small random values. Traning data is then presented to the network incrementally. The neural network uses an online backpropagation training algorithm that uses gradient descent to descend the error curve to adjust interconnection strengths. The aim of the training algorithm is to adjust the interconnection strengths in order to reduce the global error. The global error for the network is calculated using the mean sqaured error.

You can provide a learning rate and momentum parameter. The learning rate will affect the speed at which the neural network converges to an optimal solution. The momentum parameter will help gradient descent to avoid converging to a non optimal solution on the error curve called local minima. The correct size for the momentum parameter will help to find the global minima but too large a value will prevent the neural network from ever converging to a solution.

Trained neural networks can be saved to file and loaded back for later activation.

Installation
------------

[](#installation)

```
$  composer require stephenlmoshea/neuralnetwork
```

Examples
--------

[](#examples)

### Training XOR function on three layer neural network with two inputs and one output

[](#training-xor-function-on-three-layer-neural-network-with-two-inputs-and-one-output)

```
require __DIR__ . '/vendor/autoload.php';

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";
```

### Training XOR function on three layer neural network using Hyperbolic Tangent

[](#training-xor-function-on-three-layer-neural-network-using-hyperbolic-tangent)

```
require __DIR__ . '/vendor/autoload.php';

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";
```

### Saving trained neural network to file

[](#saving-trained-neural-network-to-file)

```
require __DIR__ . '/vendor/autoload.php';

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');
```

### Load trained neural network from file

[](#load-trained-neural-network-from-file)

```
require __DIR__ . '/vendor/autoload.php';

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";
```

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

2022d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5a9277725ff7fa53cc8f8aac053d3eac380aa812073752343de4ca3db49521cc?d=identicon)[stephenlmoshea](/maintainers/stephenlmoshea)

---

Top Contributors

[![stephenlmoshea](https://avatars.githubusercontent.com/u/11645622?v=4)](https://github.com/stephenlmoshea "stephenlmoshea (23 commits)")

---

Tags

artificial neural networkbackpropagationgradient descentsigmoid activation functionhyperbolic tangent activation function

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/stephenlmoshea-neuralnetwork/health.svg)

```
[![Health](https://phpackages.com/badges/stephenlmoshea-neuralnetwork/health.svg)](https://phpackages.com/packages/stephenlmoshea-neuralnetwork)
```

###  Alternatives

[matomo/matomo

Matomo is the leading Free/Libre open analytics platform

21.6k38.2k](/packages/matomo-matomo)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
