telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | |
| 6 | #include "Softmax.hpp" |
| 7 | |
| 8 | #include <cmath> |
| 9 | #include <vector> |
| 10 | |
| 11 | namespace armnn |
| 12 | { |
| 13 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 14 | /// Computes the softmax function on some inputs, into outputs, with a shape given by tensorInfo. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 15 | void Softmax(const float* in, float* out, const TensorInfo& tensorInfo, float beta) |
| 16 | { |
| 17 | unsigned int numChannels = tensorInfo.GetShape()[1]; |
| 18 | for (unsigned int n = 0; n < tensorInfo.GetShape()[0]; n++) |
| 19 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 20 | // Find maximum channel. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 21 | float max = in[n * numChannels]; |
| 22 | for (unsigned int c = 1; c < numChannels; c++) |
| 23 | { |
| 24 | float val = in[n * numChannels + c]; |
| 25 | if (val > max) |
| 26 | { |
| 27 | max = val; |
| 28 | } |
| 29 | } |
| 30 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 31 | // Exponentiate all values and sum. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 32 | std::vector<float> exponentials(numChannels); |
| 33 | float sum = 0.0f; |
| 34 | for (unsigned int c = 0; c < numChannels; c++) |
| 35 | { |
| 36 | float val = in[n * numChannels + c]; |
| 37 | exponentials[c] = expf((val - max) * beta); |
| 38 | sum += exponentials[c]; |
| 39 | } |
| 40 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 41 | // Divide exponentials by sum to give outputs. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 42 | for (unsigned int c = 0; c < numChannels; c++) |
| 43 | { |
| 44 | out[n * numChannels + c] = exponentials[c] / sum; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | } //namespace armnn |