Search in sources :

Example 1 with EuclideanDistance

use of org.nd4j.linalg.api.ops.impl.accum.distances.EuclideanDistance in project deeplearning4j by deeplearning4j.

the class KDTree method nn.

private Pair<Double, INDArray> nn(KDNode node, INDArray point, HyperRect rect, double dist, INDArray best, int _disc) {
    if (node == null || rect.minDistance(point) > dist)
        return new Pair<>(Double.POSITIVE_INFINITY, null);
    int _discNext = (_disc + 1) % dims;
    double dist2 = Nd4j.getExecutioner().execAndReturn(new EuclideanDistance(point)).getFinalResult().doubleValue();
    if (dist2 < dist) {
        best = node.getPoint();
    }
    HyperRect lower = rect.getLower(node.point, _disc);
    HyperRect upper = rect.getUpper(node.point, _disc);
    if (point.getDouble(_disc) < node.point.getDouble(_disc)) {
        Pair<Double, INDArray> left = nn(node.getLeft(), point, lower, dist, best, _discNext);
        Pair<Double, INDArray> right = nn(node.getRight(), point, upper, dist, best, _discNext);
        if (left.getFirst() < dist)
            return left;
        else if (right.getFirst() < dist)
            return right;
    } else {
        Pair<Double, INDArray> left = nn(node.getRight(), point, upper, dist, best, _discNext);
        Pair<Double, INDArray> right = nn(node.getLeft(), point, lower, dist, best, _discNext);
        if (left.getFirst() < dist)
            return left;
        else if (right.getFirst() < dist)
            return right;
    }
    return new Pair<>(dist, best);
}
Also used : EuclideanDistance(org.nd4j.linalg.api.ops.impl.accum.distances.EuclideanDistance) INDArray(org.nd4j.linalg.api.ndarray.INDArray) Pair(org.deeplearning4j.berkeley.Pair)

Example 2 with EuclideanDistance

use of org.nd4j.linalg.api.ops.impl.accum.distances.EuclideanDistance in project deeplearning4j by deeplearning4j.

the class KDTree method knn.

private void knn(KDNode node, INDArray point, HyperRect rect, double dist, List<Pair<Double, INDArray>> best, int _disc) {
    if (node == null || rect.minDistance(point) > dist)
        return;
    int _discNext = (_disc + 1) % dims;
    double distance = Nd4j.getExecutioner().execAndReturn(new EuclideanDistance(point)).getFinalResult().doubleValue();
    if (distance <= dist) {
        best.add(new Pair<>(distance, node.getPoint()));
    }
    HyperRect lower = rect.getLower(point, _disc);
    HyperRect upper = rect.getUpper(point, _disc);
    knn(node.getLeft(), point, lower, dist, best, _discNext);
    knn(node.getRight(), point, upper, dist, best, _discNext);
}
Also used : EuclideanDistance(org.nd4j.linalg.api.ops.impl.accum.distances.EuclideanDistance)

Example 3 with EuclideanDistance

use of org.nd4j.linalg.api.ops.impl.accum.distances.EuclideanDistance in project deeplearning4j by deeplearning4j.

the class L2Vertex method doForward.

@Override
public INDArray doForward(boolean training) {
    if (!canDoForward())
        throw new IllegalStateException("Cannot do forward pass: input not set");
    INDArray a = inputs[0];
    INDArray b = inputs[1];
    int[] dimensions = new int[a.rank() - 1];
    for (int i = 1; i < a.rank(); i++) {
        dimensions[i - 1] = i;
    }
    return Nd4j.getExecutioner().exec(new EuclideanDistance(a, b), dimensions);
}
Also used : EuclideanDistance(org.nd4j.linalg.api.ops.impl.accum.distances.EuclideanDistance) INDArray(org.nd4j.linalg.api.ndarray.INDArray)

Aggregations

EuclideanDistance (org.nd4j.linalg.api.ops.impl.accum.distances.EuclideanDistance)3 INDArray (org.nd4j.linalg.api.ndarray.INDArray)2 Pair (org.deeplearning4j.berkeley.Pair)1