定制 stephenlmoshea/neuralnetwork 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

stephenlmoshea/neuralnetwork

Composer 安装命令:

composer require stephenlmoshea/neuralnetwork

包简介

Artificial neural network for PHP

README 文档

README

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

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

$  composer require stephenlmoshea/neuralnetwork

Examples

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

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

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

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

统计信息

  • 总下载量: 34
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-08-19

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固