mlpack
spatial_dropout_impl.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_ANN_LAYER_SPATIAL_DROPOUT_IMPL_HPP
13 #define MLPACK_METHODS_ANN_LAYER_SPATIAL_DROPOUT_IMPL_HPP
14 
15 // In case it hasn't been included yet.
16 #include "spatial_dropout.hpp"
17 
18 namespace mlpack {
19 namespace ann {
20 
21 template<typename InputDataType, typename OutputDataType>
23  size(0),
24  ratio(0.5),
25  scale(1.0 / (1.0 - ratio)),
26  reset(false),
27  batchSize(0),
28  inputSize(0),
29  deterministic(false)
30 {
31  // Nothing to do here.
32 }
33 
34 template<typename InputDataType, typename OutputDataType>
36  const size_t size,
37  const double ratio) :
38  size(size),
39  ratio(ratio),
40  scale(1.0 / (1.0 - ratio)),
41  reset(false),
42  batchSize(0),
43  inputSize(0),
44  deterministic(false)
45 {
46  // Nothing to do here.
47 }
48 
49 template<typename InputDataType, typename OutputDataType>
50 template<typename eT>
52  const arma::Mat<eT>& input, arma::Mat<eT>& output)
53 {
54  Log::Assert(input.n_rows % size == 0, "Input features must be divisible \
55  by feature maps.");
56 
57  if (!reset)
58  {
59  batchSize = input.n_cols;
60  inputSize = input.n_rows / size;
61  reset = true;
62  }
63 
64  if (deterministic)
65  output = input;
66  else
67  {
68  output.zeros(arma::size(input));
69  arma::cube inputTemp(const_cast<arma::mat&>(input).memptr(), inputSize,
70  size, batchSize, false, false);
71  arma::cube outputTemp(const_cast<arma::mat&>(output).memptr(), inputSize,
72  size, batchSize, false, false);
73  arma::mat probabilities(1, size);
74  arma::mat maskRow(1, size);
75  probabilities.fill(ratio);
76  ann::BernoulliDistribution<> bernoulli_dist(probabilities, false);
77  maskRow = bernoulli_dist.Sample();
78  mask = arma::repmat(maskRow, inputSize, 1);
79 
80  for (size_t n = 0; n < batchSize; n++)
81  outputTemp.slice(n) = inputTemp.slice(n) % mask * scale;
82  }
83 }
84 
85 template<typename InputDataType, typename OutputDataType>
86 template<typename eT>
88  const arma::Mat<eT>& input, const arma::Mat<eT>& gy, arma::Mat<eT>& g)
89 {
90  g.zeros(arma::size(input));
91  arma::cube gyTemp(const_cast<arma::mat&>(gy).memptr(), inputSize, size,
92  batchSize, false, false);
93  arma::cube gTemp(const_cast<arma::mat&>(g).memptr(), inputSize, size,
94  batchSize, false, false);
95 
96  for (size_t n = 0; n < batchSize; n++)
97  gTemp.slice(n) = gyTemp.slice(n) % mask * scale;
98 }
99 
100 template<typename InputDataType, typename OutputDataType>
101 template<typename Archive>
103  Archive& ar,
104  const uint32_t /* version */)
105 {
106  ar(CEREAL_NVP(size));
107  ar(CEREAL_NVP(ratio));
108  ar(CEREAL_NVP(batchSize));
109  ar(CEREAL_NVP(inputSize));
110  ar(CEREAL_NVP(reset));
111  ar(CEREAL_NVP(deterministic));
112 
113  // Reset scale.
114  scale = 1.0 / (1.0 - ratio);
115 }
116 
117 } // namespace ann
118 } // namespace mlpack
119 
120 #endif
void Forward(const arma::Mat< eT > &input, arma::Mat< eT > &output)
Ordinary feed forward pass of the SpatialDropout layer.
Definition: spatial_dropout_impl.hpp:51
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
Multiple independent Bernoulli distributions.
Definition: bernoulli_distribution.hpp:34
void Backward(const arma::Mat< eT > &input, const arma::Mat< eT > &gy, arma::Mat< eT > &g)
Ordinary feed backward pass of the SpatialDropout layer.
Definition: spatial_dropout_impl.hpp:87
SpatialDropout()
Create the SpatialDropout object.
Definition: spatial_dropout_impl.hpp:22
DataType Sample() const
Return a matrix of randomly generated samples according to the probability distributions defined by t...
Definition: bernoulli_distribution_impl.hpp:50
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
Definition: spatial_dropout_impl.hpp:102
static void Assert(bool condition, const std::string &message="Assert Failed.")
Checks if the specified condition is true.
Definition: log.cpp:38