From 24b138b7e03d443ccce5cca79914141b130e726d Mon Sep 17 00:00:00 2001 From: lisyarus Date: Sat, 12 Sep 2026 20:05:18 +0300 Subject: [PATCH] Add tests for ml activation derivatives --- libs/ml/tests/neural_net/gradient.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/libs/ml/tests/neural_net/gradient.cpp b/libs/ml/tests/neural_net/gradient.cpp index e289aff9..d86a6497 100644 --- a/libs/ml/tests/neural_net/gradient.cpp +++ b/libs/ml/tests/neural_net/gradient.cpp @@ -12,6 +12,27 @@ using namespace psemek::ml; using namespace psemek::random; using namespace psemek::math; +test_case(ml_neural__net_activation) +{ + generator rng; + for (std::size_t i = 0; i < activation_type_values().size(); ++i) + { + auto a = static_cast(i); + + for (std::size_t j = 0; j < 64; ++j) + { + double const eps = 1e-6; + + double x = uniform(rng, -10.0, 10.0); + + double value = activation(x, a); + double exact_derivative = activation_derivative(value, a); + double approx_derivative = (activation(x + eps, a) - value) / eps; + expect_close(exact_derivative, approx_derivative, 1e-6); + } + } +} + test_case(ml_neural__net_gradient) { generator rng;