stephenlmoshea / neuralnetwork
PHP的人工神经网络
0.0.2
2020-12-16 11:36 UTC
Requires
- php: >=5.3.0
- monolog/monolog: ^1.18
Requires (Dev)
- phpunit/phpunit: ~5.0
This package is auto-updated.
Last update: 2024-09-16 19:36:09 UTC
README
PHP的人工神经网络。具有使用梯度下降、动量、Sigmoid和双曲正切激活函数的在线反向传播学习功能。
关于
此库允许您构建和训练多层神经网络。首先定义网络结构。包括输入、输出、层数和隐藏节点数量。然后构建网络。使用邻接矩阵表示互连强度并初始化为小的随机值。然后将训练数据逐步呈现给网络。神经网络使用在线反向传播训练算法,该算法使用梯度下降下降误差曲线以调整互连强度。训练算法的目标是通过调整互连强度来减少全局误差。网络的全局误差通过均方误差计算。
您可以提供一个学习率参数和动量参数。学习率会影响神经网络收敛到最优解的速度。动量参数将帮助梯度下降避免在误差曲线上收敛到非最优解(称为局部最小值)。动量参数的正确大小将有助于找到全局最小值,但过大的值将阻止神经网络收敛到解。
训练好的神经网络可以保存到文件,并在以后激活时加载回来。
安装
$ composer require stephenlmoshea/neuralnetwork
示例
在具有两个输入和一个输出的三层神经网络上训练XOR函数
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";
使用双曲正切在三层神经网络上训练XOR函数
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";
将训练好的神经网络保存到文件
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');
从文件加载训练好的神经网络
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";