fastmks.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_FASTMKS_FASTMKS_HPP
14 #define MLPACK_METHODS_FASTMKS_FASTMKS_HPP
15 
16 #include <mlpack/prereqs.hpp>
18 #include "fastmks_stat.hpp"
20 #include <queue>
21 
22 namespace mlpack {
23 namespace fastmks {
24 
56 template<
57  typename KernelType,
58  typename MatType = arma::mat,
59  template<typename TreeMetricType,
60  typename TreeStatType,
61  typename TreeMatType> class TreeType = tree::StandardCoverTree
62 >
63 class FastMKS
64 {
65  public:
67  typedef TreeType<metric::IPMetric<KernelType>, FastMKSStat, MatType> Tree;
68 
76  FastMKS(const bool singleMode = false, const bool naive = false);
77 
87  FastMKS(const MatType& referenceSet,
88  const bool singleMode = false,
89  const bool naive = false);
90 
102  FastMKS(const MatType& referenceSet,
103  KernelType& kernel,
104  const bool singleMode = false,
105  const bool naive = false);
106 
117  FastMKS(MatType&& referenceSet,
118  const bool singleMode = false,
119  const bool naive = false);
120 
133  FastMKS(MatType&& referenceSet,
134  KernelType& kernel,
135  const bool singleMode = false,
136  const bool naive = false);
137 
148  FastMKS(Tree* referenceTree,
149  const bool singleMode = false);
150 
154  FastMKS(const FastMKS& other);
155 
159  FastMKS(FastMKS&& other);
160 
164  FastMKS& operator=(const FastMKS& other);
165 
169  FastMKS& operator=(FastMKS&& other);
170 
172  ~FastMKS();
173 
180  void Train(const MatType& referenceSet);
181 
190  void Train(const MatType& referenceSet, KernelType& kernel);
191 
199  void Train(MatType&& referenceSet);
200 
209  void Train(MatType&& referenceSet, KernelType& kernel);
210 
218  void Train(Tree* referenceTree);
219 
240  void Search(const MatType& querySet,
241  const size_t k,
242  arma::Mat<size_t>& indices,
243  arma::mat& kernels);
244 
267  void Search(Tree* querySet,
268  const size_t k,
269  arma::Mat<size_t>& indices,
270  arma::mat& kernels);
271 
286  void Search(const size_t k,
287  arma::Mat<size_t>& indices,
288  arma::mat& products);
289 
291  const metric::IPMetric<KernelType>& Metric() const { return metric; }
293  metric::IPMetric<KernelType>& Metric() { return metric; }
294 
296  bool SingleMode() const { return singleMode; }
298  bool& SingleMode() { return singleMode; }
299 
301  bool Naive() const { return naive; }
303  bool& Naive() { return naive; }
304 
306  template<typename Archive>
307  void serialize(Archive& ar, const uint32_t /* version */);
308 
309  private:
312  const MatType* referenceSet;
314  Tree* referenceTree;
316  bool treeOwner;
318  bool setOwner;
319 
321  bool singleMode;
323  bool naive;
324 
327 
329  typedef std::pair<double, size_t> Candidate;
330 
332  struct CandidateCmp {
333  bool operator()(const Candidate& c1, const Candidate& c2)
334  {
335  return c1.first > c2.first;
336  };
337  };
338 
340  typedef std::priority_queue<Candidate, std::vector<Candidate>,
341  CandidateCmp> CandidateList;
342 };
343 
344 } // namespace fastmks
345 } // namespace mlpack
346 
347 // Include implementation.
348 #include "fastmks_impl.hpp"
349 
350 #endif
bool SingleMode() const
Get whether or not single-tree search is used.
Definition: fastmks.hpp:296
const metric::IPMetric< KernelType > & Metric() const
Get the inner-product metric induced by the given kernel.
Definition: fastmks.hpp:291
Linear algebra utility functions, generally performed on matrices or vectors.
The core includes that mlpack expects; standard C++ includes and Armadillo.
bool & Naive()
Modify whether or not brute-force (naive) search is used.
Definition: fastmks.hpp:303
The inner product metric, IPMetric, takes a given Mercer kernel (KernelType), and when Evaluate() is ...
Definition: ip_metric.hpp:32
FastMKS(const bool singleMode=false, const bool naive=false)
Create the FastMKS object with an empty reference set and default kernel.
~FastMKS()
Destructor for the FastMKS object.
metric::IPMetric< KernelType > & Metric()
Modify the inner-product metric induced by the given kernel.
Definition: fastmks.hpp:293
bool & SingleMode()
Modify whether or not single-tree search is used.
Definition: fastmks.hpp:298
bool Naive() const
Get whether or not brute-force (naive) search is used.
Definition: fastmks.hpp:301
TreeType< metric::IPMetric< KernelType >, FastMKSStat, MatType > Tree
Convenience typedef.
Definition: fastmks.hpp:67
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 given ...
The statistic used in trees with FastMKS.
void Train(const MatType &referenceSet)
"Train" the FastMKS model on the given reference set (this will just build a tree, if the current search mode is not naive mode).
FastMKS & operator=(const FastMKS &other)
Assign this model to be a copy of the given model.
An implementation of fast exact max-kernel search.
Definition: fastmks.hpp:63
A cover tree is a tree specifically designed to speed up nearest-neighbor computation in high-dimensi...
Definition: cover_tree.hpp:99
void serialize(Archive &ar, const uint32_t)
Serialize the model.