2016-08-02 11:07:47 +00:00
|
|
|
<?php
|
|
|
|
|
2016-11-20 21:53:17 +00:00
|
|
|
declare(strict_types=1);
|
2016-08-02 11:07:47 +00:00
|
|
|
|
|
|
|
namespace Phpml\NeuralNetwork\Node;
|
|
|
|
|
2016-08-02 18:30:20 +00:00
|
|
|
use Phpml\NeuralNetwork\ActivationFunction;
|
2017-11-22 21:16:10 +00:00
|
|
|
use Phpml\NeuralNetwork\ActivationFunction\Sigmoid;
|
2016-08-02 18:30:20 +00:00
|
|
|
use Phpml\NeuralNetwork\Node;
|
2017-11-06 07:56:37 +00:00
|
|
|
use Phpml\NeuralNetwork\Node\Neuron\Synapse;
|
2016-08-02 18:30:20 +00:00
|
|
|
|
|
|
|
class Neuron implements Node
|
2016-08-02 11:07:47 +00:00
|
|
|
{
|
2016-08-02 18:30:20 +00:00
|
|
|
/**
|
|
|
|
* @var Synapse[]
|
|
|
|
*/
|
2017-11-22 21:16:10 +00:00
|
|
|
protected $synapses = [];
|
2016-08-02 18:30:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ActivationFunction
|
|
|
|
*/
|
|
|
|
protected $activationFunction;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var float
|
|
|
|
*/
|
|
|
|
protected $output;
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function __construct(?ActivationFunction $activationFunction = null)
|
2016-08-02 18:30:20 +00:00
|
|
|
{
|
2017-11-22 21:16:10 +00:00
|
|
|
$this->activationFunction = $activationFunction ?: new Sigmoid();
|
2016-08-02 18:30:20 +00:00
|
|
|
$this->synapses = [];
|
|
|
|
$this->output = 0;
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function addSynapse(Synapse $synapse): void
|
2016-08-02 18:30:20 +00:00
|
|
|
{
|
|
|
|
$this->synapses[] = $synapse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Synapse[]
|
|
|
|
*/
|
|
|
|
public function getSynapses()
|
|
|
|
{
|
|
|
|
return $this->synapses;
|
|
|
|
}
|
|
|
|
|
2017-11-22 21:16:10 +00:00
|
|
|
public function getOutput(): float
|
2016-08-02 18:30:20 +00:00
|
|
|
{
|
2017-11-22 21:16:10 +00:00
|
|
|
if ($this->output === 0) {
|
2016-08-02 18:30:20 +00:00
|
|
|
$sum = 0;
|
|
|
|
foreach ($this->synapses as $synapse) {
|
|
|
|
$sum += $synapse->getOutput();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->output = $this->activationFunction->compute($sum);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->output;
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function reset(): void
|
2016-08-02 18:30:20 +00:00
|
|
|
{
|
|
|
|
$this->output = 0;
|
|
|
|
}
|
2016-08-02 11:07:47 +00:00
|
|
|
}
|