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.
   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace Phpml\NeuralNetwork\Network;
   6  
   7  use Phpml\NeuralNetwork\Layer;
   8  use Phpml\NeuralNetwork\Network;
   9  use Phpml\NeuralNetwork\Node\Input;
  10  use Phpml\NeuralNetwork\Node\Neuron;
  11  
  12  abstract class LayeredNetwork implements Network
  13  {
  14      /**
  15       * @var Layer[]
  16       */
  17      protected $layers = [];
  18  
  19      public function addLayer(Layer $layer): void
  20      {
  21          $this->layers[] = $layer;
  22      }
  23  
  24      /**
  25       * @return Layer[]
  26       */
  27      public function getLayers(): array
  28      {
  29          return $this->layers;
  30      }
  31  
  32      public function removeLayers(): void
  33      {
  34          unset($this->layers);
  35      }
  36  
  37      public function getOutputLayer(): Layer
  38      {
  39          return $this->layers[count($this->layers) - 1];
  40      }
  41  
  42      public function getOutput(): array
  43      {
  44          $result = [];
  45          foreach ($this->getOutputLayer()->getNodes() as $neuron) {
  46              $result[] = $neuron->getOutput();
  47          }
  48  
  49          return $result;
  50      }
  51  
  52      /**
  53       * @param mixed $input
  54       */
  55      public function setInput($input): Network
  56      {
  57          $firstLayer = $this->layers[0];
  58  
  59          foreach ($firstLayer->getNodes() as $key => $neuron) {
  60              if ($neuron instanceof Input) {
  61                  $neuron->setInput($input[$key]);
  62              }
  63          }
  64  
  65          foreach ($this->getLayers() as $layer) {
  66              foreach ($layer->getNodes() as $node) {
  67                  if ($node instanceof Neuron) {
  68                      $node->reset();
  69                  }
  70              }
  71          }
  72  
  73          return $this;
  74      }
  75  }