1#ifndef STAN_MATH_FWD_FUN_SOFTMAX_HPP
2#define STAN_MATH_FWD_FUN_SOFTMAX_HPP
13template <
typename ColVec,
14 require_eigen_col_vector_vt<is_fvar, ColVec>* =
nullptr>
19 if (alpha.size() == 0) {
20 return Matrix<fvar<T>, Dynamic, 1>();
22 const auto& alpha_ref =
to_ref(alpha);
26 Matrix<fvar<T>, Dynamic, 1> softmax_alpha(alpha.size());
27 for (
int k = 0; k < alpha.size(); ++k) {
28 softmax_alpha.coeffRef(k).val_ = softmax_alpha_t.coeff(k);
29 softmax_alpha.coeffRef(k).d_ = 0;
32 for (
int m = 0; m < alpha.size(); ++m) {
33 T negative_alpha_m_d_times_softmax_alpha_t_m
34 = -alpha_ref.coeff(m).d_ * softmax_alpha_t.coeff(m);
35 for (
int k = 0; k < alpha.size(); ++k) {
37 softmax_alpha.coeffRef(k).d_
38 += softmax_alpha_t.coeff(k)
39 * (alpha_ref.coeff(m).d_
40 + negative_alpha_m_d_times_softmax_alpha_t_m);
42 softmax_alpha.coeffRef(k).d_
43 += softmax_alpha_t.coeff(k)
44 * 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 ...