diff --git a/lib/NeuroNetwork.class.php b/lib/NeuroNetwork.class.php index dc6a8bf..728c002 100644 --- a/lib/NeuroNetwork.class.php +++ b/lib/NeuroNetwork.class.php @@ -186,4 +186,41 @@ { $synapse->getOutNeuron()->activate($synapse->getInNeuron()->getValue(), $synapse->getValue()); } + + + /** + * Расчет ошибки + * + * @param float $target Целевое значение + * @param float $current Текущее значение + * @return float Величина ошибки + */ + private function calculateError(float $target, float $current): float + { + return (($target - $current) ** 2) / 2; + } + + /** + * Расчет общей ошибки + * + * @param float $target Целевое значение (ПЕРЕДЕЛАТЬ) + * @return float Величина ошибки + */ + public function calculateTotalError(float $target = 0.0): float + { + $totalError = 0.0; + $endLayer = count($this->layers) - 1; + foreach($this->layers as $keyLayer => $layer) { + if ($keyLayer === 0 || $keyLayer === $endLayer) { + continue; + } + foreach($layer as $neuron) { + /** @var Neuron $neuron */ + // TODO требуется определять целевое значение и передавать первым аргументом ниже + $totalError += $this->calculateError($target, $neuron->getValue()); + } + } + + return $totalError; + } } \ No newline at end of file diff --git a/test.php b/test.php index b7d54cf..f4437ec 100644 --- a/test.php +++ b/test.php @@ -15,5 +15,6 @@ $res = $ob->run([1, 1]); echo '[Preset 2] Input data [1, 1], result: ' . $res[0] . PHP_EOL; + echo $ob->calculateTotalError(0.01);