/usr/include/mlpack/methods/fastmks/fastmks_impl.hpp is in libmlpack-dev 1.0.10-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 | /**
* @file fastmks_impl.hpp
* @author Ryan Curtin
*
* Implementation of the FastMKS class (fast max-kernel search).
*
* This file is part of MLPACK 1.0.10.
*
* MLPACK is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* MLPACK is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details (LICENSE.txt).
*
* You should have received a copy of the GNU General Public License along with
* MLPACK. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __MLPACK_METHODS_FASTMKS_FASTMKS_IMPL_HPP
#define __MLPACK_METHODS_FASTMKS_FASTMKS_IMPL_HPP
// In case it hasn't yet been included.
#include "fastmks.hpp"
#include "fastmks_rules.hpp"
#include <mlpack/core/kernels/gaussian_kernel.hpp>
#include <queue>
namespace mlpack {
namespace fastmks {
// Single dataset, no instantiated kernel.
template<typename KernelType, typename TreeType>
FastMKS<KernelType, TreeType>::FastMKS(const arma::mat& referenceSet,
const bool single,
const bool naive) :
referenceSet(referenceSet),
querySet(referenceSet),
referenceTree(NULL),
queryTree(NULL),
treeOwner(true),
single(single),
naive(naive)
{
Timer::Start("tree_building");
if (!naive)
referenceTree = new TreeType(referenceSet);
if (!naive && !single)
queryTree = new TreeType(referenceSet);
Timer::Stop("tree_building");
}
// Two datasets, no instantiated kernel.
template<typename KernelType, typename TreeType>
FastMKS<KernelType, TreeType>::FastMKS(const arma::mat& referenceSet,
const arma::mat& querySet,
const bool single,
const bool naive) :
referenceSet(referenceSet),
querySet(querySet),
referenceTree(NULL),
queryTree(NULL),
treeOwner(true),
single(single),
naive(naive)
{
Timer::Start("tree_building");
// If necessary, the trees should be built.
if (!naive)
referenceTree = new TreeType(referenceSet);
if (!naive && !single)
queryTree = new TreeType(querySet);
Timer::Stop("tree_building");
}
// One dataset, instantiated kernel.
template<typename KernelType, typename TreeType>
FastMKS<KernelType, TreeType>::FastMKS(const arma::mat& referenceSet,
KernelType& kernel,
const bool single,
const bool naive) :
referenceSet(referenceSet),
querySet(referenceSet),
referenceTree(NULL),
queryTree(NULL),
treeOwner(true),
single(single),
naive(naive),
metric(kernel)
{
Timer::Start("tree_building");
// If necessary, the reference tree should be built. There is no query tree.
if (!naive)
referenceTree = new TreeType(referenceSet, metric);
if (!naive && !single)
queryTree = new TreeType(referenceSet, metric);
Timer::Stop("tree_building");
}
// Two datasets, instantiated kernel.
template<typename KernelType, typename TreeType>
FastMKS<KernelType, TreeType>::FastMKS(const arma::mat& referenceSet,
const arma::mat& querySet,
KernelType& kernel,
const bool single,
const bool naive) :
referenceSet(referenceSet),
querySet(querySet),
referenceTree(NULL),
queryTree(NULL),
treeOwner(true),
single(single),
naive(naive),
metric(kernel)
{
Timer::Start("tree_building");
// If necessary, the trees should be built.
if (!naive)
referenceTree = new TreeType(referenceSet, metric);
if (!naive && !single)
queryTree = new TreeType(querySet, metric);
Timer::Stop("tree_building");
}
// One dataset, pre-built tree.
template<typename KernelType, typename TreeType>
FastMKS<KernelType, TreeType>::FastMKS(const arma::mat& referenceSet,
TreeType* referenceTree,
const bool single,
const bool naive) :
referenceSet(referenceSet),
querySet(referenceSet),
referenceTree(referenceTree),
queryTree(NULL),
treeOwner(false),
single(single),
naive(naive),
metric(referenceTree->Metric())
{
// The query tree cannot be the same as the reference tree.
if (referenceTree)
queryTree = new TreeType(*referenceTree);
}
// Two datasets, pre-built trees.
template<typename KernelType, typename TreeType>
FastMKS<KernelType, TreeType>::FastMKS(const arma::mat& referenceSet,
TreeType* referenceTree,
const arma::mat& querySet,
TreeType* queryTree,
const bool single,
const bool naive) :
referenceSet(referenceSet),
querySet(querySet),
referenceTree(referenceTree),
queryTree(queryTree),
treeOwner(false),
single(single),
naive(naive),
metric(referenceTree->Metric())
{
// Nothing to do.
}
template<typename KernelType, typename TreeType>
FastMKS<KernelType, TreeType>::~FastMKS()
{
// If we created the trees, we must delete them.
if (treeOwner)
{
if (queryTree)
delete queryTree;
if (referenceTree)
delete referenceTree;
}
else if (&querySet == &referenceSet)
{
// The user passed in a reference tree which we needed to copy.
if (queryTree)
delete queryTree;
}
}
template<typename KernelType, typename TreeType>
void FastMKS<KernelType, TreeType>::Search(const size_t k,
arma::Mat<size_t>& indices,
arma::mat& products)
{
// No remapping will be necessary because we are using the cover tree.
indices.set_size(k, querySet.n_cols);
products.set_size(k, querySet.n_cols);
products.fill(-DBL_MAX);
Timer::Start("computing_products");
// Naive implementation.
if (naive)
{
// Simple double loop. Stupid, slow, but a good benchmark.
for (size_t q = 0; q < querySet.n_cols; ++q)
{
for (size_t r = 0; r < referenceSet.n_cols; ++r)
{
if ((&querySet == &referenceSet) && (q == r))
continue;
const double eval = metric.Kernel().Evaluate(querySet.unsafe_col(q),
referenceSet.unsafe_col(r));
size_t insertPosition;
for (insertPosition = 0; insertPosition < indices.n_rows;
++insertPosition)
if (eval > products(insertPosition, q))
break;
if (insertPosition < indices.n_rows)
InsertNeighbor(indices, products, q, insertPosition, r, eval);
}
}
Timer::Stop("computing_products");
return;
}
// Single-tree implementation.
if (single)
{
// Create rules object (this will store the results). This constructor
// precalculates each self-kernel value.
typedef FastMKSRules<KernelType, TreeType> RuleType;
RuleType rules(referenceSet, querySet, indices, products, metric.Kernel());
typename TreeType::template SingleTreeTraverser<RuleType> traverser(rules);
for (size_t i = 0; i < querySet.n_cols; ++i)
traverser.Traverse(i, *referenceTree);
// Save the number of pruned nodes.
const size_t numPrunes = traverser.NumPrunes();
Log::Info << "Pruned " << numPrunes << " nodes." << std::endl;
Log::Info << rules.BaseCases() << " base cases." << std::endl;
Log::Info << rules.Scores() << " scores." << std::endl;
Timer::Stop("computing_products");
return;
}
// Dual-tree implementation.
typedef FastMKSRules<KernelType, TreeType> RuleType;
RuleType rules(referenceSet, querySet, indices, products, metric.Kernel());
typename TreeType::template DualTreeTraverser<RuleType> traverser(rules);
traverser.Traverse(*queryTree, *referenceTree);
const size_t numPrunes = traverser.NumPrunes();
Log::Info << "Pruned " << numPrunes << " nodes." << std::endl;
Log::Info << rules.BaseCases() << " base cases." << std::endl;
Log::Info << rules.Scores() << " scores." << std::endl;
Timer::Stop("computing_products");
return;
}
/**
* Helper function to insert a point into the neighbors and distances matrices.
*
* @param queryIndex Index of point whose neighbors we are inserting into.
* @param pos Position in list to insert into.
* @param neighbor Index of reference point which is being inserted.
* @param distance Distance from query point to reference point.
*/
template<typename KernelType, typename TreeType>
void FastMKS<KernelType, TreeType>::InsertNeighbor(arma::Mat<size_t>& indices,
arma::mat& products,
const size_t queryIndex,
const size_t pos,
const size_t neighbor,
const double distance)
{
// We only memmove() if there is actually a need to shift something.
if (pos < (products.n_rows - 1))
{
int len = (products.n_rows - 1) - pos;
memmove(products.colptr(queryIndex) + (pos + 1),
products.colptr(queryIndex) + pos,
sizeof(double) * len);
memmove(indices.colptr(queryIndex) + (pos + 1),
indices.colptr(queryIndex) + pos,
sizeof(size_t) * len);
}
// Now put the new information in the right index.
products(pos, queryIndex) = distance;
indices(pos, queryIndex) = neighbor;
}
// Return string of object.
template<typename KernelType, typename TreeType>
std::string FastMKS<KernelType, TreeType>::ToString() const
{
std::ostringstream convert;
convert << "FastMKS [" << this << "]" << std::endl;
convert << " Naive: " << naive << std::endl;
convert << " Single: " << single << std::endl;
convert << " Metric: " << std::endl;
convert << mlpack::util::Indent(metric.ToString(),2);
convert << std::endl;
return convert.str();
}
// Specialized implementation for tighter bounds for Gaussian.
/*
template<>
void FastMKS<kernel::GaussianKernel>::Search(const size_t k,
arma::Mat<size_t>& indices,
arma::mat& products)
{
Log::Warn << "Alternate implementation!" << std::endl;
// Terrible copypasta. Bad bad bad.
// No remapping will be necessary.
indices.set_size(k, querySet.n_cols);
products.set_size(k, querySet.n_cols);
products.fill(-1.0);
Timer::Start("computing_products");
size_t kernelEvaluations = 0;
// Naive implementation.
if (naive)
{
// Simple double loop. Stupid, slow, but a good benchmark.
for (size_t q = 0; q < querySet.n_cols; ++q)
{
for (size_t r = 0; r < referenceSet.n_cols; ++r)
{
const double eval = metric.Kernel().Evaluate(querySet.unsafe_col(q),
referenceSet.unsafe_col(r));
++kernelEvaluations;
size_t insertPosition;
for (insertPosition = 0; insertPosition < indices.n_rows;
++insertPosition)
if (eval > products(insertPosition, q))
break;
if (insertPosition < indices.n_rows)
InsertNeighbor(indices, products, q, insertPosition, r, eval);
}
}
Timer::Stop("computing_products");
Log::Info << "Kernel evaluations: " << kernelEvaluations << "." << std::endl;
return;
}
// Single-tree implementation.
if (single)
{
// Calculate number of pruned nodes.
size_t numPrunes = 0;
// Precalculate query products ( || q || for all q).
arma::vec queryProducts(querySet.n_cols);
for (size_t queryIndex = 0; queryIndex < querySet.n_cols; ++queryIndex)
queryProducts[queryIndex] = sqrt(metric.Kernel().Evaluate(
querySet.unsafe_col(queryIndex), querySet.unsafe_col(queryIndex)));
kernelEvaluations += querySet.n_cols;
// Screw the CoverTreeTraverser, we'll implement it by hand.
for (size_t queryIndex = 0; queryIndex < querySet.n_cols; ++queryIndex)
{
// Use an array of priority queues?
std::priority_queue<
SearchFrame<tree::CoverTree<IPMetric<kernel::GaussianKernel> > >,
std::vector<SearchFrame<tree::CoverTree<IPMetric<
kernel::GaussianKernel> > > >,
SearchFrameCompare<tree::CoverTree<IPMetric<
kernel::GaussianKernel> > > >
frameQueue;
// Add initial frame.
SearchFrame<tree::CoverTree<IPMetric<kernel::GaussianKernel> > >
nextFrame;
nextFrame.node = referenceTree;
nextFrame.eval = metric.Kernel().Evaluate(querySet.unsafe_col(queryIndex),
referenceSet.unsafe_col(referenceTree->Point()));
Log::Assert(nextFrame.eval <= 1);
++kernelEvaluations;
// The initial evaluation will be the best so far.
indices(0, queryIndex) = referenceTree->Point();
products(0, queryIndex) = nextFrame.eval;
frameQueue.push(nextFrame);
tree::CoverTree<IPMetric<kernel::GaussianKernel> >* referenceNode;
double eval;
double maxProduct;
while (!frameQueue.empty())
{
// Get the information for this node.
const SearchFrame<tree::CoverTree<IPMetric<kernel::GaussianKernel> > >&
frame = frameQueue.top();
referenceNode = frame.node;
eval = frame.eval;
// Loop through the children, seeing if we can prune them; if not, add
// them to the queue. The self-child is different -- it has the same
// parent (and therefore the same kernel evaluation).
if (referenceNode->NumChildren() > 0)
{
SearchFrame<tree::CoverTree<IPMetric<kernel::GaussianKernel> > >
childFrame;
// We must handle the self-child differently, to avoid adding it to
// the results twice.
childFrame.node = &(referenceNode->Child(0));
childFrame.eval = eval;
// Alternate pruning rule.
const double mdd = childFrame.node->FurthestDescendantDistance();
if (eval >= (1 - std::pow(mdd, 2.0) / 2.0))
maxProduct = 1;
else
maxProduct = eval * (1 - std::pow(mdd, 2.0) / 2.0) + sqrt(1 -
std::pow(eval, 2.0)) * mdd * sqrt(1 - std::pow(mdd, 2.0) / 4.0);
// Add self-child if we can't prune it.
if (maxProduct > products(products.n_rows - 1, queryIndex))
{
// But only if it has children of its own.
if (childFrame.node->NumChildren() > 0)
frameQueue.push(childFrame);
}
else
++numPrunes;
for (size_t i = 1; i < referenceNode->NumChildren(); ++i)
{
// Before we evaluate the child, let's see if it can possibly have
// a better evaluation.
const double mpdd = std::min(
referenceNode->Child(i).ParentDistance() +
referenceNode->Child(i).FurthestDescendantDistance(), 2.0);
double maxChildEval = 1;
if (eval < (1 - std::pow(mpdd, 2.0) / 2.0))
maxChildEval = eval * (1 - std::pow(mpdd, 2.0) / 2.0) + sqrt(1 -
std::pow(eval, 2.0)) * mpdd * sqrt(1 - std::pow(mpdd, 2.0)
/ 4.0);
if (maxChildEval > products(products.n_rows - 1, queryIndex))
{
// Evaluate child.
childFrame.node = &(referenceNode->Child(i));
childFrame.eval = metric.Kernel().Evaluate(
querySet.unsafe_col(queryIndex),
referenceSet.unsafe_col(referenceNode->Child(i).Point()));
++kernelEvaluations;
// Can we prune it? If we can, we can avoid putting it in the
// queue (saves time).
const double cmdd = childFrame.node->FurthestDescendantDistance();
if (childFrame.eval > (1 - std::pow(cmdd, 2.0) / 2.0))
maxProduct = 1;
else
maxProduct = childFrame.eval * (1 - std::pow(cmdd, 2.0) / 2.0)
+ sqrt(1 - std::pow(eval, 2.0)) * cmdd * sqrt(1 -
std::pow(cmdd, 2.0) / 4.0);
if (maxProduct > products(products.n_rows - 1, queryIndex))
{
// Good enough to recurse into. While we're at it, check the
// actual evaluation and see if it's an improvement.
if (childFrame.eval > products(products.n_rows - 1, queryIndex))
{
// This is a better result. Find out where to insert.
size_t insertPosition = 0;
for ( ; insertPosition < products.n_rows - 1;
++insertPosition)
if (childFrame.eval > products(insertPosition, queryIndex))
break;
// Insert into the correct position; we are guaranteed that
// insertPosition is valid.
InsertNeighbor(indices, products, queryIndex, insertPosition,
childFrame.node->Point(), childFrame.eval);
}
// Now add this to the queue (if it has any children which may
// need to be recursed into).
if (childFrame.node->NumChildren() > 0)
frameQueue.push(childFrame);
}
else
++numPrunes;
}
else
++numPrunes;
}
}
frameQueue.pop();
}
}
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;
}
// Double-tree implementation.
Log::Fatal << "Dual-tree search not implemented yet... oops..." << std::endl;
}
*/
}; // namespace fastmks
}; // namespace mlpack
#endif
|