diff --git a/CMakeLists.txt b/CMakeLists.txt index 3e9bf71..ecdd8b0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,10 @@ set(CMAKE_CXX_STANDARD 20) add_library(cenet INTERFACE include/cenet/mat.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/) add_executable(xor example/xor.cpp) diff --git a/example/mnist.cpp b/example/mnist.cpp index 4886001..36a84ff 100644 --- a/example/mnist.cpp +++ b/example/mnist.cpp @@ -1,7 +1,6 @@ #include #include -#include -#include +#include #include #include #include @@ -12,7 +11,7 @@ #include #include -using network_type = cenet::nn, +using network_type = cenet::network, cenet::conv2d_layer<8, 3, 1, 1>, cenet::relu_layer, cenet::maxpool_layer<>, diff --git a/example/xor.cpp b/example/xor.cpp index 69c642e..6205cc8 100644 --- a/example/xor.cpp +++ b/example/xor.cpp @@ -1,14 +1,9 @@ -#include -#include +#include #include #include #include #include -static double sigmoid(double input) { - return 1 / (1 + std::exp(-input)); -} - static std::uniform_real_distribution<> dist(-1, 1); int main(void) { @@ -20,7 +15,7 @@ int main(void) { }; // network net; - using network_type = cenet::nn, + using network_type = cenet::network, cenet::dense_layer<2>, cenet::sigmoid_layer, cenet::dense_layer<1>, diff --git a/include/cenet/cenet.hpp b/include/cenet/cenet.hpp new file mode 100644 index 0000000..454e52d --- /dev/null +++ b/include/cenet/cenet.hpp @@ -0,0 +1,8 @@ +#ifndef CENET_HPP +#define CENET_HPP + +#include "input.hpp" +#include "layer.hpp" +#include "network.hpp" + +#endif // CENET_HPP diff --git a/include/cenet/input.hpp b/include/cenet/input.hpp new file mode 100644 index 0000000..81005cc --- /dev/null +++ b/include/cenet/input.hpp @@ -0,0 +1,83 @@ +#ifndef CENET_INPUT_HPP +#define CENET_INPUT_HPP + +#include "mat.hpp" +#include "tensor.hpp" + +#include +#include + +namespace cenet { + +struct layer_input { + static constexpr std::uint32_t DENSE_TAG = 0x444E5345; + static constexpr std::uint32_t TENSOR_TAG = 0x544E5352; +}; + +template +class dense_input : public layer_input { + template + 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 >> + inner(Args&&... args) + : xs(std::forward(args)...) {} + + operator type&() { return xs; } + operator const type&() const { return xs; } + + type& get() { return xs; } + const type& get() const { return xs; } + + type xs; + }; +}; + +template +class tensor_input : public layer_input { + template + friend class network; + + friend class layer_info; +private: + struct inner final { + static constexpr std::uint32_t TAG = TENSOR_TAG; + + using type = tensor; + + template >> + inner(Args&&... args) + : xs(std::forward(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 +struct is_layer_input : std::false_type {}; + +template +struct is_layer_input>> : std::true_type {}; + +} // namespace detail + +} // namespace cenet + +#endif // CENET_INPUT_HPP diff --git a/include/cenet/nn.hpp b/include/cenet/layer.hpp similarity index 75% rename from include/cenet/nn.hpp rename to include/cenet/layer.hpp index 519f83b..3b0048b 100644 --- a/include/cenet/nn.hpp +++ b/include/cenet/layer.hpp @@ -1,79 +1,15 @@ -#ifndef CENET_NN_HPP -#define CENET_NN_HPP +#ifndef CENET_LAYER_HPP +#define CENET_LAYER_HPP -#include "mat.hpp" -#include "tensor.hpp" +#include "input.hpp" -#include -#include #include #include #include #include -#include -#include namespace cenet { -struct layer_input { - static constexpr std::uint32_t DENSE_TAG = 0x444E5345; - static constexpr std::uint32_t TENSOR_TAG = 0x544E5352; -}; - -template -class dense_input : public layer_input { - template - 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 >> - inner(Args&&... args) - : xs(std::forward(args)...) {} - - operator type&() { return xs; } - operator const type&() const { return xs; } - - type& get() { return xs; } - const type& get() const { return xs; } - - type xs; - }; -}; - -template -class tensor_input : public layer_input { - template - friend class nn; - - friend class layer_info; -private: - struct inner final { - static constexpr std::uint32_t TAG = TENSOR_TAG; - - using type = tensor; - - template >> - inner(Args&&... args) - : xs(std::forward(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 { protected: template @@ -93,7 +29,7 @@ protected: template class dense_layer final : public layer_info { template - friend class nn; + friend class network; private: template struct inner final { @@ -197,7 +133,7 @@ private: class sigmoid_layer final : public layer_info { template - friend class nn; + friend class network; private: template struct inner final { @@ -236,7 +172,7 @@ private: class relu_layer final : public layer_info { template - friend class nn; + friend class network; private: template struct inner final { @@ -275,7 +211,7 @@ private: template class conv2d_layer final : public layer_info { template - friend class nn; + friend class network; private: template struct inner final { @@ -453,7 +389,7 @@ private: template class maxpool_layer : public layer_info { template - friend class nn; + friend class network; private: template struct inner final { @@ -544,7 +480,7 @@ private: class flatten_layer : public layer_info { template - friend class nn; + friend class network; private: template struct inner final { @@ -587,140 +523,6 @@ private: }; }; -template -class nn { - friend class layer_info; -private: - template - struct layer_types_gen {}; - - template - struct layer_types_gen> { - using type = std::tuple...>; - }; - - using layer_types = typename layer_types_gen>::type; - using output_layer_type = std::tuple_element_t; - - template - using layer_input_t = typename std::tuple_element_t::input_type; -private: - template - auto& get_layer() { - return std::get(m_layers); - } - - template - const auto& get_layer() const { - return std::get(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().output; } -public: - void dump_network(std::ostream& os) const { dump_network(os, std::make_index_sequence{}); } -private: - template - void dump_network(std::ostream& os, std::index_sequence) const { - (get_layer().dump(os), ...); - } -public: - void load_network(std::istream& is) { load_network(is, std::make_index_sequence{}); } -private: - template - void load_network(std::istream& is, std::index_sequence) { - (get_layer().load(is), ...); - } -public: - template - void fill_random(const Dist& dist) { - fill_random(dist, std::make_index_sequence{}); - } -private: - template - void fill_random(const Dist& dist, std::index_sequence) { - (get_layer().fill_random(dist), ...); - } -public: - const output_type& feed(const input_type& input) { - feed(input); - return get_layer().output; - } -private: - template - auto feed(const input_type& input) { - if constexpr (I == 0) { - return get_layer().forward(input); - } else { - return get_layer().forward(feed(input)); - } - } -public: - struct learning_state final { - friend class nn; - - void reset() { - m_count = 0; - reset(std::make_index_sequence{}); - } - private: - template - void reset(std::index_sequence) { - (std::get(m_layers).reset(), ...); - } - private: - template - struct gen {}; - - template - struct gen> { - using type = std::tuple::gradient...>; - }; - private: - std::size_t m_count = 0; - - gen>::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(state, outputErr)); - state.m_count += 1; - } -private: - template - auto backprop_chain(learning_state& state, ErrorType error) { - if constexpr (I != 0) { - auto next = get_layer().backward(get_layer().output, std::get(state.m_layers), error); - return backprop_chain(state, next); - } else { - return error; - } - } -public: - void apply(learning_state& state, double rate) { - apply(state, rate, std::make_index_sequence{}); - } -private: - template - void apply(learning_state& state, double rate, std::index_sequence) { - (std::get(m_layers).apply_grad(std::get(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_NN_HPP +#endif // CENET_LAYER_HPP diff --git a/include/cenet/network.hpp b/include/cenet/network.hpp new file mode 100644 index 0000000..ce1db15 --- /dev/null +++ b/include/cenet/network.hpp @@ -0,0 +1,148 @@ +#ifndef CENET_NETWORK_HPP +#define CENET_NETWORK_HPP + +#include +#include +#include +#include +#include + +namespace cenet { + +template +class network { + friend class layer_info; +private: + template + struct layer_types_gen {}; + + template + struct layer_types_gen> { + using type = std::tuple...>; + }; + + using layer_types = typename layer_types_gen>::type; + using output_layer_type = std::tuple_element_t; + + template + using layer_input_t = typename std::tuple_element_t::input_type; +private: + template + auto& get_layer() { + return std::get(m_layers); + } + + template + const auto& get_layer() const { + return std::get(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().output; } +public: + void dump_network(std::ostream& os) const { dump_network(os, std::make_index_sequence{}); } +private: + template + void dump_network(std::ostream& os, std::index_sequence) const { + (get_layer().dump(os), ...); + } +public: + void load_network(std::istream& is) { load_network(is, std::make_index_sequence{}); } +private: + template + void load_network(std::istream& is, std::index_sequence) { + (get_layer().load(is), ...); + } +public: + template + void fill_random(const Dist& dist) { + fill_random(dist, std::make_index_sequence{}); + } +private: + template + void fill_random(const Dist& dist, std::index_sequence) { + (get_layer().fill_random(dist), ...); + } +public: + const output_type& feed(const input_type& input) { + feed(input); + return get_layer().output; + } +private: + template + auto feed(const input_type& input) { + if constexpr (I == 0) { + return get_layer().forward(input); + } else { + return get_layer().forward(feed(input)); + } + } +public: + struct learning_state final { + friend class network; + + void reset() { + m_count = 0; + reset(std::make_index_sequence{}); + } + private: + template + void reset(std::index_sequence) { + (std::get(m_layers).reset(), ...); + } + private: + template + struct gen {}; + + template + struct gen> { + using type = std::tuple::gradient...>; + }; + private: + std::size_t m_count = 0; + + gen>::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(state, outputErr)); + state.m_count += 1; + } +private: + template + auto backprop_chain(learning_state& state, ErrorType error) { + if constexpr (I != 0) { + auto next = get_layer().backward(get_layer().output, std::get(state.m_layers), error); + return backprop_chain(state, next); + } else { + return error; + } + } +public: + void apply(learning_state& state, double rate) { + apply(state, rate, std::make_index_sequence{}); + } +private: + template + void apply(learning_state& state, double rate, std::index_sequence) { + (std::get(m_layers).apply_grad(std::get(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