refactor: restructure the project

This commit is contained in:
2026-09-20 21:43:07 +02:00
parent 76eff9c837
commit 41cd7dbc2f
7 changed files with 257 additions and 219 deletions
+4 -1
View File
@@ -7,7 +7,10 @@ set(CMAKE_CXX_STANDARD 20)
add_library(cenet INTERFACE add_library(cenet INTERFACE
include/cenet/mat.hpp include/cenet/mat.hpp
include/cenet/tensor.hpp include/cenet/tensor.hpp
include/cenet/nn.hpp) include/cenet/input.hpp
include/cenet/layer.hpp
include/cenet/network.hpp
include/cenet/cenet.hpp)
target_include_directories(cenet INTERFACE include/) target_include_directories(cenet INTERFACE include/)
add_executable(xor example/xor.cpp) add_executable(xor example/xor.cpp)
+2 -3
View File
@@ -1,7 +1,6 @@
#include <algorithm> #include <algorithm>
#include <cassert> #include <cassert>
#include <cenet/nn.hpp> #include <cenet/cenet.hpp>
#include <cenet/tensor.hpp>
#include <chrono> #include <chrono>
#include <fstream> #include <fstream>
#include <iomanip> #include <iomanip>
@@ -12,7 +11,7 @@
#include <random> #include <random>
#include <vector> #include <vector>
using network_type = cenet::nn<cenet::tensor_input<1, 28, 28>, using network_type = cenet::network<cenet::tensor_input<1, 28, 28>,
cenet::conv2d_layer<8, 3, 1, 1>, cenet::conv2d_layer<8, 3, 1, 1>,
cenet::relu_layer, cenet::relu_layer,
cenet::maxpool_layer<>, cenet::maxpool_layer<>,
+2 -7
View File
@@ -1,14 +1,9 @@
#include <cenet/nn.hpp> #include <cenet/cenet.hpp>
#include <cmath>
#include <cstddef> #include <cstddef>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <random> #include <random>
static double sigmoid(double input) {
return 1 / (1 + std::exp(-input));
}
static std::uniform_real_distribution<> dist(-1, 1); static std::uniform_real_distribution<> dist(-1, 1);
int main(void) { int main(void) {
@@ -20,7 +15,7 @@ int main(void) {
}; };
// network net; // network net;
using network_type = cenet::nn<cenet::dense_input<2>, using network_type = cenet::network<cenet::dense_input<2>,
cenet::dense_layer<2>, cenet::dense_layer<2>,
cenet::sigmoid_layer, cenet::sigmoid_layer,
cenet::dense_layer<1>, cenet::dense_layer<1>,
+8
View File
@@ -0,0 +1,8 @@
#ifndef CENET_HPP
#define CENET_HPP
#include "input.hpp"
#include "layer.hpp"
#include "network.hpp"
#endif // CENET_HPP
+83
View File
@@ -0,0 +1,83 @@
#ifndef CENET_INPUT_HPP
#define CENET_INPUT_HPP
#include "mat.hpp"
#include "tensor.hpp"
#include <cstdint>
#include <type_traits>
namespace cenet {
struct layer_input {
static constexpr std::uint32_t DENSE_TAG = 0x444E5345;
static constexpr std::uint32_t TENSOR_TAG = 0x544E5352;
};
template <std::size_t Features>
class dense_input : public layer_input {
template <typename Input, typename... Layers>
friend class network;
friend class layer_info;
private:
struct inner final {
static constexpr std::uint32_t TAG = DENSE_TAG;
static constexpr std::size_t FEATURES = Features;
using type = mat<1, FEATURES>;
template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<type, Args...>>>
inner(Args&&... args)
: xs(std::forward<Args>(args)...) {}
operator type&() { return xs; }
operator const type&() const { return xs; }
type& get() { return xs; }
const type& get() const { return xs; }
type xs;
};
};
template <std::size_t... Dims>
class tensor_input : public layer_input {
template <typename Input, typename... Layers>
friend class network;
friend class layer_info;
private:
struct inner final {
static constexpr std::uint32_t TAG = TENSOR_TAG;
using type = tensor<double, Dims...>;
template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<type, Args...>>>
inner(Args&&... args)
: xs(std::forward<Args>(args)...) {}
operator type&() { return xs; }
operator const type&() const { return xs; }
type& get() { return xs; }
const type& get() const { return xs; }
type xs;
};
};
namespace detail {
template <typename T, typename = void>
struct is_layer_input : std::false_type {};
template <typename T>
struct is_layer_input<T, std::enable_if_t<std::is_base_of_v<layer_input, T>>> : std::true_type {};
} // namespace detail
} // namespace cenet
#endif // CENET_INPUT_HPP
+10 -208
View File
@@ -1,79 +1,15 @@
#ifndef CENET_NN_HPP #ifndef CENET_LAYER_HPP
#define CENET_NN_HPP #define CENET_LAYER_HPP
#include "mat.hpp" #include "input.hpp"
#include "tensor.hpp"
#include <algorithm>
#include <cmath>
#include <cstddef> #include <cstddef>
#include <istream> #include <istream>
#include <ostream> #include <ostream>
#include <tuple> #include <tuple>
#include <type_traits>
#include <utility>
namespace cenet { namespace cenet {
struct layer_input {
static constexpr std::uint32_t DENSE_TAG = 0x444E5345;
static constexpr std::uint32_t TENSOR_TAG = 0x544E5352;
};
template <std::size_t Features>
class dense_input : public layer_input {
template <typename Input, typename... Layers>
friend class nn;
friend class layer_info;
private:
struct inner final {
static constexpr std::uint32_t TAG = DENSE_TAG;
static constexpr std::size_t FEATURES = Features;
using type = mat<1, FEATURES>;
template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<type, Args...>>>
inner(Args&&... args)
: xs(std::forward<Args>(args)...) {}
operator type&() { return xs; }
operator const type&() const { return xs; }
type& get() { return xs; }
const type& get() const { return xs; }
type xs;
};
};
template <std::size_t... Dims>
class tensor_input : public layer_input {
template <typename Input, typename... Layers>
friend class nn;
friend class layer_info;
private:
struct inner final {
static constexpr std::uint32_t TAG = TENSOR_TAG;
using type = tensor<double, Dims...>;
template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<type, Args...>>>
inner(Args&&... args)
: xs(std::forward<Args>(args)...) {}
operator type&() { return xs; }
operator const type&() const { return xs; }
type& get() { return xs; }
const type& get() const { return xs; }
type xs;
};
};
struct layer_info { struct layer_info {
protected: protected:
template <class NN, std::size_t I> template <class NN, std::size_t I>
@@ -93,7 +29,7 @@ protected:
template <std::size_t Features> template <std::size_t Features>
class dense_layer final : public layer_info { class dense_layer final : public layer_info {
template <typename Input, typename... Layers> template <typename Input, typename... Layers>
friend class nn; friend class network;
private: private:
template <class NN, std::size_t I> template <class NN, std::size_t I>
struct inner final { struct inner final {
@@ -197,7 +133,7 @@ private:
class sigmoid_layer final : public layer_info { class sigmoid_layer final : public layer_info {
template <typename Input, typename... Layers> template <typename Input, typename... Layers>
friend class nn; friend class network;
private: private:
template <class NN, std::size_t I> template <class NN, std::size_t I>
struct inner final { struct inner final {
@@ -236,7 +172,7 @@ private:
class relu_layer final : public layer_info { class relu_layer final : public layer_info {
template <typename Input, typename... Layers> template <typename Input, typename... Layers>
friend class nn; friend class network;
private: private:
template <class NN, std::size_t I> template <class NN, std::size_t I>
struct inner final { struct inner final {
@@ -275,7 +211,7 @@ private:
template <std::size_t OutChannels, std::size_t KernelSize, std::size_t Stride = 1, std::size_t Padding = 0> template <std::size_t OutChannels, std::size_t KernelSize, std::size_t Stride = 1, std::size_t Padding = 0>
class conv2d_layer final : public layer_info { class conv2d_layer final : public layer_info {
template <typename Input, typename... Layers> template <typename Input, typename... Layers>
friend class nn; friend class network;
private: private:
template <class NN, std::size_t I> template <class NN, std::size_t I>
struct inner final { struct inner final {
@@ -453,7 +389,7 @@ private:
template <std::size_t PoolSize = 2, std::size_t Stride = 2> template <std::size_t PoolSize = 2, std::size_t Stride = 2>
class maxpool_layer : public layer_info { class maxpool_layer : public layer_info {
template <typename Input, typename... Layers> template <typename Input, typename... Layers>
friend class nn; friend class network;
private: private:
template <class NN, std::size_t I> template <class NN, std::size_t I>
struct inner final { struct inner final {
@@ -544,7 +480,7 @@ private:
class flatten_layer : public layer_info { class flatten_layer : public layer_info {
template <typename Input, typename... Layers> template <typename Input, typename... Layers>
friend class nn; friend class network;
private: private:
template <class NN, std::size_t I> template <class NN, std::size_t I>
struct inner final { struct inner final {
@@ -587,140 +523,6 @@ private:
}; };
}; };
template <typename Input, typename... Layers>
class nn {
friend class layer_info;
private:
template <typename Seq>
struct layer_types_gen {};
template <std::size_t... I>
struct layer_types_gen<std::index_sequence<I...>> {
using type = std::tuple<typename Layers::template inner<nn, I>...>;
};
using layer_types = typename layer_types_gen<std::make_index_sequence<sizeof...(Layers)>>::type;
using output_layer_type = std::tuple_element_t<sizeof...(Layers) - 1, layer_types>;
template <std::size_t I>
using layer_input_t = typename std::tuple_element_t<I, layer_types>::input_type;
private:
template <std::size_t I>
auto& get_layer() {
return std::get<I>(m_layers);
}
template <std::size_t I>
const auto& get_layer() const {
return std::get<I>(m_layers);
}
public:
using input_type = Input::inner;
using output_type = output_layer_type::output_type;
public:
const output_type& get_output() const { return get_layer<sizeof...(Layers) - 1>().output; }
public:
void dump_network(std::ostream& os) const { dump_network(os, std::make_index_sequence<sizeof...(Layers)>{}); }
private:
template <std::size_t... I>
void dump_network(std::ostream& os, std::index_sequence<I...>) const {
(get_layer<I>().dump(os), ...);
}
public:
void load_network(std::istream& is) { load_network(is, std::make_index_sequence<sizeof...(Layers)>{}); }
private:
template <std::size_t... I>
void load_network(std::istream& is, std::index_sequence<I...>) {
(get_layer<I>().load(is), ...);
}
public:
template <typename Dist>
void fill_random(const Dist& dist) {
fill_random(dist, std::make_index_sequence<sizeof...(Layers)>{});
}
private:
template <typename Dist, std::size_t... I>
void fill_random(const Dist& dist, std::index_sequence<I...>) {
(get_layer<I>().fill_random(dist), ...);
}
public:
const output_type& feed(const input_type& input) {
feed<sizeof...(Layers) - 1>(input);
return get_layer<sizeof...(Layers) - 1>().output;
}
private:
template <std::size_t I>
auto feed(const input_type& input) {
if constexpr (I == 0) {
return get_layer<I>().forward(input);
} else {
return get_layer<I>().forward(feed<I - 1>(input));
}
}
public:
struct learning_state final {
friend class nn;
void reset() {
m_count = 0;
reset(std::make_index_sequence<sizeof...(Layers)>{});
}
private:
template <std::size_t... I>
void reset(std::index_sequence<I...>) {
(std::get<I>(m_layers).reset(), ...);
}
private:
template <typename Seq>
struct gen {};
template <std::size_t... I>
struct gen<std::index_sequence<I...>> {
using type = std::tuple<typename Layers::template inner<nn, I>::gradient...>;
};
private:
std::size_t m_count = 0;
gen<std::make_index_sequence<sizeof...(Layers)>>::type m_layers;
};
public:
void backprop(const input_type& input, const output_type& expected, learning_state& state) {
feed(input);
auto outputErr = (get_output().get() - expected).hadamard(get_output()).hadamard(1.0 - get_output().get());
get_layer<0>().backward(input,
std::get<0>(state.m_layers),
backprop_chain<sizeof...(Layers) - 1>(state, outputErr));
state.m_count += 1;
}
private:
template <std::size_t I, typename ErrorType>
auto backprop_chain(learning_state& state, ErrorType error) {
if constexpr (I != 0) {
auto next = get_layer<I>().backward(get_layer<I - 1>().output, std::get<I>(state.m_layers), error);
return backprop_chain<I - 1>(state, next);
} else {
return error;
}
}
public:
void apply(learning_state& state, double rate) {
apply(state, rate, std::make_index_sequence<sizeof...(Layers)>{});
}
private:
template <std::size_t... I>
void apply(learning_state& state, double rate, std::index_sequence<I...>) {
(std::get<I>(m_layers).apply_grad(std::get<I>(state.m_layers), state.m_count, rate), ...);
}
public:
void train(const input_type& input, const output_type& expected, double learningRate) {
learning_state state;
backprop(input, expected, state);
apply(state, learningRate);
}
private:
layer_types m_layers;
};
} // namespace cenet } // namespace cenet
#endif // CENET_NN_HPP #endif // CENET_LAYER_HPP
+148
View File
@@ -0,0 +1,148 @@
#ifndef CENET_NETWORK_HPP
#define CENET_NETWORK_HPP
#include <cstddef>
#include <istream>
#include <ostream>
#include <tuple>
#include <utility>
namespace cenet {
template <typename Input, typename... Layers>
class network {
friend class layer_info;
private:
template <typename Seq>
struct layer_types_gen {};
template <std::size_t... I>
struct layer_types_gen<std::index_sequence<I...>> {
using type = std::tuple<typename Layers::template inner<network, I>...>;
};
using layer_types = typename layer_types_gen<std::make_index_sequence<sizeof...(Layers)>>::type;
using output_layer_type = std::tuple_element_t<sizeof...(Layers) - 1, layer_types>;
template <std::size_t I>
using layer_input_t = typename std::tuple_element_t<I, layer_types>::input_type;
private:
template <std::size_t I>
auto& get_layer() {
return std::get<I>(m_layers);
}
template <std::size_t I>
const auto& get_layer() const {
return std::get<I>(m_layers);
}
public:
using input_type = Input::inner;
using output_type = output_layer_type::output_type;
public:
const output_type& get_output() const { return get_layer<sizeof...(Layers) - 1>().output; }
public:
void dump_network(std::ostream& os) const { dump_network(os, std::make_index_sequence<sizeof...(Layers)>{}); }
private:
template <std::size_t... I>
void dump_network(std::ostream& os, std::index_sequence<I...>) const {
(get_layer<I>().dump(os), ...);
}
public:
void load_network(std::istream& is) { load_network(is, std::make_index_sequence<sizeof...(Layers)>{}); }
private:
template <std::size_t... I>
void load_network(std::istream& is, std::index_sequence<I...>) {
(get_layer<I>().load(is), ...);
}
public:
template <typename Dist>
void fill_random(const Dist& dist) {
fill_random(dist, std::make_index_sequence<sizeof...(Layers)>{});
}
private:
template <typename Dist, std::size_t... I>
void fill_random(const Dist& dist, std::index_sequence<I...>) {
(get_layer<I>().fill_random(dist), ...);
}
public:
const output_type& feed(const input_type& input) {
feed<sizeof...(Layers) - 1>(input);
return get_layer<sizeof...(Layers) - 1>().output;
}
private:
template <std::size_t I>
auto feed(const input_type& input) {
if constexpr (I == 0) {
return get_layer<I>().forward(input);
} else {
return get_layer<I>().forward(feed<I - 1>(input));
}
}
public:
struct learning_state final {
friend class network;
void reset() {
m_count = 0;
reset(std::make_index_sequence<sizeof...(Layers)>{});
}
private:
template <std::size_t... I>
void reset(std::index_sequence<I...>) {
(std::get<I>(m_layers).reset(), ...);
}
private:
template <typename Seq>
struct gen {};
template <std::size_t... I>
struct gen<std::index_sequence<I...>> {
using type = std::tuple<typename Layers::template inner<network, I>::gradient...>;
};
private:
std::size_t m_count = 0;
gen<std::make_index_sequence<sizeof...(Layers)>>::type m_layers;
};
public:
void backprop(const input_type& input, const output_type& expected, learning_state& state) {
feed(input);
auto outputErr = (get_output().get() - expected).hadamard(get_output()).hadamard(1.0 - get_output().get());
get_layer<0>().backward(input,
std::get<0>(state.m_layers),
backprop_chain<sizeof...(Layers) - 1>(state, outputErr));
state.m_count += 1;
}
private:
template <std::size_t I, typename ErrorType>
auto backprop_chain(learning_state& state, ErrorType error) {
if constexpr (I != 0) {
auto next = get_layer<I>().backward(get_layer<I - 1>().output, std::get<I>(state.m_layers), error);
return backprop_chain<I - 1>(state, next);
} else {
return error;
}
}
public:
void apply(learning_state& state, double rate) {
apply(state, rate, std::make_index_sequence<sizeof...(Layers)>{});
}
private:
template <std::size_t... I>
void apply(learning_state& state, double rate, std::index_sequence<I...>) {
(std::get<I>(m_layers).apply_grad(std::get<I>(state.m_layers), state.m_count, rate), ...);
}
public:
void train(const input_type& input, const output_type& expected, double learningRate) {
learning_state state;
backprop(input, expected, state);
apply(state, learningRate);
}
private:
layer_types m_layers;
};
} // namespace cenet
#endif // CENET_NETWORK_HPP