! * \brief Do inplace softmax transformation on p_rec * \param p_rec The input/output vector of the values. */
| 600 | * \param p_rec The input/output vector of the values. |
| 601 | */ |
| 602 | inline static void Softmax(std::vector<double>* p_rec) { |
| 603 | std::vector<double> &rec = *p_rec; |
| 604 | double wmax = rec[0]; |
| 605 | for (size_t i = 1; i < rec.size(); ++i) { |
| 606 | wmax = std::max(rec[i], wmax); |
| 607 | } |
| 608 | double wsum = 0.0f; |
| 609 | for (size_t i = 0; i < rec.size(); ++i) { |
| 610 | rec[i] = std::exp(rec[i] - wmax); |
| 611 | wsum += rec[i]; |
| 612 | } |
| 613 | for (size_t i = 0; i < rec.size(); ++i) { |
| 614 | rec[i] /= static_cast<double>(wsum); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | inline static void Softmax(const double* input, double* output, int len) { |
| 619 | double wmax = input[0]; |
no test coverage detected