//查找最大元素 floatmax_element(float* src, int count){ float max = src[0]; for (size_t i = 0; i < count; i++) { if(src[i] > max) max = src[i]; } return max; }
//对一行进行softmax voidsoftmax(float *src, float *dst, int count) { float max = max_element(src, count); float denominator = 0.; for (size_t i = 0; i < count; i++) { dst[i] = std::exp(src[i] - max); denominator += dst[i]; } for (size_t i = 0; i < count; i++) { dst[i] /= denominator; } }