mlpack  master
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 
21 namespace mlpack {
22 namespace fastmks {
23 
55 template<
56  typename KernelType,
57  typename MatType = arma::mat,
58  template<typename TreeMetricType,
59  typename TreeStatType,
60  typename TreeMatType> class TreeType = tree::StandardCoverTree
61 >
62 class FastMKS
63 {
64  public:
66  typedef TreeType<metric::IPMetric<KernelType>, FastMKSStat, MatType> Tree;
67 
75  FastMKS(const bool singleMode = false, const bool naive = false);
76 
86  FastMKS(const MatType& referenceSet,
87  const bool singleMode = false,
88  const bool naive = false);
89 
101  FastMKS(const MatType& referenceSet,
102  KernelType& kernel,
103  const bool singleMode = false,
104  const bool naive = false);
105 
117  FastMKS(Tree* referenceTree,
118  const bool singleMode = false);
119 
123  FastMKS(const FastMKS& other);
124 
128  FastMKS(FastMKS&& other);
129 
133  FastMKS& operator=(const FastMKS& other);
134 
136  ~FastMKS();
137 
144  void Train(const MatType& referenceSet);
145 
154  void Train(const MatType& referenceSet, KernelType& kernel);
155 
163  void Train(Tree* referenceTree);
164 
185  void Search(const MatType& querySet,
186  const size_t k,
187  arma::Mat<size_t>& indices,
188  arma::mat& kernels);
189 
212  void Search(Tree* querySet,
213  const size_t k,
214  arma::Mat<size_t>& indices,
215  arma::mat& kernels);
216 
231  void Search(const size_t k,
232  arma::Mat<size_t>& indices,
233  arma::mat& products);
234 
236  const metric::IPMetric<KernelType>& Metric() const { return metric; }
239 
241  bool SingleMode() const { return singleMode; }
243  bool& SingleMode() { return singleMode; }
244 
246  bool Naive() const { return naive; }
248  bool& Naive() { return naive; }
249 
251  template<typename Archive>
252  void Serialize(Archive& ar, const unsigned int /* version */);
253 
254  private:
257  const MatType* referenceSet;
261  bool treeOwner;
263  bool setOwner;
264 
268  bool naive;
269 
272 
274  typedef std::pair<double, size_t> Candidate;
275 
277  struct CandidateCmp {
278  bool operator()(const Candidate& c1, const Candidate& c2)
279  {
280  return c1.first > c2.first;
281  };
282  };
283 
285  typedef std::priority_queue<Candidate, std::vector<Candidate>,
286  CandidateCmp> CandidateList;
287 };
288 
289 } // namespace fastmks
290 } // namespace mlpack
291 
292 // Include implementation.
293 #include "fastmks_impl.hpp"
294 
295 #endif
std::priority_queue< Candidate, std::vector< Candidate >, CandidateCmp > CandidateList
Use a priority queue to represent the list of candidate points.
Definition: fastmks.hpp:286
bool treeOwner
If true, this object created the tree and is responsible for it.
Definition: fastmks.hpp:261
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:248
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
FastMKS & operator=(const FastMKS &other)
Assign this model to be a copy of the given model.
Compare two candidates based on the value.
Definition: fastmks.hpp:277
The core includes that mlpack expects; standard C++ includes and Armadillo.
bool setOwner
If true, we own the dataset. This happens in only a few situations.
Definition: fastmks.hpp:263
bool singleMode
If true, single-tree search is used.
Definition: fastmks.hpp:266
bool & SingleMode()
Modify whether or not single-tree search is used.
Definition: fastmks.hpp:243
bool Naive() const
Get whether or not brute-force (naive) search is used.
Definition: fastmks.hpp:246
The inner product metric, IPMetric, takes a given Mercer kernel (KernelType), and when Evaluate() is ...
Definition: ip_metric.hpp:32
Tree * referenceTree
The tree built on the reference dataset.
Definition: fastmks.hpp:259
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:236
std::pair< double, size_t > Candidate
Candidate represents a possible candidate point (value, index).
Definition: fastmks.hpp:274
metric::IPMetric< KernelType > & Metric()
Modify the inner-product metric induced by the given kernel.
Definition: fastmks.hpp:238
bool SingleMode() const
Get whether or not single-tree search is used.
Definition: fastmks.hpp:241
bool operator()(const Candidate &c1, const Candidate &c2)
Definition: fastmks.hpp:278
The statistic used in trees with FastMKS.
TreeType< metric::IPMetric< KernelType >, FastMKSStat, MatType > Tree
Convenience typedef.
Definition: fastmks.hpp:66
metric::IPMetric< KernelType > metric
The instantiated inner-product metric induced by the given kernel.
Definition: fastmks.hpp:271
const MatType * referenceSet
The reference dataset.
Definition: fastmks.hpp:257
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:268
An implementation of fast exact max-kernel search.
Definition: fastmks.hpp:62
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:99