From ed9b23ad864bcb0428c2713434be364d02200583 Mon Sep 17 00:00:00 2001 From: Relintai Date: Thu, 10 Mar 2022 19:20:29 +0100 Subject: [PATCH] Added softmax to Math. --- core/math/math.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/core/math/math.h b/core/math/math.h index 2abdf4a..6ab668a 100644 --- a/core/math/math.h +++ b/core/math/math.h @@ -9,6 +9,8 @@ #include #include +#include "core/containers/vector.h" + #define MATH_PI 3.1415926535897932384626433833 #define EPSILON 0.00001 @@ -343,6 +345,22 @@ public: static _ALWAYS_INLINE_ double linear_derived(const double x) { return 1; } + + static _ALWAYS_INLINE_ void softmax(Vector *data_in_out) { + double *dwp = data_in_out->dataw(); + int s = data_in_out->size(); + + double acc = 0; + for (int i = 0; i < s; ++i) { + double e = exp(dwp[i]); + dwp[i] = e; + acc += e; + } + + for (int i = 0; i < s; ++i) { + dwp[i] /= acc; + } + } }; #ifndef ABS