| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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>
5 #include <stan/math/prim/mat/fun/Eigen.hpp>
6 #include <stan/math/prim/mat/fun/softmax.hpp>
7
8 namespace stan {
9 namespace math {
10
11 template <typename T>
12 inline
13 Eigen::Matrix<fvar<T>, Eigen::Dynamic, 1>
14 softmax(const Eigen::Matrix<fvar<T>, Eigen::Dynamic, 1>& alpha) {
15 using Eigen::Matrix;
16 using Eigen::Dynamic;
17
18 Matrix<T, Dynamic, 1> alpha_t(alpha.size());
19 for (int k = 0; k < alpha.size(); ++k)
20 alpha_t(k) = alpha(k).val_;
21
22 Matrix<T, Dynamic, 1> softmax_alpha_t = softmax(alpha_t);
23
24 Matrix<fvar<T>, Dynamic, 1> softmax_alpha(alpha.size());
25 for (int k = 0; k < alpha.size(); ++k) {
26 softmax_alpha(k).val_ = softmax_alpha_t(k);
27 softmax_alpha(k).d_ = 0;
28 }
29
30 for (int m = 0; m < alpha.size(); ++m) {
31 T negative_alpha_m_d_times_softmax_alpha_t_m
32 = - alpha(m).d_ * softmax_alpha_t(m);
33 for (int k = 0; k < alpha.size(); ++k) {
34 if (m == k) {
35 softmax_alpha(k).d_
36 += softmax_alpha_t(k)
37 * (alpha(m).d_
38 + negative_alpha_m_d_times_softmax_alpha_t_m);
39 } else {
40 softmax_alpha(k).d_
41 += negative_alpha_m_d_times_softmax_alpha_t_m
42 * softmax_alpha_t(k);
43 }
44 }
45 }
46
47 return softmax_alpha;
48 }
49
50 }
51 }
52 #endif
core.hpp
stan::math::softmax
Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > softmax(const Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > &alpha)
Definition: softmax.hpp:14
stan
Definition: log_sum_exp.hpp:8
softmax.hpp
Eigen.hpp
stan::math::fvar
Definition: fvar.hpp:12

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