[mlpack-git] master: Add --algorithm command line option. (43db5f3)

gitdub at mlpack.org gitdub at mlpack.org
Sat Aug 20 14:56:07 EDT 2016


Repository : https://github.com/mlpack/mlpack
On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/3274b05fcc545c3b36f783316fea2e22f79c3d03...1c77230c7d3b9c45fb102cd3c632d9c7248e085e

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

commit 43db5f3a0f7f3aa726d962d676f0cc59394b3ce0
Author: MarcosPividori <marcos.pividori at gmail.com>
Date:   Thu Aug 18 19:10:05 2016 -0300

    Add --algorithm command line option.


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

43db5f3a0f7f3aa726d962d676f0cc59394b3ce0
 src/mlpack/methods/neighbor_search/kfn_main.cpp | 62 +++++++++++++++++------
 src/mlpack/methods/neighbor_search/knn_main.cpp | 66 ++++++++++++++++++-------
 2 files changed, 93 insertions(+), 35 deletions(-)

diff --git a/src/mlpack/methods/neighbor_search/kfn_main.cpp b/src/mlpack/methods/neighbor_search/kfn_main.cpp
index 0f20197..735fc12 100644
--- a/src/mlpack/methods/neighbor_search/kfn_main.cpp
+++ b/src/mlpack/methods/neighbor_search/kfn_main.cpp
@@ -77,10 +77,14 @@ PARAM_FLAG("random_basis", "Before tree-building, project the data onto a "
 PARAM_INT_IN("seed", "Random seed (if 0, std::time(NULL) is used).", "s", 0);
 
 // Search settings.
-PARAM_FLAG("naive", "If true, O(n^2) naive mode is used for computation.", "N");
-PARAM_FLAG("single_mode", "If true, single-tree search is used (as opposed to "
-    "dual-tree search).", "s");
-PARAM_FLAG("greedy", "If true, greedy single-tree search is used.", "G");
+PARAM_STRING_IN("algorithm", "Type of neighbor search: 'naive', 'single_tree', "
+    "'dual_tree', 'greedy'.", "a", "dual_tree");
+PARAM_FLAG("naive", "(Deprecated) If true, O(n^2) naive mode is used for "
+    "computation. Will be removed in mlpack 3.0.0. Use '--algorithm naive' "
+    "instead.", "N");
+PARAM_FLAG("single_mode", "(Deprecated) If true, single-tree search is used "
+    "(as opposed to dual-tree search). Will be removed in mlpack 3.0.0. Use "
+    "'--algorithm single_tree' instead.", "S");
 PARAM_DOUBLE_IN("epsilon", "If specified, will do approximate furthest neighbor"
     " search with given relative error. Must be in the range [0,1).", "e", 0);
 PARAM_DOUBLE_IN("percentage", "If specified, will do approximate furthest "
@@ -181,18 +185,48 @@ int main(int argc, char *argv[])
   // We either have to load the reference data, or we have to load the model.
   NSModel<FurthestNeighborSort> kfn;
 
-  const bool naive = CLI::HasParam("naive");
-  const bool singleMode = CLI::HasParam("single_mode");
-  const bool greedy = CLI::HasParam("greedy");
+  const string algorithm = CLI::GetParam<string>("algorithm");
+  NeighborSearchMode searchMode = DUAL_TREE_MODE;
 
-  NeighborSearchMode searchMode;
-  if (naive)
+  if (algorithm == "naive")
     searchMode = NAIVE_MODE;
-  else if (singleMode)
+  else if (algorithm == "single_tree")
     searchMode = SINGLE_TREE_MODE;
-  else if (greedy)
+  else if (algorithm == "dual_tree")
+    searchMode = DUAL_TREE_MODE;
+  else if (algorithm == "greedy")
     searchMode = GREEDY_SINGLE_TREE_MODE;
-  else searchMode = DUAL_TREE_MODE;
+  else
+    Log::Fatal << "Unknown neighbor search algorithm '" << algorithm << "'; "
+        << "valid choices are 'naive', 'single_tree', 'dual_tree' and 'greedy'."
+        << endl;
+
+  if (CLI::HasParam("single_mode"))
+  {
+    searchMode = SINGLE_TREE_MODE;
+
+    Log::Warn << "--single_mode is deprecated.  Will be removed in mlpack "
+        "3.0.0. Use '--algorithm single_tree' instead." << endl;
+
+    if (CLI::HasParam("algorithm") && algorithm != "single_tree")
+      Log::Fatal << "Contradiction between options --algorithm " << algorithm <<
+          " and --single_mode." << endl;
+  }
+
+  if (CLI::HasParam("naive"))
+  {
+    searchMode = NAIVE_MODE;
+
+    Log::Warn << "--naive is deprecated.  Will be removed in mlpack 3.0.0. Use "
+        "'--algorithm naive' instead." << endl;
+
+    if (CLI::HasParam("algorithm") && algorithm != "naive")
+      Log::Fatal << "Contradiction between options --algorithm " << algorithm <<
+          " and --naive." << endl;
+
+    if (CLI::HasParam("single_mode"))
+      Log::Warn << "--single_mode ignored because --naive is present." << endl;
+  }
 
   if (CLI::HasParam("reference_file"))
   {
@@ -287,10 +321,6 @@ int main(int argc, char *argv[])
           << kfn.Dataset().n_cols << ")." << endl;
     }
 
-    // Naive mode overrides single mode.
-    if (singleMode && naive)
-      Log::Warn << "--single_mode ignored because --naive is present." << endl;
-
     // Now run the search.
     arma::Mat<size_t> neighbors;
     arma::mat distances;
diff --git a/src/mlpack/methods/neighbor_search/knn_main.cpp b/src/mlpack/methods/neighbor_search/knn_main.cpp
index ce28303..6e8fe19 100644
--- a/src/mlpack/methods/neighbor_search/knn_main.cpp
+++ b/src/mlpack/methods/neighbor_search/knn_main.cpp
@@ -84,10 +84,14 @@ PARAM_FLAG("random_basis", "Before tree-building, project the data onto a "
 PARAM_INT_IN("seed", "Random seed (if 0, std::time(NULL) is used).", "s", 0);
 
 // Search settings.
-PARAM_FLAG("naive", "If true, O(n^2) naive mode is used for computation.", "N");
-PARAM_FLAG("single_mode", "If true, single-tree search is used (as opposed to "
-    "dual-tree search).", "S");
-PARAM_FLAG("greedy", "If true, greedy single-tree search is used.", "G");
+PARAM_STRING_IN("algorithm", "Type of neighbor search: 'naive', 'single_tree', "
+    "'dual_tree', 'greedy'.", "a", "dual_tree");
+PARAM_FLAG("naive", "(Deprecated) If true, O(n^2) naive mode is used for "
+    "computation. Will be removed in mlpack 3.0.0. Use '--algorithm naive' "
+    "instead.", "N");
+PARAM_FLAG("single_mode", "(Deprecated) If true, single-tree search is used "
+    "(as opposed to dual-tree search). Will be removed in mlpack 3.0.0. Use "
+    "'--algorithm single_tree' instead.", "S");
 PARAM_DOUBLE_IN("epsilon", "If specified, will do approximate nearest neighbor "
     "search with given relative error.", "e", 0);
 
@@ -193,18 +197,48 @@ int main(int argc, char *argv[])
   // We either have to load the reference data, or we have to load the model.
   NSModel<NearestNeighborSort> knn;
 
-  const bool naive = CLI::HasParam("naive");
-  const bool singleMode = CLI::HasParam("single_mode");
-  const bool greedy = CLI::HasParam("greedy");
+  const string algorithm = CLI::GetParam<string>("algorithm");
+  NeighborSearchMode searchMode = DUAL_TREE_MODE;
 
-  NeighborSearchMode searchMode;
-  if (naive)
+  if (algorithm == "naive")
     searchMode = NAIVE_MODE;
-  else if (singleMode)
+  else if (algorithm == "single_tree")
     searchMode = SINGLE_TREE_MODE;
-  else if (greedy)
+  else if (algorithm == "dual_tree")
+    searchMode = DUAL_TREE_MODE;
+  else if (algorithm == "greedy")
     searchMode = GREEDY_SINGLE_TREE_MODE;
-  else searchMode = DUAL_TREE_MODE;
+  else
+    Log::Fatal << "Unknown neighbor search algorithm '" << algorithm << "'; "
+        << "valid choices are 'naive', 'single_tree', 'dual_tree' and 'greedy'."
+        << endl;
+
+  if (CLI::HasParam("single_mode"))
+  {
+    searchMode = SINGLE_TREE_MODE;
+
+    Log::Warn << "--single_mode is deprecated.  Will be removed in mlpack "
+        "3.0.0. Use '--algorithm single_tree' instead." << endl;
+
+    if (CLI::HasParam("algorithm") && algorithm != "single_tree")
+      Log::Fatal << "Contradiction between options --algorithm " << algorithm <<
+          " and --single_mode." << endl;
+  }
+
+  if (CLI::HasParam("naive"))
+  {
+    searchMode = NAIVE_MODE;
+
+    Log::Warn << "--naive is deprecated.  Will be removed in mlpack 3.0.0. Use "
+        "'--algorithm naive' instead." << endl;
+
+    if (CLI::HasParam("algorithm") && algorithm != "naive")
+      Log::Fatal << "Contradiction between options --algorithm " << algorithm <<
+          " and --naive." << endl;
+
+    if (CLI::HasParam("single_mode"))
+      Log::Warn << "--single_mode ignored because --naive is present." << endl;
+  }
 
   if (CLI::HasParam("reference_file"))
   {
@@ -243,7 +277,7 @@ int main(int argc, char *argv[])
     else
       Log::Fatal << "Unknown tree type '" << treeType << "'; valid choices are "
           << "'kd', 'vp', 'rp', 'max-rp', 'cover', 'r', 'r-star', 'x', 'ball', "
-          << "'hilbert-r', 'r-plus' and 'r-plus-plus', and 'spill'." << endl;
+          << "'hilbert-r', 'r-plus', 'r-plus-plus' and 'spill'." << endl;
 
     knn.TreeType() = tree;
     knn.RandomBasis() = randomBasis;
@@ -305,12 +339,6 @@ int main(int argc, char *argv[])
       Log::Fatal << knn.Dataset().n_cols << ")." << endl;
     }
 
-    // Naive mode overrides single mode.
-    if (singleMode && naive)
-    {
-      Log::Warn << "--single_mode ignored because --naive is present." << endl;
-    }
-
     // Now run the search.
     arma::Mat<size_t> neighbors;
     arma::mat distances;




More information about the mlpack-git mailing list