diff options
Diffstat (limited to 'source/application/main/PlatformMath.cc')
-rw-r--r-- | source/application/main/PlatformMath.cc | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/source/application/main/PlatformMath.cc b/source/application/main/PlatformMath.cc index 0b8882a..26b4b72 100644 --- a/source/application/main/PlatformMath.cc +++ b/source/application/main/PlatformMath.cc @@ -15,6 +15,8 @@ * limitations under the License. */ #include "PlatformMath.hpp" +#include <algorithm> +#include <numeric> #if 0 == ARM_DSP_AVAILABLE #include <cmath> @@ -290,6 +292,24 @@ namespace math { return true; } + void MathUtils::SoftmaxF32(std::vector<float>& vec) + { + /* Fix for numerical stability and apply exp. */ + auto start = vec.begin(); + auto end = vec.end(); + + float maxValue = *std::max_element(start, end); + for (auto it = start; it != end; ++it) { + *it = std::exp((*it) - maxValue); + } + + float sumExp = std::accumulate(start, end, 0.0f); + + for (auto it = start; it != end; ++it) { + *it = (*it)/sumExp; + } + } + } /* namespace math */ } /* namespace app */ } /* namespace arm */ |