From 214c63470c4816b27960c57c927c420f6a45aaec Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Thu, 30 Mar 2023 23:38:55 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20=D1=80?= =?UTF-8?q?=D0=B0=D1=81=D1=87=D0=B5=D1=82=D0=B0=20=D0=BE=D1=88=D0=B8=D0=B1?= =?UTF-8?q?=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/NeuroNetwork.class.php | 37 +++++++++++++++++++++++++++++++++++++ test.php | 1 + 2 files changed, 38 insertions(+) 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); From eb762cbab10517fd2d290d6de6936ef19001d5e0 Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Thu, 30 Mar 2023 23:43:59 +0300 Subject: [PATCH 2/2] codestyle --- lib/NeuroNetwork.class.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/NeuroNetwork.class.php b/lib/NeuroNetwork.class.php index 728c002..443634c 100644 --- a/lib/NeuroNetwork.class.php +++ b/lib/NeuroNetwork.class.php @@ -1,11 +1,15 @@ layers = [ [new Neuron(1), new Neuron(2)], [new Neuron(3), new Neuron(4), new Neuron(5), new Neuron(6)], @@ -44,7 +49,7 @@ $this->generateSynapses(); // Получение сохраненных данных по весам - $this->getSynapsValue(); + $this->getSynapsesValues(); } /** @@ -93,7 +98,7 @@ * * @return array Сохраненный раннее массив весов */ - private function getSynapsValue(): array + private function getSynapsesValues(): array { if (!file_exists($this->storageFile)) { return [];