MCPcopy Create free account
hub / github.com/alibaba/MNN / naiveSoftmax

Function naiveSoftmax

test/op/SoftmaxTest.cpp:107–128  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

105}
106
107static std::vector<float> naiveSoftmax(const float* input, const int outside, const int axis, const int inside) {
108 std::vector<float> output(outside * axis * inside, 0);
109 for(int y = 0; y < outside; y++) {
110 for(int x = 0; x < inside; x++) {
111 const float* src = input + y * axis * inside + x;
112 float* dst = (float *)(output.data()) + y * axis * inside + x;
113 float maxValue = (float)src[0];
114 for (int z=1; z<axis; ++z) {
115 maxValue = maxValue > src[z * inside] ? maxValue : src[z * inside];
116 }
117 float sumValue = 0.0;
118 for (int z=0; z<axis; ++z) {
119 sumValue = sumValue + exp((float)src[z * inside] - maxValue);
120 }
121 sumValue = 1.0 / sumValue;
122 for (int z=0; z<axis; ++z) {
123 dst[z*inside] = (exp((float)src[z * inside] - maxValue) * sumValue);
124 }
125 }
126 }
127 return output;
128}
129
130class SoftmaxTest : public MNNTestCase {
131public:

Callers 1

runMethod · 0.85

Calls 2

expFunction · 0.50
dataMethod · 0.45

Tested by

no test coverage detected