mlpack  2.0.1
fastmks.hpp
Go to the documentation of this file.
1 
15 #ifndef __MLPACK_METHODS_FASTMKS_FASTMKS_HPP
16 #define __MLPACK_METHODS_FASTMKS_FASTMKS_HPP
17 
18 #include <mlpack/core.hpp>
20 #include "fastmks_stat.hpp"
22 
23 namespace mlpack {
24 namespace fastmks {
25 
57 template<
58  typename KernelType,
59  typename MatType = arma::mat,
60  template<typename TreeMetricType,
61  typename TreeStatType,
62  typename TreeMatType> class TreeType = tree::StandardCoverTree
63 >
64 class FastMKS
65 {
66  public:
68  typedef TreeType<metric::IPMetric<KernelType>, FastMKSStat, MatType> Tree;
69 
77  FastMKS(const bool singleMode = false, const bool naive = false);
78 
88  FastMKS(const MatType& referenceSet,
89  const bool singleMode = false,
90  const bool naive = false);
91 
103  FastMKS(const MatType& referenceSet,
104  KernelType& kernel,
105  const bool singleMode = false,
106  const bool naive = false);
107 
119  FastMKS(Tree* referenceTree,
120  const bool singleMode = false);
121 
123  ~FastMKS();
124 
131  void Train(const MatType& referenceSet);
132 
141  void Train(const MatType& referenceSet, KernelType& kernel);
142 
150  void Train(Tree* referenceTree);
151 
172  void Search(const MatType& querySet,
173  const size_t k,
174  arma::Mat<size_t>& indices,
175  arma::mat& kernels);
176 
199  void Search(Tree* querySet,
200  const size_t k,
201  arma::Mat<size_t>& indices,
202  arma::mat& kernels);
203 
218  void Search(const size_t k,
219  arma::Mat<size_t>& indices,
220  arma::mat& products);
221 
223  const metric::IPMetric<KernelType>& Metric() const { return metric; }
226 
228  bool SingleMode() const { return singleMode; }
230  bool& SingleMode() { return singleMode; }
231 
233  bool Naive() const { return naive; }
235  bool& Naive() { return naive; }
236 
238  template<typename Archive>
239  void Serialize(Archive& ar, const unsigned int /* version */);
240 
241  private:
244  const MatType* referenceSet;
248  bool treeOwner;
250  bool setOwner;
251 
255  bool naive;
256 
259 
261  void InsertNeighbor(arma::Mat<size_t>& indices,
262  arma::mat& products,
263  const size_t queryIndex,
264  const size_t pos,
265  const size_t neighbor,
266  const double distance);
267 };
268 
269 } // namespace fastmks
270 } // namespace mlpack
271 
272 // Include implementation.
273 #include "fastmks_impl.hpp"
274 
275 #endif
bool treeOwner
If true, this object created the tree and is responsible for it.
Definition: fastmks.hpp:248
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).
bool & Naive()
Modify whether or not brute-force (naive) search is used.
Definition: fastmks.hpp:235
Linear algebra utility functions, generally performed on matrices or vectors.
bool setOwner
If true, we own the dataset. This happens in only a few situations.
Definition: fastmks.hpp:250
bool singleMode
If true, single-tree search is used.
Definition: fastmks.hpp:253
bool & SingleMode()
Modify whether or not single-tree search is used.
Definition: fastmks.hpp:230
bool Naive() const
Get whether or not brute-force (naive) search is used.
Definition: fastmks.hpp:233
The inner product metric, IPMetric, takes a given Mercer kernel (KernelType), and when Evaluate() is ...
Definition: ip_metric.hpp:34
Tree * referenceTree
The tree built on the reference dataset.
Definition: fastmks.hpp:246
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.
const metric::IPMetric< KernelType > & Metric() const
Get the inner-product metric induced by the given kernel.
Definition: fastmks.hpp:223
metric::IPMetric< KernelType > & Metric()
Modify the inner-product metric induced by the given kernel.
Definition: fastmks.hpp:225
Include all of the base components required to write MLPACK methods, and the main MLPACK Doxygen docu...
bool SingleMode() const
Get whether or not single-tree search is used.
Definition: fastmks.hpp:228
void InsertNeighbor(arma::Mat< size_t > &indices, arma::mat &products, const size_t queryIndex, const size_t pos, const size_t neighbor, const double distance)
Utility function. Copied too many times from too many places.
The statistic used in trees with FastMKS.
TreeType< metric::IPMetric< KernelType >, FastMKSStat, MatType > Tree
Convenience typedef.
Definition: fastmks.hpp:68
metric::IPMetric< KernelType > metric
The instantiated inner-product metric induced by the given kernel.
Definition: fastmks.hpp:258
const MatType * referenceSet
The reference dataset.
Definition: fastmks.hpp:244
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 ...
bool naive
If true, naive (brute-force) search is used.
Definition: fastmks.hpp:255
An implementation of fast exact max-kernel search.
Definition: fastmks.hpp:64
void Serialize(Archive &ar, const unsigned int)
Serialize the model.
A cover tree is a tree specifically designed to speed up nearest-neighbor computation in high-dimensi...
Definition: cover_tree.hpp:98