refactor: change the project structure
This commit is contained in:
+2
-3
@@ -4,6 +4,5 @@ project(NeuralNetworks CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
file(GLOB_RECURSE SOURCES src/**.cpp include/**.hpp)
|
||||
add_executable(nn ${SOURCES})
|
||||
target_include_directories(nn PRIVATE include/)
|
||||
add_executable(xor src/xor.cpp)
|
||||
target_include_directories(xor PRIVATE include/)
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
|
||||
@@ -21,13 +22,20 @@ int main(void) {
|
||||
|
||||
// network net;
|
||||
nn<2, 2, 1> xorNet;
|
||||
xorNet.fill_random(dist);
|
||||
|
||||
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);
|
||||
|
||||
static constexpr double EPSILON = 0.5;
|
||||
|
||||
std::size_t gen = 0;
|
||||
|
||||
while (gen <= 100'000) {
|
||||
while (gen <= 10'000'000) {
|
||||
for (std::size_t i = 0; i < 4; ++i) {
|
||||
mat<1, 2> xs;
|
||||
xs[0][0] = static_cast<double>(i & 1);
|
||||
@@ -46,19 +54,29 @@ int main(void) {
|
||||
double yHat = xorNet.feed(xs)[0][0];
|
||||
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";
|
||||
}
|
||||
|
||||
acc += error * error;
|
||||
}
|
||||
|
||||
static constexpr double LOWER_COST = 1e-5;
|
||||
|
||||
double cost = acc / 8.0;
|
||||
|
||||
if (gen % 10 == 0) {
|
||||
if ((gen % 1000) == 0 || cost <= LOWER_COST) {
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user