| 54 | } |
| 55 | |
| 56 | inline void CalcLogSoftmax(const TConstArrayRef<double> approx, TArrayRef<double> logSoftmax) { |
| 57 | double maxApprox = *MaxElement(approx.begin(), approx.end()); |
| 58 | for (size_t dim = 0; dim < approx.size(); ++dim) { |
| 59 | logSoftmax[dim] = approx[dim] - maxApprox; |
| 60 | } |
| 61 | NCB::FastExpWithInfInplace(logSoftmax.data(), logSoftmax.ysize()); |
| 62 | double logSumExpApprox = 0; |
| 63 | for (auto curLogSoftmax : logSoftmax) { |
| 64 | logSumExpApprox += curLogSoftmax; |
| 65 | } |
| 66 | logSumExpApprox = std::log(logSumExpApprox); |
| 67 | |
| 68 | for (size_t dim = 0; dim < approx.size(); ++dim) { |
| 69 | logSoftmax[dim] = approx[dim] - maxApprox - logSumExpApprox; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | inline void CalcLogSoftmax(const TConstArrayRef<double> approx, TVector<double>* softmax) { |
| 74 | CalcLogSoftmax(approx, *softmax); |
nothing calls this directly
no test coverage detected