Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
softmax.hpp
Go to the documentation of this file.
1 #ifndef STAN__AGRAD__REV__MATRIX__SOFTMAX_HPP
2 #define STAN__AGRAD__REV__MATRIX__SOFTMAX_HPP
3 
4 #include <vector>
7 #include <stan/agrad/rev/var.hpp>
9 
10 namespace stan {
11  namespace agrad {
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  public:
21  softmax_elt_vari(double val,
22  vari** alpha,
23  const double* softmax_alpha,
24  int size,
25  int idx)
26  : vari(val),
27  alpha_(alpha),
28  softmax_alpha_(softmax_alpha),
29  size_(size),
30  idx_(idx) {
31  }
32  void chain() {
33  for (int m = 0; m < size_; ++m) {
34  if (m == idx_) {
35  alpha_[m]->adj_
36  += adj_ * softmax_alpha_[idx_] * (1 - softmax_alpha_[m]);
37  } else {
38  alpha_[m]->adj_
39  -= adj_ * softmax_alpha_[idx_] * softmax_alpha_[m];
40  }
41  }
42  }
43  };
44  }
45 
46 
57  inline Eigen::Matrix<var,Eigen::Dynamic,1>
58  softmax(const Eigen::Matrix<var,Eigen::Dynamic,1>& alpha) {
59  using Eigen::Matrix;
60  using Eigen::Dynamic;
61 
62  stan::math::check_nonzero_size("softmax(%1%)",alpha,"alpha",(double*)0);
63 
64  vari** alpha_vi_array
65  = (vari**) memalloc_.alloc(sizeof(vari*) * alpha.size());
66  for (int i = 0; i < alpha.size(); ++i)
67  alpha_vi_array[i] = alpha(i).vi_;
68 
69  Matrix<double,Dynamic,1> alpha_d(alpha.size());
70  for (int i = 0; i < alpha_d.size(); ++i)
71  alpha_d(i) = alpha(i).val();
72 
73  Matrix<double,Dynamic,1> softmax_alpha_d
74  = stan::math::softmax(alpha_d);
75 
76  double* softmax_alpha_d_array
77  = (double*) memalloc_.alloc(sizeof(double) * alpha_d.size());
78  for (int i = 0; i < alpha_d.size(); ++i)
79  softmax_alpha_d_array[i] = softmax_alpha_d(i);
80 
81  Matrix<var,Dynamic,1> softmax_alpha(alpha.size());
82  for (int k = 0; k < softmax_alpha.size(); ++k)
83  softmax_alpha(k) = var(new softmax_elt_vari(softmax_alpha_d[k],
84  alpha_vi_array,
85  softmax_alpha_d_array,
86  alpha.size(),
87  k));
88  return softmax_alpha;
89  }
90 
91 
92  }
93 }
94 
95 #endif
const double * softmax_alpha_
Definition: softmax.hpp:17
const int size_
Definition: softmax.hpp:18
Eigen::Matrix< T, Eigen::Dynamic, 1 > softmax(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &v)
Return the softmax of the specified vector.
Definition: softmax.hpp:46
memory::stack_alloc memalloc_
Definition: var_stack.cpp:16
The variable implementation base class.
Definition: vari.hpp:28
Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > softmax(const Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > &alpha)
Definition: softmax.hpp:14
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:27
int size(const std::vector< T > &x)
Definition: size.hpp:11
vari ** alpha_
Definition: softmax.hpp:16
const int idx_
Definition: softmax.hpp:19
bool check_nonzero_size(const char *function, const T_y &y, const char *name, T_result *result)
Return true if the specified matrix/vector is of non-zero size.

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