/usr/include/mlpack/methods/fastmks/fastmks.hpp is in libmlpack-dev 2.2.5-1build1.
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 | /**
* @file fastmks.hpp
* @author Ryan Curtin
*
* Definition of the FastMKS class, which implements fast exact max-kernel
* search.
*
* mlpack is free software; you may redistribute it and/or modify it under the
* terms of the 3-clause BSD license. You should have received a copy of the
* 3-clause BSD license along with mlpack. If not, see
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
*/
#ifndef MLPACK_METHODS_FASTMKS_FASTMKS_HPP
#define MLPACK_METHODS_FASTMKS_FASTMKS_HPP
#include <mlpack/prereqs.hpp>
#include <mlpack/core/metrics/ip_metric.hpp>
#include "fastmks_stat.hpp"
#include <mlpack/core/tree/cover_tree.hpp>
namespace mlpack {
namespace fastmks /** Fast max-kernel search. */ {
/**
* An implementation of fast exact max-kernel search. Given a query dataset and
* a reference dataset (or optionally just a reference dataset which is also
* used as the query dataset), fast exact max-kernel search finds, for each
* point in the query dataset, the k points in the reference set with maximum
* kernel value K(p_q, p_r), where k is a specified parameter and K() is a
* Mercer kernel.
*
* For more information, see the following paper.
*
* @code
* @inproceedings{curtin2013fast,
* title={Fast Exact Max-Kernel Search},
* author={Curtin, Ryan R. and Ram, Parikshit and Gray, Alexander G.},
* booktitle={Proceedings of the 2013 SIAM International Conference on Data
* Mining (SDM 13)},
* year={2013}
* }
* @endcode
*
* This class allows specification of the type of kernel and also of the type of
* tree. FastMKS can be run on kernels that work on arbitrary objects --
* however, this only works with cover trees and other trees that are built only
* on points in the dataset (and not centroids of regions or anything like
* that).
*
* @tparam KernelType Type of kernel to run FastMKS with.
* @tparam MatType Type of data matrix (usually arma::mat).
* @tparam TreeType Type of tree to run FastMKS with; it must satisfy the
* TreeType policy API.
*/
template<
typename KernelType,
typename MatType = arma::mat,
template<typename TreeMetricType,
typename TreeStatType,
typename TreeMatType> class TreeType = tree::StandardCoverTree
>
class FastMKS
{
public:
//! Convenience typedef.
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 singleMode Whether or not to run single-tree search.
* @param naive Whether or not to run brute-force (naive) search.
*/
FastMKS(const MatType& referenceSet,
const bool singleMode = false,
const bool naive = false);
/**
* Create the FastMKS object using the reference set (this is the set that is
* searched) with an initialized kernel. This is useful for when the kernel
* stores state. Optionally, specify whether or not single-tree search or
* naive (brute-force) search should be used.
*
* @param referenceSet Reference set of data for FastMKS.
* @param kernel Initialized kernel.
* @param single Whether or not to run single-tree search.
* @param naive Whether or not to run brute-force (naive) search.
*/
FastMKS(const MatType& referenceSet,
KernelType& kernel,
const bool singleMode = false,
const bool naive = false);
/**
* Create the FastMKS object with an already-initialized tree built on the
* reference points. Be sure that the tree is built with the metric type
* IPMetric<KernelType>. Optionally, whether or not to run single-tree search
* can be specified. Brute-force search is not available with this
* constructor since a tree is given (use one of the other constructors).
*
* @param referenceTree Tree built on reference data.
* @param single Whether or not to run single-tree search.
* @param naive Whether or not to run brute-force (naive) search.
*/
FastMKS(Tree* referenceTree,
const bool singleMode = false);
//! Destructor for the FastMKS object.
~FastMKS();
/**
* "Train" the FastMKS model on the given reference set (this will just build
* a tree, if the current search mode is not naive mode).
*
* @param referenceSet Set of reference points.
*/
void Train(const MatType& referenceSet);
/**
* "Train" the FastMKS model on the given reference set and use the given
* kernel. This will just build a tree and replace the metric, if the current
* search mode is not naive mode.
*
* @param referenceSet Set of reference points.
* @param kernel Kernel to use for search.
*/
void Train(const MatType& referenceSet, KernelType& kernel);
/**
* Train the FastMKS model on the given reference tree. This takes ownership
* of the tree, so you do not need to delete it! This will throw an exception
* if the model is searching in naive mode (i.e. if Naive() == true).
*
* @param tree Tree to use as reference data.
*/
void Train(Tree* referenceTree);
/**
* Search for the points in the reference set with maximum kernel evaluation
* to each point in the given query set. The resulting kernel evaluations are
* stored in the kernels matrix, and the corresponding point indices are
* stored in the indices matrix. The results for each point in the query set
* are stored in the corresponding column of the kernels and products
* matrices; for instance, the index of the point with maximum kernel
* evaluation to point 4 in the query set will be stored in row 0 and column 4
* of the indices matrix.
*
* If querySet only contains a few points, the extra overhead of building a
* tree to perform dual-tree search may not be warranted, and it may be faster
* to use single-tree search, either by setting singleMode to false in the
* constructor or with SingleMode().
*
* @param querySet Set of query points (can be a single point).
* @param k The number of maximum kernels to find.
* @param indices Matrix to store resulting indices of max-kernel search in.
* @param kernels Matrix to store resulting max-kernel values in.
*/
void Search(const MatType& querySet,
const size_t k,
arma::Mat<size_t>& indices,
arma::mat& kernels);
/**
* Search for the points in the reference set with maximum kernel evaluation
* to each point in the query set corresponding to the given pre-built query
* tree. The resulting kernel evaluations are stored in the kernels matrix,
* and the corresponding point indices are stored in the indices matrix. The
* results for each point in the query set are stored in the corresponding
* column of the kernels and products matrices; for instance, the index of the
* point with maximum kernel evaluation to point 4 in the query set will be
* stored in row 0 and column 4 of the indices matrix.
*
* This will throw an exception if called while the FastMKS object has
* 'single' set to true.
*
* Be aware that if your tree modifies the original input matrix, the results
* here are with respect to the modified input matrix (that is,
* queryTree->Dataset()).
*
* @param queryTree Tree built on query points.
* @param k The number of maximum kernels to find.
* @param indices Matrix to store resulting indices of max-kernel search in.
* @param kernels Matrix to store resulting max-kernel values in.
*/
void Search(Tree* querySet,
const size_t k,
arma::Mat<size_t>& indices,
arma::mat& kernels);
/**
* Search for the maximum inner products of the query set (or if no query set
* was passed, the reference set is used). The resulting maximum inner
* products are stored in the products matrix and the corresponding point
* indices are stores in the indices matrix. The results for each point in
* the query set are stored in the corresponding column of the indices and
* products matrices; for instance, the index of the point with maximum inner
* product to point 4 in the query set will be stored in row 0 and column 4 of
* the indices matrix.
*
* @param k The number of maximum kernels to find.
* @param indices Matrix to store resulting indices of max-kernel search in.
* @param products Matrix to store resulting max-kernel values in.
*/
void Search(const size_t k,
arma::Mat<size_t>& indices,
arma::mat& products);
//! Get the inner-product metric induced by the given kernel.
const metric::IPMetric<KernelType>& Metric() const { return metric; }
//! Modify the inner-product metric induced by the given kernel.
metric::IPMetric<KernelType>& Metric() { return metric; }
//! Get whether or not single-tree search is used.
bool SingleMode() const { return singleMode; }
//! Modify whether or not single-tree search is used.
bool& SingleMode() { return singleMode; }
//! Get whether or not brute-force (naive) search is used.
bool Naive() const { return naive; }
//! Modify whether or not brute-force (naive) search is used.
bool& Naive() { return naive; }
//! Serialize the model.
template<typename Archive>
void Serialize(Archive& ar, const unsigned int /* version */);
private:
//! The reference dataset. We never own this; only the tree or a higher level
//! does.
const MatType* referenceSet;
//! The tree built on the reference dataset.
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;
//! If true, naive (brute-force) search is used.
bool naive;
//! The instantiated inner-product metric induced by the given kernel.
metric::IPMetric<KernelType> metric;
//! Candidate represents a possible candidate point (value, index).
typedef std::pair<double, size_t> Candidate;
//! Compare two candidates based on the value.
struct CandidateCmp {
bool operator()(const Candidate& c1, const Candidate& c2)
{
return c1.first > c2.first;
};
};
//! Use a priority queue to represent the list of candidate points.
typedef std::priority_queue<Candidate, std::vector<Candidate>,
CandidateCmp> CandidateList;
};
} // namespace fastmks
} // namespace mlpack
// Include implementation.
#include "fastmks_impl.hpp"
#endif
|