Merge pull request #2675 from jeffin143/lmnn-kde-test
Migrate kde, lmnn , rl and rewardclipping test to catch.
This commit is contained in:
@@ -8,9 +8,7 @@ add_executable(mlpack_test
|
||||
hpt_test.cpp
|
||||
hyperplane_test.cpp
|
||||
init_rules_test.cpp
|
||||
kde_test.cpp
|
||||
linear_svm_test.cpp
|
||||
lmnn_test.cpp
|
||||
local_coordinate_coding_test.cpp
|
||||
log_test.cpp
|
||||
logistic_regression_test.cpp
|
||||
@@ -22,8 +20,6 @@ add_executable(mlpack_test
|
||||
q_learning_test.cpp
|
||||
qdafn_test.cpp
|
||||
random_test.cpp
|
||||
reward_clipping_test.cpp
|
||||
rl_components_test.cpp
|
||||
serialization.cpp
|
||||
serialization.hpp
|
||||
serialization_test.cpp
|
||||
@@ -35,9 +31,7 @@ add_executable(mlpack_test
|
||||
main_tests/det_test.cpp
|
||||
main_tests/emst_test.cpp
|
||||
main_tests/fastmks_test.cpp
|
||||
main_tests/kde_test.cpp
|
||||
main_tests/linear_svm_test.cpp
|
||||
main_tests/lmnn_test.cpp
|
||||
main_tests/local_coordinate_coding_test.cpp
|
||||
main_tests/logistic_regression_test.cpp
|
||||
main_tests/lsh_test.cpp
|
||||
@@ -83,6 +77,7 @@ add_executable(mlpack_catch_test
|
||||
image_load_test.cpp
|
||||
imputation_test.cpp
|
||||
io_test.cpp
|
||||
kde_test.cpp
|
||||
kernel_pca_test.cpp
|
||||
kernel_test.cpp
|
||||
kernel_traits_test.cpp
|
||||
@@ -95,6 +90,7 @@ add_executable(mlpack_catch_test
|
||||
layer_names_test.cpp
|
||||
lin_alg_test.cpp
|
||||
linear_regression_test.cpp
|
||||
lmnn_test.cpp
|
||||
load_save_test.cpp
|
||||
loss_functions_test.cpp
|
||||
main.cpp
|
||||
@@ -120,6 +116,8 @@ add_executable(mlpack_catch_test
|
||||
rectangle_tree_test.cpp
|
||||
recurrent_network_test.cpp
|
||||
regularized_svd_test.cpp
|
||||
reward_clipping_test.cpp
|
||||
rl_components_test.cpp
|
||||
scaling_test.cpp
|
||||
serialization_catch.cpp
|
||||
serialization_catch.hpp
|
||||
@@ -158,12 +156,14 @@ add_executable(mlpack_catch_test
|
||||
main_tests/decision_tree_test.cpp
|
||||
main_tests/hoeffding_tree_test.cpp
|
||||
main_tests/image_converter_test.cpp
|
||||
main_tests/kde_test.cpp
|
||||
main_tests/kernel_pca_test.cpp
|
||||
main_tests/kfn_test.cpp
|
||||
main_tests/kmeans_test.cpp
|
||||
main_tests/knn_test.cpp
|
||||
main_tests/krann_test.cpp
|
||||
main_tests/linear_regression_test.cpp
|
||||
main_tests/lmnn_test.cpp
|
||||
main_tests/mean_shift_test.cpp
|
||||
main_tests/nbc_test.cpp
|
||||
main_tests/nca_test.cpp
|
||||
|
||||
+110
-115
@@ -15,9 +15,8 @@
|
||||
#include <mlpack/core/tree/cover_tree.hpp>
|
||||
#include <mlpack/core/tree/rectangle_tree.hpp>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include "test_tools.hpp"
|
||||
#include "serialization.hpp"
|
||||
#include "catch.hpp"
|
||||
#include "serialization_catch.hpp"
|
||||
|
||||
using namespace mlpack;
|
||||
using namespace mlpack::kde;
|
||||
@@ -27,8 +26,6 @@ using namespace mlpack::kernel;
|
||||
|
||||
using namespace boost::serialization;
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(KDETest);
|
||||
|
||||
// Brute force gaussian KDE.
|
||||
template <typename KernelType>
|
||||
void BruteForceKDE(const arma::mat& reference,
|
||||
@@ -51,7 +48,7 @@ void BruteForceKDE(const arma::mat& reference,
|
||||
/**
|
||||
* Test if simple case is correct according to manually calculated results.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(KDESimpleTest)
|
||||
TEST_CASE("KDESimpleTest", "[KDETest]")
|
||||
{
|
||||
// Transposed reference and query sets because it's easier to read.
|
||||
arma::mat reference = { {-1.0, -1.0},
|
||||
@@ -80,13 +77,13 @@ BOOST_AUTO_TEST_CASE(KDESimpleTest)
|
||||
kde.Train(reference);
|
||||
kde.Evaluate(query, estimations);
|
||||
for (size_t i = 0; i < query.n_cols; ++i)
|
||||
BOOST_REQUIRE_CLOSE(estimations[i], estimationsResult[i], 0.01);
|
||||
REQUIRE(estimations[i] == Approx(estimationsResult[i]).epsilon(0.001));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Train(Tree...) and Evaluate(Tree...).
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(KDETreeAsArguments)
|
||||
TEST_CASE("KDETreeAsArguments", "[KDETest]")
|
||||
{
|
||||
// Transposed reference and query sets because it's easier to read.
|
||||
arma::mat reference = { {-1.0, -1.0},
|
||||
@@ -125,7 +122,7 @@ BOOST_AUTO_TEST_CASE(KDETreeAsArguments)
|
||||
kde.Train(referenceTree, &oldFromNewReferences);
|
||||
kde.Evaluate(queryTree, std::move(oldFromNewQueries), estimations);
|
||||
for (size_t i = 0; i < query.n_cols; ++i)
|
||||
BOOST_REQUIRE_CLOSE(estimations[i], estimationsResult[i], 0.01);
|
||||
REQUIRE(estimations[i] == Approx(estimationsResult[i]).epsilon(0.001));
|
||||
delete queryTree;
|
||||
delete referenceTree;
|
||||
}
|
||||
@@ -133,7 +130,7 @@ BOOST_AUTO_TEST_CASE(KDETreeAsArguments)
|
||||
/**
|
||||
* Test dual-tree implementation results against brute force results.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(GaussianKDEBruteForceTest)
|
||||
TEST_CASE("GaussianKDEBruteForceTest", "[KDETest]")
|
||||
{
|
||||
arma::mat reference = arma::randu(2, 200);
|
||||
arma::mat query = arma::randu(2, 60);
|
||||
@@ -161,13 +158,13 @@ BOOST_AUTO_TEST_CASE(GaussianKDEBruteForceTest)
|
||||
|
||||
// Check whether results are equal.
|
||||
for (size_t i = 0; i < query.n_cols; ++i)
|
||||
BOOST_REQUIRE_CLOSE(bfEstimations[i], treeEstimations[i], relError * 100);
|
||||
REQUIRE(bfEstimations[i] == Approx(treeEstimations[i]).epsilon(relError));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test single-tree implementation results against brute force results.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(GaussianSingleKDEBruteForceTest)
|
||||
TEST_CASE("GaussianSingleKDEBruteForceTest", "[KDETest]")
|
||||
{
|
||||
arma::mat reference = arma::randu(2, 300);
|
||||
arma::mat query = arma::randu(2, 100);
|
||||
@@ -195,14 +192,14 @@ BOOST_AUTO_TEST_CASE(GaussianSingleKDEBruteForceTest)
|
||||
|
||||
// Check whether results are equal.
|
||||
for (size_t i = 0; i < query.n_cols; ++i)
|
||||
BOOST_REQUIRE_CLOSE(bfEstimations[i], treeEstimations[i], relError * 100);
|
||||
REQUIRE(bfEstimations[i] == Approx(treeEstimations[i]).epsilon(relError));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test single-tree implementation results against brute force results using
|
||||
* a cover-tree and Epanechnikov kernel.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(EpanechnikovCoverSingleKDETest)
|
||||
TEST_CASE("EpanechnikovCoverSingleKDETest", "[KDETest]")
|
||||
{
|
||||
arma::mat reference = arma::randu(2, 300);
|
||||
arma::mat query = arma::randu(2, 100);
|
||||
@@ -230,14 +227,14 @@ BOOST_AUTO_TEST_CASE(EpanechnikovCoverSingleKDETest)
|
||||
|
||||
// Check whether results are equal.
|
||||
for (size_t i = 0; i < query.n_cols; ++i)
|
||||
BOOST_REQUIRE_CLOSE(bfEstimations[i], treeEstimations[i], relError * 100);
|
||||
REQUIRE(bfEstimations[i] == Approx(treeEstimations[i]).epsilon(relError));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test single-tree implementation results against brute force results using
|
||||
* a cover-tree and Gaussian kernel.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(GaussianCoverSingleKDETest)
|
||||
TEST_CASE("GaussianCoverSingleKDETest", "[KDETest]")
|
||||
{
|
||||
arma::mat reference = arma::randu(2, 300);
|
||||
arma::mat query = arma::randu(2, 100);
|
||||
@@ -265,14 +262,14 @@ BOOST_AUTO_TEST_CASE(GaussianCoverSingleKDETest)
|
||||
|
||||
// Check whether results are equal.
|
||||
for (size_t i = 0; i < query.n_cols; ++i)
|
||||
BOOST_REQUIRE_CLOSE(bfEstimations[i], treeEstimations[i], relError * 100);
|
||||
REQUIRE(bfEstimations[i] == Approx(treeEstimations[i]).epsilon(relError));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test single-tree implementation results against brute force results using
|
||||
* an octree and Epanechnikov kernel.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(EpanechnikovOctreeSingleKDETest)
|
||||
TEST_CASE("EpanechnikovOctreeSingleKDETest", "[KDETest]")
|
||||
{
|
||||
arma::mat reference = arma::randu(2, 300);
|
||||
arma::mat query = arma::randu(2, 100);
|
||||
@@ -300,13 +297,13 @@ BOOST_AUTO_TEST_CASE(EpanechnikovOctreeSingleKDETest)
|
||||
|
||||
// Check whether results are equal.
|
||||
for (size_t i = 0; i < query.n_cols; ++i)
|
||||
BOOST_REQUIRE_CLOSE(bfEstimations[i], treeEstimations[i], relError * 100);
|
||||
REQUIRE(bfEstimations[i] == Approx(treeEstimations[i]).epsilon(relError));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test BallTree dual-tree implementation results against brute force results.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(BallTreeGaussianKDETest)
|
||||
TEST_CASE("BallTreeGaussianKDETest", "[KDETest]")
|
||||
{
|
||||
arma::mat reference = arma::randu(2, 200);
|
||||
arma::mat query = arma::randu(2, 60);
|
||||
@@ -337,7 +334,7 @@ BOOST_AUTO_TEST_CASE(BallTreeGaussianKDETest)
|
||||
|
||||
// Check whether results are equal.
|
||||
for (size_t i = 0; i < query.n_cols; ++i)
|
||||
BOOST_REQUIRE_CLOSE(bfEstimations[i], treeEstimations[i], relError * 100);
|
||||
REQUIRE(bfEstimations[i] == Approx(treeEstimations[i]).epsilon(relError));
|
||||
|
||||
delete queryTree;
|
||||
delete referenceTree;
|
||||
@@ -346,7 +343,7 @@ BOOST_AUTO_TEST_CASE(BallTreeGaussianKDETest)
|
||||
/**
|
||||
* Test Octree dual-tree implementation results against brute force results.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(OctreeGaussianKDETest)
|
||||
TEST_CASE("OctreeGaussianKDETest", "[KDETest]")
|
||||
{
|
||||
arma::mat reference = arma::randu(2, 500);
|
||||
arma::mat query = arma::randu(2, 200);
|
||||
@@ -374,13 +371,13 @@ BOOST_AUTO_TEST_CASE(OctreeGaussianKDETest)
|
||||
|
||||
// Check whether results are equal.
|
||||
for (size_t i = 0; i < query.n_cols; ++i)
|
||||
BOOST_REQUIRE_CLOSE(bfEstimations[i], treeEstimations[i], relError * 100);
|
||||
REQUIRE(bfEstimations[i] == Approx(treeEstimations[i]).epsilon(relError));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test RTree dual-tree implementation results against brute force results.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(RTreeGaussianKDETest)
|
||||
TEST_CASE("RTreeGaussianKDETest", "[KDETest]")
|
||||
{
|
||||
arma::mat reference = arma::randu(2, 500);
|
||||
arma::mat query = arma::randu(2, 200);
|
||||
@@ -408,14 +405,14 @@ BOOST_AUTO_TEST_CASE(RTreeGaussianKDETest)
|
||||
|
||||
// Check whether results are equal.
|
||||
for (size_t i = 0; i < query.n_cols; ++i)
|
||||
BOOST_REQUIRE_CLOSE(bfEstimations[i], treeEstimations[i], relError * 100);
|
||||
REQUIRE(bfEstimations[i] == Approx(treeEstimations[i]).epsilon(relError));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Standard Cover Tree dual-tree implementation results against brute
|
||||
* force results using Gaussian kernel.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(StandardCoverTreeGaussianKDETest)
|
||||
TEST_CASE("StandardCoverTreeGaussianKDETest", "[KDETest]")
|
||||
{
|
||||
arma::mat reference = arma::randu(2, 500);
|
||||
arma::mat query = arma::randu(2, 200);
|
||||
@@ -443,14 +440,14 @@ BOOST_AUTO_TEST_CASE(StandardCoverTreeGaussianKDETest)
|
||||
|
||||
// Check whether results are equal.
|
||||
for (size_t i = 0; i < query.n_cols; ++i)
|
||||
BOOST_REQUIRE_CLOSE(bfEstimations[i], treeEstimations[i], relError * 100);
|
||||
REQUIRE(bfEstimations[i] == Approx(treeEstimations[i]).epsilon(relError));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Standard Cover Tree dual-tree implementation results against brute
|
||||
* force results using Epanechnikov kernel.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(StandardCoverTreeEpanechnikovKDETest)
|
||||
TEST_CASE("StandardCoverTreeEpanechnikovKDETest", "[KDETest]")
|
||||
{
|
||||
arma::mat reference = arma::randu(2, 500);
|
||||
arma::mat query = arma::randu(2, 200);
|
||||
@@ -478,13 +475,13 @@ BOOST_AUTO_TEST_CASE(StandardCoverTreeEpanechnikovKDETest)
|
||||
|
||||
// Check whether results are equal.
|
||||
for (size_t i = 0; i < query.n_cols; ++i)
|
||||
BOOST_REQUIRE_CLOSE(bfEstimations[i], treeEstimations[i], relError * 100);
|
||||
REQUIRE(bfEstimations[i] == Approx(treeEstimations[i]).epsilon(relError));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test duplicated value in reference matrix.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(DuplicatedReferenceSampleKDETest)
|
||||
TEST_CASE("DuplicatedReferenceSampleKDETest", "[KDETest]")
|
||||
{
|
||||
arma::mat reference = arma::randu(2, 30);
|
||||
arma::mat query = arma::randu(2, 10);
|
||||
@@ -518,7 +515,7 @@ BOOST_AUTO_TEST_CASE(DuplicatedReferenceSampleKDETest)
|
||||
|
||||
// Check whether results are equal.
|
||||
for (size_t i = 0; i < query.n_cols; ++i)
|
||||
BOOST_REQUIRE_CLOSE(bfEstimations[i], treeEstimations[i], relError * 100);
|
||||
REQUIRE(bfEstimations[i] == Approx(treeEstimations[i]).epsilon(relError));
|
||||
|
||||
delete queryTree;
|
||||
delete referenceTree;
|
||||
@@ -527,7 +524,7 @@ BOOST_AUTO_TEST_CASE(DuplicatedReferenceSampleKDETest)
|
||||
/**
|
||||
* Test duplicated value in query matrix.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(DuplicatedQuerySampleKDETest)
|
||||
TEST_CASE("DuplicatedQuerySampleKDETest", "[KDETest]")
|
||||
{
|
||||
arma::mat reference = arma::randu(2, 30);
|
||||
arma::mat query = arma::randu(2, 10);
|
||||
@@ -552,7 +549,7 @@ BOOST_AUTO_TEST_CASE(DuplicatedQuerySampleKDETest)
|
||||
kde.Evaluate(queryTree, oldFromNewQueries, estimations);
|
||||
|
||||
// Check whether results are equal.
|
||||
BOOST_REQUIRE_CLOSE(estimations[2], estimations[3], relError * 100);
|
||||
REQUIRE(estimations[2] == Approx(estimations[3]).epsilon(relError));
|
||||
|
||||
delete queryTree;
|
||||
delete referenceTree;
|
||||
@@ -562,7 +559,7 @@ BOOST_AUTO_TEST_CASE(DuplicatedQuerySampleKDETest)
|
||||
* Test dual-tree breadth-first implementation results against brute force
|
||||
* results.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(BreadthFirstKDETest)
|
||||
TEST_CASE("BreadthFirstKDETest", "[KDETest]")
|
||||
{
|
||||
arma::mat reference = arma::randu(2, 200);
|
||||
arma::mat query = arma::randu(2, 60);
|
||||
@@ -593,13 +590,13 @@ BOOST_AUTO_TEST_CASE(BreadthFirstKDETest)
|
||||
|
||||
// Check whether results are equal.
|
||||
for (size_t i = 0; i < query.n_cols; ++i)
|
||||
BOOST_REQUIRE_CLOSE(bfEstimations[i], treeEstimations[i], relError * 100);
|
||||
REQUIRE(bfEstimations[i] == Approx(treeEstimations[i]).epsilon(relError));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test 1-dimensional implementation results against brute force results.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(OneDimensionalTest)
|
||||
TEST_CASE("OneDimensionalTest", "[KDETest]")
|
||||
{
|
||||
arma::mat reference = arma::randu(1, 200);
|
||||
arma::mat query = arma::randu(1, 60);
|
||||
@@ -627,13 +624,13 @@ BOOST_AUTO_TEST_CASE(OneDimensionalTest)
|
||||
|
||||
// Check whether results are equal.
|
||||
for (size_t i = 0; i < query.n_cols; ++i)
|
||||
BOOST_REQUIRE_CLOSE(bfEstimations[i], treeEstimations[i], relError * 100);
|
||||
REQUIRE(bfEstimations[i] == Approx(treeEstimations[i]).epsilon(relError));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a case where an empty reference set is given to train the model.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(EmptyReferenceTest)
|
||||
TEST_CASE("EmptyReferenceTest", "[KDETest]")
|
||||
{
|
||||
arma::mat reference;
|
||||
arma::mat query = arma::randu(1, 10);
|
||||
@@ -651,13 +648,13 @@ BOOST_AUTO_TEST_CASE(EmptyReferenceTest)
|
||||
kde(relError, 0.0, kernel, KDEMode::DUAL_TREE_MODE, metric);
|
||||
|
||||
// When training using the dataset matrix.
|
||||
BOOST_REQUIRE_THROW(kde.Train(reference), std::invalid_argument);
|
||||
REQUIRE_THROWS_AS(kde.Train(reference), std::invalid_argument);
|
||||
|
||||
// When training using a tree.
|
||||
std::vector<size_t> oldFromNewReferences;
|
||||
typedef KDTree<EuclideanDistance, kde::KDEStat, arma::mat> Tree;
|
||||
Tree* referenceTree = new Tree(reference, oldFromNewReferences, 2);
|
||||
BOOST_REQUIRE_THROW(
|
||||
REQUIRE_THROWS_AS(
|
||||
kde.Train(referenceTree, &oldFromNewReferences), std::invalid_argument);
|
||||
|
||||
delete referenceTree;
|
||||
@@ -666,7 +663,7 @@ BOOST_AUTO_TEST_CASE(EmptyReferenceTest)
|
||||
/**
|
||||
* Tests when reference set values and query set values dimensions don't match.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(EvaluationMatchDimensionsTest)
|
||||
TEST_CASE("EvaluationMatchDimensionsTest", "[KDETest]")
|
||||
{
|
||||
arma::mat reference = arma::randu(3, 10);
|
||||
arma::mat query = arma::randu(1, 10);
|
||||
@@ -685,14 +682,14 @@ BOOST_AUTO_TEST_CASE(EvaluationMatchDimensionsTest)
|
||||
kde.Train(reference);
|
||||
|
||||
// When evaluating using the query dataset matrix.
|
||||
BOOST_REQUIRE_THROW(kde.Evaluate(query, estimations),
|
||||
REQUIRE_THROWS_AS(kde.Evaluate(query, estimations),
|
||||
std::invalid_argument);
|
||||
|
||||
// When evaluating using a query tree.
|
||||
typedef KDTree<EuclideanDistance, kde::KDEStat, arma::mat> Tree;
|
||||
std::vector<size_t> oldFromNewQueries;
|
||||
Tree* queryTree = new Tree(query, oldFromNewQueries, 3);
|
||||
BOOST_REQUIRE_THROW(kde.Evaluate(queryTree, oldFromNewQueries, estimations),
|
||||
REQUIRE_THROWS_AS(kde.Evaluate(queryTree, oldFromNewQueries, estimations),
|
||||
std::invalid_argument);
|
||||
delete queryTree;
|
||||
}
|
||||
@@ -700,7 +697,7 @@ BOOST_AUTO_TEST_CASE(EvaluationMatchDimensionsTest)
|
||||
/**
|
||||
* Tests when an empty query set is given to be evaluated.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(EmptyQuerySetTest)
|
||||
TEST_CASE("EmptyQuerySetTest", "[KDETest]")
|
||||
{
|
||||
arma::mat reference = arma::randu(1, 10);
|
||||
arma::mat query;
|
||||
@@ -720,26 +717,26 @@ BOOST_AUTO_TEST_CASE(EmptyQuerySetTest)
|
||||
kde.Train(reference);
|
||||
|
||||
// The query set must be empty.
|
||||
BOOST_REQUIRE_EQUAL(query.n_cols, 0);
|
||||
REQUIRE(query.n_cols == 0);
|
||||
// When evaluating using the query dataset matrix.
|
||||
BOOST_REQUIRE_NO_THROW(kde.Evaluate(query, estimations));
|
||||
REQUIRE_NOTHROW(kde.Evaluate(query, estimations));
|
||||
|
||||
// When evaluating using a query tree.
|
||||
typedef KDTree<EuclideanDistance, kde::KDEStat, arma::mat> Tree;
|
||||
std::vector<size_t> oldFromNewQueries;
|
||||
Tree* queryTree = new Tree(query, oldFromNewQueries, 3);
|
||||
BOOST_REQUIRE_NO_THROW(
|
||||
REQUIRE_NOTHROW(
|
||||
kde.Evaluate(queryTree, oldFromNewQueries, estimations));
|
||||
delete queryTree;
|
||||
|
||||
// Estimations must be empty.
|
||||
BOOST_REQUIRE_EQUAL(estimations.size(), 0);
|
||||
REQUIRE(estimations.size() == 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests serialiation of KDE models.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(SerializationTest)
|
||||
TEST_CASE("KDESerializationTest", "[KDETest]")
|
||||
{
|
||||
// Initial KDE model to be serialized.
|
||||
const double relError = 0.25;
|
||||
@@ -779,51 +776,51 @@ BOOST_AUTO_TEST_CASE(SerializationTest)
|
||||
SerializeObjectAll(kde, kdeXml, kdeText, kdeBinary);
|
||||
|
||||
// Check everything is correct.
|
||||
BOOST_REQUIRE_CLOSE(kde.RelativeError(), relError, 1e-8);
|
||||
BOOST_REQUIRE_CLOSE(kdeXml.RelativeError(), relError, 1e-8);
|
||||
BOOST_REQUIRE_CLOSE(kdeText.RelativeError(), relError, 1e-8);
|
||||
BOOST_REQUIRE_CLOSE(kdeBinary.RelativeError(), relError, 1e-8);
|
||||
REQUIRE(kde.RelativeError() == Approx(relError).epsilon(1e-10));
|
||||
REQUIRE(kdeXml.RelativeError() == Approx(relError).epsilon(1e-10));
|
||||
REQUIRE(kdeText.RelativeError() == Approx(relError).epsilon(1e-10));
|
||||
REQUIRE(kdeBinary.RelativeError() == Approx(relError).epsilon(1e-10));
|
||||
|
||||
BOOST_REQUIRE_CLOSE(kde.AbsoluteError(), absError, 1e-8);
|
||||
BOOST_REQUIRE_CLOSE(kdeXml.AbsoluteError(), absError, 1e-8);
|
||||
BOOST_REQUIRE_CLOSE(kdeText.AbsoluteError(), absError, 1e-8);
|
||||
BOOST_REQUIRE_CLOSE(kdeBinary.AbsoluteError(), absError, 1e-8);
|
||||
REQUIRE(kde.AbsoluteError() == Approx(absError).epsilon(1e-10));
|
||||
REQUIRE(kdeXml.AbsoluteError() == Approx(absError).epsilon(1e-10));
|
||||
REQUIRE(kdeText.AbsoluteError() == Approx(absError).epsilon(1e-10));
|
||||
REQUIRE(kdeBinary.AbsoluteError() == Approx(absError).epsilon(1e-10));
|
||||
|
||||
BOOST_REQUIRE_EQUAL(kde.IsTrained(), true);
|
||||
BOOST_REQUIRE_EQUAL(kdeXml.IsTrained(), true);
|
||||
BOOST_REQUIRE_EQUAL(kdeText.IsTrained(), true);
|
||||
BOOST_REQUIRE_EQUAL(kdeBinary.IsTrained(), true);
|
||||
REQUIRE(kde.IsTrained() == true);
|
||||
REQUIRE(kdeXml.IsTrained() == true);
|
||||
REQUIRE(kdeText.IsTrained() == true);
|
||||
REQUIRE(kdeBinary.IsTrained() == true);
|
||||
|
||||
const KDEMode mode = KDEMode::DUAL_TREE_MODE;
|
||||
BOOST_REQUIRE_EQUAL(kde.Mode(), mode);
|
||||
BOOST_REQUIRE_EQUAL(kdeXml.Mode(), mode);
|
||||
BOOST_REQUIRE_EQUAL(kdeText.Mode(), mode);
|
||||
BOOST_REQUIRE_EQUAL(kdeBinary.Mode(), mode);
|
||||
REQUIRE(kde.Mode() == mode);
|
||||
REQUIRE(kdeXml.Mode() == mode);
|
||||
REQUIRE(kdeText.Mode() == mode);
|
||||
REQUIRE(kdeBinary.Mode() == mode);
|
||||
|
||||
BOOST_REQUIRE_EQUAL(kde.MonteCarlo(), monteCarlo);
|
||||
BOOST_REQUIRE_EQUAL(kdeXml.MonteCarlo(), monteCarlo);
|
||||
BOOST_REQUIRE_EQUAL(kdeText.MonteCarlo(), monteCarlo);
|
||||
BOOST_REQUIRE_EQUAL(kdeBinary.MonteCarlo(), monteCarlo);
|
||||
REQUIRE(kde.MonteCarlo() == monteCarlo);
|
||||
REQUIRE(kdeXml.MonteCarlo() == monteCarlo);
|
||||
REQUIRE(kdeText.MonteCarlo() == monteCarlo);
|
||||
REQUIRE(kdeBinary.MonteCarlo() == monteCarlo);
|
||||
|
||||
BOOST_REQUIRE_CLOSE(kde.MCProb(), MCProb, 1e-8);
|
||||
BOOST_REQUIRE_CLOSE(kdeXml.MCProb(), MCProb, 1e-8);
|
||||
BOOST_REQUIRE_CLOSE(kdeText.MCProb(), MCProb, 1e-8);
|
||||
BOOST_REQUIRE_CLOSE(kdeBinary.MCProb(), MCProb, 1e-8);
|
||||
REQUIRE(kde.MCProb() == Approx(MCProb).epsilon(1e-10));
|
||||
REQUIRE(kdeXml.MCProb() == Approx(MCProb).epsilon(1e-10));
|
||||
REQUIRE(kdeText.MCProb() == Approx(MCProb).epsilon(1e-10));
|
||||
REQUIRE(kdeBinary.MCProb() == Approx(MCProb).epsilon(1e-10));
|
||||
|
||||
BOOST_REQUIRE_EQUAL(kde.MCInitialSampleSize(), initialSampleSize);
|
||||
BOOST_REQUIRE_EQUAL(kdeXml.MCInitialSampleSize(), initialSampleSize);
|
||||
BOOST_REQUIRE_EQUAL(kdeText.MCInitialSampleSize(), initialSampleSize);
|
||||
BOOST_REQUIRE_EQUAL(kdeBinary.MCInitialSampleSize(), initialSampleSize);
|
||||
REQUIRE(kde.MCInitialSampleSize() == initialSampleSize);
|
||||
REQUIRE(kdeXml.MCInitialSampleSize() == initialSampleSize);
|
||||
REQUIRE(kdeText.MCInitialSampleSize() == initialSampleSize);
|
||||
REQUIRE(kdeBinary.MCInitialSampleSize() == initialSampleSize);
|
||||
|
||||
BOOST_REQUIRE_CLOSE(kde.MCEntryCoef(), entryCoef, 1e-8);
|
||||
BOOST_REQUIRE_CLOSE(kdeXml.MCEntryCoef(), entryCoef, 1e-8);
|
||||
BOOST_REQUIRE_CLOSE(kdeText.MCEntryCoef(), entryCoef, 1e-8);
|
||||
BOOST_REQUIRE_CLOSE(kdeBinary.MCEntryCoef(), entryCoef, 1e-8);
|
||||
REQUIRE(kde.MCEntryCoef() == Approx(entryCoef).epsilon(1e-10));
|
||||
REQUIRE(kdeXml.MCEntryCoef() == Approx(entryCoef).epsilon(1e-10));
|
||||
REQUIRE(kdeText.MCEntryCoef() == Approx(entryCoef).epsilon(1e-10));
|
||||
REQUIRE(kdeBinary.MCEntryCoef() == Approx(entryCoef).epsilon(1e-10));
|
||||
|
||||
BOOST_REQUIRE_CLOSE(kde.MCBreakCoef(), breakCoef, 1e-8);
|
||||
BOOST_REQUIRE_CLOSE(kdeXml.MCBreakCoef(), breakCoef, 1e-8);
|
||||
BOOST_REQUIRE_CLOSE(kdeText.MCBreakCoef(), breakCoef, 1e-8);
|
||||
BOOST_REQUIRE_CLOSE(kdeBinary.MCBreakCoef(), breakCoef, 1e-8);
|
||||
REQUIRE(kde.MCBreakCoef() == Approx(breakCoef).epsilon(1e-10));
|
||||
REQUIRE(kdeXml.MCBreakCoef() == Approx(breakCoef).epsilon(1e-10));
|
||||
REQUIRE(kdeText.MCBreakCoef() == Approx(breakCoef).epsilon(1e-10));
|
||||
REQUIRE(kdeBinary.MCBreakCoef() == Approx(breakCoef).epsilon(1e-10));
|
||||
|
||||
// Test if execution gives the same result.
|
||||
arma::vec xmlEstimations = arma::vec(query.n_cols, arma::fill::zeros);
|
||||
@@ -836,16 +833,16 @@ BOOST_AUTO_TEST_CASE(SerializationTest)
|
||||
|
||||
for (size_t i = 0; i < query.n_cols; ++i)
|
||||
{
|
||||
BOOST_REQUIRE_CLOSE(estimations[i], xmlEstimations[i], relError * 100);
|
||||
BOOST_REQUIRE_CLOSE(estimations[i], textEstimations[i], relError * 100);
|
||||
BOOST_REQUIRE_CLOSE(estimations[i], binEstimations[i], relError * 100);
|
||||
REQUIRE(estimations[i] == Approx(xmlEstimations[i]).epsilon(relError));
|
||||
REQUIRE(estimations[i] == Approx(textEstimations[i]).epsilon(relError));
|
||||
REQUIRE(estimations[i] == Approx(binEstimations[i]).epsilon(relError));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the copy constructor and copy operator works properly.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(CopyConstructor)
|
||||
TEST_CASE("CopyConstructor", "[KDETest]")
|
||||
{
|
||||
arma::mat reference = arma::randu(2, 300);
|
||||
arma::mat query = arma::randu(2, 100);
|
||||
@@ -874,15 +871,15 @@ BOOST_AUTO_TEST_CASE(CopyConstructor)
|
||||
// Check results.
|
||||
for (size_t i = 0; i < query.n_cols; ++i)
|
||||
{
|
||||
BOOST_REQUIRE_CLOSE(estimations1[i], estimations2[i], 1e-10);
|
||||
BOOST_REQUIRE_CLOSE(estimations2[i], estimations3[i], 1e-10);
|
||||
REQUIRE(estimations1[i] == Approx(estimations2[i]).epsilon(1e-12));
|
||||
REQUIRE(estimations2[i] == Approx(estimations3[i]).epsilon(1e-12));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the move constructor works properly.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(MoveConstructor)
|
||||
TEST_CASE("MoveConstructor", "[KDETest]")
|
||||
{
|
||||
arma::mat reference = arma::randu(2, 300);
|
||||
arma::mat query = arma::randu(2, 100);
|
||||
@@ -903,15 +900,15 @@ BOOST_AUTO_TEST_CASE(MoveConstructor)
|
||||
constructor.Evaluate(query, estimations2);
|
||||
|
||||
// Check results.
|
||||
BOOST_REQUIRE_THROW(kde.Evaluate(query, estimations3), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(kde.Evaluate(query, estimations3), std::runtime_error);
|
||||
for (size_t i = 0; i < query.n_cols; ++i)
|
||||
BOOST_REQUIRE_CLOSE(estimations1[i], estimations2[i], 1e-10);
|
||||
REQUIRE(estimations1[i] == Approx(estimations2[i]).epsilon(1e-12));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if an untrained KDE works properly.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(NotTrained)
|
||||
TEST_CASE("NotTrained", "[KDETest]")
|
||||
{
|
||||
arma::mat query = arma::randu(1, 10);
|
||||
std::vector<size_t> oldFromNew;
|
||||
@@ -921,17 +918,17 @@ BOOST_AUTO_TEST_CASE(NotTrained)
|
||||
KDE<>::Tree queryTree(query, oldFromNew);
|
||||
|
||||
// Check results.
|
||||
BOOST_REQUIRE_THROW(kde.Evaluate(query, estimations), std::runtime_error);
|
||||
BOOST_REQUIRE_THROW(kde.Evaluate(&queryTree, oldFromNew, estimations),
|
||||
std::runtime_error);
|
||||
BOOST_REQUIRE_THROW(kde.Evaluate(estimations), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(kde.Evaluate(query, estimations), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(kde.Evaluate(&queryTree, oldFromNew, estimations),
|
||||
std::runtime_error);
|
||||
REQUIRE_THROWS_AS(kde.Evaluate(estimations), std::runtime_error);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test single KD-tree implementation results against brute force results using
|
||||
* Monte Carlo estimations when possible.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(GaussianSingleKDTreeMonteCarloKDE)
|
||||
TEST_CASE("GaussianSingleKDTreeMonteCarloKDE", "[KDETest]")
|
||||
{
|
||||
arma::mat reference = arma::randu(2, 3000);
|
||||
arma::mat query = arma::randu(2, 100);
|
||||
@@ -977,14 +974,14 @@ BOOST_AUTO_TEST_CASE(GaussianSingleKDTreeMonteCarloKDE)
|
||||
++correctResults;
|
||||
}
|
||||
|
||||
BOOST_REQUIRE_GT(correctResults, 70);
|
||||
REQUIRE(correctResults > 70);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test single cover-tree implementation results against brute force results
|
||||
* using Monte Carlo estimations when possible.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(GaussianSingleCoverTreeMonteCarloKDE)
|
||||
TEST_CASE("GaussianSingleCoverTreeMonteCarloKDE", "[KDETest]")
|
||||
{
|
||||
arma::mat reference = arma::randu(2, 3000);
|
||||
arma::mat query = arma::randu(2, 100);
|
||||
@@ -1030,14 +1027,14 @@ BOOST_AUTO_TEST_CASE(GaussianSingleCoverTreeMonteCarloKDE)
|
||||
++correctResults;
|
||||
}
|
||||
|
||||
BOOST_REQUIRE_GT(correctResults, 70);
|
||||
REQUIRE(correctResults > 70);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test single octree implementation results against brute force results
|
||||
* using Monte Carlo estimations when possible.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(GaussianSingleOctreeMonteCarloKDE)
|
||||
TEST_CASE("GaussianSingleOctreeMonteCarloKDE", "[KDETest]")
|
||||
{
|
||||
arma::mat reference = arma::randu(2, 3000);
|
||||
arma::mat query = arma::randu(2, 100);
|
||||
@@ -1083,14 +1080,14 @@ BOOST_AUTO_TEST_CASE(GaussianSingleOctreeMonteCarloKDE)
|
||||
++correctResults;
|
||||
}
|
||||
|
||||
BOOST_REQUIRE_GT(correctResults, 70);
|
||||
REQUIRE(correctResults > 70);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test dual kd-tree implementation results against brute force results
|
||||
* using Monte Carlo estimations when possible.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(GaussianDualKDTreeMonteCarloKDE)
|
||||
TEST_CASE("GaussianDualKDTreeMonteCarloKDE", "[KDETest]")
|
||||
{
|
||||
arma::mat reference = arma::randu(2, 3000);
|
||||
arma::mat query = arma::randu(2, 200);
|
||||
@@ -1136,14 +1133,14 @@ BOOST_AUTO_TEST_CASE(GaussianDualKDTreeMonteCarloKDE)
|
||||
++correctResults;
|
||||
}
|
||||
|
||||
BOOST_REQUIRE_GT(correctResults, 70);
|
||||
REQUIRE(correctResults > 70);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test dual Cover-tree implementation results against brute force results
|
||||
* using Monte Carlo estimations when possible.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(GaussianDualCoverTreeMonteCarloKDE)
|
||||
TEST_CASE("GaussianDualCoverTreeMonteCarloKDE", "[KDETest]")
|
||||
{
|
||||
arma::mat reference = arma::randu(2, 3000);
|
||||
arma::mat query = arma::randu(2, 200);
|
||||
@@ -1189,14 +1186,14 @@ BOOST_AUTO_TEST_CASE(GaussianDualCoverTreeMonteCarloKDE)
|
||||
++correctResults;
|
||||
}
|
||||
|
||||
BOOST_REQUIRE_GT(correctResults, 70);
|
||||
REQUIRE(correctResults > 70);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test dual octree implementation results against brute force results
|
||||
* using Monte Carlo estimations when possible.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(GaussianDualOctreeMonteCarloKDE)
|
||||
TEST_CASE("GaussianDualOctreeMonteCarloKDE", "[KDETest]")
|
||||
{
|
||||
arma::mat reference = arma::randu(2, 3000);
|
||||
arma::mat query = arma::randu(2, 200);
|
||||
@@ -1242,14 +1239,14 @@ BOOST_AUTO_TEST_CASE(GaussianDualOctreeMonteCarloKDE)
|
||||
++correctResults;
|
||||
}
|
||||
|
||||
BOOST_REQUIRE_GT(correctResults, 70);
|
||||
REQUIRE(correctResults > 70);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test dual kd-tree breadth first traversal implementation results against
|
||||
* brute force results using Monte Carlo estimations when possible.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(GaussianBreadthDualKDTreeMonteCarloKDE)
|
||||
TEST_CASE("GaussianBreadthDualKDTreeMonteCarloKDE", "[KDETest]")
|
||||
{
|
||||
arma::mat reference = arma::randu(2, 3000);
|
||||
arma::mat query = arma::randu(2, 200);
|
||||
@@ -1298,7 +1295,5 @@ BOOST_AUTO_TEST_CASE(GaussianBreadthDualKDTreeMonteCarloKDE)
|
||||
++correctResults;
|
||||
}
|
||||
|
||||
BOOST_REQUIRE_GT(correctResults, 70);
|
||||
REQUIRE(correctResults > 70);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END();
|
||||
|
||||
+112
-117
@@ -18,17 +18,14 @@
|
||||
#include <ensmallen.hpp>
|
||||
#include <mlpack/methods/neighbor_search/neighbor_search.hpp>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include "test_tools.hpp"
|
||||
#include "catch.hpp"
|
||||
#include "test_catch_tools.hpp"
|
||||
|
||||
using namespace mlpack;
|
||||
using namespace mlpack::metric;
|
||||
using namespace mlpack::lmnn;
|
||||
using namespace ens;
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(LMNNTest);
|
||||
|
||||
//
|
||||
// Tests for the Constraints.
|
||||
//
|
||||
@@ -37,7 +34,7 @@ BOOST_AUTO_TEST_SUITE(LMNNTest);
|
||||
* The target neighbors function should be correct.
|
||||
* point.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(LMNNTargetNeighborsTest)
|
||||
TEST_CASE("LMNNTargetNeighborsTest", "[LMNNTest]")
|
||||
{
|
||||
// Useful but simple dataset with six points and two classes.
|
||||
arma::mat dataset = "-0.1 -0.1 -0.1 0.1 0.1 0.1;"
|
||||
@@ -59,18 +56,18 @@ BOOST_AUTO_TEST_CASE(LMNNTargetNeighborsTest)
|
||||
|
||||
constraint.TargetNeighbors(targetNeighbors, dataset, labels, norm);
|
||||
|
||||
BOOST_REQUIRE_EQUAL(targetNeighbors(0, 0), 1);
|
||||
BOOST_REQUIRE_EQUAL(targetNeighbors(0, 1), 0);
|
||||
BOOST_REQUIRE_EQUAL(targetNeighbors(0, 2), 1);
|
||||
BOOST_REQUIRE_EQUAL(targetNeighbors(0, 3), 4);
|
||||
BOOST_REQUIRE_EQUAL(targetNeighbors(0, 4), 3);
|
||||
BOOST_REQUIRE_EQUAL(targetNeighbors(0, 5), 4);
|
||||
REQUIRE(targetNeighbors(0, 0) == 1);
|
||||
REQUIRE(targetNeighbors(0, 1) == 0);
|
||||
REQUIRE(targetNeighbors(0, 2) == 1);
|
||||
REQUIRE(targetNeighbors(0, 3) == 4);
|
||||
REQUIRE(targetNeighbors(0, 4) == 3);
|
||||
REQUIRE(targetNeighbors(0, 5) == 4);
|
||||
}
|
||||
|
||||
/**
|
||||
* The impostors function should be correct.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(LMNNImpostorsTest)
|
||||
TEST_CASE("LMNNImpostorsTest", "[LMNNTest]")
|
||||
{
|
||||
// Useful but simple dataset with six points and two classes.
|
||||
arma::mat dataset = "-0.1 -0.1 -0.1 0.1 0.1 0.1;"
|
||||
@@ -92,12 +89,12 @@ BOOST_AUTO_TEST_CASE(LMNNImpostorsTest)
|
||||
|
||||
constraint.Impostors(impostors, dataset, labels, norm);
|
||||
|
||||
BOOST_REQUIRE_EQUAL(impostors(0, 0), 3);
|
||||
BOOST_REQUIRE_EQUAL(impostors(0, 1), 4);
|
||||
BOOST_REQUIRE_EQUAL(impostors(0, 2), 5);
|
||||
BOOST_REQUIRE_EQUAL(impostors(0, 3), 0);
|
||||
BOOST_REQUIRE_EQUAL(impostors(0, 4), 1);
|
||||
BOOST_REQUIRE_EQUAL(impostors(0, 5), 2);
|
||||
REQUIRE(impostors(0, 0) == 3);
|
||||
REQUIRE(impostors(0, 1) == 4);
|
||||
REQUIRE(impostors(0, 2) == 5);
|
||||
REQUIRE(impostors(0, 3) == 0);
|
||||
REQUIRE(impostors(0, 4) == 1);
|
||||
REQUIRE(impostors(0, 5) == 2);
|
||||
}
|
||||
|
||||
//
|
||||
@@ -108,7 +105,7 @@ BOOST_AUTO_TEST_CASE(LMNNImpostorsTest)
|
||||
* The LMNN function should return the identity matrix as its initial
|
||||
* point.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(LMNNInitialPointTest)
|
||||
TEST_CASE("LMNNInitialPointTest", "[LMNNTest]")
|
||||
{
|
||||
// Cheap fake dataset.
|
||||
arma::mat dataset = arma::randu(5, 5);
|
||||
@@ -123,9 +120,9 @@ BOOST_AUTO_TEST_CASE(LMNNInitialPointTest)
|
||||
for (int col = 0; col < 5; col++)
|
||||
{
|
||||
if (row == col)
|
||||
BOOST_REQUIRE_CLOSE(initialPoint(row, col), 1.0, 1e-5);
|
||||
REQUIRE(initialPoint(row, col) == Approx( 1.0).epsilon(1e-7));
|
||||
else
|
||||
BOOST_REQUIRE_SMALL(initialPoint(row, col), 1e-5);
|
||||
REQUIRE(initialPoint(row, col) == Approx(0.0).margin(1e-5));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -133,7 +130,7 @@ BOOST_AUTO_TEST_CASE(LMNNInitialPointTest)
|
||||
/***
|
||||
* Ensure non-seprable objective function is right.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(LMNNInitialEvaluationTest)
|
||||
TEST_CASE("LMNNInitialEvaluationTest", "[LMNNTest]")
|
||||
{
|
||||
// Useful but simple dataset with six points and two classes.
|
||||
arma::mat dataset = "-0.1 -0.1 -0.1 0.1 0.1 0.1;"
|
||||
@@ -145,13 +142,13 @@ BOOST_AUTO_TEST_CASE(LMNNInitialEvaluationTest)
|
||||
double objective = lmnnfn.Evaluate(arma::eye<arma::mat>(2, 2));
|
||||
|
||||
// Result calculated by hand.
|
||||
BOOST_REQUIRE_CLOSE(objective, 9.456, 1e-5);
|
||||
REQUIRE(objective == Approx(9.456).epsilon(1e-7));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure non-seprable gradient function is right.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(LMNNInitialGradientTest)
|
||||
TEST_CASE("LMNNInitialGradientTest", "[LMNNTest]")
|
||||
{
|
||||
// Useful but simple dataset with six points and two classes.
|
||||
arma::mat dataset = "-0.1 -0.1 -0.1 0.1 0.1 0.1;"
|
||||
@@ -165,16 +162,16 @@ BOOST_AUTO_TEST_CASE(LMNNInitialGradientTest)
|
||||
lmnnfn.Gradient(coordinates, gradient);
|
||||
|
||||
// Result calculated by hand.
|
||||
BOOST_REQUIRE_CLOSE(gradient(0, 0), -0.288, 1e-5);
|
||||
BOOST_REQUIRE_SMALL(gradient(1, 0), 1e-5);
|
||||
BOOST_REQUIRE_SMALL(gradient(0, 1), 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(1, 1), 12.0, 1e-5);
|
||||
REQUIRE(gradient(0, 0) == Approx(-0.288).epsilon(1e-7));
|
||||
REQUIRE(gradient(1, 0) == Approx(0.0).margin(1e-5));
|
||||
REQUIRE(gradient(0, 1) == Approx(0.0).margin(1e-5));
|
||||
REQUIRE(gradient(1, 1) == Approx(12.0).epsilon(1e-7));
|
||||
}
|
||||
|
||||
/***
|
||||
* Ensure non-seprable EvaluateWithGradient function is right.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(LMNNInitialEvaluateWithGradientTest)
|
||||
TEST_CASE("LMNNInitialEvaluateWithGradientTest", "[LMNNTest]")
|
||||
{
|
||||
// Useful but simple dataset with six points and two classes.
|
||||
arma::mat dataset = "-0.1 -0.1 -0.1 0.1 0.1 0.1;"
|
||||
@@ -188,18 +185,18 @@ BOOST_AUTO_TEST_CASE(LMNNInitialEvaluateWithGradientTest)
|
||||
double objective = lmnnfn.EvaluateWithGradient(coordinates, gradient);
|
||||
|
||||
// Result calculated by hand.
|
||||
BOOST_REQUIRE_CLOSE(objective, 9.456, 1e-5);
|
||||
REQUIRE(objective == Approx(9.456).epsilon(1e-7));
|
||||
// Check Gradient
|
||||
BOOST_REQUIRE_CLOSE(gradient(0, 0), -0.288, 1e-5);
|
||||
BOOST_REQUIRE_SMALL(gradient(1, 0), 1e-5);
|
||||
BOOST_REQUIRE_SMALL(gradient(0, 1), 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(1, 1), 12.0, 1e-5);
|
||||
REQUIRE(gradient(0, 0) == Approx(-0.288).epsilon(1e-7));
|
||||
REQUIRE(gradient(1, 0) == Approx(0.0).margin(1e-5));
|
||||
REQUIRE(gradient(0, 1) == Approx(0.0).margin(1e-5));
|
||||
REQUIRE(gradient(1, 1) == Approx(12.0).epsilon(1e-7));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the separable objective function is right.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(LMNNSeparableObjectiveTest)
|
||||
TEST_CASE("LMNNSeparableObjectiveTest", "[LMNNTest]")
|
||||
{
|
||||
// Useful but simple dataset with six points and two classes.
|
||||
arma::mat dataset = "-0.1 -0.1 -0.1 0.1 0.1 0.1;"
|
||||
@@ -210,18 +207,18 @@ BOOST_AUTO_TEST_CASE(LMNNSeparableObjectiveTest)
|
||||
|
||||
// Result calculated by hand.
|
||||
arma::mat coordinates = arma::eye<arma::mat>(2, 2);
|
||||
BOOST_REQUIRE_CLOSE(lmnnfn.Evaluate(coordinates, 0, 1), 1.576, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(lmnnfn.Evaluate(coordinates, 1, 1), 1.576, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(lmnnfn.Evaluate(coordinates, 2, 1), 1.576, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(lmnnfn.Evaluate(coordinates, 3, 1), 1.576, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(lmnnfn.Evaluate(coordinates, 4, 1), 1.576, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(lmnnfn.Evaluate(coordinates, 5, 1), 1.576, 1e-5);
|
||||
REQUIRE(lmnnfn.Evaluate(coordinates, 0, 1) == Approx( 1.576).epsilon(1e-7));
|
||||
REQUIRE(lmnnfn.Evaluate(coordinates, 1, 1) == Approx( 1.576).epsilon(1e-7));
|
||||
REQUIRE(lmnnfn.Evaluate(coordinates, 2, 1) == Approx(1.576).epsilon(1e-7));
|
||||
REQUIRE(lmnnfn.Evaluate(coordinates, 3, 1) == Approx(1.576).epsilon(1e-7));
|
||||
REQUIRE(lmnnfn.Evaluate(coordinates, 4, 1) == Approx(1.576).epsilon(1e-7));
|
||||
REQUIRE(lmnnfn.Evaluate(coordinates, 5, 1) == Approx(1.576).epsilon(1e-7));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the separable gradient is right.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(LMNNSeparableGradientTest)
|
||||
TEST_CASE("LMNNSeparableGradientTest", "[LMNNTest]")
|
||||
{
|
||||
// Useful but simple dataset with six points and two classes.
|
||||
arma::mat dataset = "-0.1 -0.1 -0.1 0.1 0.1 0.1;"
|
||||
@@ -235,51 +232,51 @@ BOOST_AUTO_TEST_CASE(LMNNSeparableGradientTest)
|
||||
|
||||
lmnnfn.Gradient(coordinates, 0, gradient, 1);
|
||||
|
||||
BOOST_REQUIRE_CLOSE(gradient(0, 0), -0.048, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(0, 1), 0.0, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(1, 0), 0.0, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(1, 1), 2.0, 1e-5);
|
||||
REQUIRE(gradient(0, 0) == Approx(-0.048).epsilon(1e-7));
|
||||
REQUIRE(gradient(0, 1) == Approx(0.0).epsilon(1e-7));
|
||||
REQUIRE(gradient(1, 0) == Approx(0.0).epsilon(1e-7));
|
||||
REQUIRE(gradient(1, 1) == Approx(2.0).epsilon(1e-7));
|
||||
|
||||
lmnnfn.Gradient(coordinates, 1, gradient, 1);
|
||||
|
||||
BOOST_REQUIRE_CLOSE(gradient(0, 0), -0.048, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(0, 1), 0.0, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(1, 0), 0.0, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(1, 1), 2.0, 1e-5);
|
||||
REQUIRE(gradient(0, 0) == Approx(-0.048).epsilon(1e-7));
|
||||
REQUIRE(gradient(0, 1) == Approx(0.0).epsilon(1e-7));
|
||||
REQUIRE(gradient(1, 0) == Approx(0.0).epsilon(1e-7));
|
||||
REQUIRE(gradient(1, 1) == Approx(2.0).epsilon(1e-7));
|
||||
|
||||
lmnnfn.Gradient(coordinates, 2, gradient, 1);
|
||||
|
||||
BOOST_REQUIRE_CLOSE(gradient(0, 0), -0.048, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(0, 1), 0.0, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(1, 0), 0.0, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(1, 1), 2.0, 1e-5);
|
||||
REQUIRE(gradient(0, 0) == Approx(-0.048).epsilon(1e-7));
|
||||
REQUIRE(gradient(0, 1) == Approx(0.0).epsilon(1e-7));
|
||||
REQUIRE(gradient(1, 0) == Approx(0.0).epsilon(1e-7));
|
||||
REQUIRE(gradient(1, 1) == Approx(2.0).epsilon(1e-7));
|
||||
|
||||
lmnnfn.Gradient(coordinates, 3, gradient, 1);
|
||||
|
||||
BOOST_REQUIRE_CLOSE(gradient(0, 0), -0.048, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(0, 1), 0.0, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(1, 0), 0.0, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(1, 1), 2.0, 1e-5);
|
||||
REQUIRE(gradient(0, 0) == Approx(-0.048).epsilon(1e-7));
|
||||
REQUIRE(gradient(0, 1) == Approx(0.0).epsilon(1e-7));
|
||||
REQUIRE(gradient(1, 0) == Approx(0.0).epsilon(1e-7));
|
||||
REQUIRE(gradient(1, 1) == Approx(2.0).epsilon(1e-7));
|
||||
|
||||
lmnnfn.Gradient(coordinates, 4, gradient, 1);
|
||||
|
||||
BOOST_REQUIRE_CLOSE(gradient(0, 0), -0.048, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(0, 1), 0.0, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(1, 0), 0.0, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(1, 1), 2.0, 1e-5);
|
||||
REQUIRE(gradient(0, 0) == Approx(-0.048).epsilon(1e-7));
|
||||
REQUIRE(gradient(0, 1) == Approx(0.0).epsilon(1e-7));
|
||||
REQUIRE(gradient(1, 0) == Approx(0.0).epsilon(1e-7));
|
||||
REQUIRE(gradient(1, 1) == Approx(2.0).epsilon(1e-7));
|
||||
|
||||
lmnnfn.Gradient(coordinates, 5, gradient, 1);
|
||||
|
||||
BOOST_REQUIRE_CLOSE(gradient(0, 0), -0.048, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(0, 1), 0.0, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(1, 0), 0.0, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(1, 1), 2.0, 1e-5);
|
||||
REQUIRE(gradient(0, 0) == Approx(-0.048).epsilon(1e-7));
|
||||
REQUIRE(gradient(0, 1) == Approx(0.0).epsilon(1e-7));
|
||||
REQUIRE(gradient(1, 0) == Approx(0.0).epsilon(1e-7));
|
||||
REQUIRE(gradient(1, 1) == Approx(2.0).epsilon(1e-7));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the separable EvaluateWithGradient function is right.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(LMNNSeparableEvaluateWithGradientTest)
|
||||
TEST_CASE("LMNNSeparableEvaluateWithGradientTest", "[LMNNTest]")
|
||||
{
|
||||
// Useful but simple dataset with six points and two classes.
|
||||
arma::mat dataset = "-0.1 -0.1 -0.1 0.1 0.1 0.1;"
|
||||
@@ -293,61 +290,61 @@ BOOST_AUTO_TEST_CASE(LMNNSeparableEvaluateWithGradientTest)
|
||||
|
||||
double objective = lmnnfn.EvaluateWithGradient(coordinates, 0, gradient, 1);
|
||||
|
||||
BOOST_REQUIRE_CLOSE(objective, 1.576, 1e-5);
|
||||
REQUIRE(objective == Approx(1.576).epsilon(1e-7));
|
||||
|
||||
BOOST_REQUIRE_CLOSE(gradient(0, 0), -0.048, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(0, 1), 0.0, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(1, 0), 0.0, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(1, 1), 2.0, 1e-5);
|
||||
REQUIRE(gradient(0, 0) == Approx(-0.048).epsilon(1e-7));
|
||||
REQUIRE(gradient(0, 1) == Approx(0.0).epsilon(1e-7));
|
||||
REQUIRE(gradient(1, 0) == Approx(0.0).epsilon(1e-7));
|
||||
REQUIRE(gradient(1, 1) == Approx(2.0).epsilon(1e-7));
|
||||
|
||||
objective = lmnnfn.EvaluateWithGradient(coordinates, 1, gradient, 1);
|
||||
|
||||
BOOST_REQUIRE_CLOSE(objective, 1.576, 1e-5);
|
||||
REQUIRE(objective == Approx(1.576).epsilon(1e-7));
|
||||
|
||||
BOOST_REQUIRE_CLOSE(gradient(0, 0), -0.048, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(0, 1), 0.0, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(1, 0), 0.0, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(1, 1), 2.0, 1e-5);
|
||||
REQUIRE(gradient(0, 0) == Approx(-0.048).epsilon(1e-7));
|
||||
REQUIRE(gradient(0, 1) == Approx(0.0).epsilon(1e-7));
|
||||
REQUIRE(gradient(1, 0) == Approx(0.0).epsilon(1e-7));
|
||||
REQUIRE(gradient(1, 1) == Approx(2.0).epsilon(1e-7));
|
||||
|
||||
objective = lmnnfn.EvaluateWithGradient(coordinates, 2, gradient, 1);
|
||||
|
||||
BOOST_REQUIRE_CLOSE(objective, 1.576, 1e-5);
|
||||
REQUIRE(objective == Approx(1.576).epsilon(1e-7));
|
||||
|
||||
BOOST_REQUIRE_CLOSE(gradient(0, 0), -0.048, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(0, 1), 0.0, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(1, 0), 0.0, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(1, 1), 2.0, 1e-5);
|
||||
REQUIRE(gradient(0, 0) == Approx(-0.048).epsilon(1e-7));
|
||||
REQUIRE(gradient(0, 1) == Approx(0.0).epsilon(1e-7));
|
||||
REQUIRE(gradient(1, 0) == Approx(0.0).epsilon(1e-7));
|
||||
REQUIRE(gradient(1, 1) == Approx(2.0).epsilon(1e-7));
|
||||
|
||||
objective = lmnnfn.EvaluateWithGradient(coordinates, 3, gradient, 1);
|
||||
|
||||
BOOST_REQUIRE_CLOSE(objective, 1.576, 1e-5);
|
||||
REQUIRE(objective == Approx(1.576).epsilon(1e-7));
|
||||
|
||||
BOOST_REQUIRE_CLOSE(gradient(0, 0), -0.048, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(0, 1), 0.0, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(1, 0), 0.0, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(1, 1), 2.0, 1e-5);
|
||||
REQUIRE(gradient(0, 0) == Approx(-0.048).epsilon(1e-7));
|
||||
REQUIRE(gradient(0, 1) == Approx(0.0).epsilon(1e-7));
|
||||
REQUIRE(gradient(1, 0) == Approx(0.0).epsilon(1e-7));
|
||||
REQUIRE(gradient(1, 1) == Approx(2.0).epsilon(1e-7));
|
||||
|
||||
objective = lmnnfn.EvaluateWithGradient(coordinates, 4, gradient, 1);
|
||||
|
||||
BOOST_REQUIRE_CLOSE(objective, 1.576, 1e-5);
|
||||
REQUIRE(objective == Approx( 1.576).epsilon(1e-7));
|
||||
|
||||
BOOST_REQUIRE_CLOSE(gradient(0, 0), -0.048, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(0, 1), 0.0, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(1, 0), 0.0, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(1, 1), 2.0, 1e-5);
|
||||
REQUIRE(gradient(0, 0) == Approx( -0.048).epsilon(1e-7));
|
||||
REQUIRE(gradient(0, 1) == Approx( 0.0).epsilon(1e-7));
|
||||
REQUIRE(gradient(1, 0) == Approx( 0.0).epsilon(1e-7));
|
||||
REQUIRE(gradient(1, 1) == Approx( 2.0).epsilon(1e-7));
|
||||
|
||||
objective = lmnnfn.EvaluateWithGradient(coordinates, 5, gradient, 1);
|
||||
|
||||
BOOST_REQUIRE_CLOSE(objective, 1.576, 1e-5);
|
||||
REQUIRE(objective == Approx( 1.576).epsilon(1e-7));
|
||||
|
||||
BOOST_REQUIRE_CLOSE(gradient(0, 0), -0.048, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(0, 1), 0.0, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(1, 0), 0.0, 1e-5);
|
||||
BOOST_REQUIRE_CLOSE(gradient(1, 1), 2.0, 1e-5);
|
||||
REQUIRE(gradient(0, 0) == Approx( -0.048).epsilon(1e-7));
|
||||
REQUIRE(gradient(0, 1) == Approx( 0.0).epsilon(1e-7));
|
||||
REQUIRE(gradient(1, 0) == Approx( 0.0).epsilon(1e-7));
|
||||
REQUIRE(gradient(1, 1) == Approx( 2.0).epsilon(1e-7));
|
||||
}
|
||||
|
||||
// Check that final objective value using SGD optimizer is optimal.
|
||||
BOOST_AUTO_TEST_CASE(LMNNSGDSimpleDatasetTest)
|
||||
TEST_CASE("LMNNSGDSimpleDatasetTest", "[LMNNTest]")
|
||||
{
|
||||
// Useful but simple dataset with six points and two classes.
|
||||
arma::mat dataset = "-0.1 -0.1 -0.1 0.1 0.1 0.1;"
|
||||
@@ -366,11 +363,11 @@ BOOST_AUTO_TEST_CASE(LMNNSGDSimpleDatasetTest)
|
||||
double finalObj = lmnnfn.Evaluate(outputMatrix);
|
||||
|
||||
// finalObj must be less than initObj.
|
||||
BOOST_REQUIRE_LT(finalObj, initObj);
|
||||
REQUIRE(finalObj < initObj);
|
||||
}
|
||||
|
||||
// Check that final objective value using L-BFGS optimizer is optimal.
|
||||
BOOST_AUTO_TEST_CASE(LMNNLBFGSSimpleDatasetTest)
|
||||
TEST_CASE("LMNNLBFGSSimpleDatasetTest", "[LMNNTest]")
|
||||
{
|
||||
// Useful but simple dataset with six points and two classes.
|
||||
arma::mat dataset = "-0.1 -0.1 -0.1 0.1 0.1 0.1;"
|
||||
@@ -389,7 +386,7 @@ BOOST_AUTO_TEST_CASE(LMNNLBFGSSimpleDatasetTest)
|
||||
double finalObj = lmnnfn.Evaluate(outputMatrix);
|
||||
|
||||
// finalObj must be less than initObj.
|
||||
BOOST_REQUIRE_LT(finalObj, initObj);
|
||||
REQUIRE(finalObj < initObj);
|
||||
}
|
||||
|
||||
double KnnAccuracy(const arma::mat& dataset,
|
||||
@@ -432,7 +429,7 @@ double KnnAccuracy(const arma::mat& dataset,
|
||||
|
||||
// Check that final accuracy is greater than initial accuracy on
|
||||
// simple dataset.
|
||||
BOOST_AUTO_TEST_CASE(LMNNAccuracyTest)
|
||||
TEST_CASE("LMNNAccuracyTest", "[LMNNTest]")
|
||||
{
|
||||
// Useful but simple dataset with six points and two classes.
|
||||
arma::mat dataset = "-0.1 -0.1 -0.1 0.1 0.1 0.1;"
|
||||
@@ -450,16 +447,16 @@ BOOST_AUTO_TEST_CASE(LMNNAccuracyTest)
|
||||
double finalAccuracy = KnnAccuracy(outputMatrix * dataset, labels, 3);
|
||||
|
||||
// finalObj must be less than initObj.
|
||||
BOOST_REQUIRE_LT(initAccuracy, finalAccuracy);
|
||||
REQUIRE(initAccuracy < finalAccuracy);
|
||||
|
||||
// Since this is a very simple dataset final accuracy should be around 100%.
|
||||
BOOST_REQUIRE_CLOSE(finalAccuracy, 100.0, 1e-5);
|
||||
REQUIRE(finalAccuracy == Approx( 100.0).epsilon(1e-7));
|
||||
}
|
||||
|
||||
// Check that accuracy while learning square distance matrix is the same as when
|
||||
// we are learning low rank matrix. I'm ok if this passes only once out of
|
||||
// three tries.
|
||||
BOOST_AUTO_TEST_CASE(LMNNLowRankAccuracyLBFGSTest)
|
||||
TEST_CASE("LMNNLowRankAccuracyLBFGSTest", "[LMNNTest]")
|
||||
{
|
||||
bool success = false;
|
||||
for (size_t trial = 0; trial < 3; ++trial)
|
||||
@@ -508,13 +505,13 @@ BOOST_AUTO_TEST_CASE(LMNNLowRankAccuracyLBFGSTest)
|
||||
break;
|
||||
}
|
||||
|
||||
BOOST_REQUIRE_EQUAL(success, true);
|
||||
REQUIRE(success == true);
|
||||
}
|
||||
|
||||
// Check that accuracy while learning square distance matrix is the same as when
|
||||
// we are learning low rank matrix. I'm ok if this passes only once out of
|
||||
// three tries.
|
||||
BOOST_AUTO_TEST_CASE(LMNNLowRankAccuracyTest)
|
||||
TEST_CASE("LMNNLowRankAccuracyTest", "[LMNNTest]")
|
||||
{
|
||||
bool success = false;
|
||||
for (size_t trial = 0; trial < 3; ++trial)
|
||||
@@ -563,14 +560,14 @@ BOOST_AUTO_TEST_CASE(LMNNLowRankAccuracyTest)
|
||||
break;
|
||||
}
|
||||
|
||||
BOOST_REQUIRE_EQUAL(success, true);
|
||||
REQUIRE(success == true);
|
||||
}
|
||||
|
||||
// Check that accuracy while learning square distance matrix is the same as when
|
||||
// we are learning low rank matrix. I'm ok if this passes only once out of
|
||||
// five tries, since BBSGD seems to have a harder time converging.
|
||||
/*
|
||||
BOOST_AUTO_TEST_CASE(LMNNLowRankAccuracyBBSGDTest)
|
||||
TEST_CASE("LMNNLowRankAccuracyBBSGDTest", "[LMNNTest]")
|
||||
{
|
||||
bool success = false;
|
||||
for (size_t trial = 0; trial < 5; ++trial)
|
||||
@@ -621,7 +618,7 @@ BOOST_AUTO_TEST_CASE(LMNNLowRankAccuracyBBSGDTest)
|
||||
break;
|
||||
}
|
||||
|
||||
BOOST_REQUIRE_EQUAL(success, true);
|
||||
REQUIRE(success == true);
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -664,7 +661,7 @@ double CheckGradient(FunctionType& function,
|
||||
arma::norm(orgGradient + estGradient);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(LMNNFunctionGradientTest)
|
||||
TEST_CASE("LMNNFunctionGradientTest", "[LMNNTest]")
|
||||
{
|
||||
// Useful but simple dataset with six points and two classes.
|
||||
arma::mat dataset = "-0.1 -0.1 -0.1 0.1 0.1 0.1;"
|
||||
@@ -681,7 +678,7 @@ BOOST_AUTO_TEST_CASE(LMNNFunctionGradientTest)
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(LMNNFunctionGradientTest2)
|
||||
TEST_CASE("LMNNFunctionGradientTest2", "[LMNNTest]")
|
||||
{
|
||||
// Useful but simple dataset with six points and two classes.
|
||||
arma::mat dataset = "-0.1 -0.1 -0.1 0.1 0.1 0.1;"
|
||||
@@ -698,7 +695,7 @@ BOOST_AUTO_TEST_CASE(LMNNFunctionGradientTest2)
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(LMNNFunctionGradientTest3)
|
||||
TEST_CASE("LMNNFunctionGradientTest3", "[LMNNTest]")
|
||||
{
|
||||
arma::mat dataset;
|
||||
arma::Row<size_t> labels;
|
||||
@@ -715,7 +712,7 @@ BOOST_AUTO_TEST_CASE(LMNNFunctionGradientTest3)
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(LMNNFunctionGradientTest4)
|
||||
TEST_CASE("LMNNFunctionGradientTest4", "[LMNNTest]")
|
||||
{
|
||||
arma::mat dataset;
|
||||
arma::Row<size_t> labels;
|
||||
@@ -731,5 +728,3 @@ BOOST_AUTO_TEST_CASE(LMNNFunctionGradientTest4)
|
||||
CheckGradient(lmnnfn, coordinates);
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END();
|
||||
|
||||
@@ -20,8 +20,7 @@ static const std::string testName = "KDE";
|
||||
#include "test_helper.hpp"
|
||||
#include <mlpack/methods/kde/kde_main.cpp>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include "../test_tools.hpp"
|
||||
#include "../catch.hpp"
|
||||
|
||||
using namespace mlpack;
|
||||
|
||||
@@ -48,13 +47,12 @@ void ResetKDESettings()
|
||||
IO::RestoreSettings(testName);
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_SUITE(KDEMainTest, KDETestFixture);
|
||||
|
||||
/**
|
||||
* Ensure that the estimations we get for KDEMain, are the same as the ones we
|
||||
* get from the KDE class without any wrappers. Requires normalization.
|
||||
**/
|
||||
BOOST_AUTO_TEST_CASE(KDEGaussianRTreeResultsMain)
|
||||
TEST_CASE_METHOD(KDETestFixture, "KDEGaussianRTreeResultsMain",
|
||||
"[KDEMainTest][BindingTests]")
|
||||
{
|
||||
// Datasets.
|
||||
arma::mat reference = arma::randu(3, 500);
|
||||
@@ -89,14 +87,15 @@ BOOST_AUTO_TEST_CASE(KDEGaussianRTreeResultsMain)
|
||||
|
||||
// Check whether results are equal.
|
||||
for (size_t i = 0; i < query.n_cols; ++i)
|
||||
BOOST_REQUIRE_CLOSE(kdeEstimations[i], mainEstimations[i], 100 * relError);
|
||||
REQUIRE(kdeEstimations[i] == Approx( mainEstimations[i]).epsilon(relError));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that the estimations we get for KDEMain, are the same as the ones we
|
||||
* get from the KDE class without any wrappers. Doesn't require normalization.
|
||||
**/
|
||||
BOOST_AUTO_TEST_CASE(KDETriangularBallTreeResultsMain)
|
||||
TEST_CASE_METHOD(KDETestFixture, "KDETriangularBallTreeResultsMain",
|
||||
"[KDEMainTest][BindingTests]")
|
||||
{
|
||||
// Datasets.
|
||||
arma::mat reference = arma::randu(3, 300);
|
||||
@@ -129,14 +128,15 @@ BOOST_AUTO_TEST_CASE(KDETriangularBallTreeResultsMain)
|
||||
|
||||
// Check whether results are equal.
|
||||
for (size_t i = 0; i < query.n_cols; ++i)
|
||||
BOOST_REQUIRE_CLOSE(kdeEstimations[i], mainEstimations[i], 100 * relError);
|
||||
REQUIRE(kdeEstimations[i] == Approx( mainEstimations[i]).epsilon(relError));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that the estimations we get for KDEMain, are the same as the ones we
|
||||
* get from the KDE class without any wrappers in the monochromatic case.
|
||||
**/
|
||||
BOOST_AUTO_TEST_CASE(KDEMonoResultsMain)
|
||||
TEST_CASE_METHOD(KDETestFixture, "KDEMonoResultsMain",
|
||||
"[KDEMainTest][BindingTests]")
|
||||
{
|
||||
// Datasets.
|
||||
arma::mat reference = arma::randu(2, 300);
|
||||
@@ -170,24 +170,26 @@ BOOST_AUTO_TEST_CASE(KDEMonoResultsMain)
|
||||
|
||||
// Check whether results are equal.
|
||||
for (size_t i = 0; i < reference.n_cols; ++i)
|
||||
BOOST_REQUIRE_CLOSE(kdeEstimations[i], mainEstimations[i], 100 * relError);
|
||||
REQUIRE(kdeEstimations[i] == Approx( mainEstimations[i]).epsilon(relError));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensuring that absence of input data is checked.
|
||||
**/
|
||||
BOOST_AUTO_TEST_CASE(KDENoInputData)
|
||||
TEST_CASE_METHOD(KDETestFixture, "KDENoInputData",
|
||||
"[KDEMainTest][BindingTests]")
|
||||
{
|
||||
// No input data is not provided. Should throw a runtime error.
|
||||
Log::Fatal.ignoreInput = true;
|
||||
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
|
||||
Log::Fatal.ignoreInput = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check result has as many densities as query points.
|
||||
**/
|
||||
BOOST_AUTO_TEST_CASE(KDEOutputSize)
|
||||
TEST_CASE_METHOD(KDETestFixture, "KDEOutputSize",
|
||||
"[KDEMainTest][BindingTests]")
|
||||
{
|
||||
const size_t dim = 3;
|
||||
const size_t samples = 110;
|
||||
@@ -200,13 +202,14 @@ BOOST_AUTO_TEST_CASE(KDEOutputSize)
|
||||
|
||||
mlpackMain();
|
||||
// Check number of output elements.
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::vec>("predictions").size(), samples);
|
||||
REQUIRE(IO::GetParam<arma::vec>("predictions").size() == samples);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that saved model can be reused.
|
||||
**/
|
||||
BOOST_AUTO_TEST_CASE(KDEModelReuse)
|
||||
TEST_CASE_METHOD(KDETestFixture, "KDEModelReuse",
|
||||
"[KDEMainTest][BindingTests]")
|
||||
{
|
||||
const size_t dim = 3;
|
||||
const size_t samples = 100;
|
||||
@@ -236,14 +239,15 @@ BOOST_AUTO_TEST_CASE(KDEModelReuse)
|
||||
|
||||
// Check estimations are the same.
|
||||
for (size_t i = 0; i < samples; ++i)
|
||||
BOOST_REQUIRE_CLOSE(oldEstimations[i], newEstimations[i], 100 * relError);
|
||||
REQUIRE(oldEstimations[i] == Approx( newEstimations[i]).epsilon(relError));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that the estimations we get for KDEMain, are the same as the ones we
|
||||
* get from the KDE class without any wrappers using single-tree mode.
|
||||
**/
|
||||
BOOST_AUTO_TEST_CASE(KDEGaussianSingleKDTreeResultsMain)
|
||||
TEST_CASE_METHOD(KDETestFixture, "KDEGaussianSingleKDTreeResultsMain",
|
||||
"[KDEMainTest][BindingTests]")
|
||||
{
|
||||
// Datasets.
|
||||
arma::mat reference = arma::randu(3, 400);
|
||||
@@ -278,13 +282,14 @@ BOOST_AUTO_TEST_CASE(KDEGaussianSingleKDTreeResultsMain)
|
||||
|
||||
// Check whether results are equal.
|
||||
for (size_t i = 0; i < query.n_cols; ++i)
|
||||
BOOST_REQUIRE_CLOSE(kdeEstimations[i], mainEstimations[i], 100 * relError);
|
||||
REQUIRE(kdeEstimations[i] == Approx( mainEstimations[i]).epsilon(relError));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure we get an exception when an invalid kernel is specified.
|
||||
**/
|
||||
BOOST_AUTO_TEST_CASE(KDEMainInvalidKernel)
|
||||
TEST_CASE_METHOD(KDETestFixture, "KDEMainInvalidKernel",
|
||||
"[KDEMainTest][BindingTests]")
|
||||
{
|
||||
arma::mat reference = arma::randu<arma::mat>(2, 10);
|
||||
arma::mat query = arma::randu<arma::mat>(2, 5);
|
||||
@@ -295,14 +300,15 @@ BOOST_AUTO_TEST_CASE(KDEMainInvalidKernel)
|
||||
SetInputParam("kernel", std::string("linux"));
|
||||
|
||||
Log::Fatal.ignoreInput = true;
|
||||
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
|
||||
Log::Fatal.ignoreInput = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure we get an exception when an invalid tree is specified.
|
||||
**/
|
||||
BOOST_AUTO_TEST_CASE(KDEMainInvalidTree)
|
||||
TEST_CASE_METHOD(KDETestFixture, "KDEMainInvalidTree",
|
||||
"[KDEMainTest][BindingTests]")
|
||||
{
|
||||
arma::mat reference = arma::randu<arma::mat>(2, 10);
|
||||
arma::mat query = arma::randu<arma::mat>(2, 5);
|
||||
@@ -313,14 +319,15 @@ BOOST_AUTO_TEST_CASE(KDEMainInvalidTree)
|
||||
SetInputParam("tree", std::string("olive"));
|
||||
|
||||
Log::Fatal.ignoreInput = true;
|
||||
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
|
||||
Log::Fatal.ignoreInput = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure we get an exception when an invalid algorithm is specified.
|
||||
**/
|
||||
BOOST_AUTO_TEST_CASE(KDEMainInvalidAlgorithm)
|
||||
TEST_CASE_METHOD(KDETestFixture, "KDEMainInvalidAlgorithm",
|
||||
"[KDEMainTest][BindingTests]")
|
||||
{
|
||||
arma::mat reference = arma::randu<arma::mat>(2, 10);
|
||||
arma::mat query = arma::randu<arma::mat>(2, 5);
|
||||
@@ -331,7 +338,7 @@ BOOST_AUTO_TEST_CASE(KDEMainInvalidAlgorithm)
|
||||
SetInputParam("algorithm", std::string("bogosort"));
|
||||
|
||||
Log::Fatal.ignoreInput = true;
|
||||
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
|
||||
Log::Fatal.ignoreInput = false;
|
||||
}
|
||||
|
||||
@@ -339,7 +346,8 @@ BOOST_AUTO_TEST_CASE(KDEMainInvalidAlgorithm)
|
||||
* Ensure we get an exception when both reference and input_model are
|
||||
* specified.
|
||||
**/
|
||||
BOOST_AUTO_TEST_CASE(KDEMainReferenceAndModel)
|
||||
TEST_CASE_METHOD(KDETestFixture, "KDEMainReferenceAndModel",
|
||||
"[KDEMainTest][BindingTests]")
|
||||
{
|
||||
arma::mat reference = arma::randu<arma::mat>(2, 10);
|
||||
arma::mat query = arma::randu<arma::mat>(2, 5);
|
||||
@@ -351,14 +359,15 @@ BOOST_AUTO_TEST_CASE(KDEMainReferenceAndModel)
|
||||
SetInputParam("input_model", model);
|
||||
|
||||
Log::Fatal.ignoreInput = true;
|
||||
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
|
||||
Log::Fatal.ignoreInput = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure we get an exception when an invalid absolute error is specified.
|
||||
**/
|
||||
BOOST_AUTO_TEST_CASE(KDEMainInvalidAbsoluteError)
|
||||
TEST_CASE_METHOD(KDETestFixture, "KDEMainInvalidAbsoluteError",
|
||||
"[KDEMainTest][BindingTests]")
|
||||
{
|
||||
arma::mat reference = arma::randu<arma::mat>(1, 10);
|
||||
arma::mat query = arma::randu<arma::mat>(1, 5);
|
||||
@@ -370,18 +379,19 @@ BOOST_AUTO_TEST_CASE(KDEMainInvalidAbsoluteError)
|
||||
Log::Fatal.ignoreInput = true;
|
||||
// Invalid value.
|
||||
SetInputParam("abs_error", -0.1);
|
||||
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
|
||||
|
||||
// Valid value.
|
||||
SetInputParam("abs_error", 5.8);
|
||||
BOOST_REQUIRE_NO_THROW(mlpackMain());
|
||||
REQUIRE_NOTHROW(mlpackMain());
|
||||
Log::Fatal.ignoreInput = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure we get an exception when an invalid relative error is specified.
|
||||
**/
|
||||
BOOST_AUTO_TEST_CASE(KDEMainInvalidRelativeError)
|
||||
TEST_CASE_METHOD(KDETestFixture, "KDEMainInvalidRelativeError",
|
||||
"[KDEMainTest][BindingTests]")
|
||||
{
|
||||
arma::mat reference = arma::randu<arma::mat>(1, 10);
|
||||
arma::mat query = arma::randu<arma::mat>(1, 5);
|
||||
@@ -393,15 +403,15 @@ BOOST_AUTO_TEST_CASE(KDEMainInvalidRelativeError)
|
||||
Log::Fatal.ignoreInput = true;
|
||||
// Invalid under 0.
|
||||
SetInputParam("rel_error", -0.1);
|
||||
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
|
||||
|
||||
// Invalid over 1.
|
||||
SetInputParam("rel_error", 1.1);
|
||||
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
|
||||
|
||||
// Valid value.
|
||||
SetInputParam("rel_error", 0.3);
|
||||
BOOST_REQUIRE_NO_THROW(mlpackMain());
|
||||
REQUIRE_NOTHROW(mlpackMain());
|
||||
Log::Fatal.ignoreInput = false;
|
||||
}
|
||||
|
||||
@@ -409,7 +419,8 @@ BOOST_AUTO_TEST_CASE(KDEMainInvalidRelativeError)
|
||||
* Ensure we get an exception when an invalid Monte Carlo probability is
|
||||
* specified.
|
||||
**/
|
||||
BOOST_AUTO_TEST_CASE(KDEMainInvalidMCProbability)
|
||||
TEST_CASE_METHOD(KDETestFixture, "KDEMainInvalidMCProbability",
|
||||
"[KDEMainTest][BindingTests]")
|
||||
{
|
||||
arma::mat reference = arma::randu<arma::mat>(1, 10);
|
||||
arma::mat query = arma::randu<arma::mat>(1, 5);
|
||||
@@ -423,15 +434,15 @@ BOOST_AUTO_TEST_CASE(KDEMainInvalidMCProbability)
|
||||
Log::Fatal.ignoreInput = true;
|
||||
// Invalid under 0.
|
||||
SetInputParam("mc_probability", -0.1);
|
||||
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
|
||||
|
||||
// Invalid over 1.
|
||||
SetInputParam("mc_probability", 1.1);
|
||||
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
|
||||
|
||||
// Valid value.
|
||||
SetInputParam("mc_probability", 0.3);
|
||||
BOOST_REQUIRE_NO_THROW(mlpackMain());
|
||||
REQUIRE_NOTHROW(mlpackMain());
|
||||
Log::Fatal.ignoreInput = false;
|
||||
}
|
||||
|
||||
@@ -439,7 +450,8 @@ BOOST_AUTO_TEST_CASE(KDEMainInvalidMCProbability)
|
||||
* Ensure we get an exception when an invalid Monte Carlo initial sample size
|
||||
* is specified.
|
||||
**/
|
||||
BOOST_AUTO_TEST_CASE(KDEMainInvalidMCInitialSampleSize)
|
||||
TEST_CASE_METHOD(KDETestFixture, "KDEMainInvalidMCInitialSampleSize",
|
||||
"[KDEMainTest][BindingTests]")
|
||||
{
|
||||
arma::mat reference = arma::randu<arma::mat>(1, 10);
|
||||
arma::mat query = arma::randu<arma::mat>(1, 5);
|
||||
@@ -453,15 +465,15 @@ BOOST_AUTO_TEST_CASE(KDEMainInvalidMCInitialSampleSize)
|
||||
Log::Fatal.ignoreInput = true;
|
||||
// Invalid under 0.
|
||||
SetInputParam("initial_sample_size", -1);
|
||||
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
|
||||
|
||||
// Invalid 0.
|
||||
SetInputParam("initial_sample_size", 0);
|
||||
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
|
||||
|
||||
// Valid value.
|
||||
SetInputParam("initial_sample_size", 20);
|
||||
BOOST_REQUIRE_NO_THROW(mlpackMain());
|
||||
REQUIRE_NOTHROW(mlpackMain());
|
||||
Log::Fatal.ignoreInput = false;
|
||||
}
|
||||
|
||||
@@ -469,7 +481,8 @@ BOOST_AUTO_TEST_CASE(KDEMainInvalidMCInitialSampleSize)
|
||||
* Ensure we get an exception when an invalid Monte Carlo entry coefficient
|
||||
* is specified.
|
||||
**/
|
||||
BOOST_AUTO_TEST_CASE(KDEMainInvalidMCEntryCoef)
|
||||
TEST_CASE_METHOD(KDETestFixture, "KDEMainInvalidMCEntryCoef",
|
||||
"[KDEMainTest][BindingTests]")
|
||||
{
|
||||
arma::mat reference = arma::randu<arma::mat>(1, 10);
|
||||
arma::mat query = arma::randu<arma::mat>(1, 5);
|
||||
@@ -483,11 +496,11 @@ BOOST_AUTO_TEST_CASE(KDEMainInvalidMCEntryCoef)
|
||||
Log::Fatal.ignoreInput = true;
|
||||
// Invalid under 1.
|
||||
SetInputParam("mc_entry_coef", 0.5);
|
||||
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
|
||||
|
||||
// Valid greater than 1.
|
||||
SetInputParam("mc_entry_coef", 1.1);
|
||||
BOOST_REQUIRE_NO_THROW(mlpackMain());
|
||||
REQUIRE_NOTHROW(mlpackMain());
|
||||
Log::Fatal.ignoreInput = false;
|
||||
}
|
||||
|
||||
@@ -495,7 +508,8 @@ BOOST_AUTO_TEST_CASE(KDEMainInvalidMCEntryCoef)
|
||||
* Ensure we get an exception when an invalid Monte Carlo break coefficient
|
||||
* is specified.
|
||||
**/
|
||||
BOOST_AUTO_TEST_CASE(KDEMainInvalidMCBreakCoef)
|
||||
TEST_CASE_METHOD(KDETestFixture, "KDEMainInvalidMCBreakCoef",
|
||||
"[KDEMainTest][BindingTests]")
|
||||
{
|
||||
arma::mat reference = arma::randu<arma::mat>(1, 10);
|
||||
arma::mat query = arma::randu<arma::mat>(1, 5);
|
||||
@@ -509,15 +523,15 @@ BOOST_AUTO_TEST_CASE(KDEMainInvalidMCBreakCoef)
|
||||
Log::Fatal.ignoreInput = true;
|
||||
// Invalid under 0.
|
||||
SetInputParam("mc_break_coef", -0.5);
|
||||
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
|
||||
|
||||
// Valid between 0 and 1.
|
||||
SetInputParam("mc_break_coef", 0.3);
|
||||
BOOST_REQUIRE_NO_THROW(mlpackMain());
|
||||
REQUIRE_NOTHROW(mlpackMain());
|
||||
|
||||
// Invalid greater than 1.
|
||||
SetInputParam("mc_break_coef", 1.1);
|
||||
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
|
||||
Log::Fatal.ignoreInput = false;
|
||||
}
|
||||
|
||||
@@ -526,7 +540,8 @@ BOOST_AUTO_TEST_CASE(KDEMainInvalidMCBreakCoef)
|
||||
* Carlo estimations. Since this test has a random component, it might fail
|
||||
* (although it's unlikely).
|
||||
**/
|
||||
BOOST_AUTO_TEST_CASE(KDEMainMonteCarloFlag)
|
||||
TEST_CASE_METHOD(KDETestFixture, "KDEMainMonteCarloFlag",
|
||||
"[KDEMainTest][BindingTests]")
|
||||
{
|
||||
// Datasets.
|
||||
arma::mat reference = arma::randu(1, 5000);
|
||||
@@ -557,7 +572,5 @@ BOOST_AUTO_TEST_CASE(KDEMainMonteCarloFlag)
|
||||
// Check whether results are equal.
|
||||
differences = arma::abs(estimations1 - estimations2);
|
||||
const double sumDifferences = arma::accu(differences);
|
||||
BOOST_REQUIRE_GT(sumDifferences, 0);
|
||||
REQUIRE(sumDifferences > 0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END();
|
||||
|
||||
@@ -22,8 +22,8 @@ static const std::string testName = "LMNN";
|
||||
#include "test_helper.hpp"
|
||||
#include <mlpack/methods/lmnn/lmnn_main.cpp>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include "../test_tools.hpp"
|
||||
#include "../test_catch_tools.hpp"
|
||||
#include "../catch.hpp"
|
||||
|
||||
using namespace mlpack;
|
||||
|
||||
@@ -44,32 +44,31 @@ struct LMNNTestFixture
|
||||
}
|
||||
};
|
||||
|
||||
BOOST_FIXTURE_TEST_SUITE(LMNNMainTest, LMNNTestFixture);
|
||||
|
||||
/**
|
||||
* Ensure that, when labels are implicitily given with input,
|
||||
* the last column is treated as labels and that we get the
|
||||
* desired shape of output.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(LMNNExplicitImplicitLabelsTest)
|
||||
TEST_CASE_METHOD(LMNNTestFixture, "LMNNExplicitImplicitLabelsTest",
|
||||
"[LMNNMainTest][BindingTests]")
|
||||
{
|
||||
// Dataset containing labels as last column.
|
||||
arma::mat inputData;
|
||||
if (!data::Load("iris_train.csv", inputData))
|
||||
BOOST_FAIL("Cannot load iris.csv!");
|
||||
FAIL("Cannot load iris.csv!");
|
||||
|
||||
SetInputParam("input", inputData);
|
||||
|
||||
mlpackMain();
|
||||
|
||||
// Check that final output has expected number of rows and colums.
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("output").n_rows,
|
||||
REQUIRE(IO::GetParam<arma::mat>("output").n_rows ==
|
||||
inputData.n_rows - 1);
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("output").n_cols,
|
||||
REQUIRE(IO::GetParam<arma::mat>("output").n_cols ==
|
||||
inputData.n_rows - 1);
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("transformed_data").n_rows,
|
||||
REQUIRE(IO::GetParam<arma::mat>("transformed_data").n_rows ==
|
||||
inputData.n_rows - 1);
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("transformed_data").n_cols,
|
||||
REQUIRE(IO::GetParam<arma::mat>("transformed_data").n_cols ==
|
||||
inputData.n_cols);
|
||||
|
||||
// Reset Settings.
|
||||
@@ -79,11 +78,11 @@ BOOST_AUTO_TEST_CASE(LMNNExplicitImplicitLabelsTest)
|
||||
// Now check that when labels are explicitely given, the last column
|
||||
// of input is not treated as labels.
|
||||
if (!data::Load("iris.csv", inputData))
|
||||
BOOST_FAIL("Cannot load iris.csv!");
|
||||
FAIL("Cannot load iris.csv!");
|
||||
|
||||
arma::Row<size_t> labels;
|
||||
if (!data::Load("iris_labels.txt", labels))
|
||||
BOOST_FAIL("Cannot load iris_labels.txt!");
|
||||
FAIL("Cannot load iris_labels.txt!");
|
||||
|
||||
SetInputParam("input", inputData);
|
||||
SetInputParam("labels", std::move(labels));
|
||||
@@ -91,13 +90,13 @@ BOOST_AUTO_TEST_CASE(LMNNExplicitImplicitLabelsTest)
|
||||
mlpackMain();
|
||||
|
||||
// Check that final output has expected number of rows and colums.
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("output").n_rows,
|
||||
REQUIRE(IO::GetParam<arma::mat>("output").n_rows ==
|
||||
inputData.n_rows);
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("output").n_cols,
|
||||
REQUIRE(IO::GetParam<arma::mat>("output").n_cols ==
|
||||
inputData.n_rows);
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("transformed_data").n_rows,
|
||||
REQUIRE(IO::GetParam<arma::mat>("transformed_data").n_rows ==
|
||||
inputData.n_rows);
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("transformed_data").n_cols,
|
||||
REQUIRE(IO::GetParam<arma::mat>("transformed_data").n_cols ==
|
||||
inputData.n_cols);
|
||||
}
|
||||
|
||||
@@ -105,15 +104,16 @@ BOOST_AUTO_TEST_CASE(LMNNExplicitImplicitLabelsTest)
|
||||
* Ensure that when we pass optimizer of type lbfgs, we also get the desired
|
||||
* shape of output.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(LMNNOptimizerTest)
|
||||
TEST_CASE_METHOD(LMNNTestFixture, "LMNNOptimizerTest",
|
||||
"[LMNNMainTest][BindingTests]")
|
||||
{
|
||||
arma::mat inputData;
|
||||
if (!data::Load("iris.csv", inputData))
|
||||
BOOST_FAIL("Cannot load iris.csv!");
|
||||
FAIL("Cannot load iris.csv!");
|
||||
|
||||
arma::Row<size_t> labels;
|
||||
if (!data::Load("iris_labels.txt", labels))
|
||||
BOOST_FAIL("Cannot load iris_labels.txt!");
|
||||
FAIL("Cannot load iris_labels.txt!");
|
||||
|
||||
// Input random data points.
|
||||
SetInputParam("input", inputData);
|
||||
@@ -125,13 +125,13 @@ BOOST_AUTO_TEST_CASE(LMNNOptimizerTest)
|
||||
mlpackMain();
|
||||
|
||||
// Check that final output has expected number of rows and colums.
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("output").n_rows,
|
||||
REQUIRE(IO::GetParam<arma::mat>("output").n_rows ==
|
||||
inputData.n_rows);
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("output").n_cols,
|
||||
REQUIRE(IO::GetParam<arma::mat>("output").n_cols ==
|
||||
inputData.n_rows);
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("transformed_data").n_rows,
|
||||
REQUIRE(IO::GetParam<arma::mat>("transformed_data").n_rows ==
|
||||
inputData.n_rows);
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("transformed_data").n_cols,
|
||||
REQUIRE(IO::GetParam<arma::mat>("transformed_data").n_cols ==
|
||||
inputData.n_cols);
|
||||
|
||||
// Reset rettings.
|
||||
@@ -146,13 +146,13 @@ BOOST_AUTO_TEST_CASE(LMNNOptimizerTest)
|
||||
mlpackMain();
|
||||
|
||||
// Check that final output has expected number of rows and colums.
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("output").n_rows,
|
||||
REQUIRE(IO::GetParam<arma::mat>("output").n_rows ==
|
||||
inputData.n_rows);
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("output").n_cols,
|
||||
REQUIRE(IO::GetParam<arma::mat>("output").n_cols ==
|
||||
inputData.n_rows);
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("transformed_data").n_rows,
|
||||
REQUIRE(IO::GetParam<arma::mat>("transformed_data").n_rows ==
|
||||
inputData.n_rows);
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("transformed_data").n_cols,
|
||||
REQUIRE(IO::GetParam<arma::mat>("transformed_data").n_cols ==
|
||||
inputData.n_cols);
|
||||
|
||||
// Reset rettings.
|
||||
@@ -167,13 +167,13 @@ BOOST_AUTO_TEST_CASE(LMNNOptimizerTest)
|
||||
mlpackMain();
|
||||
|
||||
// Check that final output has expected number of rows and colums.
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("output").n_rows,
|
||||
REQUIRE(IO::GetParam<arma::mat>("output").n_rows ==
|
||||
inputData.n_rows);
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("output").n_cols,
|
||||
REQUIRE(IO::GetParam<arma::mat>("output").n_cols ==
|
||||
inputData.n_rows);
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("transformed_data").n_rows,
|
||||
REQUIRE(IO::GetParam<arma::mat>("transformed_data").n_rows ==
|
||||
inputData.n_rows);
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("transformed_data").n_cols,
|
||||
REQUIRE(IO::GetParam<arma::mat>("transformed_data").n_cols ==
|
||||
inputData.n_cols);
|
||||
}
|
||||
|
||||
@@ -181,15 +181,16 @@ BOOST_AUTO_TEST_CASE(LMNNOptimizerTest)
|
||||
* Ensure that when we pass a valid initial learning point, we get
|
||||
* output of the same dimensions.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(LMNNValidDistanceTest)
|
||||
TEST_CASE_METHOD(LMNNTestFixture, "LMNNValidDistanceTest",
|
||||
"[LMNNMainTest][BindingTests]")
|
||||
{
|
||||
arma::mat inputData;
|
||||
if (!data::Load("iris.csv", inputData))
|
||||
BOOST_FAIL("Cannot load iris.csv!");
|
||||
FAIL("Cannot load iris.csv!");
|
||||
|
||||
arma::Row<size_t> labels;
|
||||
if (!data::Load("iris_labels.txt", labels))
|
||||
BOOST_FAIL("Cannot load iris_labels.txt!");
|
||||
FAIL("Cannot load iris_labels.txt!");
|
||||
|
||||
// Initial learning point.
|
||||
arma::mat distance;
|
||||
@@ -203,13 +204,13 @@ BOOST_AUTO_TEST_CASE(LMNNValidDistanceTest)
|
||||
mlpackMain();
|
||||
|
||||
// Check that final output has expected number of rows and colums.
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("output").n_rows,
|
||||
REQUIRE(IO::GetParam<arma::mat>("output").n_rows ==
|
||||
inputData.n_rows - 1);
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("output").n_cols,
|
||||
REQUIRE(IO::GetParam<arma::mat>("output").n_cols ==
|
||||
inputData.n_rows);
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("transformed_data").n_rows,
|
||||
REQUIRE(IO::GetParam<arma::mat>("transformed_data").n_rows ==
|
||||
inputData.n_rows - 1);
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("transformed_data").n_cols,
|
||||
REQUIRE(IO::GetParam<arma::mat>("transformed_data").n_cols ==
|
||||
inputData.n_cols);
|
||||
}
|
||||
|
||||
@@ -217,15 +218,16 @@ BOOST_AUTO_TEST_CASE(LMNNValidDistanceTest)
|
||||
* Ensure that when we pass a valid initial square matrix as the learning
|
||||
* point, we get output of the same dimensions.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(LMNNValidDistanceTest2)
|
||||
TEST_CASE_METHOD(LMNNTestFixture, "LMNNValidDistanceTest2",
|
||||
"[LMNNMainTest][BindingTests]")
|
||||
{
|
||||
arma::mat inputData;
|
||||
if (!data::Load("iris.csv", inputData))
|
||||
BOOST_FAIL("Cannot load iris.csv!");
|
||||
FAIL("Cannot load iris.csv!");
|
||||
|
||||
arma::Row<size_t> labels;
|
||||
if (!data::Load("iris_labels.txt", labels))
|
||||
BOOST_FAIL("Cannot load iris_labels.txt!");
|
||||
FAIL("Cannot load iris_labels.txt!");
|
||||
|
||||
// Initial learning point (square matrix).
|
||||
arma::mat distance;
|
||||
@@ -239,13 +241,13 @@ BOOST_AUTO_TEST_CASE(LMNNValidDistanceTest2)
|
||||
mlpackMain();
|
||||
|
||||
// Check that final output has expected number of rows and colums.
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("output").n_rows,
|
||||
REQUIRE(IO::GetParam<arma::mat>("output").n_rows ==
|
||||
inputData.n_rows);
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("output").n_cols,
|
||||
REQUIRE(IO::GetParam<arma::mat>("output").n_cols ==
|
||||
inputData.n_rows);
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("transformed_data").n_rows,
|
||||
REQUIRE(IO::GetParam<arma::mat>("transformed_data").n_rows ==
|
||||
inputData.n_rows);
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("transformed_data").n_cols,
|
||||
REQUIRE(IO::GetParam<arma::mat>("transformed_data").n_cols ==
|
||||
inputData.n_cols);
|
||||
}
|
||||
|
||||
@@ -253,15 +255,16 @@ BOOST_AUTO_TEST_CASE(LMNNValidDistanceTest2)
|
||||
* Ensure that when we pass an invalid initial learning point, we get
|
||||
* output as the square matrix.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(LMNNInvalidDistanceTest)
|
||||
TEST_CASE_METHOD(LMNNTestFixture, "LMNNInvalidDistanceTest",
|
||||
"[LMNNMainTest][BindingTests]")
|
||||
{
|
||||
arma::mat inputData;
|
||||
if (!data::Load("iris.csv", inputData))
|
||||
BOOST_FAIL("Cannot load iris.csv!");
|
||||
FAIL("Cannot load iris.csv!");
|
||||
|
||||
arma::Row<size_t> labels;
|
||||
if (!data::Load("iris_labels.txt", labels))
|
||||
BOOST_FAIL("Cannot load iris_labels.txt!");
|
||||
FAIL("Cannot load iris_labels.txt!");
|
||||
|
||||
// Initial learning point.
|
||||
arma::mat distance;
|
||||
@@ -275,13 +278,13 @@ BOOST_AUTO_TEST_CASE(LMNNInvalidDistanceTest)
|
||||
mlpackMain();
|
||||
|
||||
// Check that final output has expected number of rows and colums.
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("output").n_rows,
|
||||
REQUIRE(IO::GetParam<arma::mat>("output").n_rows ==
|
||||
inputData.n_rows);
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("output").n_cols,
|
||||
REQUIRE(IO::GetParam<arma::mat>("output").n_cols ==
|
||||
inputData.n_rows);
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("transformed_data").n_rows,
|
||||
REQUIRE(IO::GetParam<arma::mat>("transformed_data").n_rows ==
|
||||
inputData.n_rows);
|
||||
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("transformed_data").n_cols,
|
||||
REQUIRE(IO::GetParam<arma::mat>("transformed_data").n_cols ==
|
||||
inputData.n_cols);
|
||||
}
|
||||
|
||||
@@ -289,7 +292,8 @@ BOOST_AUTO_TEST_CASE(LMNNInvalidDistanceTest)
|
||||
* Ensure that if number of available labels in a class is less than
|
||||
* the number of targets, an error occurs.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(LMNNNumTargetsTest)
|
||||
TEST_CASE_METHOD(LMNNTestFixture, "LMNNNumTargetsTest",
|
||||
"[LMNNMainTest][BindingTests]")
|
||||
{
|
||||
// Input Dataset
|
||||
arma::mat inputData = "-0.1 -0.1 -0.1 0.1 0.1 0.1;"
|
||||
@@ -302,7 +306,7 @@ BOOST_AUTO_TEST_CASE(LMNNNumTargetsTest)
|
||||
|
||||
// Check that an error is thrown.
|
||||
Log::Fatal.ignoreInput = true;
|
||||
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
|
||||
Log::Fatal.ignoreInput = false;
|
||||
}
|
||||
|
||||
@@ -310,15 +314,16 @@ BOOST_AUTO_TEST_CASE(LMNNNumTargetsTest)
|
||||
* Ensure that setting normalize as true results in a
|
||||
* different output matrix then when set to false.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(LMNNDiffNormalizationTest)
|
||||
TEST_CASE_METHOD(LMNNTestFixture, "LMNNDiffNormalizationTest",
|
||||
"[LMNNMainTest][BindingTests]")
|
||||
{
|
||||
arma::mat inputData;
|
||||
if (!data::Load("iris.csv", inputData))
|
||||
BOOST_FAIL("Cannot load iris.csv!");
|
||||
FAIL("Cannot load iris.csv!");
|
||||
|
||||
arma::Row<size_t> labels;
|
||||
if (!data::Load("iris_labels.txt", labels))
|
||||
BOOST_FAIL("Cannot load iris_labels.txt!");
|
||||
FAIL("Cannot load iris_labels.txt!");
|
||||
|
||||
// Set parameters and set normalize to true.
|
||||
SetInputParam("input", inputData);
|
||||
@@ -345,24 +350,24 @@ BOOST_AUTO_TEST_CASE(LMNNDiffNormalizationTest)
|
||||
mlpackMain();
|
||||
|
||||
// Check that the output matrices are different.
|
||||
BOOST_REQUIRE_GT(
|
||||
arma::accu(IO::GetParam<arma::mat>("output") != output), 0);
|
||||
BOOST_REQUIRE_GT(arma::accu(IO::GetParam<arma::mat>("transformed_data") !=
|
||||
transformedData), 0);
|
||||
REQUIRE(arma::accu(IO::GetParam<arma::mat>("output") != output) > 0);
|
||||
REQUIRE(arma::accu(IO::GetParam<arma::mat>("transformed_data") !=
|
||||
transformedData) > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that output is different when step_size is different.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(LMNNDiffStepSizeTest)
|
||||
TEST_CASE_METHOD(LMNNTestFixture, "LMNNDiffStepSizeTest",
|
||||
"[LMNNMainTest][BindingTests]")
|
||||
{
|
||||
arma::mat inputData;
|
||||
if (!data::Load("iris.csv", inputData))
|
||||
BOOST_FAIL("Cannot load iris.csv!");
|
||||
FAIL("Cannot load iris.csv!");
|
||||
|
||||
arma::Row<size_t> labels;
|
||||
if (!data::Load("iris_labels.txt", labels))
|
||||
BOOST_FAIL("Cannot load iris_labels.txt!");
|
||||
FAIL("Cannot load iris_labels.txt!");
|
||||
|
||||
// Set parameters with a small step_size.
|
||||
SetInputParam("input", inputData);
|
||||
@@ -386,27 +391,27 @@ BOOST_AUTO_TEST_CASE(LMNNDiffStepSizeTest)
|
||||
SetInputParam("linear_scan", (bool) true);
|
||||
|
||||
mlpackMain();
|
||||
BOOST_REQUIRE_GT(arma::accu(IO::GetParam<arma::mat>("transformed_data") !=
|
||||
transformedData), 0);
|
||||
REQUIRE(arma::accu(IO::GetParam<arma::mat>("transformed_data") !=
|
||||
transformedData) > 0);
|
||||
// Check that the output matrices are different.
|
||||
BOOST_REQUIRE_GT(
|
||||
arma::accu(IO::GetParam<arma::mat>("output") != output), 0);
|
||||
BOOST_REQUIRE_GT(arma::accu(IO::GetParam<arma::mat>("transformed_data") !=
|
||||
transformedData), 0);
|
||||
REQUIRE(arma::accu(IO::GetParam<arma::mat>("output") != output) > 0);
|
||||
REQUIRE(arma::accu(IO::GetParam<arma::mat>("transformed_data") !=
|
||||
transformedData) > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that output is different when the tolerance is different.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(LMNNDiffToleranceTest)
|
||||
TEST_CASE_METHOD(LMNNTestFixture, "LMNNDiffToleranceTest",
|
||||
"[LMNNMainTest][BindingTests]")
|
||||
{
|
||||
arma::mat inputData;
|
||||
if (!data::Load("iris.csv", inputData))
|
||||
BOOST_FAIL("Cannot load iris.csv!");
|
||||
FAIL("Cannot load iris.csv!");
|
||||
|
||||
arma::Row<size_t> labels;
|
||||
if (!data::Load("iris_labels.txt", labels))
|
||||
BOOST_FAIL("Cannot load iris_labels.txt!");
|
||||
FAIL("Cannot load iris_labels.txt!");
|
||||
|
||||
// Set parameters with a small tolerance.
|
||||
SetInputParam("input", inputData);
|
||||
@@ -430,24 +435,24 @@ BOOST_AUTO_TEST_CASE(LMNNDiffToleranceTest)
|
||||
mlpackMain();
|
||||
|
||||
// Check that the output matrices are different.
|
||||
BOOST_REQUIRE_GT(
|
||||
arma::accu(IO::GetParam<arma::mat>("output") != output), 0);
|
||||
BOOST_REQUIRE_GT(arma::accu(IO::GetParam<arma::mat>("transformed_data") !=
|
||||
transformedData), 0);
|
||||
REQUIRE(arma::accu(IO::GetParam<arma::mat>("output") != output) > 0);
|
||||
REQUIRE(arma::accu(IO::GetParam<arma::mat>("transformed_data") !=
|
||||
transformedData) > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that output is different when batch_size is different.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(LMNNDiffBatchSizeTest)
|
||||
TEST_CASE_METHOD(LMNNTestFixture, "LMNNDiffBatchSizeTest",
|
||||
"[LMNNMainTest][BindingTests]")
|
||||
{
|
||||
arma::mat inputData;
|
||||
if (!data::Load("iris.csv", inputData))
|
||||
BOOST_FAIL("Cannot load iris.csv!");
|
||||
FAIL("Cannot load iris.csv!");
|
||||
|
||||
arma::Row<size_t> labels;
|
||||
if (!data::Load("iris_labels.txt", labels))
|
||||
BOOST_FAIL("Cannot load iris_labels.txt!");
|
||||
FAIL("Cannot load iris_labels.txt!");
|
||||
|
||||
// Set parameters with a small batch_size.
|
||||
SetInputParam("input", inputData);
|
||||
@@ -473,25 +478,25 @@ BOOST_AUTO_TEST_CASE(LMNNDiffBatchSizeTest)
|
||||
mlpackMain();
|
||||
|
||||
// Check that the output matrices are different.
|
||||
BOOST_REQUIRE_GT(
|
||||
arma::accu(IO::GetParam<arma::mat>("output") != output), 0);
|
||||
BOOST_REQUIRE_GT(arma::accu(IO::GetParam<arma::mat>("transformed_data") !=
|
||||
transformedData), 0);
|
||||
REQUIRE(arma::accu(IO::GetParam<arma::mat>("output") != output) > 0);
|
||||
REQUIRE(arma::accu(IO::GetParam<arma::mat>("transformed_data") !=
|
||||
transformedData) > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that different value of number of targets results in a
|
||||
* different output matrix.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(LMNNDiffNumTargetsTest)
|
||||
TEST_CASE_METHOD(LMNNTestFixture, "LMNNDiffNumTargetsTest",
|
||||
"[LMNNMainTest][BindingTests]")
|
||||
{
|
||||
arma::mat inputData;
|
||||
if (!data::Load("iris.csv", inputData))
|
||||
BOOST_FAIL("Cannot load iris.csv!");
|
||||
FAIL("Cannot load iris.csv!");
|
||||
|
||||
arma::Row<size_t> labels;
|
||||
if (!data::Load("iris_labels.txt", labels))
|
||||
BOOST_FAIL("Cannot load iris_labels.txt!");
|
||||
FAIL("Cannot load iris_labels.txt!");
|
||||
|
||||
// Set parameters.
|
||||
SetInputParam("input", inputData);
|
||||
@@ -517,25 +522,25 @@ BOOST_AUTO_TEST_CASE(LMNNDiffNumTargetsTest)
|
||||
mlpackMain();
|
||||
|
||||
// Check that the output matrices are different.
|
||||
BOOST_REQUIRE_GT(
|
||||
arma::accu(IO::GetParam<arma::mat>("output") != output), 0);
|
||||
BOOST_REQUIRE_GT(arma::accu(IO::GetParam<arma::mat>("transformed_data") !=
|
||||
transformedData), 0);
|
||||
REQUIRE(arma::accu(IO::GetParam<arma::mat>("output") != output) > 0);
|
||||
REQUIRE(arma::accu(IO::GetParam<arma::mat>("transformed_data") !=
|
||||
transformedData) > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that different value of regularization results in a
|
||||
* different output matrix.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(LMNNDiffRegularizationTest)
|
||||
TEST_CASE_METHOD(LMNNTestFixture, "LMNNDiffRegularizationTest",
|
||||
"[LMNNMainTest][BindingTests]")
|
||||
{
|
||||
arma::mat inputData;
|
||||
if (!data::Load("iris.csv", inputData))
|
||||
BOOST_FAIL("Cannot load iris.csv!");
|
||||
FAIL("Cannot load iris.csv!");
|
||||
|
||||
arma::Row<size_t> labels;
|
||||
if (!data::Load("iris_labels.txt", labels))
|
||||
BOOST_FAIL("Cannot load iris_labels.txt!");
|
||||
FAIL("Cannot load iris_labels.txt!");
|
||||
|
||||
// Set parameters.
|
||||
SetInputParam("input", inputData);
|
||||
@@ -561,25 +566,25 @@ BOOST_AUTO_TEST_CASE(LMNNDiffRegularizationTest)
|
||||
mlpackMain();
|
||||
|
||||
// Check that the output matrices are different.
|
||||
BOOST_REQUIRE_GT(
|
||||
arma::accu(IO::GetParam<arma::mat>("output") != output), 0);
|
||||
BOOST_REQUIRE_GT(arma::accu(IO::GetParam<arma::mat>("transformed_data") !=
|
||||
transformedData), 0);
|
||||
REQUIRE(arma::accu(IO::GetParam<arma::mat>("output") != output) > 0);
|
||||
REQUIRE(arma::accu(IO::GetParam<arma::mat>("transformed_data") !=
|
||||
transformedData) > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that different value of range results in a
|
||||
* different output matrix.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(LMNNDiffRangeTest)
|
||||
TEST_CASE_METHOD(LMNNTestFixture, "LMNNDiffRangeTest",
|
||||
"[LMNNMainTest][BindingTests]")
|
||||
{
|
||||
arma::mat inputData;
|
||||
if (!data::Load("iris.csv", inputData))
|
||||
BOOST_FAIL("Cannot load iris.csv!");
|
||||
FAIL("Cannot load iris.csv!");
|
||||
|
||||
arma::Row<size_t> labels;
|
||||
if (!data::Load("iris_labels.txt", labels))
|
||||
BOOST_FAIL("Cannot load iris_labels.txt!");
|
||||
FAIL("Cannot load iris_labels.txt!");
|
||||
|
||||
// Set parameters.
|
||||
SetInputParam("input", inputData);
|
||||
@@ -604,25 +609,25 @@ BOOST_AUTO_TEST_CASE(LMNNDiffRangeTest)
|
||||
mlpackMain();
|
||||
|
||||
// Check that the output matrices are different.
|
||||
BOOST_REQUIRE_GT(
|
||||
arma::accu(IO::GetParam<arma::mat>("output") != output), 0);
|
||||
BOOST_REQUIRE_GT(arma::accu(IO::GetParam<arma::mat>("transformed_data") !=
|
||||
transformedData), 0);
|
||||
REQUIRE(arma::accu(IO::GetParam<arma::mat>("output") != output) > 0);
|
||||
REQUIRE(arma::accu(IO::GetParam<arma::mat>("transformed_data") !=
|
||||
transformedData) > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that using a different value of max_iteration
|
||||
* results in a different output matrix.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(LMNNDiffMaxIterationTest)
|
||||
TEST_CASE_METHOD(LMNNTestFixture, "LMNNDiffMaxIterationTest",
|
||||
"[LMNNMainTest][BindingTests]")
|
||||
{
|
||||
arma::mat inputData;
|
||||
if (!data::Load("iris.csv", inputData))
|
||||
BOOST_FAIL("Cannot load iris.csv!");
|
||||
FAIL("Cannot load iris.csv!");
|
||||
|
||||
arma::Row<size_t> labels;
|
||||
if (!data::Load("iris_labels.txt", labels))
|
||||
BOOST_FAIL("Cannot load iris_labels.txt!");
|
||||
FAIL("Cannot load iris_labels.txt!");
|
||||
|
||||
// Set parameters with a small max_iterations.
|
||||
SetInputParam("input", inputData);
|
||||
@@ -652,25 +657,25 @@ BOOST_AUTO_TEST_CASE(LMNNDiffMaxIterationTest)
|
||||
mlpackMain();
|
||||
|
||||
// Check that the output matrices are different.
|
||||
BOOST_REQUIRE_GT(
|
||||
arma::accu(IO::GetParam<arma::mat>("output") != output), 0);
|
||||
BOOST_REQUIRE_GT(arma::accu(IO::GetParam<arma::mat>("transformed_data") !=
|
||||
transformedData), 0);
|
||||
REQUIRE(arma::accu(IO::GetParam<arma::mat>("output") != output) > 0);
|
||||
REQUIRE(arma::accu(IO::GetParam<arma::mat>("transformed_data") !=
|
||||
transformedData) > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that using a different value of passes
|
||||
* results in a different output matrix.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(LMNNDiffPassesTest)
|
||||
TEST_CASE_METHOD(LMNNTestFixture, "LMNNDiffPassesTest",
|
||||
"[LMNNMainTest][BindingTests]")
|
||||
{
|
||||
arma::mat inputData;
|
||||
if (!data::Load("iris.csv", inputData))
|
||||
BOOST_FAIL("Cannot load iris.csv!");
|
||||
FAIL("Cannot load iris.csv!");
|
||||
|
||||
arma::Row<size_t> labels;
|
||||
if (!data::Load("iris_labels.txt", labels))
|
||||
BOOST_FAIL("Cannot load iris_labels.txt!");
|
||||
FAIL("Cannot load iris_labels.txt!");
|
||||
|
||||
// Set parameters with a small passes.
|
||||
SetInputParam("input", inputData);
|
||||
@@ -696,10 +701,9 @@ BOOST_AUTO_TEST_CASE(LMNNDiffPassesTest)
|
||||
mlpackMain();
|
||||
|
||||
// Check that the output matrices are different.
|
||||
BOOST_REQUIRE_GT(
|
||||
arma::accu(IO::GetParam<arma::mat>("output") != output), 0);
|
||||
BOOST_REQUIRE_GT(arma::accu(IO::GetParam<arma::mat>("transformed_data") !=
|
||||
transformedData), 0);
|
||||
REQUIRE(arma::accu(IO::GetParam<arma::mat>("output") != output) > 0);
|
||||
REQUIRE(arma::accu(IO::GetParam<arma::mat>("transformed_data") !=
|
||||
transformedData) > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -707,15 +711,16 @@ BOOST_AUTO_TEST_CASE(LMNNDiffPassesTest)
|
||||
* and regularization, step size, max iterations, rank, passes & tolerance are
|
||||
* always non-negative
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(LMNNBoundsTest)
|
||||
TEST_CASE_METHOD(LMNNTestFixture, "LMNNBoundsTest",
|
||||
"[LMNNMainTest][BindingTests]")
|
||||
{
|
||||
arma::mat inputData;
|
||||
if (!data::Load("iris.csv", inputData))
|
||||
BOOST_FAIL("Cannot load iris.csv!");
|
||||
FAIL("Cannot load iris.csv!");
|
||||
|
||||
arma::Row<size_t> labels;
|
||||
if (!data::Load("iris_labels.txt", labels))
|
||||
BOOST_FAIL("Cannot load iris_labels.txt!");
|
||||
FAIL("Cannot load iris_labels.txt!");
|
||||
|
||||
// Test for number of targets value.
|
||||
|
||||
@@ -725,7 +730,7 @@ BOOST_AUTO_TEST_CASE(LMNNBoundsTest)
|
||||
SetInputParam("k", (int) 0);
|
||||
|
||||
Log::Fatal.ignoreInput = true;
|
||||
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
|
||||
Log::Fatal.ignoreInput = false;
|
||||
|
||||
// Reset settings.
|
||||
@@ -740,7 +745,7 @@ BOOST_AUTO_TEST_CASE(LMNNBoundsTest)
|
||||
SetInputParam("range", (int) 0);
|
||||
|
||||
Log::Fatal.ignoreInput = true;
|
||||
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
|
||||
Log::Fatal.ignoreInput = false;
|
||||
|
||||
// Reset settings.
|
||||
@@ -755,7 +760,7 @@ BOOST_AUTO_TEST_CASE(LMNNBoundsTest)
|
||||
SetInputParam("batch_size", (int) 0);
|
||||
|
||||
Log::Fatal.ignoreInput = true;
|
||||
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
|
||||
Log::Fatal.ignoreInput = false;
|
||||
|
||||
// Reset settings.
|
||||
@@ -770,7 +775,7 @@ BOOST_AUTO_TEST_CASE(LMNNBoundsTest)
|
||||
SetInputParam("regularization", (double) -1.0);
|
||||
|
||||
Log::Fatal.ignoreInput = true;
|
||||
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
|
||||
Log::Fatal.ignoreInput = false;
|
||||
|
||||
// Reset settings.
|
||||
@@ -785,7 +790,7 @@ BOOST_AUTO_TEST_CASE(LMNNBoundsTest)
|
||||
SetInputParam("step_size", (double) -1.0);
|
||||
|
||||
Log::Fatal.ignoreInput = true;
|
||||
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
|
||||
Log::Fatal.ignoreInput = false;
|
||||
|
||||
// Reset settings.
|
||||
@@ -800,7 +805,7 @@ BOOST_AUTO_TEST_CASE(LMNNBoundsTest)
|
||||
SetInputParam("max_iterations", (int) -1.0);
|
||||
|
||||
Log::Fatal.ignoreInput = true;
|
||||
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
|
||||
Log::Fatal.ignoreInput = false;
|
||||
|
||||
// Reset settings.
|
||||
@@ -815,7 +820,7 @@ BOOST_AUTO_TEST_CASE(LMNNBoundsTest)
|
||||
SetInputParam("passes", (int) -1.0);
|
||||
|
||||
Log::Fatal.ignoreInput = true;
|
||||
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
|
||||
Log::Fatal.ignoreInput = false;
|
||||
|
||||
// Reset settings.
|
||||
@@ -830,7 +835,7 @@ BOOST_AUTO_TEST_CASE(LMNNBoundsTest)
|
||||
SetInputParam("rank", (int) -1.0);
|
||||
|
||||
Log::Fatal.ignoreInput = true;
|
||||
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
|
||||
Log::Fatal.ignoreInput = false;
|
||||
|
||||
// Reset settings.
|
||||
@@ -845,8 +850,6 @@ BOOST_AUTO_TEST_CASE(LMNNBoundsTest)
|
||||
SetInputParam("tolerance", (double) -1.0);
|
||||
|
||||
Log::Fatal.ignoreInput = true;
|
||||
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
|
||||
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
|
||||
Log::Fatal.ignoreInput = false;
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END();
|
||||
|
||||
@@ -30,18 +30,16 @@
|
||||
|
||||
#include <ensmallen.hpp>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include "test_tools.hpp"
|
||||
#include "catch.hpp"
|
||||
|
||||
using namespace mlpack;
|
||||
using namespace mlpack::ann;
|
||||
using namespace ens;
|
||||
using namespace mlpack::rl;
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(RewardClippingTest);
|
||||
|
||||
// Test checking that reward clipping works with vanilla update.
|
||||
BOOST_AUTO_TEST_CASE(ClippedRewardTest)
|
||||
TEST_CASE("ClippedRewardTest", "[RewardClippingTest]")
|
||||
{
|
||||
Pendulum task;
|
||||
RewardClipping<Pendulum> rewardClipping(task, -2.0, +2.0);
|
||||
@@ -51,12 +49,12 @@ BOOST_AUTO_TEST_CASE(ClippedRewardTest)
|
||||
action.action[0] = mlpack::math::Random(-1.0, 1.0);
|
||||
double reward = rewardClipping.Sample(state, action);
|
||||
|
||||
BOOST_REQUIRE(reward <= 2.0);
|
||||
BOOST_REQUIRE(reward >= -2.0);
|
||||
REQUIRE(reward <= 2.0);
|
||||
REQUIRE(reward >= -2.0);
|
||||
}
|
||||
|
||||
//! Test DQN in Acrobot task.
|
||||
BOOST_AUTO_TEST_CASE(RewardClippedAcrobotWithDQN)
|
||||
TEST_CASE("RewardClippedAcrobotWithDQN", "[RewardClippingTest]")
|
||||
{
|
||||
// We will allow three trials, although it would be very uncommon for the test
|
||||
// to use more than one.
|
||||
@@ -129,7 +127,5 @@ BOOST_AUTO_TEST_CASE(RewardClippedAcrobotWithDQN)
|
||||
break;
|
||||
}
|
||||
|
||||
BOOST_REQUIRE(converged);
|
||||
REQUIRE(converged);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END();
|
||||
|
||||
@@ -23,19 +23,17 @@
|
||||
#include <mlpack/methods/reinforcement_learning/replay/random_replay.hpp>
|
||||
#include <mlpack/methods/reinforcement_learning/policy/greedy_policy.hpp>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include "test_tools.hpp"
|
||||
#include "catch.hpp"
|
||||
#include "test_catch_tools.hpp"
|
||||
|
||||
using namespace mlpack;
|
||||
using namespace mlpack::rl;
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(RLComponentsTest)
|
||||
|
||||
/**
|
||||
* Constructs a Pendulum instance and check if the main routine works as it
|
||||
* should be working.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(SimplePendulumTest)
|
||||
TEST_CASE("SimplePendulumTest", "[RLComponentsTest]")
|
||||
{
|
||||
Pendulum task = Pendulum();
|
||||
task.MaxSteps() = 20;
|
||||
@@ -45,7 +43,7 @@ BOOST_AUTO_TEST_CASE(SimplePendulumTest)
|
||||
action.action[0] = math::Random(-2.0, 2.0);
|
||||
double reward, minReward = 0.0;
|
||||
|
||||
BOOST_REQUIRE(!task.IsTerminal(state));
|
||||
REQUIRE(!task.IsTerminal(state));
|
||||
|
||||
while (!task.IsTerminal(state))
|
||||
{
|
||||
@@ -54,22 +52,22 @@ BOOST_AUTO_TEST_CASE(SimplePendulumTest)
|
||||
}
|
||||
|
||||
// The reward is always negative. Check if not lower than lowest possible.
|
||||
BOOST_REQUIRE(minReward >= -(pow(M_PI, 2) + 6.404));
|
||||
REQUIRE(minReward >= -(pow(M_PI, 2) + 6.404));
|
||||
|
||||
// Check if the number of steps performed is less or equal as the maximum
|
||||
// allowed, since we use a random action there is no guarantee that we will
|
||||
// reach the maximum number of steps.
|
||||
BOOST_REQUIRE_LE(task.StepsPerformed(), 20);
|
||||
REQUIRE(task.StepsPerformed() <= 20);
|
||||
|
||||
// The action is simply the torque. Check if dimension is 1.
|
||||
BOOST_REQUIRE_EQUAL(1, static_cast<size_t>(Pendulum::Action::size));
|
||||
REQUIRE(1 == static_cast<size_t>(Pendulum::Action::size));
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a Continuous MountainCar instance and check if the main rountine
|
||||
* works as it should be.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(SimpleContinuousMountainCarTest)
|
||||
TEST_CASE("SimpleContinuousMountainCarTest", "[RLComponentsTest]")
|
||||
{
|
||||
ContinuousMountainCar task = ContinuousMountainCar();
|
||||
task.MaxSteps() = 5;
|
||||
@@ -79,24 +77,24 @@ BOOST_AUTO_TEST_CASE(SimpleContinuousMountainCarTest)
|
||||
action.action[0] = math::Random(-1.0, 1.0);
|
||||
double reward = task.Sample(state, action);
|
||||
// Maximum reward possible is 100.
|
||||
BOOST_REQUIRE(reward <= 100.0);
|
||||
BOOST_REQUIRE(!task.IsTerminal(state));
|
||||
REQUIRE(reward <= 100.0);
|
||||
REQUIRE(!task.IsTerminal(state));
|
||||
|
||||
while (!task.IsTerminal(state))
|
||||
task.Sample(state, action, state);
|
||||
|
||||
// Check if the number of steps performed is the same as the maximum allowed.
|
||||
BOOST_REQUIRE_EQUAL(task.StepsPerformed(), 5);
|
||||
REQUIRE(task.StepsPerformed() == 5);
|
||||
|
||||
// Check if the size of the action space is 1.
|
||||
BOOST_REQUIRE_EQUAL(1, action.size);
|
||||
REQUIRE(1 == action.size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a Acrobot instance and check if the main rountine works as
|
||||
* it should be.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(SimpleAcrobotTest)
|
||||
TEST_CASE("SimpleAcrobotTest", "[RLComponentsTest]")
|
||||
{
|
||||
Acrobot task = Acrobot();
|
||||
task.MaxSteps() = 5;
|
||||
@@ -106,24 +104,24 @@ BOOST_AUTO_TEST_CASE(SimpleAcrobotTest)
|
||||
action.action = Acrobot::Action::actions::negativeTorque;
|
||||
double reward = task.Sample(state, action);
|
||||
|
||||
BOOST_REQUIRE_EQUAL(reward, -1.0);
|
||||
BOOST_REQUIRE(!task.IsTerminal(state));
|
||||
REQUIRE(reward == -1.0);
|
||||
REQUIRE(!task.IsTerminal(state));
|
||||
|
||||
while (!task.IsTerminal(state))
|
||||
task.Sample(state, action, state);
|
||||
|
||||
// Check if the number of steps performed is the same as the maximum allowed.
|
||||
BOOST_REQUIRE_EQUAL(task.StepsPerformed(), 5);
|
||||
REQUIRE(task.StepsPerformed() == 5);
|
||||
|
||||
// Check if the size of the action space is 3.
|
||||
BOOST_REQUIRE_EQUAL(3, static_cast<size_t>(Acrobot::Action::size));
|
||||
REQUIRE(3 == static_cast<size_t>(Acrobot::Action::size));
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a MountainCar instance and check if the main rountine works as
|
||||
* it should be.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(SimpleMountainCarTest)
|
||||
TEST_CASE("SimpleMountainCarTest", "[RLComponentsTest]")
|
||||
{
|
||||
MountainCar task = MountainCar();
|
||||
task.MaxSteps() = 5;
|
||||
@@ -133,24 +131,24 @@ BOOST_AUTO_TEST_CASE(SimpleMountainCarTest)
|
||||
action.action = MountainCar::Action::actions::backward;
|
||||
double reward = task.Sample(state, action);
|
||||
|
||||
BOOST_REQUIRE_EQUAL(reward, -1.0);
|
||||
BOOST_REQUIRE(!task.IsTerminal(state));
|
||||
REQUIRE(reward == -1.0);
|
||||
REQUIRE(!task.IsTerminal(state));
|
||||
|
||||
while (!task.IsTerminal(state))
|
||||
task.Sample(state, action, state);
|
||||
|
||||
// Check if the number of steps performed is the same as the maximum allowed.
|
||||
BOOST_REQUIRE_EQUAL(task.StepsPerformed(), 5);
|
||||
REQUIRE(task.StepsPerformed() == 5);
|
||||
|
||||
// Check if the size of the action space is 3.
|
||||
BOOST_REQUIRE_EQUAL(3, static_cast<size_t>(MountainCar::Action::size));
|
||||
REQUIRE(3 == static_cast<size_t>(MountainCar::Action::size));
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a CartPole instance and check if the main routine works as
|
||||
* it should be.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(SimpleCartPoleTest)
|
||||
TEST_CASE("SimpleCartPoleTest", "[RLComponentsTest]")
|
||||
{
|
||||
CartPole task = CartPole();
|
||||
task.MaxSteps() = 5;
|
||||
@@ -160,23 +158,23 @@ BOOST_AUTO_TEST_CASE(SimpleCartPoleTest)
|
||||
action.action = CartPole::Action::actions::backward;
|
||||
double reward = task.Sample(state, action);
|
||||
|
||||
BOOST_REQUIRE_EQUAL(reward, 1.0);
|
||||
BOOST_REQUIRE(!task.IsTerminal(state));
|
||||
REQUIRE(reward == 1.0);
|
||||
REQUIRE(!task.IsTerminal(state));
|
||||
|
||||
while (!task.IsTerminal(state))
|
||||
task.Sample(state, action, state);
|
||||
|
||||
// Check if the number of steps performed is the same as the maximum allowed.
|
||||
BOOST_REQUIRE_EQUAL(task.StepsPerformed(), 5);
|
||||
REQUIRE(task.StepsPerformed() == 5);
|
||||
|
||||
BOOST_REQUIRE_EQUAL(2, static_cast<size_t>(CartPole::Action::size));
|
||||
REQUIRE(2 == static_cast<size_t>(CartPole::Action::size));
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a DoublePoleCart instance and check if the main routine works as
|
||||
* it should be.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(DoublePoleCartTest)
|
||||
TEST_CASE("DoublePoleCartTest", "[RLComponentsTest]")
|
||||
{
|
||||
DoublePoleCart task = DoublePoleCart();
|
||||
task.MaxSteps() = 5;
|
||||
@@ -186,22 +184,22 @@ BOOST_AUTO_TEST_CASE(DoublePoleCartTest)
|
||||
action.action = DoublePoleCart::Action::actions::backward;
|
||||
double reward = task.Sample(state, action);
|
||||
|
||||
BOOST_REQUIRE_EQUAL(reward, 1.0);
|
||||
BOOST_REQUIRE(!task.IsTerminal(state));
|
||||
REQUIRE(reward == 1.0);
|
||||
REQUIRE(!task.IsTerminal(state));
|
||||
|
||||
while (!task.IsTerminal(state))
|
||||
task.Sample(state, action, state);
|
||||
|
||||
// Check if the number of steps performed is the same as the maximum allowed.
|
||||
BOOST_REQUIRE_EQUAL(task.StepsPerformed(), 5);
|
||||
BOOST_REQUIRE_EQUAL(2, static_cast<size_t>(DoublePoleCart::Action::size));
|
||||
REQUIRE(task.StepsPerformed() == 5);
|
||||
REQUIRE(2 == static_cast<size_t>(DoublePoleCart::Action::size));
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a ContinuousDoublePoleCart instance and check if the main
|
||||
* routine works as it should be.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(ContinuousDoublePoleCartTest)
|
||||
TEST_CASE("ContinuousDoublePoleCartTest", "[RLComponentsTest]")
|
||||
{
|
||||
ContinuousDoublePoleCart task = ContinuousDoublePoleCart();
|
||||
task.MaxSteps() = 5;
|
||||
@@ -211,22 +209,22 @@ BOOST_AUTO_TEST_CASE(ContinuousDoublePoleCartTest)
|
||||
action.action[0] = math::Random(-1.0, 1.0);
|
||||
double reward = task.Sample(state, action);
|
||||
|
||||
BOOST_REQUIRE_EQUAL(reward, 1.0);
|
||||
BOOST_REQUIRE(!task.IsTerminal(state));
|
||||
REQUIRE(reward == 1.0);
|
||||
REQUIRE(!task.IsTerminal(state));
|
||||
|
||||
while (!task.IsTerminal(state))
|
||||
task.Sample(state, action, state);
|
||||
|
||||
// Check if the number of steps performed is the same as the maximum allowed.
|
||||
BOOST_REQUIRE_EQUAL(task.StepsPerformed(), 5);
|
||||
BOOST_REQUIRE_EQUAL(1, action.size);
|
||||
REQUIRE(task.StepsPerformed() == 5);
|
||||
REQUIRE(1 == action.size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a random replay instance and check if it works as
|
||||
* it should be.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(RandomReplayTest)
|
||||
TEST_CASE("RandomReplayTest", "[RLComponentsTest]")
|
||||
{
|
||||
RandomReplay<MountainCar> replay(1, 3);
|
||||
MountainCar env;
|
||||
@@ -248,18 +246,18 @@ BOOST_AUTO_TEST_CASE(RandomReplayTest)
|
||||
sampledTerminal);
|
||||
|
||||
CheckMatrices(state.Encode(), sampledState);
|
||||
BOOST_REQUIRE_EQUAL(sampledAction.size(), 1);
|
||||
BOOST_REQUIRE_EQUAL(action.action, sampledAction[0].action);
|
||||
BOOST_REQUIRE_CLOSE(reward, arma::as_scalar(sampledReward), 1e-5);
|
||||
REQUIRE(sampledAction.size() == 1);
|
||||
REQUIRE(action.action == sampledAction[0].action);
|
||||
REQUIRE(reward == Approx(arma::as_scalar(sampledReward)).epsilon(1e-7));
|
||||
CheckMatrices(nextState.Encode(), sampledNextState);
|
||||
BOOST_REQUIRE_EQUAL(false, arma::as_scalar(sampledTerminal));
|
||||
BOOST_REQUIRE_EQUAL(1, replay.Size());
|
||||
REQUIRE(false == arma::as_scalar(sampledTerminal));
|
||||
REQUIRE(1 == replay.Size());
|
||||
|
||||
//! Overwrite the memory with a nonsense record
|
||||
for (size_t i = 0; i < 5; ++i)
|
||||
replay.Store(nextState, action, reward, state, true, 0.9);
|
||||
|
||||
BOOST_REQUIRE_EQUAL(3, replay.Size());
|
||||
REQUIRE(3 == replay.Size());
|
||||
|
||||
//! Sample several times, the original record shouldn't appear
|
||||
for (size_t i = 0; i < 30; ++i)
|
||||
@@ -269,7 +267,7 @@ BOOST_AUTO_TEST_CASE(RandomReplayTest)
|
||||
|
||||
CheckMatrices(state.Encode(), sampledNextState);
|
||||
CheckMatrices(nextState.Encode(), sampledState);
|
||||
BOOST_REQUIRE_EQUAL(true, arma::as_scalar(sampledTerminal));
|
||||
REQUIRE(true == arma::as_scalar(sampledTerminal));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -277,15 +275,15 @@ BOOST_AUTO_TEST_CASE(RandomReplayTest)
|
||||
* Construct a greedy policy instance and check if it works as
|
||||
* it should be.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(GreedyPolicyTest)
|
||||
TEST_CASE("GreedyPolicyTest", "[RLComponentsTest]")
|
||||
{
|
||||
GreedyPolicy<CartPole> policy(1.0, 10, 0.0, 0.99);
|
||||
for (size_t i = 0; i < 15; ++i)
|
||||
policy.Anneal();
|
||||
BOOST_REQUIRE_CLOSE(0.0, policy.Epsilon(), 1e-5);
|
||||
REQUIRE(0.0 == Approx(policy.Epsilon()).epsilon(1e-7));
|
||||
arma::colvec actionValue = arma::randn<arma::colvec>(CartPole::Action::size);
|
||||
CartPole::Action action = policy.Sample(actionValue);
|
||||
BOOST_REQUIRE_CLOSE(actionValue[action.action], actionValue.max(), 1e-5);
|
||||
}
|
||||
REQUIRE(actionValue[action.action] ==
|
||||
Approx(actionValue.max()).epsilon(1e-7));
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user