| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
4 #include <stan/math/prim/arr/err/check_nonzero_size.hpp>
5 #include <stan/math/prim/mat/fun/Eigen.hpp>
6 #include <cmath>
7
8 namespace stan {
9 namespace math {
10
44 template <typename T>
45 inline Eigen::Matrix<T, Eigen::Dynamic, 1>
46 softmax(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 T sum(0.0);
51 T max_v = v.maxCoeff();
52 for (int i = 0; i < v.size(); ++i) {
53 theta(i) = exp(v(i) - max_v); // extra work for (v[i] == max_v)
54 sum += theta(i); // extra work vs. sum() w. auto-diff
55 }
56 for (int i = 0; i < v.size(); ++i)
57 theta(i) /= sum;
58 return theta;
59 }
60
61 }
62 }
63 #endif
stan::math::sum
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
stan::math::softmax
Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > softmax(const Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > &alpha)
Definition: softmax.hpp:14
stan
Definition: log_sum_exp.hpp:8
stan::math::check_nonzero_size
bool check_nonzero_size(const char *function, const char *name, const T_y &y)
Return true if the specified matrix/vector is of non-zero size.
Definition: check_nonzero_size.hpp:29
stan::math::exp
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
Eigen.hpp
check_nonzero_size.hpp

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