Stan Math Library  2.20.0
reverse mode automatic differentiation
softmax.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_FWD_MAT_FUN_SOFTMAX_HPP
2 #define STAN_MATH_FWD_MAT_FUN_SOFTMAX_HPP
3 
4 #include <stan/math/fwd/core.hpp>
7 
8 namespace stan {
9 namespace math {
10 
11 template <typename T>
12 inline Eigen::Matrix<fvar<T>, Eigen::Dynamic, 1> softmax(
13  const Eigen::Matrix<fvar<T>, Eigen::Dynamic, 1>& alpha) {
14  using Eigen::Dynamic;
15  using Eigen::Matrix;
16 
17  Matrix<T, Dynamic, 1> alpha_t(alpha.size());
18  for (int k = 0; k < alpha.size(); ++k)
19  alpha_t(k) = alpha(k).val_;
20 
21  Matrix<T, Dynamic, 1> softmax_alpha_t = softmax(alpha_t);
22 
23  Matrix<fvar<T>, Dynamic, 1> softmax_alpha(alpha.size());
24  for (int k = 0; k < alpha.size(); ++k) {
25  softmax_alpha(k).val_ = softmax_alpha_t(k);
26  softmax_alpha(k).d_ = 0;
27  }
28 
29  for (int m = 0; m < alpha.size(); ++m) {
30  T negative_alpha_m_d_times_softmax_alpha_t_m
31  = -alpha(m).d_ * softmax_alpha_t(m);
32  for (int k = 0; k < alpha.size(); ++k) {
33  if (m == k) {
34  softmax_alpha(k).d_
35  += softmax_alpha_t(k)
36  * (alpha(m).d_ + negative_alpha_m_d_times_softmax_alpha_t_m);
37  } else {
38  softmax_alpha(k).d_
39  += negative_alpha_m_d_times_softmax_alpha_t_m * softmax_alpha_t(k);
40  }
41  }
42  }
43 
44  return softmax_alpha;
45 }
46 
47 } // namespace math
48 } // namespace stan
49 #endif
Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > softmax(const Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > &alpha)
Definition: softmax.hpp:12
This template class represents scalars used in forward-mode automatic differentiation, which consist of values and directional derivatives of the specified template type.
Definition: fvar.hpp:41

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