Check in modifications.

This commit is contained in:
Ryan Curtin
2012-05-21 15:54:30 +00:00
parent a9581ba870
commit 08e203341f
3 changed files with 99 additions and 7 deletions
+5 -3
View File
@@ -18,19 +18,21 @@ namespace maxip {
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);
}
+8
View File
@@ -91,6 +91,8 @@ void MaxIP<KernelType>::Search(const size_t k,
Timer::Start("computing_products");
size_t kernelEvaluations = 0;
// Naive implementation.
if (naive)
{
@@ -101,6 +103,7 @@ void MaxIP<KernelType>::Search(const size_t k,
{
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 @@ void MaxIP<KernelType>::Search(const size_t k,
}
Timer::Stop("computing_products");
Log::Info << "Kernel evaluations: " << kernelEvaluations << "." << std::endl;
return;
}
@@ -169,6 +174,7 @@ void MaxIP<KernelType>::Search(const size_t k,
// 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 @@ void MaxIP<KernelType>::Search(const size_t k,
}
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;
+86 -4
View File
@@ -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("products_file", "File to save inner products into.", "p", "");
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 @@ PARAM_FLAG("single", "If true, single-tree search is used (as opposed to "
int main(int argc, char** argv)
{
distanceEvaluations = 0;
CLI::ParseCommandLine(argc, argv);
// Get reference dataset filename.
@@ -73,7 +81,10 @@ int main(int argc, char** argv)
}
// 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 @@ int main(int argc, char** argv)
(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 @@ int main(int argc, char** argv)
(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.