1 #ifndef STAN_MATH_PRIM_FUN_LOG_SOFTMAX_HPP
2 #define STAN_MATH_PRIM_FUN_LOG_SOFTMAX_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/err.hpp>
6 #include <stan/math/prim/fun/Eigen.hpp>
7 #include <stan/math/prim/fun/log_sum_exp.hpp>
8 #include <stan/math/prim/fun/to_ref.hpp>
9 #include <stan/math/prim/functor/apply_vector_unary.hpp>
10 
11 namespace stan {
12 namespace math {
13 
14 /**
15  * Return the natural logarithm of the softmax of the specified
16  * vector.
17  *
18  * \f$
19  * \log \mbox{softmax}(y)
20  * \ = \ y - \log \sum_{k=1}^K \exp(y_k)
21  * \ = \ y - \mbox{log\_sum\_exp}(y).
22  * \f$
23  *
24  * For the log softmax function, the entries in the Jacobian are
25  * \f$
26  * \frac{\partial}{\partial y_m} \mbox{softmax}(y)[k]
27  * = \left\{
28  * \begin{array}{ll}
29  * 1 - \mbox{softmax}(y)[m]
30  * & \mbox{ if } m = k, \mbox{ and}
31  * \\[6pt]
32  * \mbox{softmax}(y)[m]
33  * & \mbox{ if } m \neq k.
34  * \end{array}
35  * \right.
36  * \f$
37  *
38  * @tparam Container type of input vector to transform
39  * @param[in] x vector to transform
40  * @return log unit simplex result of the softmax transform of the vector.
41  */
42 template <typename Container, require_st_arithmetic<Container>* = nullptr,
43           require_container_t<Container>* = nullptr>
log_softmax(const Container & x)44 inline auto log_softmax(const Container& x) {
45   check_nonzero_size("log_softmax", "v", x);
46   return make_holder(
47       [](const auto& a) {
48         return apply_vector_unary<ref_type_t<Container>>::apply(
49             a, [](const auto& v) { return v.array() - log_sum_exp(v); });
50       },
51       to_ref(x));
52 }
53 
54 }  // namespace math
55 }  // namespace stan
56 #endif
57