[mlpack-svn] r12739 - mlpack/trunk/src/mlpack/methods/maxip

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Mon May 21 11:54:30 EDT 2012


Author: rcurtin
Date: 2012-05-21 11:54:30 -0400 (Mon, 21 May 2012)
New Revision: 12739

Modified:
   mlpack/trunk/src/mlpack/methods/maxip/ip_metric_impl.hpp
   mlpack/trunk/src/mlpack/methods/maxip/max_ip_impl.hpp
   mlpack/trunk/src/mlpack/methods/maxip/max_ip_main.cpp
Log:
Check in modifications.


Modified: mlpack/trunk/src/mlpack/methods/maxip/ip_metric_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/maxip/ip_metric_impl.hpp	2012-05-20 22:05:24 UTC (rev 12738)
+++ mlpack/trunk/src/mlpack/methods/maxip/ip_metric_impl.hpp	2012-05-21 15:54:30 UTC (rev 12739)
@@ -18,19 +18,21 @@
 
 template<typename KernelType>
 template<typename Vec1Type, typename Vec2Type>
-double IPMetric<KernelType>::Evaluate(const Vec1Type& a, const Vec2Type& b)
+inline double IPMetric<KernelType>::Evaluate(const Vec1Type& a, const Vec2Type& b)
 {
   // This is the metric induced by the kernel function.
   // Maybe we can do better by caching some of this?
+  ++distanceEvaluations;
   return KernelType::Evaluate(a, a) + KernelType::Evaluate(b, b) -
       2 * KernelType::Evaluate(a, b);
 }
 
 template<>
 template<typename Vec1Type, typename Vec2Type>
-double IPMetric<kernel::LinearKernel>::Evaluate(const Vec1Type& a,
-                                                const Vec2Type& b)
+inline double IPMetric<kernel::LinearKernel>::Evaluate(const Vec1Type& a,
+                                                       const Vec2Type& b)
 {
+  ++distanceEvaluations;
   return metric::LMetric<2>::Evaluate(a, b);
 }
 

Modified: mlpack/trunk/src/mlpack/methods/maxip/max_ip_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/maxip/max_ip_impl.hpp	2012-05-20 22:05:24 UTC (rev 12738)
+++ mlpack/trunk/src/mlpack/methods/maxip/max_ip_impl.hpp	2012-05-21 15:54:30 UTC (rev 12739)
@@ -91,6 +91,8 @@
 
   Timer::Start("computing_products");
 
+  size_t kernelEvaluations = 0;
+
   // Naive implementation.
   if (naive)
   {
@@ -101,6 +103,7 @@
       {
         const double eval = KernelType::Evaluate(querySet.unsafe_col(q),
                                                  referenceSet.unsafe_col(r));
+        ++kernelEvaluations;
 
         size_t insertPosition;
         for (insertPosition = 0; insertPosition < indices.n_rows;
@@ -114,6 +117,8 @@
     }
 
     Timer::Stop("computing_products");
+    
+    Log::Info << "Kernel evaluations: " << kernelEvaluations << "." << std::endl;
     return;
   }
 
@@ -169,6 +174,7 @@
           // Evaluate the kernel.  Then see if it is a result to keep.
           eval = KernelType::Evaluate(querySet.unsafe_col(queryIndex),
               referenceSet.unsafe_col(referenceNode->Point()));
+          ++kernelEvaluations;
 
           // Is the result good enough to be saved?
           if (eval > products(products.n_rows - 1, queryIndex))
@@ -211,6 +217,8 @@
     }
 
     Log::Info << "Pruned " << numPrunes << " nodes." << std::endl;
+    Log::Info << "Kernel evaluations: " << kernelEvaluations << "." << std::endl;
+    Log::Info << "Distance evaluations: " << distanceEvaluations << "." << std::endl;
 
     Timer::Stop("computing_products");
     return;

Modified: mlpack/trunk/src/mlpack/methods/maxip/max_ip_main.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/maxip/max_ip_main.cpp	2012-05-20 22:05:24 UTC (rev 12738)
+++ mlpack/trunk/src/mlpack/methods/maxip/max_ip_main.cpp	2012-05-21 15:54:30 UTC (rev 12739)
@@ -4,9 +4,15 @@
  *
  * Main executable for maximum inner product search.
  */
+// I can't believe I'm doing this.
+#include <stddef.h>
+size_t distanceEvaluations;
+
 #include <mlpack/core.hpp>
 #include <mlpack/core/kernels/linear_kernel.hpp>
-#include <mlpack/core/kernels/polynomial_kernel.hpp>1
+#include <mlpack/core/kernels/polynomial_kernel.hpp>
+#include <mlpack/core/kernels/cosine_distance.hpp>
+#include <mlpack/core/kernels/gaussian_kernel.hpp>
 
 #include "max_ip.hpp"
 
@@ -35,8 +41,8 @@
 PARAM_STRING("indices_file", "File to save indices of inner products into.",
     "i", "");
 
-PARAM_STRING("kernel", "Kernel type to use: 'linear', 'polynomial'.", "K",
-    "linear");
+PARAM_STRING("kernel", "Kernel type to use: 'linear', 'polynomial', 'cosine'.",
+    "K", "linear");
 
 PARAM_FLAG("naive", "If true, O(n^2) naive mode is used for computation.", "N");
 PARAM_FLAG("single", "If true, single-tree search is used (as opposed to "
@@ -44,6 +50,8 @@
 
 int main(int argc, char** argv)
 {
+  distanceEvaluations = 0;
+
   CLI::ParseCommandLine(argc, argv);
 
   // Get reference dataset filename.
@@ -73,7 +81,10 @@
   }
 
   // Check on kernel type.
-  if ((kernelType != "linear") && (kernelType != "polynomial"))
+  if ((kernelType != "linear") && (kernelType != "polynomial") &&
+      (kernelType != "cosine") && (kernelType != "polynomial5") &&
+      (kernelType != "polynomial10") && (kernelType != "polynomial0.5") &&
+      (kernelType != "polynomial0.1") && (kernelType != "gaussian"))
   {
     Log::Fatal << "Invalid kernel type: '" << kernelType << "'; must be ";
     Log::Fatal << "'linear' or 'polynomial'." << endl;
@@ -118,6 +129,41 @@
           (single && !naive), naive);
       maxip.Search(k, indices, products);
     }
+    else if (kernelType == "cosine")
+    {
+      MaxIP<CosineDistance> maxip(referenceData, (single && !naive), naive);
+      maxip.Search(k, indices, products);
+    }
+    else if (kernelType == "polynomial5")
+    {
+      MaxIP<PolynomialNoOffsetKernel<5> > maxip(referenceData,
+          (single && !naive), naive);
+      maxip.Search(k, indices, products);
+    }
+    else if (kernelType == "polynomial10")
+    {
+      MaxIP<PolynomialNoOffsetKernel<10> > maxip(referenceData,
+          (single && !naive), naive);
+      maxip.Search(k, indices, products);
+    }
+    else if (kernelType == "polynomial0.5")
+    {
+      MaxIP<PolynomialFractionKernel<2> > maxip(referenceData,
+          (single && !naive), naive);
+      maxip.Search(k, indices, products);
+    }
+    else if (kernelType == "polynomial0.1")
+    {
+      MaxIP<PolynomialFractionKernel<10> > maxip(referenceData,
+          (single && !naive), naive);
+      maxip.Search(k, indices, products);
+    }
+    else if (kernelType == "gaussian")
+    {
+      MaxIP<GaussianStaticKernel> maxip(referenceData, (single && !naive),
+          naive);
+      maxip.Search(k, indices, products);
+    }
   }
   else
   {
@@ -133,6 +179,42 @@
           (single && !naive), naive);
       maxip.Search(k, indices, products);
     }
+    else if (kernelType == "cosine")
+    {
+      MaxIP<CosineDistance> maxip(referenceData, queryData, (single && !naive),
+        naive);
+      maxip.Search(k, indices, products);
+    }
+    else if (kernelType == "polynomial5")
+    {
+      MaxIP<PolynomialNoOffsetKernel<5> > maxip(referenceData, queryData,
+          (single && !naive), naive);
+      maxip.Search(k, indices, products);
+    }
+    else if (kernelType == "polynomial10")
+    {
+      MaxIP<PolynomialNoOffsetKernel<10> > maxip(referenceData, queryData,
+          (single && !naive), naive);
+      maxip.Search(k, indices, products);
+    }
+    else if (kernelType == "polynomial0.5")
+    {
+      MaxIP<PolynomialFractionKernel<2> > maxip(referenceData, queryData,
+          (single && !naive), naive);
+      maxip.Search(k, indices, products);
+    }
+    else if (kernelType == "polynomial0.1")
+    {
+      MaxIP<PolynomialFractionKernel<10> > maxip(referenceData, queryData,
+          (single && !naive), naive);
+      maxip.Search(k, indices, products);
+    }
+    else if (kernelType == "gaussian")
+    {
+      MaxIP<GaussianStaticKernel> maxip(referenceData, queryData,
+          (single && !naive), naive);
+      maxip.Search(k, indices, products);
+    }
   }
 
   // Save output, if we were asked to.




More information about the mlpack-svn mailing list