diff --git a/include/nn.hpp b/include/nn.hpp index b17bbf7..bbd39f6 100644 --- a/include/nn.hpp +++ b/include/nn.hpp @@ -14,6 +14,20 @@ template class nn { private: + template + struct gradient { + static constexpr std::size_t input_count = In; + static constexpr std::size_t output_count = Out; + + mat weights; + mat<1, Out> biases; + + void reset() { + weights = mat{}; + biases = mat<1, Out>{}; + } + }; + template struct layer { static constexpr std::size_t input_count = In; @@ -22,6 +36,11 @@ private: mat weights; mat<1, Out> biases; mat<1, Out> activations; + + void apply(const gradient& gradient, double divisor, double rate) { + weights -= ((gradient.weights / divisor) * rate); + biases -= ((gradient.biases / divisor) * rate); + } }; private: static auto make_output() { @@ -108,29 +127,55 @@ private: layer.activations[0][i] = sigmoid(layer.activations[0][i]); } } +private: + template + static auto make_gradients_tuple(std::index_sequence) { + constexpr std::array dims = { N, Layers... }; + return std::tuple(dims), std::get(dims)>...>{}; + } public: - void train(const input_type& input, const output_type& expected, double learningRate) { + 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: + std::size_t m_count = 0; + + decltype(make_gradients_tuple(std::make_index_sequence{})) m_layers; + }; +public: + void backprop(const input_type& input, const output_type& expected, learning_state& state) { feed(input); auto outputErr = (get_output() - expected).hadamard(get_output()).hadamard(1.0 - get_output()); - train_chain(input, learningRate, outputErr); + backprop_chain(input, state, outputErr); + state.m_count += 1; } private: template - void train_chain(const input_type& input, double learningRate, ErrorType error) { - auto next = train(input, learningRate, error); + void backprop_chain(const input_type& input, learning_state& state, ErrorType error) { + auto next = backprop(input, state, error); if constexpr (I > 0) { - train_chain(input, learningRate, next); + backprop_chain(input, state, next); } } template - auto train(const input_type& input, double learningRate, ErrorType prevErr) { - auto& layer = std::get(m_layers); + auto backprop(const input_type& input, learning_state& state, ErrorType prevErr) { + const auto& layer = std::get(m_layers); + auto& gradient = std::get(state.m_layers); const auto& layerInput = get_layer_input(input); - layer.weights -= learningRate * (layerInput.transpose() * prevErr); - layer.biases -= learningRate * prevErr; + gradient.weights += layerInput.transpose() * prevErr; + gradient.biases += prevErr; auto backpropErr = prevErr * layer.weights.transpose(); if constexpr (I > 0) { @@ -139,6 +184,21 @@ private: return backpropErr; } } +public: + void apply(const learning_state& state, double rate) { + apply(state, rate, std::make_index_sequence{}); + } +private: + template + void apply(const learning_state& state, double rate, std::index_sequence) { + (std::get(m_layers).apply(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: template const auto& get_layer_input(const input_type& input) {