mlpack
dropout_impl.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_ANN_LAYER_DROPOUT_IMPL_HPP
14 #define MLPACK_METHODS_ANN_LAYER_DROPOUT_IMPL_HPP
15 
16 // In case it hasn't yet been included.
17 #include "dropout.hpp"
18 
19 namespace mlpack {
20 namespace ann {
21 
22 template<typename InputDataType, typename OutputDataType>
24  const double ratio) :
25  ratio(ratio),
26  scale(1.0 / (1.0 - ratio)),
27  deterministic(false)
28 {
29  // Nothing to do here.
30 }
31 
32 template<typename InputDataType, typename OutputDataType>
34  const Dropout& layer) :
35  ratio(layer.ratio),
36  scale(layer.scale),
37  deterministic(layer.deterministic)
38 {
39  // Nothing to do here.
40 }
41 
42 template<typename InputDataType, typename OutputDataType>
44  const Dropout&& layer) :
45  ratio(std::move(layer.ratio)),
46  scale(std::move(scale)),
47  deterministic(std::move(deterministic))
48 {
49  // Nothing to do here.
50 }
51 
52 template<typename InputDataType, typename OutputDataType>
55 operator=(const Dropout& layer)
56 {
57  if (this != &layer)
58  {
59  ratio = layer.ratio;
60  scale = layer.scale;
61  deterministic = layer.deterministic;
62  }
63  return *this;
64 }
65 
66 template<typename InputDataType, typename OutputDataType>
70 {
71  if (this != &layer)
72  {
73  ratio = std::move(layer.ratio);
74  scale = std::move(layer.scale);
75  deterministic = std::move(layer.deterministic);
76  }
77  return *this;
78 }
79 
80 template<typename InputDataType, typename OutputDataType>
81 template<typename eT>
83  const arma::Mat<eT>& input,
84  arma::Mat<eT>& output)
85 {
86  // The dropout mask will not be multiplied in the deterministic mode
87  // (during testing).
88  if (deterministic)
89  {
90  output = input;
91  }
92  else
93  {
94  // Scale with input / (1 - ratio) and set values to zero with probability
95  // 'ratio'.
96  mask = arma::randu<arma::Mat<eT> >(input.n_rows, input.n_cols);
97  mask.transform([&](double val) { return (val > ratio); });
98  output = input % mask * scale;
99  }
100 }
101 
102 template<typename InputDataType, typename OutputDataType>
103 template<typename eT>
105  const arma::Mat<eT>& /* input */,
106  const arma::Mat<eT>& gy,
107  arma::Mat<eT>& g)
108 {
109  g = gy % mask * scale;
110 }
111 
112 template<typename InputDataType, typename OutputDataType>
113 template<typename Archive>
115  Archive& ar,
116  const uint32_t /* version */)
117 {
118  ar(CEREAL_NVP(ratio));
119 
120  // Reset scale.
121  scale = 1.0 / (1.0 - ratio);
122 }
123 
124 } // namespace ann
125 } // namespace mlpack
126 
127 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
Definition: pointer_wrapper.hpp:23
Dropout(const double ratio=0.5)
Create the Dropout object using the specified ratio parameter.
Definition: dropout_impl.hpp:23
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
Definition: dropout_impl.hpp:114
void Forward(const arma::Mat< eT > &input, arma::Mat< eT > &output)
Ordinary feed forward pass of the dropout layer.
Definition: dropout_impl.hpp:82
The dropout layer is a regularizer that randomly with probability &#39;ratio&#39; sets input values to zero a...
Definition: dropout.hpp:53
void Backward(const arma::Mat< eT > &, const arma::Mat< eT > &gy, arma::Mat< eT > &g)
Ordinary feed backward pass of the dropout layer.
Definition: dropout_impl.hpp:104
Dropout & operator=(const Dropout &layer)
Copy assignment operator.
Definition: dropout_impl.hpp:55