MLPACK  1.0.10
lsh_search.hpp
Go to the documentation of this file.
1 
38 #ifndef __MLPACK_METHODS_NEIGHBOR_SEARCH_LSH_SEARCH_HPP
39 #define __MLPACK_METHODS_NEIGHBOR_SEARCH_LSH_SEARCH_HPP
40 
41 #include <mlpack/core.hpp>
42 #include <vector>
43 #include <string>
44 
47 
48 namespace mlpack {
49 namespace neighbor {
50 
58 template<typename SortPolicy = NearestNeighborSort>
59 class LSHSearch
60 {
61  public:
83  LSHSearch(const arma::mat& referenceSet,
84  const arma::mat& querySet,
85  const size_t numProj,
86  const size_t numTables,
87  const double hashWidth = 0.0,
88  const size_t secondHashSize = 99901,
89  const size_t bucketSize = 500);
90 
111  LSHSearch(const arma::mat& referenceSet,
112  const size_t numProj,
113  const size_t numTables,
114  const double hashWidth = 0.0,
115  const size_t secondHashSize = 99901,
116  const size_t bucketSize = 500);
117 
136  void Search(const size_t k,
137  arma::Mat<size_t>& resultingNeighbors,
138  arma::mat& distances,
139  const size_t numTablesToSearch = 0);
140 
141  // Returns a string representation of this object.
142  std::string ToString() const;
143 
144  private:
158  void BuildHash();
159 
171  void ReturnIndicesFromTable(const size_t queryIndex,
172  arma::uvec& referenceIndices,
173  size_t numTablesToSearch);
174 
182  double BaseCase(const size_t queryIndex, const size_t referenceIndex);
183 
196  void InsertNeighbor(const size_t queryIndex, const size_t pos,
197  const size_t neighbor, const double distance);
198 
200  const arma::mat& referenceSet;
201 
203  const arma::mat& querySet;
204 
206  const size_t numProj;
207 
209  const size_t numTables;
210 
212  std::vector<arma::mat> projections; // should be [numProj x dims] x numTables
213 
215  arma::mat offsets; // should be numProj x numTables
216 
218  double hashWidth;
219 
221  const size_t secondHashSize;
222 
224  arma::vec secondHashWeights;
225 
227  const size_t bucketSize;
228 
231 
233  arma::Mat<size_t> secondHashTable;
234 
237  arma::Col<size_t> bucketContentSize;
238 
241  arma::Col<size_t> bucketRowInHashTable;
242 
244  arma::mat* distancePtr;
245 
247  arma::Mat<size_t>* neighborPtr;
248 }; // class LSHSearch
249 
250 }; // namespace neighbor
251 }; // namespace mlpack
252 
253 // Include implementation.
254 #include "lsh_search_impl.hpp"
255 
256 #endif