[mlpack-git] master: Remove RandomSeed() calls from tests. (a4af83d)
gitdub at mlpack.org
gitdub at mlpack.org
Wed Jun 29 11:59:55 EDT 2016
Repository : https://github.com/mlpack/mlpack
On branch : master
Link : https://github.com/mlpack/mlpack/compare/809ed4bf33cef9de8412fc167cb0e356a369e3b6...eaa7182ebed8cce3fd6191dc1f8170546ea297da
>---------------------------------------------------------------
commit a4af83d6af23f47b75089a22705a2c1397cd49c5
Author: Ryan Curtin <ryan at ratml.org>
Date: Wed Jun 29 11:51:03 2016 -0400
Remove RandomSeed() calls from tests.
>---------------------------------------------------------------
a4af83d6af23f47b75089a22705a2c1397cd49c5
src/mlpack/methods/amf/init_rules/CMakeLists.txt | 1 +
src/mlpack/methods/amf/init_rules/given_init.hpp | 75 ++++++++++++++++++++++++
src/mlpack/tests/adaboost_test.cpp | 1 -
src/mlpack/tests/kmeans_test.cpp | 1 -
src/mlpack/tests/mean_shift_test.cpp | 1 -
src/mlpack/tests/nmf_test.cpp | 15 +++--
src/mlpack/tests/range_search_test.cpp | 2 -
7 files changed, 86 insertions(+), 10 deletions(-)
diff --git a/src/mlpack/methods/amf/init_rules/CMakeLists.txt b/src/mlpack/methods/amf/init_rules/CMakeLists.txt
index 28989df..17ddfe2 100644
--- a/src/mlpack/methods/amf/init_rules/CMakeLists.txt
+++ b/src/mlpack/methods/amf/init_rules/CMakeLists.txt
@@ -4,6 +4,7 @@ set(SOURCES
random_init.hpp
random_acol_init.hpp
average_init.hpp
+ given_init.hpp
)
# Add directory name to sources.
diff --git a/src/mlpack/methods/amf/init_rules/given_init.hpp b/src/mlpack/methods/amf/init_rules/given_init.hpp
new file mode 100644
index 0000000..8100ede
--- /dev/null
+++ b/src/mlpack/methods/amf/init_rules/given_init.hpp
@@ -0,0 +1,75 @@
+/**
+ * @file given_initialization.hpp
+ * @author Ryan Curtin
+ *
+ * Initialization rule for alternating matrix factorization (AMF). This simple
+ * initialization is performed by assigning a given matrix to W and H.
+ */
+#ifndef MLPACK_METHODS_AMF_INIT_RULES_GIVEN_INIT_HPP
+#define MLPACK_METHODS_AMF_INIT_RULES_GIVEN_INIT_HPP
+
+#include <mlpack/core.hpp>
+
+namespace mlpack {
+namespace amf {
+
+/**
+ * This initialization rule for AMF simply fills the W and H matrices with the
+ * matrices given to the constructor of this object. Note that this object does
+ * not use std::move() during the Initialize() method, so it can be reused for
+ * multiple AMF objects, but will incur copies of the W and H matrices.
+ */
+class GivenInitialization
+{
+ public:
+ // Empty constructor required for the InitializeRule template.
+ GivenInitialization() { }
+
+ // Initialize the GivenInitialization object with the given matrices.
+ GivenInitialization(const arma::mat& w, const arma::mat& h) : w(w), h(h) { }
+
+ // Initialize the GivenInitialization object, taking control of the given
+ // matrices.
+ GivenInitialization(const arma::mat&& w, const arma::mat&& h) :
+ w(std::move(w)),
+ h(std::move(h))
+ { }
+
+ /**
+ * Fill W and H with random uniform noise.
+ *
+ * @param V Input matrix.
+ * @param r Rank of decomposition.
+ * @param W W matrix, to be filled with random noise.
+ * @param H H matrix, to be filled with random noise.
+ */
+ template<typename MatType>
+ inline void Initialize(const MatType& /* V */,
+ const size_t /* r */,
+ arma::mat& W,
+ arma::mat& H)
+ {
+ // Initialize to the given matrices.
+ W = w;
+ H = h;
+ }
+
+ //! Serialize the object (in this case, there is nothing to serialize).
+ template<typename Archive>
+ void Serialize(Archive& ar, const unsigned int /* version */)
+ {
+ ar & data::CreateNVP(w, "w");
+ ar & data::CreateNVP(h, "h");
+ }
+
+ private:
+ //! The W matrix for initialization.
+ arma::mat w;
+ //! The H matrix for initialization.
+ arma::mat h;
+};
+
+} // namespace amf
+} // namespace mlpack
+
+#endif
diff --git a/src/mlpack/tests/adaboost_test.cpp b/src/mlpack/tests/adaboost_test.cpp
index 9edc57a..e164958 100644
--- a/src/mlpack/tests/adaboost_test.cpp
+++ b/src/mlpack/tests/adaboost_test.cpp
@@ -555,7 +555,6 @@ BOOST_AUTO_TEST_CASE(WeakLearnerErrorNonLinearSepData_DS)
*/
BOOST_AUTO_TEST_CASE(ClassifyTest_VERTEBRALCOL)
{
- mlpack::math::RandomSeed(std::time(NULL));
arma::mat inputData;
if (!data::Load("vc2.csv", inputData))
BOOST_FAIL("Cannot load test dataset vc2.csv!");
diff --git a/src/mlpack/tests/kmeans_test.cpp b/src/mlpack/tests/kmeans_test.cpp
index 9353557..f606064 100644
--- a/src/mlpack/tests/kmeans_test.cpp
+++ b/src/mlpack/tests/kmeans_test.cpp
@@ -390,7 +390,6 @@ BOOST_AUTO_TEST_CASE(RefinedStartTest)
// Our dataset will be five Gaussians of largely varying numbers of points and
// we expect that the refined starting policy should return good guesses at
// what these Gaussians are.
- math::RandomSeed(std::time(NULL));
arma::mat data(3, 3000);
data.randn();
diff --git a/src/mlpack/tests/mean_shift_test.cpp b/src/mlpack/tests/mean_shift_test.cpp
index e7577d7..6670289 100644
--- a/src/mlpack/tests/mean_shift_test.cpp
+++ b/src/mlpack/tests/mean_shift_test.cpp
@@ -90,7 +90,6 @@ BOOST_AUTO_TEST_CASE(MeanShiftSimpleTest) {
// recovers those four centers.
BOOST_AUTO_TEST_CASE(GaussianClustering)
{
- math::RandomSeed(std::time(NULL));
GaussianDistribution g1("0.0 0.0 0.0", arma::eye<arma::mat>(3, 3));
GaussianDistribution g2("5.0 5.0 5.0", 2 * arma::eye<arma::mat>(3, 3));
GaussianDistribution g3("-3.0 3.0 -1.0", arma::eye<arma::mat>(3, 3));
diff --git a/src/mlpack/tests/nmf_test.cpp b/src/mlpack/tests/nmf_test.cpp
index f50b10a..d97f151 100644
--- a/src/mlpack/tests/nmf_test.cpp
+++ b/src/mlpack/tests/nmf_test.cpp
@@ -7,6 +7,7 @@
#include <mlpack/core.hpp>
#include <mlpack/methods/amf/amf.hpp>
#include <mlpack/methods/amf/init_rules/random_acol_init.hpp>
+#include <mlpack/methods/amf/init_rules/given_init.hpp>
#include <mlpack/methods/amf/update_rules/nmf_mult_div.hpp>
#include <mlpack/methods/amf/update_rules/nmf_als.hpp>
#include <mlpack/methods/amf/update_rules/nmf_mult_dist.hpp>
@@ -131,7 +132,6 @@ BOOST_AUTO_TEST_CASE(SparseNMFAcolDistTest)
while (sparseResidue != sparseResidue && denseResidue != denseResidue)
{
- mlpack::math::RandomSeed(std::time(NULL));
mat w, h;
sp_mat v;
v.sprandu(20, 20, 0.3);
@@ -143,11 +143,16 @@ BOOST_AUTO_TEST_CASE(SparseNMFAcolDistTest)
size_t r = 15;
SimpleResidueTermination srt(1e-10, 10000);
- AMF<SimpleResidueTermination, RandomAcolInitialization<> > nmf(srt);
- const size_t seed = mlpack::math::RandInt(1000000);
- mlpack::math::RandomSeed(seed); // Set random seed so results are the same.
+
+ // Get an initialization.
+ arma::mat iw, ih;
+ RandomAcolInitialization<>::Initialize(v, r, iw, ih);
+ GivenInitialization g(std::move(iw), std::move(ih));
+
+ // The GivenInitialization will force the same initialization for both
+ // Apply() calls.
+ AMF<SimpleResidueTermination, GivenInitialization> nmf(srt, g);
nmf.Apply(v, r, w, h);
- mlpack::math::RandomSeed(seed);
nmf.Apply(dv, r, dw, dh);
// Reconstruct matrices.
diff --git a/src/mlpack/tests/range_search_test.cpp b/src/mlpack/tests/range_search_test.cpp
index 1c9f73b..43a8ace 100644
--- a/src/mlpack/tests/range_search_test.cpp
+++ b/src/mlpack/tests/range_search_test.cpp
@@ -1244,8 +1244,6 @@ BOOST_AUTO_TEST_CASE(MoveTrainTest)
BOOST_AUTO_TEST_CASE(RSModelTest)
{
- math::RandomSeed(std::time(NULL));
-
// Ensure that we can build an RSModel and get correct results.
arma::mat queryData = arma::randu<arma::mat>(10, 50);
arma::mat referenceData = arma::randu<arma::mat>(10, 200);
More information about the mlpack-git
mailing list