softmax(): use std::max_element (#20306)
parent
4cce4385af
commit
bca53f337f
|
@ -1,5 +1,6 @@
|
|||
#include <assert.h>
|
||||
#include <math.h>
|
||||
#include <algorithm>
|
||||
#include "commonmodel.h"
|
||||
#include "common/clutil.h"
|
||||
#include "common/mat.h"
|
||||
|
@ -48,18 +49,10 @@ void frame_free(ModelFrame* frame) {
|
|||
}
|
||||
|
||||
void softmax(const float* input, float* output, size_t len) {
|
||||
float max_val = -FLT_MAX;
|
||||
for(int i = 0; i < len; i++) {
|
||||
const float v = input[i];
|
||||
if( v > max_val ) {
|
||||
max_val = v;
|
||||
}
|
||||
}
|
||||
|
||||
const float max_val = *std::max_element(input, input + len);
|
||||
float denominator = 0;
|
||||
for(int i = 0; i < len; i++) {
|
||||
float const v = input[i];
|
||||
float const v_exp = expf(v - max_val);
|
||||
float const v_exp = expf(input[i] - max_val);
|
||||
denominator += v_exp;
|
||||
output[i] = v_exp;
|
||||
}
|
||||
|
@ -68,7 +61,6 @@ void softmax(const float* input, float* output, size_t len) {
|
|||
for(int i = 0; i < len; i++) {
|
||||
output[i] *= inv_denominator;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
float sigmoid(float input) {
|
||||
|
|
Loading…
Reference in New Issue