diff --git a/CMakeLists.txt b/CMakeLists.txt index ed96a51..3e9bf71 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,11 +1,17 @@ cmake_minimum_required(VERSION 3.14) -project(NeuralNetworks CXX) +project(cenet CXX) set(CMAKE_CXX_STANDARD 20) -add_executable(xor src/xor.cpp) -target_include_directories(xor PRIVATE include/) +add_library(cenet INTERFACE + include/cenet/mat.hpp + include/cenet/tensor.hpp + include/cenet/nn.hpp) +target_include_directories(cenet INTERFACE include/) -add_executable(mnist src/mnist.cpp) -target_include_directories(mnist PRIVATE include/) +add_executable(xor example/xor.cpp) +target_link_libraries(xor PRIVATE cenet) + +add_executable(mnist example/mnist.cpp) +target_link_libraries(mnist PRIVATE cenet) diff --git a/src/mnist.cpp b/example/mnist.cpp similarity index 92% rename from src/mnist.cpp rename to example/mnist.cpp index 8b1c3f9..4886001 100644 --- a/src/mnist.cpp +++ b/example/mnist.cpp @@ -1,9 +1,7 @@ -#include "mat.hpp" -#include "nn.hpp" -#include "tensor.hpp" - #include #include +#include +#include #include #include #include @@ -14,28 +12,28 @@ #include #include -using network_type = nn, - conv2d_layer<8, 3, 1, 1>, - relu_layer, - maxpool_layer<>, - conv2d_layer<16, 3, 1, 1>, - relu_layer, - maxpool_layer<>, - flatten_layer, - dense_layer<10>, - sigmoid_layer>; +using network_type = cenet::nn, + cenet::conv2d_layer<8, 3, 1, 1>, + cenet::relu_layer, + cenet::maxpool_layer<>, + cenet::conv2d_layer<16, 3, 1, 1>, + cenet::relu_layer, + cenet::maxpool_layer<>, + cenet::flatten_layer, + cenet::dense_layer<10>, + cenet::sigmoid_layer>; static std::uniform_real_distribution<> dist(-1, 1); struct data { - tensor xs; - mat<1, 10> ys; + cenet::tensor xs; + cenet::mat<1, 10> ys; }; using dataset = std::vector; template -static std::size_t col_largest_idx(const mat_col& col) { +static std::size_t col_largest_idx(const cenet::mat_col& col) { std::size_t maxIdx = 0; std::remove_cv_t max = col[0]; for (std::size_t i = 1; i < N; ++i) { diff --git a/src/xor.cpp b/example/xor.cpp similarity index 88% rename from src/xor.cpp rename to example/xor.cpp index 8c33145..69c642e 100644 --- a/src/xor.cpp +++ b/example/xor.cpp @@ -1,5 +1,4 @@ -#include "nn.hpp" - +#include #include #include #include @@ -21,7 +20,11 @@ int main(void) { }; // network net; - using network_type = nn, dense_layer<2>, sigmoid_layer, dense_layer<1>, sigmoid_layer>; + using network_type = cenet::nn, + cenet::dense_layer<2>, + cenet::sigmoid_layer, + cenet::dense_layer<1>, + cenet::sigmoid_layer>; network_type xorNet; static constexpr const char* NETWORK_PATH = "./xor.nn"; @@ -38,7 +41,7 @@ int main(void) { while (gen <= 10'000'000) { for (std::size_t i = 0; i < 4; ++i) { - mat<1, 2> xs; + cenet::mat<1, 2> xs; xs[0][0] = static_cast(i & 1); xs[0][1] = static_cast(i > 1); @@ -48,7 +51,7 @@ int main(void) { double acc = 0.0; for (std::size_t i = 0; i < 4; ++i) { - mat<1, 2> xs; + cenet::mat<1, 2> xs; xs[0][0] = static_cast(i & 1); xs[0][1] = static_cast(i > 1); diff --git a/include/mat.hpp b/include/cenet/mat.hpp similarity index 98% rename from include/mat.hpp rename to include/cenet/mat.hpp index 98a8e8e..833c155 100644 --- a/include/mat.hpp +++ b/include/cenet/mat.hpp @@ -1,11 +1,13 @@ -#ifndef MAT_HPP -#define MAT_HPP +#ifndef CENET_MAT_HPP +#define CENET_MAT_HPP #include #include #include #include +namespace cenet { + template = 1)>> class mat_col { template @@ -289,4 +291,6 @@ mat operator/(T lhs, const mat& rhs) { return res; } -#endif // MAT_HPP +} // namespace cenet + +#endif // CENET_MAT_HPP diff --git a/include/nn.hpp b/include/cenet/nn.hpp similarity index 99% rename from include/nn.hpp rename to include/cenet/nn.hpp index 86552af..519f83b 100644 --- a/include/nn.hpp +++ b/include/cenet/nn.hpp @@ -1,5 +1,5 @@ -#ifndef NN_HPP -#define NN_HPP +#ifndef CENET_NN_HPP +#define CENET_NN_HPP #include "mat.hpp" #include "tensor.hpp" @@ -13,6 +13,8 @@ #include #include +namespace cenet { + struct layer_input { static constexpr std::uint32_t DENSE_TAG = 0x444E5345; static constexpr std::uint32_t TENSOR_TAG = 0x544E5352; @@ -719,4 +721,6 @@ private: layer_types m_layers; }; -#endif // NN_HPP +} // namespace cenet + +#endif // CENET_NN_HPP diff --git a/include/tensor.hpp b/include/cenet/tensor.hpp similarity index 97% rename from include/tensor.hpp rename to include/cenet/tensor.hpp index 3b125c4..cd82e6f 100644 --- a/include/tensor.hpp +++ b/include/cenet/tensor.hpp @@ -1,14 +1,15 @@ -#ifndef TENSOR_HPP -#define TENSOR_HPP +#ifndef CENET_TENSOR_HPP +#define CENET_TENSOR_HPP #include #include #include -#include #include #include #include +namespace cenet { + template class tensor { public: @@ -180,4 +181,6 @@ private: T m_items[SIZE]; }; -#endif // TENSOR_HPP +} // namespace cenet + +#endif // CENET_TENSOR_HPP