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 namespace mlpack {
21 namespace ann {
22 
27 {
28  public:
35  GaussianInitialization(const double mean = 0, const double variance = 1) :
36  mean(mean), variance(variance)
37  {
38  // Nothing to do here.
39  }
40 
48  template<typename eT>
49  void Initialize(arma::Mat<eT>& W,
50  const size_t rows,
51  const size_t cols)
52  {
53  if (W.is_empty())
54  W.set_size(rows, cols);
55 
56  W.imbue( [&]() { return arma::as_scalar(mlpack::math::RandNormal(mean, variance)); } );
57  }
58 
64  template<typename eT>
65  void Initialize(arma::Mat<eT>& W)
66  {
67  if (W.is_empty())
68  Log::Fatal << "Cannot initialize an empty matrix." << std::endl;
69 
70  W.imbue( [&]() { return arma::as_scalar(mlpack::math::RandNormal(mean, variance)); } );
71  }
72 
81  template<typename eT>
82  void Initialize(arma::Cube<eT> & W,
83  const size_t rows,
84  const size_t cols,
85  const size_t slices)
86  {
87  if (W.is_empty())
88  W.set_size(rows, cols, slices);
89 
90  for (size_t i = 0; i < slices; ++i)
91  Initialize(W.slice(i), rows, cols);
92  }
93 
99  template<typename eT>
100  void Initialize(arma::Cube<eT> & W)
101  {
102  if (W.is_empty())
103  Log::Fatal << "Cannot initialize an empty matrix." << std::endl;
104 
105  for (size_t i = 0; i < W.n_slices; ++i)
106  Initialize(W.slice(i));
107  }
108 
109  private:
111  double mean;
112 
114  double variance;
115 }; // class GaussianInitialization
116 
117 } // namespace ann
118 } // namespace mlpack
119 
120 #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:141
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.