Stan Math Library  2.8.0
reverse mode automatic differentiation
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
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>
46  softmax(const Eigen::Matrix<T, Eigen::Dynamic, 1>& v) {
47  using std::exp;
48  stan::math::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
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > softmax(const Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > &alpha)
Definition: softmax.hpp:14
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.
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10

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