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

71 lines
1.1 KiB
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 Node $node
* @param float|null $weight
*/
public function __construct(Node $node, float $weight = null)
{
$this->node = $node;
$this->weight = $weight ?: $this->generateRandomWeight();
}
/**
* @return float
*/
protected function generateRandomWeight(): float
{
return 1 / rand(5, 25) * (rand(0, 1) ? -1 : 1);
}
/**
* @return float
*/
public function getOutput(): float
{
return $this->weight * $this->node->getOutput();
}
/**
* @param float $delta
*/
public function changeWeight($delta)
{
$this->weight += $delta;
}
/**
* @return float
*/
public function getWeight()
{
return $this->weight;
}
/**
* @return Node
*/
public function getNode()
{
return $this->node;
}
}