php-ml/src/NeuralNetwork/Node/Neuron/Synapse.php

55 lines
967 B
PHP
Raw Normal View History

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
namespace Phpml\NeuralNetwork\Node\Neuron;
2016-08-02 18:30:20 +00:00
use Phpml\NeuralNetwork\Node;
class Synapse
2016-08-02 18:30:20 +00:00
{
/**
* @var float
*/
protected $weight;
/**
* @var Node
*/
protected $node;
/**
* @param float|null $weight
*/
public function __construct(Node $node, ?float $weight = null)
2016-08-02 18:30:20 +00:00
{
$this->node = $node;
$this->weight = $weight ?? $this->generateRandomWeight();
2016-08-02 18:30:20 +00:00
}
public function getOutput(): float
2016-08-02 18:30:20 +00:00
{
return $this->weight * $this->node->getOutput();
}
public function changeWeight(float $delta): void
2016-08-02 18:30:20 +00:00
{
$this->weight += $delta;
}
public function getWeight(): float
2016-08-02 18:30:20 +00:00
{
return $this->weight;
}
public function getNode(): Node
2016-08-02 18:30:20 +00:00
{
return $this->node;
}
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;
}
2016-08-02 18:30:20 +00:00
}