[mlpack-git] master: Test Serialize(). Clean up test, too. (c2107ad)
gitdub at big.cc.gt.atl.ga.us
gitdub at big.cc.gt.atl.ga.us
Tue Dec 8 11:11:08 EST 2015
Repository : https://github.com/mlpack/mlpack
On branch : master
Link : https://github.com/mlpack/mlpack/compare/157595c68e3d26679e90152f07e1ee28e5e563c2...fc50782ab165567b0f04b11534b4ddc499262330
>---------------------------------------------------------------
commit c2107ad52332204be2e7706dd901ec6f0bf72688
Author: Ryan Curtin <ryan at ratml.org>
Date: Mon Dec 7 16:13:12 2015 +0000
Test Serialize(). Clean up test, too.
>---------------------------------------------------------------
c2107ad52332204be2e7706dd901ec6f0bf72688
src/mlpack/tests/adaboost_test.cpp | 220 +++++++++++++++++++++++++++----------
1 file changed, 165 insertions(+), 55 deletions(-)
diff --git a/src/mlpack/tests/adaboost_test.cpp b/src/mlpack/tests/adaboost_test.cpp
index 2e8fff1..829438e 100644
--- a/src/mlpack/tests/adaboost_test.cpp
+++ b/src/mlpack/tests/adaboost_test.cpp
@@ -4,16 +4,18 @@
*
* Tests for AdaBoost class.
*/
-
#include <mlpack/core.hpp>
#include <mlpack/methods/adaboost/adaboost.hpp>
#include <boost/test/unit_test.hpp>
#include "old_boost_test_definitions.hpp"
+#include "serialization.hpp"
-using namespace mlpack;
using namespace arma;
+using namespace mlpack;
using namespace mlpack::adaboost;
+using namespace mlpack::decision_stump;
+using namespace mlpack::perceptron;
BOOST_AUTO_TEST_SUITE(AdaBoostTest);
@@ -36,7 +38,7 @@ BOOST_AUTO_TEST_CASE(HammingLossBoundIris)
// Run the perceptron for perceptronIter iterations.
int perceptronIter = 400;
- perceptron::Perceptron<> p(inputData, labels.row(0), max(labels.row(0)) + 1,
+ Perceptron<> p(inputData, labels.row(0), max(labels.row(0)) + 1,
perceptronIter);
// Define parameters for AdaBoost.
@@ -53,8 +55,7 @@ BOOST_AUTO_TEST_CASE(HammingLossBoundIris)
countError++;
double hammingLoss = (double) countError / labels.n_cols;
- double ztP = a.GetztProduct();
- BOOST_REQUIRE_LE(hammingLoss, ztP);
+ BOOST_REQUIRE_LE(hammingLoss, a.ZtProduct());
}
/**
@@ -79,7 +80,7 @@ BOOST_AUTO_TEST_CASE(WeakLearnerErrorIris)
int perceptronIter = 400;
arma::Row<size_t> perceptronPrediction(labels.n_cols);
- perceptron::Perceptron<> p(inputData, labels.row(0), max(labels.row(0)) + 1,
+ Perceptron<> p(inputData, labels.row(0), max(labels.row(0)) + 1,
perceptronIter);
p.Classify(inputData, perceptronPrediction);
@@ -124,7 +125,7 @@ BOOST_AUTO_TEST_CASE(HammingLossBoundVertebralColumn)
// Define your own weak learner, perceptron in this case.
// Run the perceptron for perceptronIter iterations.
size_t perceptronIter = 800;
- perceptron::Perceptron<> p(inputData, labels.row(0), max(labels.row(0)) + 1,
+ Perceptron<> p(inputData, labels.row(0), max(labels.row(0)) + 1,
perceptronIter);
// Define parameters for AdaBoost.
@@ -141,8 +142,7 @@ BOOST_AUTO_TEST_CASE(HammingLossBoundVertebralColumn)
countError++;
double hammingLoss = (double) countError / labels.n_cols;
- double ztP = a.GetztProduct();
- BOOST_REQUIRE_LE(hammingLoss, ztP);
+ BOOST_REQUIRE_LE(hammingLoss, a.ZtProduct());
}
/**
@@ -164,8 +164,8 @@ BOOST_AUTO_TEST_CASE(WeakLearnerErrorVertebralColumn)
// Run the perceptron for perceptronIter iterations.
size_t perceptronIter = 800;
- arma::Row<size_t> perceptronPrediction(labels.n_cols);
- perceptron::Perceptron<> p(inputData, labels.row(0), max(labels.row(0)) + 1,
+ Row<size_t> perceptronPrediction(labels.n_cols);
+ Perceptron<> p(inputData, labels.row(0), max(labels.row(0)) + 1,
perceptronIter);
p.Classify(inputData, perceptronPrediction);
@@ -210,7 +210,7 @@ BOOST_AUTO_TEST_CASE(HammingLossBoundNonLinearSepData)
// Define your own weak learner, perceptron in this case.
// Run the perceptron for perceptronIter iterations.
size_t perceptronIter = 800;
- perceptron::Perceptron<> p(inputData, labels.row(0), max(labels.row(0)) + 1,
+ Perceptron<> p(inputData, labels.row(0), max(labels.row(0)) + 1,
perceptronIter);
// Define parameters for AdaBoost.
@@ -227,8 +227,7 @@ BOOST_AUTO_TEST_CASE(HammingLossBoundNonLinearSepData)
countError++;
double hammingLoss = (double) countError / labels.n_cols;
- double ztP = a.GetztProduct();
- BOOST_REQUIRE_LE(hammingLoss, ztP);
+ BOOST_REQUIRE_LE(hammingLoss, a.ZtProduct());
}
/**
@@ -250,8 +249,8 @@ BOOST_AUTO_TEST_CASE(WeakLearnerErrorNonLinearSepData)
// Run the perceptron for perceptronIter iterations.
size_t perceptronIter = 800;
- arma::Row<size_t> perceptronPrediction(labels.n_cols);
- perceptron::Perceptron<> p(inputData, labels.row(0), max(labels.row(0)) + 1,
+ Row<size_t> perceptronPrediction(labels.n_cols);
+ Perceptron<> p(inputData, labels.row(0), max(labels.row(0)) + 1,
perceptronIter);
p.Classify(inputData, perceptronPrediction);
@@ -296,14 +295,13 @@ BOOST_AUTO_TEST_CASE(HammingLossIris_DS)
// Define your own weak learner, decision stumps in this case.
const size_t numClasses = 3;
const size_t inpBucketSize = 6;
- decision_stump::DecisionStump<> ds(inputData, labels.row(0),
- numClasses, inpBucketSize);
+ DecisionStump<> ds(inputData, labels.row(0), numClasses, inpBucketSize);
// Define parameters for AdaBoost.
size_t iterations = 50;
double tolerance = 1e-10;
- AdaBoost<arma::mat, decision_stump::DecisionStump<>> a(inputData,
- labels.row(0), ds, iterations, tolerance);
+ AdaBoost<mat, DecisionStump<>> a(inputData, labels.row(0), ds, iterations,
+ tolerance);
arma::Row<size_t> predictedLabels;
a.Classify(inputData, predictedLabels);
@@ -314,8 +312,7 @@ BOOST_AUTO_TEST_CASE(HammingLossIris_DS)
countError++;
double hammingLoss = (double) countError / labels.n_cols;
- double ztP = a.GetztProduct();
- BOOST_REQUIRE_LE(hammingLoss, ztP);
+ BOOST_REQUIRE_LE(hammingLoss, a.ZtProduct());
}
/**
@@ -342,8 +339,7 @@ BOOST_AUTO_TEST_CASE(WeakLearnerErrorIris_DS)
arma::Row<size_t> dsPrediction(labels.n_cols);
- decision_stump::DecisionStump<> ds(inputData, labels.row(0), numClasses,
- inpBucketSize);
+ DecisionStump<> ds(inputData, labels.row(0), numClasses, inpBucketSize);
ds.Classify(inputData, dsPrediction);
size_t countWeakLearnerError = 0;
@@ -356,8 +352,8 @@ BOOST_AUTO_TEST_CASE(WeakLearnerErrorIris_DS)
size_t iterations = 50;
double tolerance = 1e-10;
- AdaBoost<arma::mat, decision_stump::DecisionStump<>> a(inputData,
- labels.row(0), ds, iterations, tolerance);
+ AdaBoost<mat, DecisionStump<>> a(inputData, labels.row(0), ds, iterations,
+ tolerance);
arma::Row<size_t> predictedLabels;
a.Classify(inputData, predictedLabels);
@@ -391,15 +387,14 @@ BOOST_AUTO_TEST_CASE(HammingLossBoundVertebralColumn_DS)
const size_t numClasses = 3;
const size_t inpBucketSize = 6;
- decision_stump::DecisionStump<> ds(inputData, labels.row(0),
- numClasses, inpBucketSize);
+ DecisionStump<> ds(inputData, labels.row(0), numClasses, inpBucketSize);
// Define parameters for AdaBoost.
size_t iterations = 50;
double tolerance = 1e-10;
- AdaBoost<arma::mat, decision_stump::DecisionStump<>> a(inputData,
- labels.row(0), ds, iterations, tolerance);
+ AdaBoost<mat, DecisionStump<>> a(inputData, labels.row(0), ds, iterations,
+ tolerance);
arma::Row<size_t> predictedLabels;
a.Classify(inputData, predictedLabels);
@@ -410,8 +405,7 @@ BOOST_AUTO_TEST_CASE(HammingLossBoundVertebralColumn_DS)
countError++;
double hammingLoss = (double) countError / labels.n_cols;
- double ztP = a.GetztProduct();
- BOOST_REQUIRE_LE(hammingLoss, ztP);
+ BOOST_REQUIRE_LE(hammingLoss, a.ZtProduct());
}
/**
@@ -435,8 +429,7 @@ BOOST_AUTO_TEST_CASE(WeakLearnerErrorVertebralColumn_DS)
const size_t inpBucketSize = 6;
arma::Row<size_t> dsPrediction(labels.n_cols);
- decision_stump::DecisionStump<> ds(inputData, labels.row(0), numClasses,
- inpBucketSize);
+ DecisionStump<> ds(inputData, labels.row(0), numClasses, inpBucketSize);
size_t countWeakLearnerError = 0;
for (size_t i = 0; i < labels.n_cols; i++)
@@ -448,8 +441,8 @@ BOOST_AUTO_TEST_CASE(WeakLearnerErrorVertebralColumn_DS)
// Define parameters for AdaBoost.
size_t iterations = 50;
double tolerance = 1e-10;
- AdaBoost<arma::mat, decision_stump::DecisionStump<>> a(inputData,
- labels.row(0), ds, iterations, tolerance);
+ AdaBoost<mat, DecisionStump<>> a(inputData, labels.row(0), ds, iterations,
+ tolerance);
arma::Row<size_t> predictedLabels;
a.Classify(inputData, predictedLabels);
@@ -482,15 +475,14 @@ BOOST_AUTO_TEST_CASE(HammingLossBoundNonLinearSepData_DS)
const size_t numClasses = 2;
const size_t inpBucketSize = 6;
- decision_stump::DecisionStump<> ds(inputData, labels.row(0),
- numClasses, inpBucketSize);
+ DecisionStump<> ds(inputData, labels.row(0), numClasses, inpBucketSize);
// Define parameters for Adaboost.
size_t iterations = 50;
double tolerance = 1e-10;
- AdaBoost<arma::mat, mlpack::decision_stump::DecisionStump<> > a(inputData,
- labels.row(0), ds, iterations, tolerance);
+ AdaBoost<mat, DecisionStump<> > a(inputData, labels.row(0), ds, iterations,
+ tolerance);
arma::Row<size_t> predictedLabels;
a.Classify(inputData, predictedLabels);
@@ -501,8 +493,7 @@ BOOST_AUTO_TEST_CASE(HammingLossBoundNonLinearSepData_DS)
countError++;
double hammingLoss = (double) countError / labels.n_cols;
- double ztP = a.GetztProduct();
- BOOST_REQUIRE_LE(hammingLoss, ztP);
+ BOOST_REQUIRE_LE(hammingLoss, a.ZtProduct());
}
/**
@@ -527,8 +518,7 @@ BOOST_AUTO_TEST_CASE(WeakLearnerErrorNonLinearSepData_DS)
arma::Row<size_t> dsPrediction(labels.n_cols);
- decision_stump::DecisionStump<> ds(inputData, labels.row(0),
- numClasses, inpBucketSize);
+ DecisionStump<> ds(inputData, labels.row(0), numClasses, inpBucketSize);
size_t countWeakLearnerError = 0;
for (size_t i = 0; i < labels.n_cols; i++)
@@ -540,8 +530,8 @@ BOOST_AUTO_TEST_CASE(WeakLearnerErrorNonLinearSepData_DS)
size_t iterations = 500;
double tolerance = 1e-23;
- AdaBoost<arma::mat, mlpack::decision_stump::DecisionStump<> > a(inputData,
- labels.row(0), ds, iterations, tolerance);
+ AdaBoost<mat, DecisionStump<> > a(inputData, labels.row(0), ds, iterations,
+ tolerance);
arma::Row<size_t> predictedLabels;
a.Classify(inputData, predictedLabels);
@@ -585,8 +575,8 @@ BOOST_AUTO_TEST_CASE(ClassifyTest_VERTEBRALCOL)
if (!data::Load("vc2_test_labels.txt",trueTestLabels))
BOOST_FAIL("Cannot load labels for vc2_test_labels.txt");
- arma::Row<size_t> perceptronPrediction(labels.n_cols);
- perceptron::Perceptron<> p(inputData, labels.row(0), max(labels.row(0)) + 1,
+ Row<size_t> perceptronPrediction(labels.n_cols);
+ Perceptron<> p(inputData, labels.row(0), max(labels.row(0)) + 1,
perceptronIter);
p.Classify(inputData, perceptronPrediction);
@@ -638,14 +628,13 @@ BOOST_AUTO_TEST_CASE(ClassifyTest_NONLINSEP)
arma::Row<size_t> dsPrediction(labels.n_cols);
- decision_stump::DecisionStump<> ds(inputData, labels.row(0),
- numClasses, inpBucketSize);
+ DecisionStump<> ds(inputData, labels.row(0), numClasses, inpBucketSize);
// Define parameters for AdaBoost.
size_t iterations = 50;
double tolerance = 1e-10;
- AdaBoost<arma::mat, mlpack::decision_stump::DecisionStump<> > a(
- inputData, labels.row(0), ds, iterations, tolerance);
+ AdaBoost<mat, DecisionStump<> > a(inputData, labels.row(0), ds, iterations,
+ tolerance);
arma::Row<size_t> predictedLabels(testData.n_cols);
a.Classify(testData, predictedLabels);
@@ -679,7 +668,7 @@ BOOST_AUTO_TEST_CASE(ClassifyTest_IRIS)
// Run the perceptron for perceptronIter iterations.
size_t perceptronIter = 800;
- perceptron::Perceptron<> p(inputData, labels.row(0), max(labels.row(0)) + 1,
+ Perceptron<> p(inputData, labels.row(0), max(labels.row(0)) + 1,
perceptronIter);
// Define parameters for AdaBoost.
@@ -723,7 +712,7 @@ BOOST_AUTO_TEST_CASE(TrainTest)
BOOST_FAIL("Cannot load labels for iris_train_labels.csv");
size_t perceptronIter = 800;
- perceptron::Perceptron<> p(inputData, labels.row(0), max(labels.row(0)) + 1,
+ Perceptron<> p(inputData, labels.row(0), max(labels.row(0)) + 1,
perceptronIter);
// Now train AdaBoost.
@@ -737,7 +726,7 @@ BOOST_AUTO_TEST_CASE(TrainTest)
if (!data::Load("vc2_labels.txt", labels))
BOOST_FAIL("Cannot load labels for vc2_labels.txt");
- perceptron::Perceptron<> p2(inputData, labels.row(0), max(labels.row(0)) + 1,
+ Perceptron<> p2(inputData, labels.row(0), max(labels.row(0)) + 1,
perceptronIter);
a.Train(inputData, labels.row(0), p2, iterations, tolerance);
@@ -765,4 +754,125 @@ BOOST_AUTO_TEST_CASE(TrainTest)
BOOST_REQUIRE_LE(lError, 0.30);
}
+BOOST_AUTO_TEST_CASE(PerceptronSerializationTest)
+{
+ // Build an AdaBoost object.
+ mat data = randu<mat>(10, 500);
+ Row<size_t> labels(500);
+ for (size_t i = 0; i < 250; ++i)
+ labels[i] = 0;
+ for (size_t i = 250; i < 500; ++i)
+ labels[i] = 1;
+
+ Perceptron<> p(data, labels, 2, 800);
+ AdaBoost<> ab(data, labels, p, 50, 1e-10);
+
+ // Now create another dataset to train with.
+ mat otherData = randu<mat>(5, 200);
+ Row<size_t> otherLabels(200);
+ for (size_t i = 0; i < 100; ++i)
+ otherLabels[i] = 1;
+ for (size_t i = 100; i < 150; ++i)
+ otherLabels[i] = 0;
+ for (size_t i = 150; i < 200; ++i)
+ otherLabels[i] = 2;
+
+ Perceptron<> p2(otherData, otherLabels, 3, 500);
+ AdaBoost<> abText(otherData, otherLabels, p2, 50, 1e-10);
+
+ AdaBoost<> abXml, abBinary;
+
+ SerializeObjectAll(ab, abXml, abText, abBinary);
+
+ // Now check that the objects are the same.
+ BOOST_REQUIRE_CLOSE(ab.Tolerance(), abXml.Tolerance(), 1e-5);
+ BOOST_REQUIRE_CLOSE(ab.Tolerance(), abText.Tolerance(), 1e-5);
+ BOOST_REQUIRE_CLOSE(ab.Tolerance(), abBinary.Tolerance(), 1e-5);
+
+ BOOST_REQUIRE_CLOSE(ab.ZtProduct(), abXml.ZtProduct(), 1e-5);
+ BOOST_REQUIRE_CLOSE(ab.ZtProduct(), abText.ZtProduct(), 1e-5);
+ BOOST_REQUIRE_CLOSE(ab.ZtProduct(), abBinary.ZtProduct(), 1e-5);
+
+ BOOST_REQUIRE_EQUAL(ab.WeakLearners(), abXml.WeakLearners());
+ BOOST_REQUIRE_EQUAL(ab.WeakLearners(), abText.WeakLearners());
+ BOOST_REQUIRE_EQUAL(ab.WeakLearners(), abBinary.WeakLearners());
+
+ for (size_t i = 0; i < ab.WeakLearners(); ++i)
+ {
+ CheckMatrices(ab.WeakLearner(i).Weights(),
+ abXml.WeakLearner(i).Weights(),
+ abText.WeakLearner(i).Weights(),
+ abBinary.WeakLearner(i).Weights());
+
+ CheckMatrices(ab.WeakLearner(i).Biases(),
+ abXml.WeakLearner(i).Biases(),
+ abText.WeakLearner(i).Biases(),
+ abBinary.WeakLearner(i).Biases());
+ }
+}
+
+BOOST_AUTO_TEST_CASE(DecisionStumpSerializationTest)
+{
+ // Build an AdaBoost object.
+ mat data = randu<mat>(10, 500);
+ Row<size_t> labels(500);
+ for (size_t i = 0; i < 250; ++i)
+ labels[i] = 0;
+ for (size_t i = 250; i < 500; ++i)
+ labels[i] = 1;
+
+ DecisionStump<> p(data, labels, 2, 800);
+ AdaBoost<mat, DecisionStump<>> ab(data, labels, p, 50, 1e-10);
+
+ // Now create another dataset to train with.
+ mat otherData = randu<mat>(5, 200);
+ Row<size_t> otherLabels(200);
+ for (size_t i = 0; i < 100; ++i)
+ otherLabels[i] = 1;
+ for (size_t i = 100; i < 150; ++i)
+ otherLabels[i] = 0;
+ for (size_t i = 150; i < 200; ++i)
+ otherLabels[i] = 2;
+
+ DecisionStump<> p2(otherData, otherLabels, 3, 500);
+ AdaBoost<mat, DecisionStump<>> abText(otherData, otherLabels, p2, 50, 1e-10);
+
+ AdaBoost<mat, DecisionStump<>> abXml, abBinary;
+
+ SerializeObjectAll(ab, abXml, abText, abBinary);
+
+ // Now check that the objects are the same.
+ BOOST_REQUIRE_CLOSE(ab.Tolerance(), abXml.Tolerance(), 1e-5);
+ BOOST_REQUIRE_CLOSE(ab.Tolerance(), abText.Tolerance(), 1e-5);
+ BOOST_REQUIRE_CLOSE(ab.Tolerance(), abBinary.Tolerance(), 1e-5);
+
+ BOOST_REQUIRE_CLOSE(ab.ZtProduct(), abXml.ZtProduct(), 1e-5);
+ BOOST_REQUIRE_CLOSE(ab.ZtProduct(), abText.ZtProduct(), 1e-5);
+ BOOST_REQUIRE_CLOSE(ab.ZtProduct(), abBinary.ZtProduct(), 1e-5);
+
+ BOOST_REQUIRE_EQUAL(ab.WeakLearners(), abXml.WeakLearners());
+ BOOST_REQUIRE_EQUAL(ab.WeakLearners(), abText.WeakLearners());
+ BOOST_REQUIRE_EQUAL(ab.WeakLearners(), abBinary.WeakLearners());
+
+ for (size_t i = 0; i < ab.WeakLearners(); ++i)
+ {
+ BOOST_REQUIRE_EQUAL(ab.WeakLearner(i).SplitDimension(),
+ abXml.WeakLearner(i).SplitDimension());
+ BOOST_REQUIRE_EQUAL(ab.WeakLearner(i).SplitDimension(),
+ abText.WeakLearner(i).SplitDimension());
+ BOOST_REQUIRE_EQUAL(ab.WeakLearner(i).SplitDimension(),
+ abBinary.WeakLearner(i).SplitDimension());
+
+ CheckMatrices(ab.WeakLearner(i).Split(),
+ abXml.WeakLearner(i).Split(),
+ abText.WeakLearner(i).Split(),
+ abBinary.WeakLearner(i).Split());
+
+ CheckMatrices(ab.WeakLearner(i).BinLabels(),
+ abXml.WeakLearner(i).BinLabels(),
+ abText.WeakLearner(i).BinLabels(),
+ abBinary.WeakLearner(i).BinLabels());
+ }
+}
+
BOOST_AUTO_TEST_SUITE_END();
More information about the mlpack-git
mailing list