Add softplus and squareplus activation functions to ml library (idk why they're just neat lol)
Some checks failed
Run tests / Run tests (push) Failing after 8m18s

This commit is contained in:
Nikita Lisitsa 2026-09-06 14:40:55 +03:00
parent c1c04fd139
commit 6201361b8c

View file

@ -4,7 +4,6 @@
#include <cmath> #include <cmath>
#include <algorithm> #include <algorithm>
#include <exception>
#include <cstdint> #include <cstdint>
namespace psemek::ml namespace psemek::ml
@ -18,6 +17,8 @@ namespace psemek::ml
(sigmoid) (sigmoid)
(tanh) (tanh)
(relu) (relu)
(softplus)
(squareplus)
) )
template <typename T> template <typename T>
@ -27,14 +28,21 @@ namespace psemek::ml
case activation_type::id: case activation_type::id:
return x; return x;
case activation_type::sigmoid: case activation_type::sigmoid:
return 1.f / (1.f + std::exp(-x)); return T{1} / (T{1} + std::exp(-x));
case activation_type::tanh: case activation_type::tanh:
return 2.f / (1.f + std::exp(- 2.f * x)) - 1.f; return T{2} / (T{1} + std::exp(- T{2} * x)) - T{1};
case activation_type::relu: case activation_type::relu:
return std::max(T{0}, x); return std::max(T{0}, x);
default: case activation_type::softplus:
throw util::unknown_enum_value_exception{type}; return (x < T{0})
? std::log(T{1} + std::exp(x))
: x + std::log(T{1} + std::exp(-1));
case activation_type::squareplus:
return (x < T{0})
? T{2} / (std::sqrt(x * x + T{4}) - x)
: (x + std::sqrt(x * x + T{4})) / T{2};
} }
throw util::unknown_enum_value_exception{type};
} }
template <typename T> template <typename T>
@ -49,9 +57,15 @@ namespace psemek::ml
return T{1} - value * value; return T{1} - value * value;
case activation_type::relu: case activation_type::relu:
return value == T{0} ? T{0} : T{1}; return value == T{0} ? T{0} : T{1};
default: case activation_type::softplus:
throw util::unknown_enum_value_exception{type}; return T{1} - std::exp(- value);
case activation_type::squareplus:
{
auto v2 = value * value;
return v2 / (T{1} + v2);
}
} }
throw util::unknown_enum_value_exception{type};
} }
} }