Stan Math Library  2.6.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
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 
6 #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
const int size_
Definition: softmax.hpp:18
Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > softmax(const Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > &alpha)
Definition: softmax.hpp:14
The variable implementation base class.
Definition: vari.hpp:28
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:32
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.
const double * softmax_alpha_
Definition: softmax.hpp:17
const int idx_
Definition: softmax.hpp:19
int size(const std::vector< T > &x)
Definition: size.hpp:11
vari ** alpha_
Definition: softmax.hpp:16
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...

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