2016-08-02 18:30:20 +00:00
|
|
|
<?php
|
|
|
|
|
2016-11-20 21:53:17 +00:00
|
|
|
declare(strict_types=1);
|
2016-08-02 18:30:20 +00:00
|
|
|
|
2016-08-05 08:20:31 +00:00
|
|
|
namespace Phpml\NeuralNetwork\Node\Neuron;
|
2016-08-02 18:30:20 +00:00
|
|
|
|
|
|
|
use Phpml\NeuralNetwork\Node;
|
|
|
|
|
2016-08-05 08:20:31 +00:00
|
|
|
class Synapse
|
2016-08-02 18:30:20 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var float
|
|
|
|
*/
|
|
|
|
protected $weight;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Node
|
|
|
|
*/
|
|
|
|
protected $node;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param float|null $weight
|
|
|
|
*/
|
2017-11-14 20:21:23 +00:00
|
|
|
public function __construct(Node $node, ?float $weight = null)
|
2016-08-02 18:30:20 +00:00
|
|
|
{
|
|
|
|
$this->node = $node;
|
2020-03-03 17:52:29 +00:00
|
|
|
$this->weight = $weight ?? $this->generateRandomWeight();
|
2016-08-02 18:30:20 +00:00
|
|
|
}
|
|
|
|
|
2017-11-22 21:16:10 +00:00
|
|
|
public function getOutput(): float
|
2016-08-02 18:30:20 +00:00
|
|
|
{
|
|
|
|
return $this->weight * $this->node->getOutput();
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function changeWeight(float $delta): void
|
2016-08-02 18:30:20 +00:00
|
|
|
{
|
|
|
|
$this->weight += $delta;
|
|
|
|
}
|
|
|
|
|
2017-11-22 21:16:10 +00:00
|
|
|
public function getWeight(): float
|
2016-08-02 18:30:20 +00:00
|
|
|
{
|
|
|
|
return $this->weight;
|
|
|
|
}
|
|
|
|
|
2017-11-06 07:56:37 +00:00
|
|
|
public function getNode(): Node
|
2016-08-02 18:30:20 +00:00
|
|
|
{
|
|
|
|
return $this->node;
|
|
|
|
}
|
2017-11-22 21:16:10 +00:00
|
|
|
|
|
|
|
protected function generateRandomWeight(): float
|
|
|
|
{
|
2018-10-28 06:44:52 +00:00
|
|
|
return (1 / random_int(5, 25) * random_int(0, 1)) > 0 ? -1 : 1;
|
2017-11-22 21:16:10 +00:00
|
|
|
}
|
2016-08-02 18:30:20 +00:00
|
|
|
}
|