Stan Math Library  2.20.0
reverse mode automatic differentiation
softmax.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_SOFTMAX_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_SOFTMAX_HPP
3 
6 #include <cmath>
7 
8 namespace stan {
9 namespace math {
10 
44 template <typename T>
45 inline Eigen::Matrix<T, Eigen::Dynamic, 1> softmax(
46  const Eigen::Matrix<T, Eigen::Dynamic, 1>& v) {
47  using std::exp;
48  check_nonzero_size("softmax", "v", v);
49  Eigen::Matrix<T, Eigen::Dynamic, 1> theta(v.size());
50  theta = (v.array() - v.maxCoeff()).exp();
51  return theta.array() / theta.sum();
52 }
53 
54 } // namespace math
55 } // namespace stan
56 #endif
void check_nonzero_size(const char *function, const char *name, const T_y &y)
Check if the specified matrix/vector is of non-zero size.
Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > softmax(const Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > &alpha)
Definition: softmax.hpp:12
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:11

     [ Stan Home Page ] © 2011–2018, Stan Development Team.