mirror of
https://github.com/Sosokker/CPP-Neural-Network.git
synced 2025-12-18 18:14:04 +01:00
Add main.cpp/ Update CMake
This commit is contained in:
parent
ae1da7bfd9
commit
6e8ed83911
@ -1,3 +1,7 @@
|
|||||||
|
# Set the C and C++ compiler paths
|
||||||
|
set(CMAKE_C_COMPILER "C:/msys64/mingw64/bin/gcc.exe")
|
||||||
|
set(CMAKE_CXX_COMPILER "C:/msys64/mingw64/bin/g++.exe")
|
||||||
|
|
||||||
cmake_minimum_required(VERSION 3.10)
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
|
||||||
project(CPP-Neural-Network)
|
project(CPP-Neural-Network)
|
||||||
@ -5,21 +9,33 @@ project(CPP-Neural-Network)
|
|||||||
# Set C++ standard
|
# Set C++ standard
|
||||||
set(CMAKE_CXX_STANDARD 11)
|
set(CMAKE_CXX_STANDARD 11)
|
||||||
|
|
||||||
# Set the directories for header files and source files
|
# Add the Eigen library directory (assuming Eigen is in the project root)
|
||||||
include_directories(include)
|
|
||||||
include_directories(${CMAKE_SOURCE_DIR}/include)
|
|
||||||
include_directories(${CMAKE_SOURCE_DIR}/Eigen)
|
include_directories(${CMAKE_SOURCE_DIR}/Eigen)
|
||||||
include_directories(${CMAKE_SOURCE_DIR}/include/Activation)
|
|
||||||
include_directories(${CMAKE_SOURCE_DIR}/include/Layers)
|
|
||||||
|
|
||||||
# Add source files to the project
|
include_directories(${CMAKE_SOURCE_DIR}/src)
|
||||||
file(GLOB_RECURSE SOURCES "src/*.cpp")
|
include_directories(${CMAKE_SOURCE_DIR}/src/Activation)
|
||||||
|
include_directories(${CMAKE_SOURCE_DIR}/src/Loss)
|
||||||
|
include_directories(${CMAKE_SOURCE_DIR}/src/Layers)
|
||||||
|
include_directories(${CMAKE_SOURCE_DIR}/src/utils)
|
||||||
|
|
||||||
|
set(SOURCES
|
||||||
|
main.cpp
|
||||||
|
Activation/ActivationFunc.cpp
|
||||||
|
Loss/Loss.cpp
|
||||||
|
Layers/DenseLayer.cpp
|
||||||
|
utils/utils.cpp
|
||||||
|
../include/Activation/Activation.hpp
|
||||||
|
../include/Layers/DenseLayer.hpp
|
||||||
|
../include/Layers/layers.hpp
|
||||||
|
../include/utils/utils.hpp
|
||||||
|
../include/Eigen/Dense
|
||||||
|
)
|
||||||
|
|
||||||
# Create the executable
|
# Create the executable
|
||||||
add_executable(CPP-Neural-Network ${SOURCES})
|
add_executable(NeuralNetwork ${SOURCES})
|
||||||
|
|
||||||
# Link the Eigen library
|
# Link the Eigen library
|
||||||
target_include_directories(CPP-Neural-Network PRIVATE ${CMAKE_SOURCE_DIR}/Eigen)
|
target_include_directories(NeuralNetwork PRIVATE ${CMAKE_SOURCE_DIR}/Eigen)
|
||||||
|
|
||||||
# Set the output directory for the executable
|
# Set the output directory for the executable
|
||||||
set_target_properties(CPP-Neural-Network PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
set_target_properties(NeuralNetwork PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||||
|
|||||||
121
src/main.cpp
121
src/main.cpp
@ -0,0 +1,121 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include "../include/Eigen/Dense"
|
||||||
|
#include "Activation/ActivationFunc.cpp"
|
||||||
|
#include "Loss/Loss.cpp"
|
||||||
|
#include "Layers/DenseLayer.hpp"
|
||||||
|
#include "utils/utils.hpp"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
std::vector<Layer*> network;
|
||||||
|
network.push_back(new DenseLayer(2, 4));
|
||||||
|
network.push_back(new TanhActivation());
|
||||||
|
network.push_back(new DenseLayer(4, 1));
|
||||||
|
network.push_back(new SigmoidActivation());
|
||||||
|
|
||||||
|
std::vector<Eigen::VectorXd> x_train;
|
||||||
|
std::vector<Eigen::VectorXd> y_train;
|
||||||
|
|
||||||
|
// ! SAMPLE
|
||||||
|
Eigen::VectorXd x_sample1(2);
|
||||||
|
x_sample1 << 0.2, 0.3;
|
||||||
|
x_train.push_back(x_sample1);
|
||||||
|
|
||||||
|
Eigen::VectorXd y_sample1(1);
|
||||||
|
y_sample1 << 1.0; // Positive class
|
||||||
|
y_train.push_back(y_sample1);
|
||||||
|
|
||||||
|
Eigen::VectorXd x_sample2(2);
|
||||||
|
x_sample2 << -0.5, 0.8;
|
||||||
|
x_train.push_back(x_sample2);
|
||||||
|
|
||||||
|
Eigen::VectorXd y_sample2(1);
|
||||||
|
y_sample2 << 0.0; // Negative class
|
||||||
|
y_train.push_back(y_sample2);
|
||||||
|
|
||||||
|
Eigen::VectorXd x_sample3(2);
|
||||||
|
x_sample3 << 0.7, -0.2;
|
||||||
|
x_train.push_back(x_sample3);
|
||||||
|
|
||||||
|
Eigen::VectorXd y_sample3(1);
|
||||||
|
y_sample3 << 1.0; // Positive class
|
||||||
|
y_train.push_back(y_sample3);
|
||||||
|
|
||||||
|
Eigen::VectorXd x_sample4(2);
|
||||||
|
x_sample4 << -0.8, -0.5;
|
||||||
|
x_train.push_back(x_sample4);
|
||||||
|
|
||||||
|
Eigen::VectorXd y_sample4(1);
|
||||||
|
y_sample4 << 0.0; // Negative class
|
||||||
|
y_train.push_back(y_sample4);
|
||||||
|
|
||||||
|
Eigen::VectorXd x_sample5(2);
|
||||||
|
x_sample5 << 0.9, 0.1;
|
||||||
|
x_train.push_back(x_sample5);
|
||||||
|
|
||||||
|
Eigen::VectorXd y_sample5(1);
|
||||||
|
y_sample5 << 1.0; // Positive class
|
||||||
|
y_train.push_back(y_sample5);
|
||||||
|
|
||||||
|
Eigen::VectorXd x_sample6(2);
|
||||||
|
x_sample6 << -0.3, 0.6;
|
||||||
|
x_train.push_back(x_sample6);
|
||||||
|
|
||||||
|
Eigen::VectorXd y_sample6(1);
|
||||||
|
y_sample6 << 0.0; // Negative class
|
||||||
|
y_train.push_back(y_sample6);
|
||||||
|
|
||||||
|
Eigen::VectorXd x_sample7(2);
|
||||||
|
x_sample7 << 0.5, -0.7;
|
||||||
|
x_train.push_back(x_sample7);
|
||||||
|
|
||||||
|
Eigen::VectorXd y_sample7(1);
|
||||||
|
y_sample7 << 1.0; // Positive class
|
||||||
|
y_train.push_back(y_sample7);
|
||||||
|
|
||||||
|
Eigen::VectorXd x_sample8(2);
|
||||||
|
x_sample8 << -0.1, 0.4;
|
||||||
|
x_train.push_back(x_sample8);
|
||||||
|
|
||||||
|
Eigen::VectorXd y_sample8(1);
|
||||||
|
y_sample8 << 0.0; // Negative class
|
||||||
|
y_train.push_back(y_sample8);
|
||||||
|
|
||||||
|
Eigen::VectorXd x_sample9(2);
|
||||||
|
x_sample9 << 0.6, 0.0;
|
||||||
|
x_train.push_back(x_sample9);
|
||||||
|
|
||||||
|
Eigen::VectorXd y_sample9(1);
|
||||||
|
y_sample9 << 1.0; // Positive class
|
||||||
|
y_train.push_back(y_sample9);
|
||||||
|
|
||||||
|
Eigen::VectorXd x_sample10(2);
|
||||||
|
x_sample10 << -0.6, -0.3;
|
||||||
|
x_train.push_back(x_sample10);
|
||||||
|
|
||||||
|
Eigen::VectorXd y_sample10(1);
|
||||||
|
y_sample10 << 0.0; // Negative class
|
||||||
|
y_train.push_back(y_sample10);
|
||||||
|
|
||||||
|
auto loss_function = binary_cross_entropy;
|
||||||
|
auto loss_prime_function = binary_cross_entropy_prime;
|
||||||
|
|
||||||
|
// Train
|
||||||
|
int epochs = 1000;
|
||||||
|
double learning_rate = 0.01;
|
||||||
|
train(network, loss_function, loss_prime_function, x_train, y_train, epochs, learning_rate, true);
|
||||||
|
|
||||||
|
//* Use the trained model for predictions
|
||||||
|
Eigen::VectorXd input_to_predict;
|
||||||
|
Eigen::VectorXd predicted_output = predict(network, input_to_predict);
|
||||||
|
|
||||||
|
std::cout << "Predicted Output: " << predicted_output.transpose() << std::endl;
|
||||||
|
|
||||||
|
// ! Clean up: Delete the layers in the network
|
||||||
|
for (auto layer : network) {
|
||||||
|
delete layer;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user