PHP MLP Backprop Code
5) { $weightsHO[$k] = 5; } } } //************************************ function WeightChangesIH() //adjust the weights input-hidden { global $trainInputs; global $numHidden; global $numInputs; global $hiddenVal; global $weightsHO; global $weightsIH; global $LR_IH; global $patNum; global $errThisPat; for($i = 0;$i<$numHidden;$i++) { for($k = 0;$k<$numInputs;$k++) { $x = 1 - ($hiddenVal[$i] * $hiddenVal[$i]); $x = $x * $weightsHO[$i] * $errThisPat * $LR_IH; $x = $x * $trainInputs[$patNum][$k]; $weightChange = $x; $weightsIH[$k][$i] = $weightsIH[$k][$i] - $weightChange; } } } //************************************ function initWeights() { global $numHidden; global $numInputs; global $weightsIH; global $weightsHO; for($j = 0;$j<$numHidden;$j++) { $weightsHO[$j] = (rand()/32767 - 0.5)/2; for($i = 0;$i<$numInputs;$i++) { $weightsIH[$i][$j] = (rand()/32767 - 0.5)/5; } } } //************************************ function initData() { global $trainInputs; global $trainOutput; print "initialising data\n"; // the data here is the XOR data // it has been rescaled to the range // [-1][1] // an extra input valued 1 is also added // to act as the bias // the output must lie in the range -1 to 1 $trainInputs[0][0] = 1; $trainInputs[0][1] = -1; $trainInputs[0][2] = 1; //bias $trainOutput[0] = 1; $trainInputs[1][0] = -1; $trainInputs[1][1] = 1; $trainInputs[1][2] = 1; //bias $trainOutput[1] = 1; $trainInputs[2][0] = 1; $trainInputs[2][1] = 1; $trainInputs[2][2] = 1; //bias $trainOutput[2] = -1; $trainInputs[3][0] = -1; $trainInputs[3][1] = -1; $trainInputs[3][2] = 1; //bias $trainOutput[3] = -1; } //************************************ function displayResults() { global $numPatterns; global $patNum; global $outPred; global $trainOutput; for($i = 0;$i<$numPatterns;$i++) { $patNum = $i; calcNet(); print "pat = ".($patNum+1)." actual = ".$trainOutput[$patNum]." neural model = ".$outPred."\n"; } } //************************************ function calcOverallError() { global $numPatterns; global $patNum; global $errThisPat; global $RMSerror; $RMSerror = 0.0; for($i = 0;$i<$numPatterns;$i++) { $patNum = $i; calcNet(); $RMSerror = $RMSerror + ($errThisPat * $errThisPat); } $RMSerror = $RMSerror/$numPatterns; $RMSerror = sqrt($RMSerror); } ?>