1#ifndef STAN_MATH_FWD_FUN_SOFTMAX_HPP
2#define STAN_MATH_FWD_FUN_SOFTMAX_HPP
14template <
typename ColVec,
15 require_eigen_col_vector_vt<is_fvar, ColVec>* =
nullptr>
20 if (alpha.size() == 0) {
21 return Matrix<fvar<T>, Dynamic, 1>();
23 const auto& alpha_ref =
to_ref(alpha);
27 Matrix<fvar<T>, Dynamic, 1> softmax_alpha(alpha.size());
28 for (
int k = 0; k < alpha.size(); ++k) {
29 softmax_alpha.coeffRef(k).val_ = softmax_alpha_t.coeff(k);
30 softmax_alpha.coeffRef(k).d_ = 0;
33 for (
int m = 0; m < alpha.size(); ++m) {
34 T negative_alpha_m_d_times_softmax_alpha_t_m
35 = -alpha_ref.coeff(m).d_ * softmax_alpha_t.coeff(m);
36 for (
int k = 0; k < alpha.size(); ++k) {
38 softmax_alpha.coeffRef(k).d_
39 += softmax_alpha_t.coeff(k)
40 * (alpha_ref.coeff(m).d_
41 + negative_alpha_m_d_times_softmax_alpha_t_m);
43 softmax_alpha.coeffRef(k).d_
44 += softmax_alpha_t.coeff(k)
45 * negative_alpha_m_d_times_softmax_alpha_t_m;
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
auto softmax(const ColVec &alpha)
T value_of(const fvar< T > &v)
Return the value of the specified variable.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...