mlpack
alpha_dropout_impl.hpp
Go to the documentation of this file.
1 
16 #ifndef MLPACK_METHODS_ANN_LAYER_ALPHA_DROPOUT_IMPL_HPP
17 #define MLPACK_METHODS_ANN_LAYER_ALPHA_DROPOUT_IMPL_HPP
18 
19 // In case it hasn't yet been included.
20 #include "alpha_dropout.hpp"
21 
22 namespace mlpack {
23 namespace ann {
24 
25 template<typename InputDataType, typename OutputDataType>
27  const double ratio,
28  const double alphaDash) :
29  ratio(ratio),
30  alphaDash(alphaDash),
31  deterministic(false)
32 {
33  Ratio(ratio);
34 }
35 
36 template<typename InputDataType, typename OutputDataType>
37 template<typename eT>
39  const arma::Mat<eT>& input, arma::Mat<eT>& output)
40 {
41  // The dropout mask will not be multiplied in the deterministic mode
42  // (during testing).
43  if (deterministic)
44  {
45  output = input;
46  }
47  else
48  {
49  // Set values to alphaDash with probability ratio. Then apply affine
50  // transformation so as to keep mean and variance of outputs to their
51  // original values.
52  mask = arma::randu< arma::Mat<eT> >(input.n_rows, input.n_cols);
53  mask.transform( [&](double val) { return (val > ratio); } );
54  output = (input % mask + alphaDash * (1 - mask)) * a + b;
55  }
56 }
57 
58 template<typename InputDataType, typename OutputDataType>
59 template<typename eT>
61  const arma::Mat<eT>& /* input */, const arma::Mat<eT>& gy, arma::Mat<eT>& g)
62 {
63  g = gy % mask * a;
64 }
65 
66 template<typename InputDataType, typename OutputDataType>
67 template<typename Archive>
69  Archive& ar, const uint32_t /* version */)
70 {
71  ar(CEREAL_NVP(ratio));
72  ar(CEREAL_NVP(alphaDash));
73  ar(CEREAL_NVP(a));
74  ar(CEREAL_NVP(b));
75 }
76 
77 } // namespace ann
78 } // namespace mlpack
79 
80 #endif
void Forward(const arma::Mat< eT > &input, arma::Mat< eT > &output)
Ordinary feed forward pass of the alpha_dropout layer.
Definition: alpha_dropout_impl.hpp:38
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
double Ratio() const
The probability of setting a value to alphaDash.
Definition: alpha_dropout.hpp:99
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
Definition: alpha_dropout_impl.hpp:68
void Backward(const arma::Mat< eT > &, const arma::Mat< eT > &gy, arma::Mat< eT > &g)
Ordinary feed backward pass of the alpha_dropout layer.
Definition: alpha_dropout_impl.hpp:60
AlphaDropout(const double ratio=0.5, const double alphaDash=-alpha *lambda)
Create the Alpha_Dropout object using the specified ratio.
Definition: alpha_dropout_impl.hpp:26