[mlpack-git] master: Add empty constructor and test. (a33dfc3)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Tue Dec 22 17:02:14 EST 2015


Repository : https://github.com/mlpack/mlpack

On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/6ab20afd8adaf9dcb86bc9a8ea98a24dd8b18743...eb41f4bc27b484c347acc006255104e2f8cc4eef

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

commit a33dfc36edd7381609669b3457f8891fd97c04d2
Author: ryan <ryan at ratml.org>
Date:   Tue Dec 22 15:20:15 2015 -0500

    Add empty constructor and test.


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

a33dfc36edd7381609669b3457f8891fd97c04d2
 src/mlpack/methods/fastmks/fastmks.hpp      | 13 ++++++++-
 src/mlpack/methods/fastmks/fastmks_impl.hpp | 44 +++++++++++++++++++++++++++--
 src/mlpack/tests/fastmks_test.cpp           | 11 ++++++++
 3 files changed, 65 insertions(+), 3 deletions(-)

diff --git a/src/mlpack/methods/fastmks/fastmks.hpp b/src/mlpack/methods/fastmks/fastmks.hpp
index 6ba3bd3..7e1da5c 100644
--- a/src/mlpack/methods/fastmks/fastmks.hpp
+++ b/src/mlpack/methods/fastmks/fastmks.hpp
@@ -61,12 +61,21 @@ class FastMKS
   typedef TreeType<metric::IPMetric<KernelType>, FastMKSStat, MatType> Tree;
 
   /**
+   * Create the FastMKS object with an empty reference set and default kernel.
+   * Make sure to call Train() before Search() is called!
+   *
+   * @param singleMode Whether or not to run single-tree search.
+   * @param naive Whether or not to run brute-force (naive) search.
+   */
+  FastMKS(const bool singleMode = false, const bool naive = false);
+
+  /**
    * Create the FastMKS object with the given reference set (this is the set
    * that is searched).  Optionally, specify whether or not single-tree search
    * or naive (brute-force) search should be used.
    *
    * @param referenceSet Set of reference data.
-   * @param single Whether or not to run single-tree search.
+   * @param singleMode Whether or not to run single-tree search.
    * @param naive Whether or not to run brute-force (naive) search.
    */
   FastMKS(const MatType& referenceSet,
@@ -194,6 +203,8 @@ class FastMKS
   Tree* referenceTree;
   //! If true, this object created the tree and is responsible for it.
   bool treeOwner;
+  //! If true, we own the dataset.  This happens in only a few situations.
+  bool setOwner;
 
   //! If true, single-tree search is used.
   bool singleMode;
diff --git a/src/mlpack/methods/fastmks/fastmks_impl.hpp b/src/mlpack/methods/fastmks/fastmks_impl.hpp
index 4b9879f..1d7630f 100644
--- a/src/mlpack/methods/fastmks/fastmks_impl.hpp
+++ b/src/mlpack/methods/fastmks/fastmks_impl.hpp
@@ -18,6 +18,27 @@
 namespace mlpack {
 namespace fastmks {
 
+// No data; create a model on an empty dataset.
+template<typename KernelType,
+         typename MatType,
+         template<typename TreeMetricType,
+                  typename TreeStatType,
+                  typename TreeMatType> class TreeType>
+FastMKS<KernelType, MatType, TreeType>::FastMKS(const bool singleMode,
+                                                const bool naive) :
+    referenceSet(new MatType()),
+    referenceTree(NULL),
+    treeOwner(true),
+    setOwner(true),
+    singleMode(singleMode),
+    naive(naive)
+{
+  Timer::Start("tree_building");
+  if (!naive)
+    referenceTree = new Tree(*referenceSet);
+  Timer::Stop("tree_building");
+}
+
 // No instantiated kernel.
 template<typename KernelType,
          typename MatType,
@@ -31,14 +52,13 @@ FastMKS<KernelType, MatType, TreeType>::FastMKS(
     referenceSet(&referenceSet),
     referenceTree(NULL),
     treeOwner(true),
+    setOwner(false),
     singleMode(singleMode),
     naive(naive)
 {
   Timer::Start("tree_building");
-
   if (!naive)
     referenceTree = new Tree(referenceSet);
-
   Timer::Stop("tree_building");
 }
 
@@ -55,6 +75,7 @@ FastMKS<KernelType, MatType, TreeType>::FastMKS(const MatType& referenceSet,
     referenceSet(&referenceSet),
     referenceTree(NULL),
     treeOwner(true),
+    setOwner(false),
     singleMode(singleMode),
     naive(naive),
     metric(kernel)
@@ -79,6 +100,7 @@ FastMKS<KernelType, MatType, TreeType>::FastMKS(Tree* referenceTree,
     referenceSet(&referenceTree->Dataset()),
     referenceTree(referenceTree),
     treeOwner(false),
+    setOwner(false),
     singleMode(singleMode),
     naive(false),
     metric(referenceTree->Metric())
@@ -96,6 +118,8 @@ FastMKS<KernelType, MatType, TreeType>::~FastMKS()
   // If we created the trees, we must delete them.
   if (treeOwner && referenceTree)
     delete referenceTree;
+  if (setOwner)
+    delete referenceSet;
 }
 
 template<typename KernelType,
@@ -109,6 +133,14 @@ void FastMKS<KernelType, MatType, TreeType>::Search(
     arma::Mat<size_t>& indices,
     arma::mat& kernels)
 {
+  if (k > referenceSet->n_cols)
+  {
+    std::stringstream ss;
+    ss << "requested value of k (" << k << ") is greater than the number of "
+        << "points in the reference set (" << referenceSet->n_cols << ")";
+    throw std::invalid_argument(ss.str());
+  }
+
   Timer::Start("computing_products");
 
   // No remapping will be necessary because we are using the cover tree.
@@ -189,6 +221,14 @@ void FastMKS<KernelType, MatType, TreeType>::Search(
     arma::Mat<size_t>& indices,
     arma::mat& kernels)
 {
+  if (k > referenceSet->n_cols)
+  {
+    std::stringstream ss;
+    ss << "requested value of k (" << k << ") is greater than the number of "
+        << "points in the reference set (" << referenceSet->n_cols << ")";
+    throw std::invalid_argument(ss.str());
+  }
+
   // If either naive mode or single mode is specified, this must fail.
   if (naive || singleMode)
   {
diff --git a/src/mlpack/tests/fastmks_test.cpp b/src/mlpack/tests/fastmks_test.cpp
index 3a55d84..388d2b1 100644
--- a/src/mlpack/tests/fastmks_test.cpp
+++ b/src/mlpack/tests/fastmks_test.cpp
@@ -199,7 +199,18 @@ BOOST_AUTO_TEST_CASE(SparsePolynomialFastMKSTest)
       BOOST_REQUIRE_EQUAL(sparseIndices(j, i), denseIndices(j, i));
     }
   }
+}
+
+// Make sure the empty constructor works.
+BOOST_AUTO_TEST_CASE(EmptyConstructorTest)
+{
+  FastMKS<LinearKernel> f;
 
+  arma::mat queryData = arma::randu<arma::mat>(5, 100);
+  arma::Mat<size_t> indices;
+  arma::mat products;
+  BOOST_REQUIRE_THROW(f.Search(queryData, 3, indices, products),
+      std::invalid_argument);
 }
 
 BOOST_AUTO_TEST_SUITE_END();



More information about the mlpack-git mailing list