2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
14 /// Computes the softmax function on some inputs, into outputs, with a shape given by tensorInfo.
15 void Softmax(const float* in, float* out, const TensorInfo& tensorInfo, float beta)
17 unsigned int numChannels = tensorInfo.GetShape()[1];
18 for (unsigned int n = 0; n < tensorInfo.GetShape()[0]; n++)
20 // Find maximum channel.
21 float max = in[n * numChannels];
22 for (unsigned int c = 1; c < numChannels; c++)
24 float val = in[n * numChannels + c];
31 // Exponentiate all values and sum.
32 std::vector<float> exponentials(numChannels);
34 for (unsigned int c = 0; c < numChannels; c++)
36 float val = in[n * numChannels + c];
37 exponentials[c] = expf((val - max) * beta);
38 sum += exponentials[c];
41 // Divide exponentials by sum to give outputs.
42 for (unsigned int c = 0; c < numChannels; c++)
44 out[n * numChannels + c] = exponentials[c] / sum;