gaussian_init.hpp
Go to the documentation of this file.
1 
14 #ifndef MLPACK_METHODS_ANN_INIT_RULES_GAUSSIAN_INIT_HPP
15 #define MLPACK_METHODS_ANN_INIT_RULES_GAUSSIAN_INIT_HPP
16 
17 #include <mlpack/prereqs.hpp>
19 
20 using namespace mlpack::math;
21 
22 namespace mlpack {
23 namespace ann {
24 
29 {
30  public:
37  GaussianInitialization(const double mean = 0, const double variance = 1) :
38  mean(mean), variance(variance)
39  {
40  // Nothing to do here.
41  }
42 
50  template<typename eT>
51  void Initialize(arma::Mat<eT>& W,
52  const size_t rows,
53  const size_t cols)
54  {
55  if (W.is_empty())
56  W.set_size(rows, cols);
57 
58  W.imbue( [&]() { return arma::as_scalar(RandNormal(mean, variance)); } );
59  }
60 
66  template<typename eT>
67  void Initialize(arma::Mat<eT>& W)
68  {
69  if (W.is_empty())
70  Log::Fatal << "Cannot initialize an empty matrix." << std::endl;
71 
72  W.imbue( [&]() { return arma::as_scalar(RandNormal(mean, variance)); } );
73  }
74 
83  template<typename eT>
84  void Initialize(arma::Cube<eT> & W,
85  const size_t rows,
86  const size_t cols,
87  const size_t slices)
88  {
89  if (W.is_empty())
90  W.set_size(rows, cols, slices);
91 
92  for (size_t i = 0; i < slices; ++i)
93  Initialize(W.slice(i), rows, cols);
94  }
95 
101  template<typename eT>
102  void Initialize(arma::Cube<eT> & W)
103  {
104  if (W.is_empty())
105  Log::Fatal << "Cannot initialize an empty matrix." << std::endl;
106 
107  for (size_t i = 0; i < W.n_slices; ++i)
108  Initialize(W.slice(i));
109  }
110 
111  private:
113  double mean;
114 
116  double variance;
117 }; // class GaussianInitialization
118 
119 } // namespace ann
120 } // namespace mlpack
121 
122 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
void Initialize(arma::Mat< eT > &W, const size_t rows, const size_t cols)
Initialize the elements weight matrix using a Gaussian Distribution.
The core includes that mlpack expects; standard C++ includes and Armadillo.
void Initialize(arma::Cube< eT > &W)
Initialize randomly the elements of the specified weight 3rd order tensor.
GaussianInitialization(const double mean=0, const double variance=1)
Initialize the gaussian with the given mean and variance.
static MLPACK_EXPORT util::PrefixedOutStream Fatal
Prints fatal messages prefixed with [FATAL], then terminates the program.
Definition: log.hpp:90
double RandNormal()
Generates a normally distributed random number with mean 0 and variance 1.
Definition: random.hpp:127
Miscellaneous math routines.
Definition: ccov.hpp:20
void Initialize(arma::Mat< eT > &W)
Initialize the elements weight matrix using a Gaussian Distribution.
Miscellaneous math random-related routines.
void Initialize(arma::Cube< eT > &W, const size_t rows, const size_t cols, const size_t slices)
Initialize randomly the elements of the specified weight 3rd order tensor.
This class is used to initialize weigth matrix with a gaussian.