[mlpack-git] master: Properly check relative error. (5b99eda)

gitdub at mlpack.org gitdub at mlpack.org
Wed Jun 22 14:09:07 EDT 2016


Repository : https://github.com/mlpack/mlpack
On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/a9f5622c8a14409111f2d71bf5c0f8aaa8ad4ae1...37fda23945b4f998cd5fa6ec011ae345236c8552

>---------------------------------------------------------------

commit 5b99eda7868665906459b21640014139831c5ca2
Author: MarcosPividori <marcos.pividori at gmail.com>
Date:   Mon Jun 6 15:04:28 2016 -0300

    Properly check relative error.
    
    BOOST_REQUIRE_CLOSE_FRACTION(VAL, REF, ERR) requires:
      abs(VAL - REF) <= ERR * REF   &&   abs(VAL - REF) <= ERR * VAL
    
    REQUIRE_RELATIVE_ERR(VAL, REF, ERR) only requires:
      abs(VAL - REF) <= ERR * REF


>---------------------------------------------------------------

5b99eda7868665906459b21640014139831c5ca2
 src/mlpack/tests/akfn_test.cpp                  | 14 +++++++-------
 src/mlpack/tests/aknn_test.cpp                  | 20 ++++++++++----------
 src/mlpack/tests/old_boost_test_definitions.hpp |  5 +++++
 3 files changed, 22 insertions(+), 17 deletions(-)

diff --git a/src/mlpack/tests/akfn_test.cpp b/src/mlpack/tests/akfn_test.cpp
index 59178c5..350e41e 100644
--- a/src/mlpack/tests/akfn_test.cpp
+++ b/src/mlpack/tests/akfn_test.cpp
@@ -64,7 +64,7 @@ BOOST_AUTO_TEST_CASE(DualTreeVsNaive1)
     kfn->Search(dataset, 15, neighborsTree, distancesTree);
 
     for (size_t i = 0; i < neighborsTree.n_elem; i++)
-      BOOST_REQUIRE_CLOSE(distancesTree(i), distancesNaive(i), epsilon * 100);
+      REQUIRE_RELATIVE_ERR(distancesTree(i), distancesNaive(i), epsilon);
 
     // Clean the memory.
     delete kfn;
@@ -95,7 +95,7 @@ BOOST_AUTO_TEST_CASE(DualTreeVsNaive2)
   kfn.Search(15, neighborsTree, distancesTree);
 
   for (size_t i = 0; i < neighborsTree.n_elem; i++)
-    BOOST_REQUIRE_CLOSE(distancesTree[i], distancesNaive[i], 5);
+    REQUIRE_RELATIVE_ERR(distancesTree[i], distancesNaive[i], 0.05);
 }
 
 /**
@@ -122,7 +122,7 @@ BOOST_AUTO_TEST_CASE(SingleTreeVsNaive)
   kfn.Search(15, neighborsTree, distancesTree);
 
   for (size_t i = 0; i < neighborsTree.n_elem; i++)
-    BOOST_REQUIRE_CLOSE(distancesTree[i], distancesNaive[i], 5);
+    REQUIRE_RELATIVE_ERR(distancesTree[i], distancesNaive[i], 0.05);
 }
 
 /**
@@ -152,7 +152,7 @@ BOOST_AUTO_TEST_CASE(SingleCoverTreeTest)
   coverTreeSearch.Search(data, 15, coverTreeNeighbors, coverTreeDistances);
 
   for (size_t i = 0; i < coverTreeNeighbors.n_elem; ++i)
-    BOOST_REQUIRE_CLOSE(coverTreeDistances[i], naiveDistances[i], 5);
+    REQUIRE_RELATIVE_ERR(coverTreeDistances[i], naiveDistances[i], 0.05);
 }
 
 /**
@@ -182,7 +182,7 @@ BOOST_AUTO_TEST_CASE(DualCoverTreeTest)
   coverTreeSearch.Search(dataset, 15, coverTreeNeighbors, coverTreeDistances);
 
   for (size_t i = 0; i < coverTreeNeighbors.n_elem; ++i)
-    BOOST_REQUIRE_CLOSE(coverTreeDistances[i], naiveDistances[i], 5);
+    REQUIRE_RELATIVE_ERR(coverTreeDistances[i], naiveDistances[i], 0.05);
 }
 
 /**
@@ -209,7 +209,7 @@ BOOST_AUTO_TEST_CASE(SingleBallTreeTest)
   ballTreeSearch.Search(data, 15, ballNeighbors, ballDistances);
 
   for (size_t i = 0; i < ballNeighbors.n_elem; ++i)
-    BOOST_REQUIRE_CLOSE(ballDistances(i), naiveDistances(i), 5);
+    REQUIRE_RELATIVE_ERR(ballDistances(i), naiveDistances(i), 0.05);
 }
 
 /**
@@ -235,7 +235,7 @@ BOOST_AUTO_TEST_CASE(DualBallTreeTest)
   ballTreeSearch.Search(15, ballNeighbors, ballDistances);
 
   for (size_t i = 0; i < ballNeighbors.n_elem; ++i)
-    BOOST_REQUIRE_CLOSE(ballDistances(i), naiveDistances(i), 5);
+    REQUIRE_RELATIVE_ERR(ballDistances(i), naiveDistances(i), 0.05);
 }
 
 BOOST_AUTO_TEST_SUITE_END();
diff --git a/src/mlpack/tests/aknn_test.cpp b/src/mlpack/tests/aknn_test.cpp
index 0fab49b..be14bf1 100644
--- a/src/mlpack/tests/aknn_test.cpp
+++ b/src/mlpack/tests/aknn_test.cpp
@@ -67,7 +67,7 @@ BOOST_AUTO_TEST_CASE(DualTreeVsNaive1)
     knn->Search(dataset, 15, neighborsTree, distancesTree);
 
     for (size_t i = 0; i < neighborsTree.n_elem; i++)
-      BOOST_REQUIRE_CLOSE(distancesTree(i), distancesNaive(i), epsilon * 100);
+      REQUIRE_RELATIVE_ERR(distancesTree(i), distancesNaive(i), epsilon);
 
     // Clean the memory.
     delete knn;
@@ -98,7 +98,7 @@ BOOST_AUTO_TEST_CASE(DualTreeVsNaive2)
   knn.Search(15, neighborsTree, distancesTree);
 
   for (size_t i = 0; i < neighborsTree.n_elem; i++)
-    BOOST_REQUIRE_CLOSE(distancesTree(i), distancesNaive(i), 5);
+    REQUIRE_RELATIVE_ERR(distancesTree(i), distancesNaive(i), 0.05);
 }
 
 /**
@@ -125,7 +125,7 @@ BOOST_AUTO_TEST_CASE(SingleTreeVsNaive)
   knn.Search(15, neighborsTree, distancesTree);
 
   for (size_t i = 0; i < neighborsTree.n_elem; i++)
-    BOOST_REQUIRE_CLOSE(distancesTree[i], distancesNaive[i], 5);
+    REQUIRE_RELATIVE_ERR(distancesTree[i], distancesNaive[i], 0.05);
 }
 
 /**
@@ -155,7 +155,7 @@ BOOST_AUTO_TEST_CASE(SingleCoverTreeTest)
   coverTreeSearch.Search(data, 15, coverTreeNeighbors, coverTreeDistances);
 
   for (size_t i = 0; i < coverTreeNeighbors.n_elem; ++i)
-    BOOST_REQUIRE_CLOSE(coverTreeDistances[i], naiveDistances[i], 5);
+    REQUIRE_RELATIVE_ERR(coverTreeDistances[i], naiveDistances[i], 0.05);
 }
 
 /**
@@ -185,7 +185,7 @@ BOOST_AUTO_TEST_CASE(DualCoverTreeTest)
   coverTreeSearch.Search(&referenceTree, 15, coverNeighbors, coverDistances);
 
   for (size_t i = 0; i < coverNeighbors.n_elem; ++i)
-    BOOST_REQUIRE_CLOSE(coverDistances[i], naiveDistances[i], 5);
+    REQUIRE_RELATIVE_ERR(coverDistances[i], naiveDistances[i], 0.05);
 }
 
 /**
@@ -212,7 +212,7 @@ BOOST_AUTO_TEST_CASE(SingleBallTreeTest)
   ballTreeSearch.Search(data, 15, ballNeighbors, ballDistances);
 
   for (size_t i = 0; i < ballNeighbors.n_elem; ++i)
-    BOOST_REQUIRE_CLOSE(ballDistances(i), naiveDistances(i), 5);
+    REQUIRE_RELATIVE_ERR(ballDistances(i), naiveDistances(i), 0.05);
 }
 
 /**
@@ -238,7 +238,7 @@ BOOST_AUTO_TEST_CASE(DualBallTreeTest)
   ballTreeSearch.Search(15, ballNeighbors, ballDistances);
 
   for (size_t i = 0; i < ballNeighbors.n_elem; ++i)
-    BOOST_REQUIRE_CLOSE(ballDistances(i), naiveDistances(i), 5);
+    REQUIRE_RELATIVE_ERR(ballDistances(i), naiveDistances(i), 0.05);
 }
 
 // Make sure sparse nearest neighbors works with kd trees.
@@ -271,7 +271,7 @@ BOOST_AUTO_TEST_CASE(SparseKNNKDTreeTest)
 
   for (size_t i = 0; i < naiveNeighbors.n_cols; ++i)
     for (size_t j = 0; j < naiveNeighbors.n_rows; ++j)
-      BOOST_REQUIRE_CLOSE(naiveDistances(j, i), sparseDistances(j, i), 5);
+      REQUIRE_RELATIVE_ERR(sparseDistances(j, i), naiveDistances(j, i), 0.05);
 }
 
 // Ensure that we can build an NSModel<NearestNeighborSearch> and get correct
@@ -330,7 +330,7 @@ BOOST_AUTO_TEST_CASE(KNNModelTest)
       BOOST_REQUIRE_EQUAL(distances.n_cols, baselineDistances.n_cols);
       BOOST_REQUIRE_EQUAL(distances.n_elem, baselineDistances.n_elem);
       for (size_t k = 0; k < distances.n_elem; ++k)
-        BOOST_REQUIRE_CLOSE(distances[k], baselineDistances[k], 5);
+        REQUIRE_RELATIVE_ERR(distances[k], baselineDistances[k], 0.05);
     }
   }
 }
@@ -389,7 +389,7 @@ BOOST_AUTO_TEST_CASE(KNNModelMonochromaticTest)
       BOOST_REQUIRE_EQUAL(distances.n_cols, baselineDistances.n_cols);
       BOOST_REQUIRE_EQUAL(distances.n_elem, baselineDistances.n_elem);
       for (size_t k = 0; k < distances.n_elem; ++k)
-        BOOST_REQUIRE_CLOSE(distances[k], baselineDistances[k], 5);
+        REQUIRE_RELATIVE_ERR(distances[k], baselineDistances[k], 0.05);
     }
   }
 }
diff --git a/src/mlpack/tests/old_boost_test_definitions.hpp b/src/mlpack/tests/old_boost_test_definitions.hpp
index 9d98c0b..1586f62 100644
--- a/src/mlpack/tests/old_boost_test_definitions.hpp
+++ b/src/mlpack/tests/old_boost_test_definitions.hpp
@@ -35,4 +35,9 @@
 
 #endif
 
+// Require the approximation L to be within a relative error of E respect to the
+// actual value R.
+#define REQUIRE_RELATIVE_ERR( L, R, E ) \
+    BOOST_REQUIRE_LE( abs((R) - (L)), (E) * (R))
+
 #endif




More information about the mlpack-git mailing list