[mlpack-git] master: Add tests for distribution Serialize(). (2236fe0)
gitdub at big.cc.gt.atl.ga.us
gitdub at big.cc.gt.atl.ga.us
Fri Jul 10 18:59:26 EDT 2015
Repository : https://github.com/mlpack/mlpack
On branch : master
Link : https://github.com/mlpack/mlpack/compare/4a97187bbba7ce8a6191b714949dd818ef0f37d2...e5905e62c15d1bcff21e6359b11efcd7ab6d7ca0
>---------------------------------------------------------------
commit 2236fe0bdc2dcec4b7976ef29cb4c6b9970a750e
Author: ryan <ryan at ratml.org>
Date: Fri Apr 17 12:29:08 2015 -0400
Add tests for distribution Serialize().
>---------------------------------------------------------------
2236fe0bdc2dcec4b7976ef29cb4c6b9970a750e
src/mlpack/tests/serialization_test.cpp | 258 ++++++++++++++++++++++++++++++++
1 file changed, 258 insertions(+)
diff --git a/src/mlpack/tests/serialization_test.cpp b/src/mlpack/tests/serialization_test.cpp
index 878f5f6..e34f417 100644
--- a/src/mlpack/tests/serialization_test.cpp
+++ b/src/mlpack/tests/serialization_test.cpp
@@ -16,7 +16,11 @@
#include <boost/test/unit_test.hpp>
#include "old_boost_test_definitions.hpp"
+#include <mlpack/core/dists/regression_distribution.hpp>
+
using namespace mlpack;
+using namespace mlpack::distribution;
+using namespace mlpack::regression;
using namespace arma;
using namespace boost;
using namespace boost::archive;
@@ -75,6 +79,8 @@ void TestArmadilloSerialization(MatType& x)
BOOST_REQUIRE_SMALL(double(x(j, i)), 1e-8);
else
BOOST_REQUIRE_CLOSE(double(orig(j, i)), double(x(j, i)), 1e-8);
+
+ remove("test");
}
// Test all serialization strategies.
@@ -160,4 +166,256 @@ BOOST_AUTO_TEST_CASE(EmptySparseMatrixSerializeTest)
TestAllArmadilloSerialization(m);
}
+// Save and load an mlpack object.
+// The re-loaded copy is placed in 'newT'.
+template<typename T, typename IArchiveType, typename OArchiveType>
+void SerializeObject(T& t, T& newT)
+{
+ ofstream ofs("test");
+ OArchiveType o(ofs);
+
+ bool success = true;
+ try
+ {
+ o << data::CreateNVP(t, "t");
+ }
+ catch (archive_exception& e)
+ {
+ success = false;
+ }
+ ofs.close();
+
+ BOOST_REQUIRE_EQUAL(success, true);
+
+ ifstream ifs("test");
+ IArchiveType i(ifs);
+
+ try
+ {
+ i >> data::CreateNVP(newT, "t");
+ }
+ catch (archive_exception& e)
+ {
+ success = false;
+ }
+ ifs.close();
+
+ BOOST_REQUIRE_EQUAL(success, true);
+}
+
+// Test mlpack serialization with all three archive types.
+template<typename T>
+void SerializeObjectAll(T& t, T& xmlT, T& textT, T& binaryT)
+{
+ SerializeObject<T, xml_iarchive, xml_oarchive>(t, xmlT);
+ SerializeObject<T, text_iarchive, text_oarchive>(t, textT);
+ SerializeObject<T, binary_iarchive, binary_oarchive>(t, binaryT);
+}
+
+// Utility function to check the equality of two Armadillo matrices.
+void CheckMatrices(const mat& x,
+ const mat& xmlX,
+ const mat& textX,
+ const mat& binaryX)
+{
+ // First check dimensions.
+ BOOST_REQUIRE_EQUAL(x.n_rows, xmlX.n_rows);
+ BOOST_REQUIRE_EQUAL(x.n_rows, textX.n_rows);
+ BOOST_REQUIRE_EQUAL(x.n_rows, binaryX.n_rows);
+
+ BOOST_REQUIRE_EQUAL(x.n_cols, xmlX.n_cols);
+ BOOST_REQUIRE_EQUAL(x.n_cols, textX.n_cols);
+ BOOST_REQUIRE_EQUAL(x.n_cols, binaryX.n_cols);
+
+ BOOST_REQUIRE_EQUAL(x.n_elem, xmlX.n_elem);
+ BOOST_REQUIRE_EQUAL(x.n_elem, textX.n_elem);
+ BOOST_REQUIRE_EQUAL(x.n_elem, binaryX.n_elem);
+
+ // Now check elements.
+ for (size_t i = 0; i < x.n_elem; ++i)
+ {
+ const double val = x[i];
+ if (val == 0.0)
+ {
+ BOOST_REQUIRE_SMALL(xmlX[i], 1e-8);
+ BOOST_REQUIRE_SMALL(textX[i], 1e-8);
+ BOOST_REQUIRE_SMALL(binaryX[i], 1e-8);
+ }
+ else
+ {
+ BOOST_REQUIRE_CLOSE(val, xmlX[i], 1e-8);
+ BOOST_REQUIRE_CLOSE(val, textX[i], 1e-8);
+ BOOST_REQUIRE_CLOSE(val, binaryX[i], 1e-8);
+ }
+ }
+}
+
+// Now, test mlpack objects.
+BOOST_AUTO_TEST_CASE(DiscreteDistributionTest)
+{
+ // I assume that I am properly saving vectors, so, this should be
+ // straightforward.
+ vec prob;
+ prob.randu(12);
+ DiscreteDistribution t(prob);
+
+ DiscreteDistribution xmlT, textT, binaryT;
+
+ // Load and save with all serializers.
+ SerializeObjectAll(t, xmlT, textT, binaryT);
+
+ for (size_t i = 0; i < 12; ++i)
+ {
+ vec obs(1);
+ obs[0] = i;
+ const double prob = t.Probability(obs);
+ if (prob == 0.0)
+ {
+ BOOST_REQUIRE_SMALL(xmlT.Probability(obs), 1e-8);
+ BOOST_REQUIRE_SMALL(textT.Probability(obs), 1e-8);
+ BOOST_REQUIRE_SMALL(binaryT.Probability(obs), 1e-8);
+ }
+ else
+ {
+ BOOST_REQUIRE_CLOSE(prob, xmlT.Probability(obs), 1e-8);
+ BOOST_REQUIRE_CLOSE(prob, textT.Probability(obs), 1e-8);
+ BOOST_REQUIRE_CLOSE(prob, binaryT.Probability(obs), 1e-8);
+ }
+ }
+}
+
+BOOST_AUTO_TEST_CASE(GaussianDistributionTest)
+{
+ vec mean(10);
+ mean.randu();
+ // Generate a covariance matrix.
+ mat cov;
+ cov.randu(10, 10);
+ cov = (cov * cov.t());
+
+ GaussianDistribution g(mean, cov);
+ GaussianDistribution xmlG, textG, binaryG;
+
+ SerializeObjectAll(g, xmlG, textG, binaryG);
+
+ BOOST_REQUIRE_EQUAL(g.Dimensionality(), xmlG.Dimensionality());
+ BOOST_REQUIRE_EQUAL(g.Dimensionality(), textG.Dimensionality());
+ BOOST_REQUIRE_EQUAL(g.Dimensionality(), binaryG.Dimensionality());
+
+ // First, check the means.
+ CheckMatrices(g.Mean(), xmlG.Mean(), textG.Mean(), binaryG.Mean());
+
+ // Now, check the covariance.
+ CheckMatrices(g.Covariance(), xmlG.Covariance(), textG.Covariance(),
+ binaryG.Covariance());
+
+ // Lastly, run some observations through and make sure the probability is the
+ // same. This should test anything cached internally.
+ arma::mat randomObs;
+ randomObs.randu(10, 500);
+
+ for (size_t i = 0; i < 500; ++i)
+ {
+ const double prob = g.Probability(randomObs.unsafe_col(i));
+
+ if (prob == 0.0)
+ {
+ BOOST_REQUIRE_SMALL(xmlG.Probability(randomObs.unsafe_col(i)), 1e-8);
+ BOOST_REQUIRE_SMALL(textG.Probability(randomObs.unsafe_col(i)), 1e-8);
+ BOOST_REQUIRE_SMALL(binaryG.Probability(randomObs.unsafe_col(i)), 1e-8);
+ }
+ else
+ {
+ BOOST_REQUIRE_CLOSE(prob, xmlG.Probability(randomObs.unsafe_col(i)),
+ 1e-8);
+ BOOST_REQUIRE_CLOSE(prob, textG.Probability(randomObs.unsafe_col(i)),
+ 1e-8);
+ BOOST_REQUIRE_CLOSE(prob, binaryG.Probability(randomObs.unsafe_col(i)),
+ 1e-8);
+ }
+ }
+}
+
+BOOST_AUTO_TEST_CASE(LaplaceDistributionTest)
+{
+ vec mean(20);
+ mean.randu();
+
+ LaplaceDistribution l(mean, 2.5);
+ LaplaceDistribution xmlL, textL, binaryL;
+
+ SerializeObjectAll(l, xmlL, textL, binaryL);
+
+ BOOST_REQUIRE_CLOSE(l.Scale(), xmlL.Scale(), 1e-8);
+ BOOST_REQUIRE_CLOSE(l.Scale(), textL.Scale(), 1e-8);
+ BOOST_REQUIRE_CLOSE(l.Scale(), binaryL.Scale(), 1e-8);
+
+ CheckMatrices(l.Mean(), xmlL.Mean(), textL.Mean(), binaryL.Mean());
+}
+
+BOOST_AUTO_TEST_CASE(LinearRegressionTest)
+{
+ // Generate some random data.
+ mat data;
+ data.randn(15, 800);
+ vec responses;
+ responses.randn(800, 1);
+
+ LinearRegression lr(data, responses, 0.05); // Train the model.
+ LinearRegression xmlLr, textLr, binaryLr;
+
+ SerializeObjectAll(lr, xmlLr, textLr, binaryLr);
+
+ BOOST_REQUIRE_CLOSE(lr.Lambda(), xmlLr.Lambda(), 1e-8);
+ BOOST_REQUIRE_CLOSE(lr.Lambda(), textLr.Lambda(), 1e-8);
+ BOOST_REQUIRE_CLOSE(lr.Lambda(), binaryLr.Lambda(), 1e-8);
+
+ CheckMatrices(lr.Parameters(), xmlLr.Parameters(), textLr.Parameters(),
+ binaryLr.Parameters());
+}
+
+BOOST_AUTO_TEST_CASE(RegressionDistributionTest)
+{
+ // Generate some random data.
+ mat data;
+ data.randn(15, 800);
+ vec responses;
+ responses.randn(800, 1);
+
+ RegressionDistribution rd(data, responses);
+ RegressionDistribution xmlRd, textRd, binaryRd;
+
+ // Okay, now save it and load it.
+ SerializeObjectAll(rd, xmlRd, textRd, binaryRd);
+
+ // Check the gaussian distribution.
+ CheckMatrices(rd.Err().Mean(),
+ xmlRd.Err().Mean(),
+ textRd.Err().Mean(),
+ binaryRd.Err().Mean());
+ CheckMatrices(rd.Err().Covariance(),
+ xmlRd.Err().Covariance(),
+ textRd.Err().Covariance(),
+ binaryRd.Err().Covariance());
+
+ // Check the regression function.
+ if (rd.Rf().Lambda() == 0.0)
+ {
+ BOOST_REQUIRE_SMALL(xmlRd.Rf().Lambda(), 1e-8);
+ BOOST_REQUIRE_SMALL(textRd.Rf().Lambda(), 1e-8);
+ BOOST_REQUIRE_SMALL(binaryRd.Rf().Lambda(), 1e-8);
+ }
+ else
+ {
+ BOOST_REQUIRE_CLOSE(rd.Rf().Lambda(), xmlRd.Rf().Lambda(), 1e-8);
+ BOOST_REQUIRE_CLOSE(rd.Rf().Lambda(), textRd.Rf().Lambda(), 1e-8);
+ BOOST_REQUIRE_CLOSE(rd.Rf().Lambda(), binaryRd.Rf().Lambda(), 1e-8);
+ }
+
+ CheckMatrices(rd.Rf().Parameters(),
+ xmlRd.Rf().Parameters(),
+ textRd.Rf().Parameters(),
+ binaryRd.Rf().Parameters());
+}
+
BOOST_AUTO_TEST_SUITE_END();
More information about the mlpack-git
mailing list