feat: introduce batching ability

This commit is contained in:
2026-09-17 23:13:54 +02:00
parent fe2eeee7a8
commit 894c148997
+69 -9
View File
@@ -14,6 +14,20 @@
template <std::size_t N, std::size_t... Layers> template <std::size_t N, std::size_t... Layers>
class nn { class nn {
private: private:
template <std::size_t In, std::size_t Out>
struct gradient {
static constexpr std::size_t input_count = In;
static constexpr std::size_t output_count = Out;
mat<In, Out> weights;
mat<1, Out> biases;
void reset() {
weights = mat<In, Out>{};
biases = mat<1, Out>{};
}
};
template <std::size_t In, std::size_t Out> template <std::size_t In, std::size_t Out>
struct layer { struct layer {
static constexpr std::size_t input_count = In; static constexpr std::size_t input_count = In;
@@ -22,6 +36,11 @@ private:
mat<In, Out> weights; mat<In, Out> weights;
mat<1, Out> biases; mat<1, Out> biases;
mat<1, Out> activations; mat<1, Out> activations;
void apply(const gradient<In, Out>& gradient, double divisor, double rate) {
weights -= ((gradient.weights / divisor) * rate);
biases -= ((gradient.biases / divisor) * rate);
}
}; };
private: private:
static auto make_output() { static auto make_output() {
@@ -108,29 +127,55 @@ private:
layer.activations[0][i] = sigmoid(layer.activations[0][i]); layer.activations[0][i] = sigmoid(layer.activations[0][i]);
} }
} }
private:
template <std::size_t... I>
static auto make_gradients_tuple(std::index_sequence<I...>) {
constexpr std::array<std::size_t, sizeof...(Layers) + 1> dims = { N, Layers... };
return std::tuple<gradient<std::get<I>(dims), std::get<I + 1>(dims)>...>{};
}
public: 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<sizeof...(Layers)>{});
}
private:
template <std::size_t... I>
void reset(std::index_sequence<I...>) {
(std::get<I>(m_layers).reset(), ...);
}
private:
std::size_t m_count = 0;
decltype(make_gradients_tuple(std::make_index_sequence<sizeof...(Layers)>{})) m_layers;
};
public:
void backprop(const input_type& input, const output_type& expected, learning_state& state) {
feed(input); feed(input);
auto outputErr = (get_output() - expected).hadamard(get_output()).hadamard(1.0 - get_output()); auto outputErr = (get_output() - expected).hadamard(get_output()).hadamard(1.0 - get_output());
train_chain<sizeof...(Layers) - 1>(input, learningRate, outputErr); backprop_chain<sizeof...(Layers) - 1>(input, state, outputErr);
state.m_count += 1;
} }
private: private:
template <std::size_t I, typename ErrorType> template <std::size_t I, typename ErrorType>
void train_chain(const input_type& input, double learningRate, ErrorType error) { void backprop_chain(const input_type& input, learning_state& state, ErrorType error) {
auto next = train<I>(input, learningRate, error); auto next = backprop<I>(input, state, error);
if constexpr (I > 0) { if constexpr (I > 0) {
train_chain<I - 1>(input, learningRate, next); backprop_chain<I - 1>(input, state, next);
} }
} }
template <std::size_t I, typename ErrorType> template <std::size_t I, typename ErrorType>
auto train(const input_type& input, double learningRate, ErrorType prevErr) { auto backprop(const input_type& input, learning_state& state, ErrorType prevErr) {
auto& layer = std::get<I>(m_layers); const auto& layer = std::get<I>(m_layers);
auto& gradient = std::get<I>(state.m_layers);
const auto& layerInput = get_layer_input<I>(input); const auto& layerInput = get_layer_input<I>(input);
layer.weights -= learningRate * (layerInput.transpose() * prevErr); gradient.weights += layerInput.transpose() * prevErr;
layer.biases -= learningRate * prevErr; gradient.biases += prevErr;
auto backpropErr = prevErr * layer.weights.transpose(); auto backpropErr = prevErr * layer.weights.transpose();
if constexpr (I > 0) { if constexpr (I > 0) {
@@ -139,6 +184,21 @@ private:
return backpropErr; return backpropErr;
} }
} }
public:
void apply(const learning_state& state, double rate) {
apply(state, rate, std::make_index_sequence<sizeof...(Layers)>{});
}
private:
template <std::size_t... I>
void apply(const learning_state& state, double rate, std::index_sequence<I...>) {
(std::get<I>(m_layers).apply(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: private:
template <std::size_t I> template <std::size_t I>
const auto& get_layer_input(const input_type& input) { const auto& get_layer_input(const input_type& input) {