TFLite: reduced duplicated calculation of exp in softmax.h (float)

This commit is contained in:
danielyou0230 2020-08-13 11:27:14 -07:00
parent c979f5a424
commit a8d859546f

View File

@ -49,15 +49,15 @@ inline void Softmax(const SoftmaxParams& params,
// Compute sum.
float sum = 0.f;
for (int c = 0; c < depth; ++c) {
sum += std::exp((input_data[i * depth + c] - max) *
static_cast<float>(params.beta));
const float exp_c = std::exp((input_data[i * depth + c] - max) *
static_cast<float>(params.beta));
output_data[i * depth + c] = exp_c;
sum += exp_c;
}
// Compute result.
for (int c = 0; c < depth; ++c) {
output_data[i * depth + c] = std::exp((input_data[i * depth + c] - max) *
static_cast<float>(params.beta)) /
sum;
output_data[i * depth + c] = output_data[i * depth + c] / sum;
}
}
}