softmax(): use std::max_element (#20306)

albatross
Dean Lee 2021-03-11 22:05:13 +08:00 committed by GitHub
parent 4cce4385af
commit bca53f337f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 11 deletions

View File

@ -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) {