perf: optimize gradients

This commit is contained in:
2026-09-18 12:13:01 +02:00
parent 894c148997
commit 5e32b6f983
2 changed files with 54 additions and 8 deletions
+3
View File
@@ -144,6 +144,9 @@ public:
const T* data() const { return m_items; }
constexpr std::size_t size() const { return M * N; }
public:
bool operator==(const mat& rhs) const { return std::memcmp(m_items, rhs.m_items, sizeof(m_items)) == 0; }
bool operator!=(const mat& rhs) const { return std::memcmp(m_items, rhs.m_items, sizeof(m_items)) != 0; }
private:
T m_items[M * N];
};
+50 -7
View File
@@ -4,6 +4,7 @@
#include "mat.hpp"
#include <array>
#include <cmath>
#include <cstddef>
#include <istream>
#include <ostream>
@@ -22,6 +23,13 @@ private:
mat<In, Out> weights;
mat<1, Out> biases;
mat<In, Out> momentumWeights;
mat<1, Out> momentumBiases;
mat<In, Out> varianceWeights;
mat<1, Out> varianceBiases;
std::size_t time = 0;
void reset() {
weights = mat<In, Out>{};
biases = mat<1, Out>{};
@@ -37,9 +45,43 @@ private:
mat<1, Out> biases;
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);
void apply(gradient<In, Out>& gradient, double divisor, double rate) {
++gradient.time;
constexpr double beta1 = 0.9;
constexpr double beta2 = 0.999;
constexpr double epsilon = 1e-8;
for (std::size_t row = 0; row < In; ++row) {
for (std::size_t col = 0; col < Out; ++col) {
double avgGrad = gradient.weights[row][col] / divisor;
gradient.momentumWeights[row][col] =
beta1 * gradient.momentumWeights[row][col] + (1.0 - beta1) * avgGrad;
gradient.varianceWeights[row][col] =
beta2 * gradient.varianceWeights[row][col] + (1.0 - beta2) * (avgGrad * avgGrad);
double momentumCorrected =
gradient.momentumWeights[row][col] / (1.0 - std::pow(beta1, gradient.time));
double varianceCorrected =
gradient.varianceWeights[row][col] / (1.0 - std::pow(beta2, gradient.time));
weights[row][col] -= (rate * momentumCorrected) / (std::sqrt(varianceCorrected) + epsilon);
}
}
for (std::size_t col = 0; col < Out; ++col) {
double avgGrad = gradient.biases[0][col] / divisor;
gradient.momentumBiases[0][col] = beta1 * gradient.momentumBiases[0][col] + (1.0 - beta1) * avgGrad;
gradient.varianceBiases[0][col] =
beta2 * gradient.varianceBiases[0][col] + (1.0 - beta2) * (avgGrad * avgGrad);
double momentumCorrected = gradient.momentumBiases[0][col] / (1.0 - std::pow(beta1, gradient.time));
double varianceCorrected = gradient.varianceBiases[0][col] / (1.0 - std::pow(beta2, gradient.time));
biases[0][col] -= (rate * momentumCorrected) / (std::sqrt(varianceCorrected) + epsilon);
}
}
};
private:
@@ -94,8 +136,9 @@ private:
template <std::size_t I, typename Dist>
void fill_random(const Dist& dist) {
auto& layer = std::get<I>(m_layers);
layer.weights = decltype(layer.weights)::random(dist);
layer.biases = decltype(layer.biases)::random(dist);
double scale = std::sqrt(2.0 / static_cast<double>(std::tuple_element_t<I, layers_type>::input_count));
layer.weights = decltype(layer.weights)::random(dist) * scale;
layer.biases = decltype(layer.biases)::random(dist) * scale;
}
public:
const output_type& feed(const input_type& input) {
@@ -185,12 +228,12 @@ private:
}
}
public:
void apply(const learning_state& state, double rate) {
void apply(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...>) {
void apply(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: