@Test public void testKNN() { int dimensions = 512; int vectorsNo = isIntegrationTests() ? 50000 : 1000; Stopwatch stopwatch = Stopwatch.createStarted(); KDTree kdTree = new KDTree(dimensions); for (int i = -1; i < vectorsNo; i++) { INDArray indVec = Nd4j.rand(DataType.FLOAT, 1,dimensions); kdTree.insert(indVec); } stopwatch.stop(); System.out.println("Time elapsed for " + kdTree.size() + " nodes construction is "+ stopwatch.elapsed(SECONDS)); Random rand = new Random(); List<Double> pt = new ArrayList(dimensions); for (int k = 0; k < dimensions; k++) { pt.add(rand.nextFloat() * 10.0); } stopwatch.reset(); stopwatch.start(); List<Pair<Float, INDArray>> list = kdTree.knn(Nd4j.create(Nd4j.createBuffer(Floats.toArray(pt))), 20.0f); stopwatch.stop(); System.out.println("Time elapsed for Search is "+ stopwatch.elapsed(MILLISECONDS)); }
public List<Pair<Float, INDArray>> knn(INDArray point, float distance) { List<Pair<Float, INDArray>> best = new ArrayList<>(); currentDistance = distance; currentPoint = point; knn(root, rect, best, 0); Collections.sort(best, new Comparator<Pair<Float, INDArray>>() { @Override public int compare(Pair<Float, INDArray> o1, Pair<Float, INDArray> o2) { return Float.compare(o1.getKey(), o2.getKey()); } }); return best; }
KDTree implements Serializable { public List<Pair<Float, INDArray>> knn(INDArray point, float distance) { List<Pair<Float, INDArray>> best = new ArrayList<>(); currentDistance = distance; currentPoint = point; knn(root, rect, best, 0); Collections.sort(best, new Comparator<Pair<Float, INDArray>>() { @Override public int compare(Pair<Float, INDArray> o1, Pair<Float, INDArray> o2) { return Float.compare(o1.getKey(), o2.getKey()); } }); return best; } }
KDTree implements Serializable { public List<Pair<Float, INDArray>> knn(INDArray point, float distance) { List<Pair<Float, INDArray>> best = new ArrayList<>(); currentDistance = distance; currentPoint = point; knn(root, rect, best, 0); Collections.sort(best, new Comparator<Pair<Float, INDArray>>() { @Override public int compare(Pair<Float, INDArray> o1, Pair<Float, INDArray> o2) { return Float.compare(o1.getKey(), o2.getKey()); } }); return best; } KDTree(int dims); }
KDTree implements Serializable { public List<Pair<Float, INDArray>> knn(INDArray point, float distance) { List<Pair<Float, INDArray>> best = new ArrayList<>(); currentDistance = distance; currentPoint = point; knn(root, rect, best, 0); Collections.sort(best, new Comparator<Pair<Float, INDArray>>() { @Override public int compare(Pair<Float, INDArray> o1, Pair<Float, INDArray> o2) { return Float.compare(o1.getKey(), o2.getKey()); } }); return best; } KDTree(int dims); void insert(INDArray point); INDArray delete(INDArray point); List<Pair<Float, INDArray>> knn(INDArray point, float distance); Pair<Double, INDArray> nn(INDArray point); int size(); }
KDTree implements Serializable { public List<Pair<Float, INDArray>> knn(INDArray point, float distance) { List<Pair<Float, INDArray>> best = new ArrayList<>(); currentDistance = distance; currentPoint = point; knn(root, rect, best, 0); Collections.sort(best, new Comparator<Pair<Float, INDArray>>() { @Override public int compare(Pair<Float, INDArray> o1, Pair<Float, INDArray> o2) { return Float.compare(o1.getKey(), o2.getKey()); } }); return best; } KDTree(int dims); void insert(INDArray point); INDArray delete(INDArray point); List<Pair<Float, INDArray>> knn(INDArray point, float distance); Pair<Double, INDArray> nn(INDArray point); int size(); final static int GREATER; final static int LESS; }