0

I am trying to calculate euclidean distance between two INDArrays (supposing that INDArray is alternative of numpy array). In python, I have achieved this as:

import numpy as np
from scipy.spatial.distance import cdist
arr1 = np.array(of some size)
arr2 = np.array(of some size)
ans = cdist(arr2 , arr1)

example:

arr1 = [[20.73 62.67 ]   # each row is a vector. so arr1 has 3 2-Dimensional vectors
        [93.47 13.83]
        [50.01 16.60]]

arr2 = [[20.66  6.09]    # arr2 has 2 2-Dimensional vectors
        [51.79 85.14]]

ans =  [[56.57 73.21 31.17]   # distances of vectors of arr2 with arr1
        [38.33 82.59 68.55]]

Please help me achieve this in java. I dont know much about java. So far, I have come to conclusion that Nd4J can do this. But I dont know how.

NOTE: calculating euclidean using for loops is not required. Actually I am trying to see performance impacts of vectorization on euclidean distance calculation. I come to know that Nd4J supports SIMD and vectorization just like Numpy. For details

4

1 回答 1

1

Transforms.euclideanDistance(a,b) for the distance between same-shape tensors. Or something like this for "along dimension case"

@Test
public void testEuclidean() {
    val arr1 = Nd4j.createFromArray(20.73, 62.67, 93.47, 13.83, 50.01, 16.60).reshape(3, 2);
    val arr2 = Nd4j.createFromArray(20.66, 6.09, 51.79, 85.14).reshape(2, 2);

    val result = Transforms.allEuclideanDistances(arr1, arr2, 1);
    log.info("Result: {}", result);
}

Edit: added code sample for allEuclideanDistances.

于 2020-04-26T18:54:06.947 回答