migrate ann_* and related test from boost to catch2

This commit is contained in:
jeffin143
2020-08-01 20:16:20 +05:30
parent b74c50d0ca
commit 0997b7e6fe
6 changed files with 483 additions and 510 deletions
+5 -6
View File
@@ -1,10 +1,5 @@
# mlpack test executable.
add_executable(mlpack_test
ann_dist_test.cpp
ann_layer_test.cpp
ann_regularizer_test.cpp
ann_test_tools.hpp
ann_visitor_test.cpp
arma_extend_test.cpp
async_learning_test.cpp
augmented_rnns_tasks_test.cpp
@@ -147,6 +142,11 @@ add_executable(mlpack_catch_test
adaboost_test.cpp
akfn_test.cpp
aknn_test.cpp
ann_dist_test.cpp
ann_layer_test.cpp
ann_regularizer_test.cpp
ann_test_tools.hpp
ann_visitor_test.cpp
armadillo_svd_test.cpp
bias_svd_test.cpp
block_krylov_svd_test.cpp
@@ -223,7 +223,6 @@ add_custom_command(TARGET mlpack_test
# The list of long running parallel tests
set(parallel_tests
"ANNLayerTest;"
"AsyncLearningTest;"
"LocalCoordinateCodingTest;"
"FeedForwardNetworkTest;"
+24 -33
View File
@@ -16,20 +16,18 @@
#include <mlpack/methods/ann/dists/normal_distribution.hpp>
#include <mlpack/methods/ann/init_rules/random_init.hpp>
#include <boost/test/unit_test.hpp>
#include "test_tools.hpp"
#include "catch.hpp"
#include "test_catch_tools.hpp"
#include <mlpack/methods/ann/activation_functions/logistic_function.hpp>
using namespace mlpack;
using namespace mlpack::ann;
BOOST_AUTO_TEST_SUITE(ANNDistTest);
/**
* Simple bernoulli distribution module test.
*/
BOOST_AUTO_TEST_CASE(SimpleBernoulliDistributionTest)
TEST_CASE("SimpleBernoulliDistributionTest", "[ANNDistTest]")
{
arma::mat param = arma::mat("1 1 0");
BernoulliDistribution<> module(param, false);
@@ -43,7 +41,7 @@ BOOST_AUTO_TEST_CASE(SimpleBernoulliDistributionTest)
/**
* Jacobian bernoulli distribution module test when we don't apply logistic.
*/
BOOST_AUTO_TEST_CASE(JacobianBernoulliDistributionTest)
TEST_CASE("JacobianBernoulliDistributionTest", "[ANNDistTest]")
{
for (size_t i = 0; i < 5; ++i)
{
@@ -78,15 +76,14 @@ BOOST_AUTO_TEST_CASE(JacobianBernoulliDistributionTest)
}
module.LogProbBackward(target, jacobianB);
BOOST_REQUIRE_LE(arma::max(arma::max(arma::abs(jacobianA - jacobianB))),
1e-5);
REQUIRE(arma::max(arma::max(arma::abs(jacobianA - jacobianB))) <= 1e-5);
}
}
/**
* Jacobian bernoulli distribution module test when we apply logistic.
*/
BOOST_AUTO_TEST_CASE(JacobianBernoulliDistributionLogisticTest)
TEST_CASE("JacobianBernoulliDistributionLogisticTest", "[ANNDistTest]")
{
for (size_t i = 0; i < 5; ++i)
{
@@ -124,15 +121,14 @@ BOOST_AUTO_TEST_CASE(JacobianBernoulliDistributionLogisticTest)
}
module.LogProbBackward(target, jacobianB);
BOOST_REQUIRE_LE(arma::max(arma::max(arma::abs(jacobianA - jacobianB))),
3e-5);
REQUIRE(arma::max(arma::max(arma::abs(jacobianA - jacobianB))) <= 3e-5);
}
}
/**
* Normal Distribution module test.
*/
BOOST_AUTO_TEST_CASE(NormalDistributionTest)
TEST_CASE("NormalDistributionTest", "[ANNDistTest]")
{
arma::vec mu = {1.1, 1.2, 1.5, 1.7};
arma::vec sigma = {0.1, 0.11, 0.5, 0.23};
@@ -145,29 +141,29 @@ BOOST_AUTO_TEST_CASE(NormalDistributionTest)
normalDist.LogProbability(x, prob);
// Testing output of log probability for some random mu, sigma and x.
BOOST_REQUIRE_CLOSE(prob[0], 1.2586464, 1e-3);
BOOST_REQUIRE_CLOSE(prob[1], 0.8751131, 1e-3);
BOOST_REQUIRE_CLOSE(prob[2], -0.30579138, 1e-3);
BOOST_REQUIRE_CLOSE(prob[3], -5.498411, 1e-3);
REQUIRE(prob[0] == Approx( 1.2586464).epsilon(1e-5));
REQUIRE(prob[1] == Approx( 0.8751131).epsilon(1e-5));
REQUIRE(prob[2] == Approx( -0.30579138).epsilon(1e-5));
REQUIRE(prob[3] == Approx( -5.498411).epsilon(1e-5));
arma::vec dmu, dsigma;
normalDist.ProbBackward(x, dmu, dsigma);
// Testing output of dmu and dsigma for some random mu, sigma and x.
BOOST_REQUIRE_CLOSE(dmu[0], -17.603287, 1e-3);
BOOST_REQUIRE_CLOSE(dsigma[0], -26.40487, 1e-3);
BOOST_REQUIRE_CLOSE(dmu[1], -19.827663, 1e-3);
BOOST_REQUIRE_CLOSE(dsigma[1], -3.7852707, 1e-3);
BOOST_REQUIRE_CLOSE(dmu[2], 0.5892323, 1e-3);
BOOST_REQUIRE_CLOSE(dsigma[2], -1.2373875, 1e-3);
BOOST_REQUIRE_CLOSE(dmu[3], 0.061901994, 1e-3);
BOOST_REQUIRE_CLOSE(dsigma[3], 0.19751444, 1e-3);
REQUIRE(dmu[0] == Approx( -17.603287).epsilon(1e-5));
REQUIRE(dsigma[0] == Approx( -26.40487).epsilon(1e-5));
REQUIRE(dmu[1] == Approx( -19.827663).epsilon(1e-5));
REQUIRE(dsigma[1] == Approx( -3.7852707).epsilon(1e-5));
REQUIRE(dmu[2] == Approx( 0.5892323).epsilon(1e-5));
REQUIRE(dsigma[2] == Approx( -1.2373875).epsilon(1e-5));
REQUIRE(dmu[3] == Approx( 0.061901994).epsilon(1e-5));
REQUIRE(dsigma[3] == Approx( 0.19751444).epsilon(1e-5));
}
/**
* Jacobian Normal Distribution module test for mean.
*/
BOOST_AUTO_TEST_CASE(JacobianNormalDistributionMeanTest)
TEST_CASE("JacobianNormalDistributionMeanTest", "[ANNDistTest]")
{
for (size_t i = 0; i < 5; i++)
{
@@ -226,15 +222,14 @@ BOOST_AUTO_TEST_CASE(JacobianNormalDistributionMeanTest)
jacobianB.col(k) = deltaMu % deriv;
}
BOOST_REQUIRE_LE(arma::max(arma::max(arma::abs(jacobianA - jacobianB))),
5e-3);
REQUIRE(arma::max(arma::max(arma::abs(jacobianA - jacobianB))) <= 5e-3);
}
}
/**
* Jacobian Normal Distribution module test for standard deviation.
*/
BOOST_AUTO_TEST_CASE(JacobianNormalDistributionStandardDeviationTest)
TEST_CASE("JacobianNormalDistributionStandardDeviationTest", "[ANNDistTest]")
{
for (size_t i = 0; i < 5; i++)
{
@@ -293,10 +288,6 @@ BOOST_AUTO_TEST_CASE(JacobianNormalDistributionStandardDeviationTest)
jacobianB.col(k) = deltaSigma % deriv;
}
BOOST_REQUIRE_LE(arma::max(arma::max(arma::abs(jacobianA - jacobianB))),
5e-3);
REQUIRE(arma::max(arma::max(arma::abs(jacobianA - jacobianB))) <= 5e-3);
}
}
BOOST_AUTO_TEST_SUITE_END();
File diff suppressed because it is too large Load Diff
+8 -12
View File
@@ -16,16 +16,14 @@
#include <mlpack/methods/ann/init_rules/random_init.hpp>
#include <mlpack/methods/ann/regularizer/regularizer.hpp>
#include <boost/test/unit_test.hpp>
#include "catch.hpp"
#include "ann_test_tools.hpp"
#include "serialization.hpp"
#include "serialization_catch.hpp"
using namespace mlpack;
using namespace mlpack::ann;
BOOST_AUTO_TEST_SUITE(ANNRegularizerTest);
BOOST_AUTO_TEST_CASE(GradientL1RegularizerTest)
TEST_CASE("GradientL1RegularizerTest", "[ANNRegularizerTest]")
{
// Add function gradient instantiation.
struct GradientFunction
@@ -51,10 +49,10 @@ BOOST_AUTO_TEST_CASE(GradientL1RegularizerTest)
L1Regularizer reg;
} function;
BOOST_REQUIRE_LE(CheckRegularizerGradient(function), 1e-4);
REQUIRE(CheckRegularizerGradient(function) <= 1e-4);
}
BOOST_AUTO_TEST_CASE(GradientL2RegularizerTest)
TEST_CASE("GradientL2RegularizerTest", "[ANNRegularizerTest]")
{
// Add function gradient instantiation.
struct GradientFunction
@@ -80,10 +78,10 @@ BOOST_AUTO_TEST_CASE(GradientL2RegularizerTest)
L2Regularizer reg;
} function;
BOOST_REQUIRE_LE(CheckRegularizerGradient(function), 1e-4);
REQUIRE(CheckRegularizerGradient(function) <= 1e-4);
}
BOOST_AUTO_TEST_CASE(GradientOrthogonalRegularizerTest)
TEST_CASE("GradientOrthogonalRegularizerTest", "[ANNRegularizerTest]")
{
// Add function gradient instantiation.
struct GradientFunction
@@ -111,7 +109,5 @@ BOOST_AUTO_TEST_CASE(GradientOrthogonalRegularizerTest)
OrthogonalRegularizer reg;
} function;
BOOST_REQUIRE_LE(CheckRegularizerGradient(function), 1e-4);
REQUIRE(CheckRegularizerGradient(function) <= 1e-4);
}
BOOST_AUTO_TEST_SUITE_END();
+5 -9
View File
@@ -15,18 +15,16 @@
#include <mlpack/methods/ann/visitor/weight_set_visitor.hpp>
#include <mlpack/methods/ann/visitor/reset_visitor.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::ann;
BOOST_AUTO_TEST_SUITE(ANNVisitorTest);
/**
* Test that the BiasSetVisitor works properly.
*/
BOOST_AUTO_TEST_CASE(BiasSetVisitorTest)
TEST_CASE("BiasSetVisitorTest", "[ANNVisitorTest]")
{
LayerTypes<> linear = new Linear<>(10, 10);
@@ -43,16 +41,14 @@ BOOST_AUTO_TEST_CASE(BiasSetVisitorTest)
size_t biasSize = boost::apply_visitor(BiasSetVisitor(weight, 0), linear);
BOOST_REQUIRE_EQUAL(biasSize, 10);
REQUIRE(biasSize == 10);
arma::mat input(10, 1), output;
input.randu();
boost::apply_visitor(ForwardVisitor(input, output), linear);
BOOST_REQUIRE_EQUAL(arma::accu(output), 55);
REQUIRE(arma::accu(output) == 55);
boost::apply_visitor(DeleteVisitor(), linear);
}
BOOST_AUTO_TEST_SUITE_END();
+2 -2
View File
@@ -33,7 +33,7 @@ inline void CheckMatrices(const arma::mat& a,
for (size_t i = 0; i < a.n_elem; ++i)
{
if (std::abs(a[i]) < tolerance / 2)
REQUIRE(b[i] == Approx(0.0).margin(tolerance / 200));
REQUIRE(b[i] == Approx(0.0).margin(tolerance / 2));
else
REQUIRE(a[i] == Approx(b[i]).epsilon(tolerance / 100));
}
@@ -62,7 +62,7 @@ inline void CheckMatrices(const arma::cube& a,
for (size_t i = 0; i < a.n_elem; ++i)
{
if (std::abs(a[i]) < tolerance / 2)
REQUIRE(b[i] == Approx(0.0).margin(tolerance / 200));
REQUIRE(b[i] == Approx(0.0).margin(tolerance / 2));
else
REQUIRE(a[i] == Approx(b[i]).epsilon(tolerance / 100));
}