nguyen_widrow_init.hpp
Go to the documentation of this file.
1 
26 #ifndef MLPACK_METHODS_ANN_INIT_RULES_NGUYEN_WIDROW_INIT_HPP
27 #define MLPACK_METHODS_ANN_INIT_RULES_NGUYEN_WIDROW_INIT_HPP
28 
29 #include <mlpack/prereqs.hpp>
30 
31 #include "init_rules_traits.hpp"
32 #include "random_init.hpp"
33 
34 namespace mlpack {
35 namespace ann {
36 
54 {
55  public:
63  NguyenWidrowInitialization(const double lowerBound = -0.5,
64  const double upperBound = 0.5) :
65  lowerBound(lowerBound), upperBound(upperBound) { }
66 
75  template<typename eT>
76  void Initialize(arma::Mat<eT>& W, const size_t rows, const size_t cols)
77  {
78  RandomInitialization randomInit(lowerBound, upperBound);
79  randomInit.Initialize(W, rows, cols);
80 
81  double beta = 0.7 * std::pow(cols, 1.0 / rows);
82  W *= (beta / arma::norm(W));
83  }
84 
91  template<typename eT>
92  void Initialize(arma::Mat<eT>& W)
93  {
94  RandomInitialization randomInit(lowerBound, upperBound);
95  randomInit.Initialize(W);
96 
97  double beta = 0.7 * std::pow(W.n_cols, 1.0 / W.n_rows);
98  W *= (beta / arma::norm(W));
99  }
100 
110  template<typename eT>
111  void Initialize(arma::Cube<eT>& W,
112  const size_t rows,
113  const size_t cols,
114  const size_t slices)
115  {
116  if (W.is_empty())
117  W.set_size(rows, cols, slices);
118 
119  for (size_t i = 0; i < slices; ++i)
120  Initialize(W.slice(i), rows, cols);
121  }
122 
129  template<typename eT>
130  void Initialize(arma::Cube<eT>& W)
131  {
132  if (W.is_empty())
133  Log::Fatal << "Cannot initialize an empty matrix." << std::endl;
134 
135  for (size_t i = 0; i < W.n_slices; ++i)
136  Initialize(W.slice(i));
137  }
138 
139  private:
141  double lowerBound;
142 
144  double upperBound;
145 }; // class NguyenWidrowInitialization
146 
148 template<>
150 {
151  public:
153  static const bool UseLayer = false;
154 };
155 
156 
157 } // namespace ann
158 } // namespace mlpack
159 
160 #endif
void Initialize(arma::Cube< eT > &W)
Initialize the elements of the specified weight 3rd order tensor with the Nguyen-Widrow method...
Linear algebra utility functions, generally performed on matrices or vectors.
This class is used to initialize randomly the weight matrix.
Definition: random_init.hpp:24
The core includes that mlpack expects; standard C++ includes and Armadillo.
void Initialize(arma::Cube< eT > &W, const size_t rows, const size_t cols, const size_t slices)
Initialize the elements of the specified weight 3rd order tensor with the Nguyen-Widrow method...
void Initialize(arma::Mat< eT > &W)
Initialize the elements of the specified weight matrix with the Nguyen-Widrow method.
void Initialize(arma::Mat< eT > &W, const size_t rows, const size_t cols)
Initialize randomly the elements of the specified weight matrix.
Definition: random_init.hpp:56
static MLPACK_EXPORT util::PrefixedOutStream Fatal
Prints fatal messages prefixed with [FATAL], then terminates the program.
Definition: log.hpp:90
NguyenWidrowInitialization(const double lowerBound=-0.5, const double upperBound=0.5)
Initialize the random initialization rule with the given lower bound and upper bound.
This is a template class that can provide information about various initialization methods...
void Initialize(arma::Mat< eT > &W, const size_t rows, const size_t cols)
Initialize the elements of the specified weight matrix with the Nguyen-Widrow method.
This class is used to initialize the weight matrix with the Nguyen-Widrow method. ...