| Stan Math Library 2.11.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_REV_MAT_FUN_SOFTMAX_HPP
2 #define STAN_MATH_REV_MAT_FUN_SOFTMAX_HPP
3
4 #include <stan/math/prim/arr/err/check_nonzero_size.hpp>
5 #include <stan/math/prim/mat/fun/Eigen.hpp>
6 #include <stan/math/prim/mat/fun/softmax.hpp>
7 #include <stan/math/rev/core.hpp>
8 #include <vector>
9
10 namespace stan {
11 namespace math {
12
13 namespace {
14 class softmax_elt_vari : public vari {
15 private:
16 vari** alpha_;
17 const double* softmax_alpha_;
18 const int size_; // array sizes
19 const int idx_; // in in softmax output
20
21 public:
22 softmax_elt_vari(double val,
23 vari** alpha,
24 const double* softmax_alpha,
25 int size,
26 int idx)
27 : vari(val),
28 alpha_(alpha),
29 softmax_alpha_(softmax_alpha),
30 size_(size),
31 idx_(idx) {
32 }
33 void chain() {
34 for (int m = 0; m < size_; ++m) {
35 if (m == idx_) {
36 alpha_[m]->adj_
37 += adj_ * softmax_alpha_[idx_] * (1 - softmax_alpha_[m]);
38 } else {
39 alpha_[m]->adj_
40 -= adj_ * softmax_alpha_[idx_] * softmax_alpha_[m];
41 }
42 }
43 }
44 };
45 }
46
47
58 inline Eigen::Matrix<var, Eigen::Dynamic, 1>
59 softmax(const Eigen::Matrix<var, Eigen::Dynamic, 1>& alpha) {
60 using Eigen::Matrix;
61 using Eigen::Dynamic;
62
63 stan::math::check_nonzero_size("softmax", "alpha", alpha);
64
65 vari** alpha_vi_array
66 = reinterpret_cast<vari**>(ChainableStack::memalloc_
67 .alloc(sizeof(vari*) * alpha.size()));
68 for (int i = 0; i < alpha.size(); ++i)
69 alpha_vi_array[i] = alpha(i).vi_;
70
71 Matrix<double, Dynamic, 1> alpha_d(alpha.size());
72 for (int i = 0; i < alpha_d.size(); ++i)
73 alpha_d(i) = alpha(i).val();
74
75 Matrix<double, Dynamic, 1> softmax_alpha_d
76 = stan::math::softmax(alpha_d);
77
78 double* softmax_alpha_d_array
79 = reinterpret_cast<double*>(ChainableStack::memalloc_
80 .alloc(sizeof(double) * alpha_d.size()));
81 for (int i = 0; i < alpha_d.size(); ++i)
82 softmax_alpha_d_array[i] = softmax_alpha_d(i);
83
84 Matrix<var, Dynamic, 1> softmax_alpha(alpha.size());
85 for (int k = 0; k < softmax_alpha.size(); ++k)
86 softmax_alpha(k) = var(new softmax_elt_vari(softmax_alpha_d[k],
87 alpha_vi_array,
88 softmax_alpha_d_array,
89 alpha.size(),
90 k));
91 return softmax_alpha;
92 }
93
94
95 }
96 }
97
98 #endif
size_
const int size_
Definition: softmax.hpp:18
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
stan::math::AutodiffStackStorage::memalloc_
static stack_alloc memalloc_
Definition: autodiffstackstorage.hpp:16
stan::math::vari
The variable implementation base class.
Definition: vari.hpp:30
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:31
stan::math::check_nonzero_size
bool check_nonzero_size(const char *function, const char *name, const T_y &y)
Return true if the specified matrix/vector is of non-zero size.
Definition: check_nonzero_size.hpp:30
softmax.hpp
softmax_alpha_
const double * softmax_alpha_
Definition: softmax.hpp:17
idx_
const int idx_
Definition: softmax.hpp:19
Eigen.hpp
stan::math::size
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
alpha_
vari ** alpha_
Definition: softmax.hpp:16
check_nonzero_size.hpp
stan::math::stack_alloc::alloc
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...
Definition: stack_alloc.hpp:166

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