fix error 0.5 and add math pdf

This commit is contained in:
2026-11-25 12:18:18 +03:00
parent 0efd4bb8b1
commit 72f0b2c837
6 changed files with 124 additions and 10 deletions

View File

@@ -2,6 +2,7 @@
#include <hack/logger/logger.hpp>
#include "utils.hpp"
#include <array>
#include <math.h>
// Линейная форма - это прямой расчет прохода ошибки по математиченским формулам.
@@ -26,7 +27,16 @@ namespace linear
return 1.0 / (1 + r);
}
inline void run()
// Результат одного шага: новые веса, ошибка и выход сети.
struct result
{
utils::matrix_leaner W_1;
utils::matrix_leaner W_2;
float error;
std::array<float, 3> out;
};
inline result run()
{
std::vector<float> Inp = { 1.0, 2.0, 3.0 };
std::vector<float> T = { 0.1, 0.2, 0.3 };
@@ -100,10 +110,11 @@ namespace linear
hack::log()("Out 2 =", Out_2);
hack::log()("Out 3 =", Out_3);
// Ошибка
auto E_1 = std::pow((Out_1 - T[0]), 2);
auto E_2 = std::pow((Out_2 - T[1]), 2);
auto E_3 = std::pow((Out_3 - T[2]), 2);
// Ошибка: E = (1/2) * sum((Out_j - T_j)^2), как в конспекте.
const float E_1 = 0.5f * (Out_1 - T[0]) * (Out_1 - T[0]);
const float E_2 = 0.5f * (Out_2 - T[1]) * (Out_2 - T[1]);
const float E_3 = 0.5f * (Out_3 - T[2]) * (Out_3 - T[2]);
const float E = E_1 + E_2 + E_3;
hack::log()();
hack::log()("Ошибка");
@@ -111,12 +122,13 @@ namespace linear
hack::log()("E 1 =", E_1);
hack::log()("E 2 =", E_2);
hack::log()("E 3 =", E_3);
hack::log()("E =", E);
// ОБРАТНЫЙ ПРОХОД (backward)
// Вклад нейрона в ошибку (дельта)
auto delta_1_2 = 2 * (Out_1 - T[0]) * Out_1 * (1 - Out_1);
auto delta_2_2= 2 * (Out_2 - T[1]) * Out_2 * (1 - Out_2);
auto delta_3_2 = 2 * (Out_3 - T[2]) * Out_3 * (1 - Out_3);
const float delta_1_2 = (Out_1 - T[0]) * Out_1 * (1.0f - Out_1);
const float delta_2_2 = (Out_2 - T[1]) * Out_2 * (1.0f - Out_2);
const float delta_3_2 = (Out_3 - T[2]) * Out_3 * (1.0f - Out_3);
hack::log()();
hack::log()("=========================");
@@ -296,5 +308,7 @@ namespace linear
hack::log()(W_2[0]);
hack::log()(W_2[1]);
hack::log()(W_2[2]);
return { W_1, W_2, E, { Out_1, Out_2, Out_3 } };
}
}

View File

@@ -24,7 +24,16 @@ namespace matrix
{ 1.2, 1.5, 1.8 }
};
inline void run()
// Результат одного шага: новые веса, ошибка и выход сети.
struct result
{
utils::matrix<3, 3> W_1;
utils::matrix<3, 3> W_2;
float error;
utils::matrix<3, 1> out;
};
inline result run()
{
// =================================================
// ПРЯМОЙ ПРОХОД (forward)
@@ -50,7 +59,7 @@ namespace matrix
hack::log()("Error 2 = ", Err_2);
hack::log()("Error 3 = ", Err_3);
auto Err = 0.5 * (SQR(Err_1) + SQR(Err_2) + SQR(Err_3));
const float Err = 0.5f * (SQR(Err_1) + SQR(Err_2) + SQR(Err_3));
hack::log()("ERROR = ", Err);
// =================================================
@@ -99,5 +108,6 @@ namespace matrix
auto W_2_new = W_2 - delta_Out.transpose() * Int.transpose() * utils::LEARNING_RATE;
W_2_new.print("W 2 new");
return { W_1_new, W_2_new, Err, Out };
}
}