Stan Math Library: stan/math/fwd/mat/fun/log_softmax.hpp Source File

| Stan Math Library 2.10.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

log_softmax.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_FWD_MAT_FUN_LOG_SOFTMAX_HPP
2 #define STAN_MATH_FWD_MAT_FUN_LOG_SOFTMAX_HPP
3
4 #include <stan/math/fwd/core.hpp>
5 #include <stan/math/fwd/mat/fun/softmax.hpp>
6 #include <stan/math/prim/mat/fun/Eigen.hpp>
7 #include <stan/math/prim/mat/fun/log_softmax.hpp>
8 #include <stan/math/prim/mat/fun/softmax.hpp>
9
10 namespace stan {
11 namespace math {
12
13 template <typename T>
14 inline
15 Eigen::Matrix<fvar<T>, Eigen::Dynamic, 1>
16 log_softmax(const Eigen::Matrix<fvar<T>, Eigen::Dynamic, 1>& alpha) {
17 using stan::math::softmax;
18 using stan::math::log_softmax;
19 using Eigen::Matrix;
20 using Eigen::Dynamic;
21
22 Matrix<T, Dynamic, 1> alpha_t(alpha.size());
23 for (int k = 0; k < alpha.size(); ++k)
24 alpha_t(k) = alpha(k).val_;
25
26 Matrix<T, Dynamic, 1> softmax_alpha_t = softmax(alpha_t);
27 Matrix<T, Dynamic, 1> log_softmax_alpha_t = log_softmax(alpha_t);
28
29 Matrix<fvar<T>, Dynamic, 1> log_softmax_alpha(alpha.size());
30 for (int k = 0; k < alpha.size(); ++k) {
31 log_softmax_alpha(k).val_ = log_softmax_alpha_t(k);
32 log_softmax_alpha(k).d_ = 0;
33 }
34
35 // for each input position
36 for (int m = 0; m < alpha.size(); ++m) {
37 T negative_alpha_m_d_times_softmax_alpha_t_m
38 = - alpha(m).d_ * softmax_alpha_t(m);
39 // for each output position
40 for (int k = 0; k < alpha.size(); ++k) {
41 // chain from input to output
42 if (m == k)
43 log_softmax_alpha(k).d_
44 += alpha(m).d_
45 + negative_alpha_m_d_times_softmax_alpha_t_m;
46 else
47 log_softmax_alpha(k).d_
48 += negative_alpha_m_d_times_softmax_alpha_t_m;
49 }
50 }
51
52 return log_softmax_alpha;
53 }
54
55
56 }
57 }
58
59 #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
stan::math::log_softmax
Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > log_softmax(const Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > &alpha)
Definition: log_softmax.hpp:16
softmax.hpp
Eigen.hpp
log_softmax.hpp
stan::math::fvar
Definition: fvar.hpp:13

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