refactor: change the project structure

This commit is contained in:
2026-09-17 21:56:40 +02:00
parent 7b89186599
commit 32bb5a98f5
2 changed files with 24 additions and 7 deletions
+2 -3
View File
@@ -4,6 +4,5 @@ project(NeuralNetworks CXX)
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD 20)
file(GLOB_RECURSE SOURCES src/**.cpp include/**.hpp) add_executable(xor src/xor.cpp)
add_executable(nn ${SOURCES}) target_include_directories(xor PRIVATE include/)
target_include_directories(nn PRIVATE include/)
+21 -3
View File
@@ -2,6 +2,7 @@
#include <cmath> #include <cmath>
#include <cstddef> #include <cstddef>
#include <fstream>
#include <iostream> #include <iostream>
#include <random> #include <random>
@@ -21,13 +22,20 @@ int main(void) {
// network net; // network net;
nn<2, 2, 1> xorNet; nn<2, 2, 1> xorNet;
static constexpr const char* NETWORK_PATH = "./xor.nn";
std::ifstream file(NETWORK_PATH, std::ios::in | std::ios::binary);
if (file.is_open())
xorNet.load_network(file);
else
xorNet.fill_random(dist); xorNet.fill_random(dist);
static constexpr double EPSILON = 0.5; static constexpr double EPSILON = 0.5;
std::size_t gen = 0; std::size_t gen = 0;
while (gen <= 100'000) { while (gen <= 10'000'000) {
for (std::size_t i = 0; i < 4; ++i) { for (std::size_t i = 0; i < 4; ++i) {
mat<1, 2> xs; mat<1, 2> xs;
xs[0][0] = static_cast<double>(i & 1); xs[0][0] = static_cast<double>(i & 1);
@@ -46,19 +54,29 @@ int main(void) {
double yHat = xorNet.feed(xs)[0][0]; double yHat = xorNet.feed(xs)[0][0];
double error = yHat - s_trainData[i]; double error = yHat - s_trainData[i];
if ((gen % 10) == 0) { if ((gen % 1000) == 0) {
std::cout << xs[0][0] << " ^ " << xs[0][1] << " = " << yHat << " (error = " << error << ")\n"; std::cout << xs[0][0] << " ^ " << xs[0][1] << " = " << yHat << " (error = " << error << ")\n";
} }
acc += error * error; acc += error * error;
} }
static constexpr double LOWER_COST = 1e-5;
double cost = acc / 8.0; double cost = acc / 8.0;
if (gen % 10 == 0) { if ((gen % 1000) == 0 || cost <= LOWER_COST) {
std::cout << "Generation " << gen << ", cost = " << cost << '\n'; std::cout << "Generation " << gen << ", cost = " << cost << '\n';
} }
if (cost <= LOWER_COST) {
std::ofstream outFile(NETWORK_PATH, std::ios::out | std::ios::binary);
if (!outFile.is_open()) break;
xorNet.dump_network(outFile);
std::cout << "Saved the network to " << NETWORK_PATH << "!\n";
break;
}
++gen; ++gen;
} }