diff --git a/src/mlpack/methods/ann/layer/log_softmax_impl.hpp b/src/mlpack/methods/ann/layer/log_softmax_impl.hpp index 63f0b59e5f..bf432513de 100644 --- a/src/mlpack/methods/ann/layer/log_softmax_impl.hpp +++ b/src/mlpack/methods/ann/layer/log_softmax_impl.hpp @@ -68,32 +68,31 @@ void LogSoftMaxType::Forward(const MatType& input, MatType& output) MatType maxInput = repmat(max(input), input.n_rows, 1); output = (maxInput - input); - //// Approximation of the base-e exponential function. The acuracy however is - //// about 0.00001 lower as using exp. Credits go to Leon Bottou. - //output.transform([](double x) - //{ - ////! Fast approximation of exp(-x) for x positive. - //static constexpr double A0 = 1.0; - //static constexpr double A1 = 0.125; - //static constexpr double A2 = 0.0078125; - //static constexpr double A3 = 0.00032552083; - //static constexpr double A4 = 1.0172526e-5; + // Approximation of the base-e exponential function. The acuracy however is + // about 0.00001 lower as using exp. Credits go to Leon Bottou. + output.transform([](double x) + { + //! Fast approximation of exp(-x) for x positive. + static constexpr double A0 = 1.0; + static constexpr double A1 = 0.125; + static constexpr double A2 = 0.0078125; + static constexpr double A3 = 0.00032552083; + static constexpr double A4 = 1.0172526e-5; - //if (x < 13.0) - //{ - //double y = A0 + x * (A1 + x * (A2 + x * (A3 + x * A4))); - //y *= y; - //y *= y; - //y *= y; - //y = 1 / y; + if (x < 13.0) + { + double y = A0 + x * (A1 + x * (A2 + x * (A3 + x * A4))); + y *= y; + y *= y; + y *= y; + y = 1 / y; - //return y; - //} + return y; + } - //return 0.0; - // }); - // - output = exp(output * (-1)); + return 0.0; + }); + maxInput.each_row() += log(sum(output)); output = input - maxInput; }