Added test for glorot initialization

This commit is contained in:
Prabhat
2018-02-28 05:29:26 +05:30
parent 7601630618
commit 148f2a1e99
2 changed files with 42 additions and 7 deletions
@@ -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
+23
View File
@@ -23,6 +23,7 @@
#include <mlpack/methods/ann/init_rules/random_init.hpp>
#include <mlpack/methods/ann/init_rules/const_init.hpp>
#include <mlpack/methods/ann/init_rules/gaussian_init.hpp>
#include <mlpack/methods/ann/init_rules/glorot_init.hpp>
#include <boost/test/unit_test.hpp>
#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();