diff --git a/src/mlpack/methods/ann/init_rules/glorot_init.hpp b/src/mlpack/methods/ann/init_rules/glorot_init.hpp index 2bf52df083..10c30ab7a3 100644 --- a/src/mlpack/methods/ann/init_rules/glorot_init.hpp +++ b/src/mlpack/methods/ann/init_rules/glorot_init.hpp @@ -52,7 +52,7 @@ class GlorotInitialization /** * Initialize */ - GlorotInitialization() : + GlorotInitialization(const bool uniform = true) : uniform(uniform) { // Nothing to do here. } @@ -69,13 +69,20 @@ class GlorotInitialization const size_t rows, const size_t cols) { - double_t a = sqrt(6)/sqrt(rows + cols); // limit of uniform distribution + double_t a = sqrt(6)/sqrt(rows + cols); // limit of distribution - if (W.is_empty()) - { - W = arma::mat(rows, cols); - } + if (W.is_empty()) + { + W = arma::mat(rows, cols); + } + + if(uniform) W.imbue( [&]() { return arma::as_scalar(Random(-a, a)); } ); + else + { + double_t var = 2/(rows + cols); + W.imbue([&]() { return arma::as_scalar(RandNormal(0.0, var)); }); + } } /** @@ -100,7 +107,12 @@ class GlorotInitialization Initialize(W.slice(i), rows, cols); } - }; // class GlorotInitialization + private: + //! Mode used i.e. Uniform or Normal + bool uniform; + + +}; // class GlorotInitialization } // namespace ann } // namespace mlpack diff --git a/src/mlpack/tests/init_rules_test.cpp b/src/mlpack/tests/init_rules_test.cpp index 079c4263e5..633006f3e6 100644 --- a/src/mlpack/tests/init_rules_test.cpp +++ b/src/mlpack/tests/init_rules_test.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include "test_tools.hpp" @@ -285,4 +286,26 @@ BOOST_AUTO_TEST_CASE(NetworkInitTest) BOOST_REQUIRE_EQUAL(gaussianModel.Parameters().n_elem, 42); } +/** + * Simple test of the GlorotInitialization class. + */ + BOOST_AUTO_TEST_CASE(GlorotInitTest) + { + arma::mat weights; + arma::cube weights3d; + + GlorotInitialization glorotInit; + + glorotInit.Initialize(weights, 100, 100); + glorotInit.Initialize(weights3d, 100, 100, 2); + + BOOST_REQUIRE_EQUAL(weights.n_rows, 100); + BOOST_REQUIRE_EQUAL(weights.n_cols, 100); + + BOOST_REQUIRE_EQUAL(weights3d.n_rows, 100); + BOOST_REQUIRE_EQUAL(weights3d.n_cols, 100); + BOOST_REQUIRE_EQUAL(weights3d.n_slices, 2); + } + + BOOST_AUTO_TEST_SUITE_END();