1 #ifndef STAN__MATH__MATRIX__SOFTMAX_HPP
2 #define STAN__MATH__MATRIX__SOFTMAX_HPP
45 inline Eigen::Matrix<T,Eigen::Dynamic,1>
46 softmax(
const Eigen::Matrix<T,Eigen::Dynamic,1>& v) {
49 Eigen::Matrix<T,Eigen::Dynamic,1> theta(v.size());
51 T max_v = v.maxCoeff();
52 for (
int i = 0; i < v.size(); ++i) {
53 theta(i) =
exp(v(i) - max_v);
56 for (
int i = 0; i < v.size(); ++i)
Eigen::Matrix< T, Eigen::Dynamic, 1 > softmax(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &v)
Return the softmax of the specified vector.
Eigen::Matrix< T, Rows, Cols > exp(const Eigen::Matrix< T, Rows, Cols > &m)
Return the element-wise exponentiation of the matrix or vector.
double sum(std::vector< double > &x)
fvar< T > exp(const fvar< T > &x)
bool check_nonzero_size(const char *function, const T_y &y, const char *name, T_result *result)
Return true if the specified matrix/vector is of non-zero size.