Automatic Differentiation
 
Loading...
Searching...
No Matches

◆ softmax() [5/5]

template<typename Container , require_st_arithmetic< Container > * = nullptr, require_container_t< Container > * = nullptr, require_not_t< bool_constant< is_eigen< std::decay_t< Container > >::value &&!is_eigen_vector< std::decay_t< Container > >::value > > * = nullptr>
auto stan::math::softmax ( Container &&  x)
inline

Return the softmax of the specified vector, or of each vector in a container.

\( \mbox{softmax}(y) = \frac{\exp(y)} {\sum_{k=1}^K \exp(y_k)}, \)

The entries in the Jacobian of the softmax function are given by \( \begin{array}{l} \displaystyle \frac{\partial}{\partial y_m} \mbox{softmax}(y)[k] \\[8pt] \displaystyle \mbox{ } \ \ \ = \left\{ \begin{array}{ll} \mbox{softmax}(y)[k] \times (1 - \mbox{softmax}(y)[m]) & \mbox{ if } m = k, \mbox{ and} \\[6pt] -\mbox{softmax}(y)[k] \times \mbox{softmax}(y)[m] & \mbox{ if } m \neq k. \end{array} \right. \end{array} \)

Template Parameters
Containertype of input: an Eigen vector, std::vector of doubles, or nested container whose scalar type is arithmetic
Parameters
xvector or container of vectors to transform
Returns
softmax of the input, preserving the container structure; an empty result if any input vector is empty

Definition at line 52 of file softmax.hpp.