Пример #1
0
 /**
  * @throws \Exception
  */
 public function calculate()
 {
     $_sum = array_sum($this->getSignals());
     switch (Net::getDirection()) {
         case Net::DIRECT:
             $this->setValue($this->_binarSigmoid($_sum) + $this->getLayout()->getOffset());
             break;
         case Net::REVERSE:
             $this->setValue($_sum * $this->_diffBinarSigmoid($this->_value));
     }
 }
Пример #2
0
 /**
  * @param $initialValues
  * @param $teacherValues
  * @dataProvider prepareValues
  */
 public function testIterations($initialValues, $teacherValues)
 {
     $this->_layoutCollection = $this->initAllLayouts(self::LAYOUT_NUMBER);
     Net::setIterationsCount(0);
     //Preparing collection with neurons
     $this->initNeuronsInLayoutCollection($this->_layoutCollection, $initialValues, $teacherValues);
     $_iterations = array();
     foreach ($this->_layoutCollection as $layout) {
         array_push($_iterations, $layout->getLevel());
     }
     $this->assertSame($this->_iterationArray, $_iterations);
 }
Пример #3
0
 /**
  * @param array $signals
  * @param int $direction
  * @return array
  */
 public function prepareNeuronToCalculation($signals = array(), $direction)
 {
     $assoc = Factory::neuron(Factory::ASSOC, 0);
     $react = Factory::neuron(Factory::REACT, 0);
     //Adding Signals
     foreach ($signals as $signal) {
         $assoc->addSignal($signal);
         $react->addSignal($signal);
     }
     $this->_addLayoutToNeuron($assoc);
     $this->_addLayoutToNeuron($react);
     //Calculation
     Net::setDirection($direction);
     return array(Factory::ASSOC => $assoc, Factory::REACT => $react);
 }
Пример #4
0
 /**
  * Full Test: check whether back propagation was successful
  * @throws \Exception
  */
 public function testAbsolute()
 {
     $_values = $this->parseYaml($this->_getCurrentFolder(__DIR__, "absolute", "absolute.yml"));
     $_values = $this->prepareValues($_values);
     $this->initCounts($this->_app, $_values);
     //Initializing and start
     $this->_app->init($_values);
     $this->_app->start();
     Net::setMode(Net::PRODUCTION_MODE);
     $this->_app->start();
     //run app with same values but in production mode
     $lastLayout = $this->_app->getLayoutCollection()->getLastLayout();
     /** @var NeuronNet\Neuron\React $winner */
     $winner = $lastLayout->seek($this->_app->getWinner());
     //Compare standard teacher value with winner`s teacher value
     $this->assertEquals(self::WINNER_TEACHER_VALUE, $winner->getTeacherValue());
 }
Пример #5
0
 /**
  * @throws \Exception
  * @return bool
  */
 public function calculate()
 {
     //Calculating value
     $_sum = array_sum($this->getSignals());
     $_sum += $this->getLayout()->getOffset();
     $this->_value = $this->_binarSigmoid($_sum);
     if (Net::isTeacherMode()) {
         Net::setDirection(Net::REVERSE);
         $_value = ($this->_teacherValue - $this->_value) * $this->_diffBinarSigmoid($_sum);
         //new value will be mistake
         if (Net::isDebugEnabled()) {
             //Print value and teacher value/
             var_dump("Summ: {$_value}. Teacher : " . $this->_teacherValue . ". Value: " . $this->_value);
         }
         $this->setValue($_value);
     } else {
         $this->getLayout()->compare($this);
         //add to lead neurons if this neuron has the max value
     }
     return true;
 }
Пример #6
0
 /**
  * Test Weight Correction after sending signal. Direction Should be always Reverse
  * @param $signal
  * @param $weight
  * @dataProvider prepareSendingData
  * @depends testDirectSend
  * @depends testReverseSend
  */
 public function testWeightCorrection($signal, $weight)
 {
     Net::setDirection(Net::REVERSE);
     //signal propagation
     $this->_to->setValue($signal);
     $this->_from->setValue($this->_oldValue);
     $this->_relation->setWeight($weight);
     $this->_relation->send($this->_to);
     //Test whether weight correction is null or no
     $this->assertNotEquals($weight, $this->getWeightFromRelation($this->_relation), "Back Propagation Stuck");
 }
Пример #7
0
 public function setUp()
 {
     Net::setMode(Net::TEACHER_MODE);
 }
Пример #8
0
 /**
  * Check what mode we have -> iterate only in one side when we are using production mode, and use back propagation if we have also mistake flow
  * @return bool
  */
 public function valid()
 {
     if (!Net::validateIteration()) {
         return false;
     }
     if (!Net::isTeacherMode()) {
         return $this->_level != $this->count();
     } else {
         //back propaganation
         return !($this->_level == $this->count() && !Net::isMistakeFlow() && ($this->_level == 0 && Net::isMistakeFlow()));
     }
 }
Пример #9
0
 /**
  * @throws \Exception
  */
 public function calculate()
 {
     Net::setDirection(Net::DIRECT);
     $this->setValue($this->_value);
     //do nothing
 }