Neuron.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * Artificial Neural Network - Version 2.2
  4. *
  5. * For updates and changes visit the project page at http://ann.thwien.de/
  6. *
  7. *
  8. *
  9. * <b>LICENCE</b>
  10. *
  11. * The BSD 2-Clause License
  12. *
  13. * http://opensource.org/licenses/bsd-license.php
  14. *
  15. * Copyright (c) 2002, Eddy Young
  16. * Copyright (c) 2007 - 2012, Thomas Wien
  17. * All rights reserved.
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions
  21. * are met:
  22. *
  23. * 1. Redistributions of source code must retain the above copyright
  24. * notice, this list of conditions and the following disclaimer.
  25. *
  26. * 2. Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in the
  28. * documentation and/or other materials provided with the distribution.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  33. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  34. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  35. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  36. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  39. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  40. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  41. * POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. * @author Eddy Young <jeyoung_at_priscimon_dot_com>
  44. * @author Thomas Wien <info_at_thwien_dot_de>
  45. * @version ANN Version 1.0 by Eddy Young
  46. * @version ANN Version 2.2 by Thomas Wien
  47. * @copyright Copyright (c) 2002 by Eddy Young
  48. * @copyright Copyright (c) 2007-2012 by Thomas Wien
  49. * @package ANN
  50. */
  51. namespace ANN;
  52. /**
  53. * @package ANN
  54. * @access private
  55. */
  56. final class Neuron
  57. {
  58. /**#@+
  59. * @ignore
  60. */
  61. /**
  62. * @var array
  63. */
  64. protected $arrInputs = null;
  65. /**
  66. * @var array
  67. */
  68. protected $arrWeights = null;
  69. /**
  70. * @var float
  71. */
  72. protected $floatOutput = null;
  73. /**
  74. * @var float
  75. */
  76. protected $floatDelta = 0;
  77. /**
  78. * @var Network
  79. */
  80. protected $objNetwork = null;
  81. protected $floatLearningRate = 0;
  82. /**#@-*/
  83. /**
  84. * @param Network $objNetwork
  85. * @uses Maths::randomDelta()
  86. */
  87. public function __construct(Network $objNetwork)
  88. {
  89. $this->objNetwork = $objNetwork;
  90. $this->floatDelta = Maths::randomDelta();
  91. $this->floatLearningRate = $this->objNetwork->floatLearningRate;
  92. }
  93. /**
  94. * @param array &$arrInputs
  95. * @uses initializeWeights()
  96. */
  97. public function setInputs(&$arrInputs)
  98. {
  99. $this->arrInputs = $arrInputs;
  100. $this->arrInputs[] = 1; // bias
  101. if($this->arrWeights === null)
  102. $this->initializeWeights();
  103. }
  104. /**
  105. * @param float $floatDelta
  106. */
  107. public function setDelta($floatDelta)
  108. {
  109. $this->floatDelta = $floatDelta;
  110. }
  111. /**
  112. * @return array
  113. */
  114. public function getWeights()
  115. {
  116. return $this->arrWeights;
  117. }
  118. /**
  119. * @param integer $intKeyNeuron
  120. * @return float
  121. */
  122. public function getWeight($intKeyNeuron)
  123. {
  124. return $this->arrWeights[$intKeyNeuron];
  125. }
  126. /**
  127. * @return float
  128. */
  129. public function getOutput()
  130. {
  131. return $this->floatOutput;
  132. }
  133. /**
  134. * @return float
  135. */
  136. public function getDelta()
  137. {
  138. return $this->floatDelta;
  139. }
  140. /**
  141. * @uses Maths::randomWeight()
  142. */
  143. protected function initializeWeights()
  144. {
  145. foreach($this->arrInputs as $intKey => $floatInput)
  146. $this->arrWeights[$intKey] = Maths::randomWeight();
  147. }
  148. /**
  149. * @uses Maths::sigmoid()
  150. */
  151. public function activate()
  152. {
  153. $floatSum = 0;
  154. foreach($this->arrInputs as $intKey => $floatInput)
  155. $floatSum += $floatInput * $this->arrWeights[$intKey];
  156. $this->floatOutput = Maths::sigmoid($floatSum);
  157. }
  158. public function adjustWeights()
  159. {
  160. $floatLearningRateDeltaFactor = $this->floatLearningRate * $this->floatDelta;
  161. foreach ($this->arrWeights as $intKey => $floatWeight)
  162. $this->arrWeights[$intKey] += $this->arrInputs[$intKey] * $floatLearningRateDeltaFactor;
  163. }
  164. }