Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace Phpml\NeuralNetwork\Node\Neuron;
   6  
   7  use Phpml\NeuralNetwork\Node;
   8  
   9  class Synapse
  10  {
  11      /**
  12       * @var float
  13       */
  14      protected $weight;
  15  
  16      /**
  17       * @var Node
  18       */
  19      protected $node;
  20  
  21      /**
  22       * @param float|null $weight
  23       */
  24      public function __construct(Node $node, ?float $weight = null)
  25      {
  26          $this->node = $node;
  27          $this->weight = $weight ?: $this->generateRandomWeight();
  28      }
  29  
  30      public function getOutput(): float
  31      {
  32          return $this->weight * $this->node->getOutput();
  33      }
  34  
  35      public function changeWeight(float $delta): void
  36      {
  37          $this->weight += $delta;
  38      }
  39  
  40      public function getWeight(): float
  41      {
  42          return $this->weight;
  43      }
  44  
  45      public function getNode(): Node
  46      {
  47          return $this->node;
  48      }
  49  
  50      protected function generateRandomWeight(): float
  51      {
  52          return (1 / random_int(5, 25) * random_int(0, 1)) > 0 ? -1 : 1;
  53      }
  54  }