Automatic Differentiation
 
Loading...
Searching...
No Matches
softmax.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_SOFTMAX_HPP
2#define STAN_MATH_PRIM_FUN_SOFTMAX_HPP
3
7#include <cmath>
8
9namespace stan {
10namespace math {
11
45template <typename ColVec,
46 require_eigen_col_vector_vt<std::is_arithmetic, ColVec>* = nullptr>
47inline plain_type_t<ColVec> softmax(const ColVec& v) {
48 using std::exp;
49 if (v.size() == 0) {
50 return v;
51 }
52 const auto& v_ref = to_ref(v);
53 const auto theta = (v_ref.array() - v_ref.maxCoeff()).exp().eval();
54 return theta.array() / theta.sum();
55}
56
57} // namespace math
58} // namespace stan
59
60#endif
auto softmax(const ColVec &alpha)
Definition softmax.hpp:16
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:13
typename plain_type< T >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...